Bump version to 4.1-6
[LibreOffice.git] / writerfilter / source / doctok / WW8PieceTableImpl.cxx
blob9214243edfca8645f26112fee8853ea2d75284bf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <algorithm>
21 #include <iterator>
23 #include <resourcemodel/exceptions.hxx>
24 #include <WW8PieceTableImpl.hxx>
25 #include <WW8Clx.hxx>
27 namespace writerfilter {
28 namespace doctok
30 using namespace ::std;
32 ostream & operator << (ostream & o, const WW8PieceTable & rPieceTable)
34 rPieceTable.dump(o);
36 return o;
39 WW8PieceTableImpl::WW8PieceTableImpl(WW8Stream & rStream,
40 sal_uInt32 nOffset,
41 sal_uInt32 nCount)
43 WW8Clx aClx(rStream, nOffset, nCount);
45 sal_uInt32 nPieceCount = aClx.getPieceCount();
47 if (nPieceCount > 0)
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;
81 ++aIt)
83 if (aIt->getCp() <= rCp)
85 aResult = aIt;
87 //break;
91 return aResult;
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();
104 else
106 for (tEntries::const_iterator aIt = mEntries.begin();
107 aIt != aEnd; ++aIt)
109 if (aIt->getFc() <= rFc)
111 tEntries::const_iterator aItNext = aIt;
112 ++aItNext;
114 if (aItNext != aEnd)
116 sal_uInt32 nOffset = rFc.get() - aIt->getFc().get();
117 sal_uInt32 nLength = aItNext->getCp() - aIt->getCp();
119 if (! aIt->isComplex())
120 nLength *= 2;
122 if (nOffset < nLength)
124 aResult = aIt;
126 break;
135 return aResult;
138 Cp WW8PieceTableImpl::getFirstCp() const
140 Cp aResult;
142 if (getCount() > 0)
143 aResult = getCp(0);
144 else
145 throw ExceptionNotFound("WW8PieceTableImpl::getFirstCp");
147 return aResult;
150 Fc WW8PieceTableImpl::getFirstFc() const
152 Fc aResult;
154 if (getCount() > 0)
155 aResult = getFc(0);
156 else
157 throw ExceptionNotFound(" WW8PieceTableImpl::getFirstFc");
159 return aResult;
162 Cp WW8PieceTableImpl::getLastCp() const
164 Cp aResult;
166 if (getCount() > 0)
167 aResult = getCp(getCount() - 1);
168 else
169 throw ExceptionNotFound("WW8PieceTableImpl::getLastCp");
171 return aResult;
174 Fc WW8PieceTableImpl::getLastFc() const
176 Fc aResult;
178 if (getCount() > 0)
179 aResult = getFc(getCount() - 1);
180 else
181 throw ExceptionNotFound("WW8PieceTableImpl::getLastFc");
183 return aResult;
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
198 Cp cpResult;
200 if (mEntries.size() > 0)
202 Fc aFc;
204 if (rFc < mEntries.begin()->getFc())
205 aFc = mEntries.begin()->getFc();
206 else
207 aFc = rFc;
209 tEntries::const_iterator aIt = findFc(aFc);
211 if (aIt != mEntries.end())
213 cpResult = aIt->getCp() + (aFc - aIt->getFc());
215 else
216 throw ExceptionNotFound("WW8PieceTableImpl::fc2cp: " + aFc.toString());
219 return cpResult;
222 Fc WW8PieceTableImpl::cp2fc(const Cp & rCp) const
224 Fc aResult;
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;
237 else
238 throw ExceptionNotFound
239 ("WW8PieceTableImpl::cp2fc: " + rCp.toString());
241 else
242 aResult = mCp2FcCache[rCp];
244 return aResult;
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();
256 return bResult;
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();
268 return bResult;
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: */