1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
23 #include <resourcemodel/exceptions.hxx>
24 #include <WW8PieceTableImpl.hxx>
27 namespace writerfilter
{
30 using namespace ::std
;
32 ostream
& operator << (ostream
& o
, const WW8PieceTable
& rPieceTable
)
39 WW8PieceTableImpl::WW8PieceTableImpl(WW8Stream
& rStream
,
43 WW8Clx
aClx(rStream
, nOffset
, nCount
);
45 sal_uInt32 nPieceCount
= aClx
.getPieceCount();
49 for (sal_uInt32 n
= 0; n
< nPieceCount
; n
++)
51 Cp
aCp(aClx
.getCp(n
));
52 Fc
aFc(aClx
.getFc(n
), aClx
.isComplexFc(n
));
54 CpAndFc
aCpAndFc(aCp
, aFc
, PROP_DOC
);
56 mEntries
.push_back(aCpAndFc
);
59 CpAndFc aBack
= mEntries
.back();
60 Cp
aCp(aClx
.getCp(aClx
.getPieceCount()));
61 Fc
aFc(aBack
.getFc() + (aCp
- aBack
.getCp()));
63 CpAndFc
aCpAndFc(aCp
, aFc
, PROP_DOC
);
65 mEntries
.push_back(aCpAndFc
);
69 sal_uInt32
WW8PieceTableImpl::getCount() const
71 return mEntries
.size();
74 WW8PieceTableImpl::tEntries::const_iterator
75 WW8PieceTableImpl::findCp(const Cp
& rCp
) const
77 tEntries::const_iterator aResult
= mEntries
.end();
78 tEntries::const_iterator aEnd
= mEntries
.end();
80 for (tEntries::const_iterator aIt
= mEntries
.begin(); aIt
!= aEnd
;
83 if (aIt
->getCp() <= rCp
)
94 WW8PieceTableImpl::tEntries::const_iterator
95 WW8PieceTableImpl::findFc(const Fc
& rFc
) const
97 tEntries::const_iterator aResult
= mEntries
.end();
98 tEntries::const_iterator aEnd
= mEntries
.end();
100 if (mEntries
.size() > 0)
102 if (rFc
< mEntries
.begin()->getFc())
103 aResult
= mEntries
.begin();
106 for (tEntries::const_iterator aIt
= mEntries
.begin();
109 if (aIt
->getFc() <= rFc
)
111 tEntries::const_iterator aItNext
= aIt
;
116 sal_uInt32 nOffset
= rFc
.get() - aIt
->getFc().get();
117 sal_uInt32 nLength
= aItNext
->getCp() - aIt
->getCp();
119 if (! aIt
->isComplex())
122 if (nOffset
< nLength
)
138 Cp
WW8PieceTableImpl::getFirstCp() const
145 throw ExceptionNotFound("WW8PieceTableImpl::getFirstCp");
150 Fc
WW8PieceTableImpl::getFirstFc() const
157 throw ExceptionNotFound(" WW8PieceTableImpl::getFirstFc");
162 Cp
WW8PieceTableImpl::getLastCp() const
167 aResult
= getCp(getCount() - 1);
169 throw ExceptionNotFound("WW8PieceTableImpl::getLastCp");
174 Fc
WW8PieceTableImpl::getLastFc() const
179 aResult
= getFc(getCount() - 1);
181 throw ExceptionNotFound("WW8PieceTableImpl::getLastFc");
186 Cp
WW8PieceTableImpl::getCp(sal_uInt32 nIndex
) const
188 return mEntries
[nIndex
].getCp();
191 Fc
WW8PieceTableImpl::getFc(sal_uInt32 nIndex
) const
193 return mEntries
[nIndex
].getFc();
196 Cp
WW8PieceTableImpl::fc2cp(const Fc
& rFc
) const
200 if (mEntries
.size() > 0)
204 if (rFc
< mEntries
.begin()->getFc())
205 aFc
= mEntries
.begin()->getFc();
209 tEntries::const_iterator aIt
= findFc(aFc
);
211 if (aIt
!= mEntries
.end())
213 cpResult
= aIt
->getCp() + (aFc
- aIt
->getFc());
216 throw ExceptionNotFound("WW8PieceTableImpl::fc2cp: " + aFc
.toString());
222 Fc
WW8PieceTableImpl::cp2fc(const Cp
& rCp
) const
226 Cp2FcHashMap_t::iterator aItCp
= mCp2FcCache
.find(rCp
);
228 if (aItCp
== mCp2FcCache
.end())
230 tEntries::const_iterator aIt
= findCp(rCp
);
232 if (aIt
!= mEntries
.end())
234 aResult
= aIt
->getFc() + (rCp
- aIt
->getCp());
235 mCp2FcCache
[rCp
] = aResult
;
238 throw ExceptionNotFound
239 ("WW8PieceTableImpl::cp2fc: " + rCp
.toString());
242 aResult
= mCp2FcCache
[rCp
];
247 bool WW8PieceTableImpl::isComplex(const Cp
& rCp
) const
249 bool bResult
= false;
251 tEntries::const_iterator aIt
= findCp(rCp
);
253 if (aIt
!= mEntries
.end())
254 bResult
= aIt
->isComplex();
259 bool WW8PieceTableImpl::isComplex(const Fc
& rFc
) const
261 bool bResult
= false;
263 tEntries::const_iterator aIt
= findFc(rFc
);
265 if (aIt
!= mEntries
.end())
266 bResult
= aIt
->isComplex();
271 CpAndFc
WW8PieceTableImpl::createCpAndFc
272 (const Cp
& rCp
, PropertyType eType
) const
274 return CpAndFc(rCp
, cp2fc(rCp
), eType
);
277 CpAndFc
WW8PieceTableImpl::createCpAndFc
278 (const Fc
& rFc
, PropertyType eType
) const
280 return CpAndFc(fc2cp(rFc
), rFc
, eType
);
283 void WW8PieceTableImpl::dump(ostream
& o
) const
285 o
<< "<piecetable>" << endl
;
286 copy(mEntries
.begin(), mEntries
.end(), ostream_iterator
<CpAndFc
>(o
, "\n"));
287 o
<< "</piecetable>" << endl
;
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */