bump product version to 4.1.6.2
[LibreOffice.git] / lotuswordpro / source / filter / lwpidxmgr.cxx
blobccd2c388a6126d062e8b14e002d4bb8455e902e4
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 * Change History
68 Jan 2005 Created
69 **********************************************************************************************************************/
71 #include "lwpidxmgr.hxx"
72 #include "lwptools.hxx"
74 const sal_uInt8 LwpIndexManager::MAXOBJECTIDS = 255;
76 LwpIndexManager::LwpIndexManager()
77 :m_nKeyCount(0), m_nLeafCount(0)
80 m_TempVec.resize( LwpIndexManager::MAXOBJECTIDS );
84 LwpIndexManager::~LwpIndexManager()
86 //Clear m_ObjectKeys
87 std::vector<LwpKey*>::iterator it;
89 for( it = m_ObjectKeys.begin(); it != m_ObjectKeys.end(); ++it )
91 LwpKey* pKey = *it;
92 if( pKey )
94 delete pKey;
95 pKey = NULL;
98 m_ObjectKeys.clear();
102 * @descr Read all index records, VO_ROOTLEAFOBJINDEX, VO_ROOTOBJINDEX, VO_LEAFOBJINDEX
104 void LwpIndexManager::Read(LwpSvStream* pStrm)
106 //Read index obj
107 LwpObjectHeader ObjHdr;
108 ObjHdr.Read(*pStrm);
109 LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
110 static_cast<sal_uInt16>(ObjHdr.GetSize()) );
112 if( ObjHdr.GetTag() == VO_ROOTLEAFOBJINDEX )
114 ReadLeafData(pObjStrm);
115 ReadTimeTable(pObjStrm);
116 delete pObjStrm;
118 else
120 ReadRootData(pObjStrm);
121 delete pObjStrm;
123 for (sal_uInt16 k = 0; k < m_nLeafCount; k++)
125 //Read leaf
126 sal_Int64 nPos = m_ChildIndex[k]+LwpSvStream::LWP_STREAM_BASE;
127 sal_Int64 nActualPos = pStrm->Seek(nPos);
129 if (nPos != nActualPos)
130 throw BadSeek();
132 //Old Code
133 //ReadLeafIndex(pStrm);
134 //New Code
135 ReadObjIndex( pStrm );
138 //Read object in root, these objects are between the leaf objects
139 if(k!=m_nLeafCount-1)
141 m_ObjectKeys.push_back(m_RootObjs[k]);
142 m_nKeyCount ++;
145 m_RootObjs.clear();
150 * @descr Read data in VO_ROOTOBJINDEX
152 void LwpIndexManager::ReadRootData(LwpObjectStream* pObjStrm)
155 sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
156 m_nLeafCount = KeyCount ? KeyCount + 1 : 0;
158 if(KeyCount)
160 //read object keys
161 LwpKey* akey = new LwpKey();
162 akey->id.Read(pObjStrm);
163 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);
188 //Add new method to handle ObjIndex data
190 * @descr Read data in VO_OBJINDEX
192 void LwpIndexManager::ReadObjIndexData(LwpObjectStream* pObjStrm)
194 sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
195 sal_uInt16 LeafCount = KeyCount + 1;
197 std::vector<LwpKey*> vObjIndexs;
199 if(KeyCount)
201 LwpKey* akey = new LwpKey();
202 akey->id.Read(pObjStrm);
203 vObjIndexs.push_back(akey);
205 sal_uInt16 k = 0;
207 for (k = 1; k < KeyCount; k++)
209 akey = new LwpKey();
210 akey->id.ReadCompressed(pObjStrm, vObjIndexs[k-1]->id);
211 vObjIndexs.push_back(akey);
214 for (k = 0; k < KeyCount; k++)
215 vObjIndexs[k]->offset = pObjStrm->QuickReaduInt32();
217 for (k = 0; k < LeafCount; k++)
218 m_TempVec[k] = pObjStrm->QuickReaduInt32();
221 for( sal_uInt16 j=0; j<LeafCount; j++ )
223 sal_Int64 nPos = m_TempVec[j]+LwpSvStream::LWP_STREAM_BASE;
224 sal_Int64 nActualPos = pObjStrm->GetStream()->Seek(nPos);
226 if (nPos != nActualPos)
227 throw BadSeek();
230 ReadLeafIndex(pObjStrm->GetStream());
232 if(j!=LeafCount-1)
234 m_ObjectKeys.push_back(vObjIndexs[j]);
236 m_nKeyCount ++;
240 vObjIndexs.clear();
241 m_TempVec.clear();
246 * @descr Read VO_OBJINDEX
248 void LwpIndexManager::ReadObjIndex( LwpSvStream *pStrm )
251 LwpObjectHeader ObjHdr;
252 ObjHdr.Read(*pStrm);
253 LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
254 static_cast<sal_uInt16>(ObjHdr.GetSize()) );
256 if( (sal_uInt32)VO_OBJINDEX == ObjHdr.GetTag() )
258 ReadObjIndexData( pObjStrm );
260 else if( (sal_uInt32)VO_LEAFOBJINDEX == ObjHdr.GetTag() )
262 ReadLeafData(pObjStrm);
265 delete pObjStrm;
270 * @descr Read VO_LEAFOBJINDEX
272 void LwpIndexManager::ReadLeafIndex( LwpSvStream *pStrm )
274 LwpObjectHeader ObjHdr;
275 ObjHdr.Read(*pStrm);
276 LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
277 static_cast<sal_uInt16>(ObjHdr.GetSize()) );
279 ReadLeafData(pObjStrm);
281 delete pObjStrm;
284 * @descr Read data in VO_LEAFOBJINDEX
286 void LwpIndexManager::ReadLeafData( LwpObjectStream *pObjStrm )
288 sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
290 if(KeyCount)
292 LwpKey* akey = new LwpKey();
293 //read object keys: id & offset
294 akey->id.Read(pObjStrm);
295 m_ObjectKeys.push_back(akey);
297 for (sal_uInt8 k = 1; k < KeyCount; k++)
299 akey = new LwpKey();
300 akey->id.ReadCompressed(pObjStrm, m_ObjectKeys.at(m_nKeyCount+k-1)->id);
301 m_ObjectKeys.push_back(akey);
304 for (sal_uInt8 j = 0; j < KeyCount; j++)
305 m_ObjectKeys.at(m_nKeyCount+j)->offset = pObjStrm->QuickReaduInt32();
307 m_nKeyCount += KeyCount;
310 * @descr Read time table in VO_ROOTLEAFOBJINDEX and VO_ROOTOBJINDEX
312 void LwpIndexManager::ReadTimeTable(LwpObjectStream *pObjStrm)
314 sal_uInt16 nTimeCount = pObjStrm->QuickReaduInt16();
316 for(sal_uInt16 i=0; i<nTimeCount; ++i)
318 sal_uInt32 atime = pObjStrm->QuickReaduInt32();
319 m_TimeTable.push_back(atime);
323 * @descr get object offset per the object id
325 sal_uInt32 LwpIndexManager::GetObjOffset( LwpObjectID objid )
328 //sal_uInt16 L, U, M;
329 sal_uInt32 L, U, M;
332 L = 0;
333 U = m_nKeyCount;
334 while (L != U)
336 M = (L + U) >> 1;
338 if (objid.GetLow() > m_ObjectKeys[M]->id.GetLow())
339 L = M + 1;
340 else if (objid.GetLow() < m_ObjectKeys[M]->id.GetLow())
341 U = M;
342 else if (objid.GetHigh() > m_ObjectKeys[M]->id.GetHigh())
343 L = M + 1;
344 else if (objid.GetHigh() < m_ObjectKeys[M]->id.GetHigh())
345 U = M;
346 else
348 return(m_ObjectKeys[M]->offset);
351 return BAD_OFFSET;
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */