1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WW8PieceTableImpl.cxx,v $
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 ************************************************************************/
34 #ifndef INCLUDED_DOCTOK_EXCEPTIONS
35 #include <resourcemodel/exceptions.hxx>
37 #include <WW8PieceTableImpl.hxx>
40 namespace writerfilter
{
43 using namespace ::std
;
45 ostream
& operator << (ostream
& o
, const WW8PieceTable
& rPieceTable
)
52 WW8PieceTableImpl::WW8PieceTableImpl(WW8Stream
& rStream
,
56 WW8Clx
aClx(rStream
, nOffset
, nCount
);
58 sal_uInt32 nPieceCount
= aClx
.getPieceCount();
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
;
96 if (aIt
->getCp() <= rCp
)
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();
119 for (tEntries::const_iterator aIt
= mEntries
.begin();
122 if (aIt
->getFc() <= rFc
)
124 tEntries::const_iterator aItNext
= aIt
;
129 sal_uInt32 nOffset
= rFc
.get() - aIt
->getFc().get();
130 sal_uInt32 nLength
= aItNext
->getCp() - aIt
->getCp();
132 if (! aIt
->isComplex())
135 if (nOffset
< nLength
)
151 Cp
WW8PieceTableImpl::getFirstCp() const
158 throw ExceptionNotFound("WW8PieceTableImpl::getFirstCp");
163 Fc
WW8PieceTableImpl::getFirstFc() const
170 throw ExceptionNotFound(" WW8PieceTableImpl::getFirstFc");
175 Cp
WW8PieceTableImpl::getLastCp() const
180 aResult
= getCp(getCount() - 1);
182 throw ExceptionNotFound("WW8PieceTableImpl::getLastCp");
187 Fc
WW8PieceTableImpl::getLastFc() const
192 aResult
= getFc(getCount() - 1);
194 throw ExceptionNotFound("WW8PieceTableImpl::getLastFc");
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
213 if (mEntries
.size() > 0)
217 if (rFc
< mEntries
.begin()->getFc())
218 aFc
= mEntries
.begin()->getFc();
222 tEntries::const_iterator aIt
= findFc(aFc
);
224 if (aIt
!= mEntries
.end())
226 cpResult
= aIt
->getCp() + (aFc
- aIt
->getFc());
229 throw ExceptionNotFound("WW8PieceTableImpl::fc2cp: " + aFc
.toString());
235 Fc
WW8PieceTableImpl::cp2fc(const Cp
& rCp
) const
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
;
251 throw ExceptionNotFound
252 ("WW8PieceTableImpl::cp2fc: " + rCp
.toString());
255 aResult
= mCp2FcCache
[rCp
];
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();
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();
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
;