merge the formfield patch from ooo-build
[ooovba.git] / writerfilter / source / doctok / WW8PieceTableImpl.cxx
blob1764eed4ce5c0ca3c95b12d76eb21ad99bbff87e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WW8PieceTableImpl.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <algorithm>
32 #include <iterator>
34 #ifndef INCLUDED_DOCTOK_EXCEPTIONS
35 #include <resourcemodel/exceptions.hxx>
36 #endif
37 #include <WW8PieceTableImpl.hxx>
38 #include <WW8Clx.hxx>
40 namespace writerfilter {
41 namespace doctok
43 using namespace ::std;
45 ostream & operator << (ostream & o, const WW8PieceTable & rPieceTable)
47 rPieceTable.dump(o);
49 return o;
52 WW8PieceTableImpl::WW8PieceTableImpl(WW8Stream & rStream,
53 sal_uInt32 nOffset,
54 sal_uInt32 nCount)
56 WW8Clx aClx(rStream, nOffset, nCount);
58 sal_uInt32 nPieceCount = aClx.getPieceCount();
60 if (nPieceCount > 0)
62 for (sal_uInt32 n = 0; n < nPieceCount; n++)
64 Cp aCp(aClx.getCp(n));
65 Fc aFc(aClx.getFc(n), aClx.isComplexFc(n));
67 CpAndFc aCpAndFc(aCp, aFc, PROP_DOC);
69 mEntries.push_back(aCpAndFc);
72 CpAndFc aBack = mEntries.back();
73 Cp aCp(aClx.getCp(aClx.getPieceCount()));
74 Fc aFc(aBack.getFc() + (aCp - aBack.getCp()));
76 CpAndFc aCpAndFc(aCp, aFc, PROP_DOC);
78 mEntries.push_back(aCpAndFc);
82 sal_uInt32 WW8PieceTableImpl::getCount() const
84 return mEntries.size();
87 WW8PieceTableImpl::tEntries::const_iterator
88 WW8PieceTableImpl::findCp(const Cp & rCp) const
90 tEntries::const_iterator aResult = mEntries.end();
91 tEntries::const_iterator aEnd = mEntries.end();
93 for (tEntries::const_iterator aIt = mEntries.begin(); aIt != aEnd;
94 aIt++)
96 if (aIt->getCp() <= rCp)
98 aResult = aIt;
100 //break;
104 return aResult;
107 WW8PieceTableImpl::tEntries::const_iterator
108 WW8PieceTableImpl::findFc(const Fc & rFc) const
110 tEntries::const_iterator aResult = mEntries.end();
111 tEntries::const_iterator aEnd = mEntries.end();
113 if (mEntries.size() > 0)
115 if (rFc < mEntries.begin()->getFc())
116 aResult = mEntries.begin();
117 else
119 for (tEntries::const_iterator aIt = mEntries.begin();
120 aIt != aEnd; aIt++)
122 if (aIt->getFc() <= rFc)
124 tEntries::const_iterator aItNext = aIt;
125 aItNext++;
127 if (aItNext != aEnd)
129 sal_uInt32 nOffset = rFc.get() - aIt->getFc().get();
130 sal_uInt32 nLength = aItNext->getCp() - aIt->getCp();
132 if (! aIt->isComplex())
133 nLength *= 2;
135 if (nOffset < nLength)
137 aResult = aIt;
139 break;
148 return aResult;
151 Cp WW8PieceTableImpl::getFirstCp() const
153 Cp aResult;
155 if (getCount() > 0)
156 aResult = getCp(0);
157 else
158 throw ExceptionNotFound("WW8PieceTableImpl::getFirstCp");
160 return aResult;
163 Fc WW8PieceTableImpl::getFirstFc() const
165 Fc aResult;
167 if (getCount() > 0)
168 aResult = getFc(0);
169 else
170 throw ExceptionNotFound(" WW8PieceTableImpl::getFirstFc");
172 return aResult;
175 Cp WW8PieceTableImpl::getLastCp() const
177 Cp aResult;
179 if (getCount() > 0)
180 aResult = getCp(getCount() - 1);
181 else
182 throw ExceptionNotFound("WW8PieceTableImpl::getLastCp");
184 return aResult;
187 Fc WW8PieceTableImpl::getLastFc() const
189 Fc aResult;
191 if (getCount() > 0)
192 aResult = getFc(getCount() - 1);
193 else
194 throw ExceptionNotFound("WW8PieceTableImpl::getLastFc");
196 return aResult;
199 Cp WW8PieceTableImpl::getCp(sal_uInt32 nIndex) const
201 return mEntries[nIndex].getCp();
204 Fc WW8PieceTableImpl::getFc(sal_uInt32 nIndex) const
206 return mEntries[nIndex].getFc();
209 Cp WW8PieceTableImpl::fc2cp(const Fc & rFc) const
211 Cp cpResult;
213 if (mEntries.size() > 0)
215 Fc aFc;
217 if (rFc < mEntries.begin()->getFc())
218 aFc = mEntries.begin()->getFc();
219 else
220 aFc = rFc;
222 tEntries::const_iterator aIt = findFc(aFc);
224 if (aIt != mEntries.end())
226 cpResult = aIt->getCp() + (aFc - aIt->getFc());
228 else
229 throw ExceptionNotFound("WW8PieceTableImpl::fc2cp: " + aFc.toString());
232 return cpResult;
235 Fc WW8PieceTableImpl::cp2fc(const Cp & rCp) const
237 Fc aResult;
239 Cp2FcHashMap_t::iterator aItCp = mCp2FcCache.find(rCp);
241 if (aItCp == mCp2FcCache.end())
243 tEntries::const_iterator aIt = findCp(rCp);
245 if (aIt != mEntries.end())
247 aResult = aIt->getFc() + (rCp - aIt->getCp());
248 mCp2FcCache[rCp] = aResult;
250 else
251 throw ExceptionNotFound
252 ("WW8PieceTableImpl::cp2fc: " + rCp.toString());
254 else
255 aResult = mCp2FcCache[rCp];
257 return aResult;
260 bool WW8PieceTableImpl::isComplex(const Cp & rCp) const
262 bool bResult = false;
264 tEntries::const_iterator aIt = findCp(rCp);
266 if (aIt != mEntries.end())
267 bResult = aIt->isComplex();
269 return bResult;
272 bool WW8PieceTableImpl::isComplex(const Fc & rFc) const
274 bool bResult = false;
276 tEntries::const_iterator aIt = findFc(rFc);
278 if (aIt != mEntries.end())
279 bResult = aIt->isComplex();
281 return bResult;
284 CpAndFc WW8PieceTableImpl::createCpAndFc
285 (const Cp & rCp, PropertyType eType) const
287 return CpAndFc(rCp, cp2fc(rCp), eType);
290 CpAndFc WW8PieceTableImpl::createCpAndFc
291 (const Fc & rFc, PropertyType eType) const
293 return CpAndFc(fc2cp(rFc), rFc, eType);
296 void WW8PieceTableImpl::dump(ostream & o) const
298 o << "<piecetable>" << endl;
299 copy(mEntries.begin(), mEntries.end(), ostream_iterator<CpAndFc>(o, "\n"));
300 o << "</piecetable>" << endl;