build fix
[LibreOffice.git] / lotuswordpro / source / filter / lwpidxmgr.cxx
blobc38c1a16bacf46b2afd57b2ce5478e840eea2ae8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
10 * Sun Microsystems Inc., October, 2000
12 * GNU Lesser General Public License Version 2.1
13 * =============================================
14 * Copyright 2000 by Sun Microsystems, Inc.
15 * 901 San Antonio Road, Palo Alto, CA 94303, USA
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License version 2.1, as published by the Free Software Foundation.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
32 * Sun Industry Standards Source License Version 1.1
33 * =================================================
34 * The contents of this file are subject to the Sun Industry Standards
35 * Source License Version 1.1 (the "License"); You may not use this file
36 * except in compliance with the License. You may obtain a copy of the
37 * License at http://www.openoffice.org/license.html.
39 * Software provided under this License is provided on an "AS IS" basis,
40 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 * See the License for the specific provisions governing your rights and
44 * obligations concerning the Software.
46 * The Initial Developer of the Original Code is: IBM Corporation
48 * Copyright: 2008 by IBM Corporation
50 * All Rights Reserved.
52 * Contributor(s): _______________________________________
55 ************************************************************************/
57 /**********************************************************************************************************************
58 * @file
59 * index manager implementation
60 * Three record types are related with index information, and two ways are used.
61 * 1. VO_ROOTOBJINDEX works with one or more VO_LEAFOBJINDEX records to
62 * provide the map of all object ids and their offsets.
63 * VO_ROOTOBJINDEX includes k (object id, offset) and timetable which is used to map index to actual low id
64 * 2. VO_ROOTLEAFOBJINDEX is used when the file is so small that the number of objects is less than 256(?)
65 * VO_ROOTLEAFOBJINDEX contains directly the (object id, offset) map and time table.
67 **********************************************************************************************************************/
69 #include "lwpidxmgr.hxx"
70 #include "lwptools.hxx"
71 #include <memory>
73 const sal_uInt8 LwpIndexManager::MAXOBJECTIDS = 255;
75 LwpIndexManager::LwpIndexManager()
76 :m_nKeyCount(0), m_nLeafCount(0)
79 m_TempVec.resize( LwpIndexManager::MAXOBJECTIDS );
83 LwpIndexManager::~LwpIndexManager()
85 //Clear m_ObjectKeys
86 std::vector<LwpKey*>::iterator it;
88 for( it = m_ObjectKeys.begin(); it != m_ObjectKeys.end(); ++it )
90 LwpKey* pKey = *it;
91 if( pKey )
93 delete pKey;
94 pKey = nullptr;
97 m_ObjectKeys.clear();
101 * @descr Read all index records, VO_ROOTLEAFOBJINDEX, VO_ROOTOBJINDEX, VO_LEAFOBJINDEX
103 void LwpIndexManager::Read(LwpSvStream* pStrm)
105 //Read index obj
106 LwpObjectHeader ObjHdr;
107 ObjHdr.Read(*pStrm);
108 LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
109 static_cast<sal_uInt16>(ObjHdr.GetSize()) );
111 if( ObjHdr.GetTag() == VO_ROOTLEAFOBJINDEX )
113 ReadLeafData(pObjStrm);
114 ReadTimeTable(pObjStrm);
115 delete pObjStrm;
117 else
119 ReadRootData(pObjStrm);
120 delete pObjStrm;
122 for (sal_uInt16 k = 0; k < m_nLeafCount; k++)
124 //Read leaf
125 sal_Int64 nPos = m_ChildIndex[k]+LwpSvStream::LWP_STREAM_BASE;
126 sal_Int64 nActualPos = pStrm->Seek(nPos);
128 if (nPos != nActualPos)
129 throw BadSeek();
131 //Old Code
132 //ReadLeafIndex(pStrm);
133 //New Code
134 ReadObjIndex( pStrm );
136 //Read object in root, these objects are between the leaf objects
137 if(k!=m_nLeafCount-1)
139 m_ObjectKeys.push_back(m_RootObjs[k]);
140 m_nKeyCount ++;
143 m_RootObjs.clear();
148 * @descr Read data in VO_ROOTOBJINDEX
150 void LwpIndexManager::ReadRootData(LwpObjectStream* pObjStrm)
153 sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
154 m_nLeafCount = KeyCount ? KeyCount + 1 : 0;
156 if (m_nLeafCount > SAL_N_ELEMENTS(m_ChildIndex))
157 throw std::range_error("corrupt RootData");
159 if (KeyCount)
161 //read object keys
162 LwpKey* akey = new LwpKey();
163 akey->id.Read(pObjStrm);
164 m_RootObjs.push_back(akey);
166 sal_uInt16 k = 0;
168 for (k = 1; k < KeyCount; k++)
170 akey = new LwpKey();
171 akey->id.ReadCompressed(pObjStrm, m_RootObjs[k-1]->id);
172 m_RootObjs.push_back(akey);
175 for (k = 0; k < KeyCount; k++)
176 m_RootObjs[k]->offset = pObjStrm->QuickReaduInt32();
178 //read leaf index offset
179 for (k = 0; k < m_nLeafCount; k++)
180 m_ChildIndex[k] = pObjStrm->QuickReaduInt32();
183 ReadTimeTable(pObjStrm);
187 //Add new method to handle ObjIndex data
189 * @descr Read data in VO_OBJINDEX
191 void LwpIndexManager::ReadObjIndexData(LwpObjectStream* pObjStrm)
193 sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
194 sal_uInt16 LeafCount = KeyCount + 1;
196 std::vector<LwpKey*> vObjIndexs;
198 if (KeyCount)
200 LwpKey* akey = new LwpKey();
201 akey->id.Read(pObjStrm);
202 vObjIndexs.push_back(akey);
204 sal_uInt16 k = 0;
206 for (k = 1; k < KeyCount; k++)
208 akey = new LwpKey();
209 akey->id.ReadCompressed(pObjStrm, vObjIndexs[k-1]->id);
210 vObjIndexs.push_back(akey);
213 for (k = 0; k < KeyCount; k++)
214 vObjIndexs[k]->offset = pObjStrm->QuickReaduInt32();
216 for (k = 0; k < LeafCount; k++)
217 m_TempVec.at(k) = pObjStrm->QuickReaduInt32();
220 for( sal_uInt16 j=0; j<LeafCount; j++ )
222 sal_Int64 nPos = m_TempVec.at(j) + LwpSvStream::LWP_STREAM_BASE;
223 sal_Int64 nActualPos = pObjStrm->GetStream()->Seek(nPos);
225 if (nPos != nActualPos)
226 throw BadSeek();
228 ReadLeafIndex(pObjStrm->GetStream());
230 if(j!=LeafCount-1)
232 m_ObjectKeys.push_back(vObjIndexs[j]);
234 m_nKeyCount ++;
238 vObjIndexs.clear();
239 m_TempVec.clear();
243 * @descr Read VO_OBJINDEX
245 void LwpIndexManager::ReadObjIndex( LwpSvStream *pStrm )
248 LwpObjectHeader ObjHdr;
249 ObjHdr.Read(*pStrm);
250 std::unique_ptr<LwpObjectStream> pObjStrm( new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
251 static_cast<sal_uInt16>(ObjHdr.GetSize()) ) );
253 if( (sal_uInt32)VO_OBJINDEX == ObjHdr.GetTag() )
255 ReadObjIndexData( pObjStrm.get() );
257 else if( (sal_uInt32)VO_LEAFOBJINDEX == ObjHdr.GetTag() )
259 ReadLeafData( pObjStrm.get() );
264 * @descr Read VO_LEAFOBJINDEX
266 void LwpIndexManager::ReadLeafIndex( LwpSvStream *pStrm )
268 LwpObjectHeader ObjHdr;
269 ObjHdr.Read(*pStrm);
270 std::unique_ptr<LwpObjectStream> pObjStrm( new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
271 static_cast<sal_uInt16>(ObjHdr.GetSize()) ) );
273 ReadLeafData(pObjStrm.get());
276 * @descr Read data in VO_LEAFOBJINDEX
278 void LwpIndexManager::ReadLeafData( LwpObjectStream *pObjStrm )
280 sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
282 if(KeyCount)
284 LwpKey* akey = new LwpKey();
285 //read object keys: id & offset
286 akey->id.Read(pObjStrm);
287 m_ObjectKeys.push_back(akey);
289 for (sal_uInt16 k = 1; k < KeyCount; k++)
291 akey = new LwpKey();
292 akey->id.ReadCompressed(pObjStrm, m_ObjectKeys.at(m_nKeyCount+k-1)->id);
293 m_ObjectKeys.push_back(akey);
296 for (sal_uInt16 j = 0; j < KeyCount; j++)
297 m_ObjectKeys.at(m_nKeyCount+j)->offset = pObjStrm->QuickReaduInt32();
299 m_nKeyCount += KeyCount;
302 * @descr Read time table in VO_ROOTLEAFOBJINDEX and VO_ROOTOBJINDEX
304 void LwpIndexManager::ReadTimeTable(LwpObjectStream *pObjStrm)
306 sal_uInt16 nTimeCount = pObjStrm->QuickReaduInt16();
308 for(sal_uInt16 i=0; i<nTimeCount; ++i)
310 sal_uInt32 atime = pObjStrm->QuickReaduInt32();
311 m_TimeTable.push_back(atime);
315 * @descr get object offset per the object id
317 sal_uInt32 LwpIndexManager::GetObjOffset( LwpObjectID objid )
320 //sal_uInt16 L, U, M;
321 sal_uInt32 L, U, M;
323 L = 0;
324 U = m_nKeyCount;
325 while (L != U)
327 M = (L + U) >> 1;
329 if (objid.GetLow() > m_ObjectKeys[M]->id.GetLow())
330 L = M + 1;
331 else if (objid.GetLow() < m_ObjectKeys[M]->id.GetLow())
332 U = M;
333 else if (objid.GetHigh() > m_ObjectKeys[M]->id.GetHigh())
334 L = M + 1;
335 else if (objid.GetHigh() < m_ObjectKeys[M]->id.GetHigh())
336 U = M;
337 else
339 return(m_ObjectKeys[M]->offset);
342 return BAD_OFFSET;
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */