update dev300-m57
[ooovba.git] / lotuswordpro / source / filter / lwpidxmgr.cxx
blob1e8b01562f51769338268fa9140d63a0e10097dd
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * either of the following licenses
6 * - GNU Lesser General Public License Version 2.1
7 * - Sun Industry Standards Source License Version 1.1
9 * Sun Microsystems Inc., October, 2000
11 * GNU Lesser General Public License Version 2.1
12 * =============================================
13 * Copyright 2000 by Sun Microsystems, Inc.
14 * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License version 2.1, as published by the Free Software Foundation.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * MA 02111-1307 USA
31 * Sun Industry Standards Source License Version 1.1
32 * =================================================
33 * The contents of this file are subject to the Sun Industry Standards
34 * Source License Version 1.1 (the "License"); You may not use this file
35 * except in compliance with the License. You may obtain a copy of the
36 * License at http://www.openoffice.org/license.html.
38 * Software provided under this License is provided on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
40 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
41 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
42 * See the License for the specific provisions governing your rights and
43 * obligations concerning the Software.
45 * The Initial Developer of the Original Code is: IBM Corporation
47 * Copyright: 2008 by IBM Corporation
49 * All Rights Reserved.
51 * Contributor(s): _______________________________________
54 ************************************************************************/
55 /**********************************************************************************************************************
56 * @file
57 * index manager implementation
58 * Three record types are related with index information, and two ways are used.
59 * 1. VO_ROOTOBJINDEX works with one or more VO_LEAFOBJINDEX records to
60 * provide the map of all object ids and their offsets.
61 * VO_ROOTOBJINDEX includes k (object id, offset) and timetable which is used to map index to actual low id
62 * 2. VO_ROOTLEAFOBJINDEX is used when the file is so small that the number of objects is less than 256(?)
63 * VO_ROOTLEAFOBJINDEX contains directly the (object id, offset) map and time table.
64 /**********************************************************************************************************************/
65 /**********************************************************************************************************************
66 * Change History
67 Jan 2005 Created
68 /**********************************************************************************************************************/
70 #include "lwpidxmgr.hxx"
72 const sal_uInt8 LwpIndexManager::MAXOBJECTIDS = 255;
74 LwpIndexManager::LwpIndexManager()
75 :m_nKeyCount(0), m_nLeafCount(0)
78 m_TempVec.resize( LwpIndexManager::MAXOBJECTIDS );
82 LwpIndexManager::~LwpIndexManager()
84 //Clear m_ObjectKeys
85 std::vector<LwpKey*>::iterator it;
87 for( it = m_ObjectKeys.begin(); it != m_ObjectKeys.end(); it++ )
89 LwpKey* pKey = *it;
90 if( pKey )
92 delete pKey;
93 pKey = NULL;
96 m_ObjectKeys.clear();
99 /**
100 * @descr Read all index records, VO_ROOTLEAFOBJINDEX, VO_ROOTOBJINDEX, VO_LEAFOBJINDEX
102 void LwpIndexManager::Read(LwpSvStream* pStrm)
104 //Read index obj
105 LwpObjectHeader ObjHdr;
106 ObjHdr.Read(*pStrm);
107 LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
108 static_cast<sal_uInt16>(ObjHdr.GetSize()) );
110 if( ObjHdr.GetTag() == VO_ROOTLEAFOBJINDEX )
112 ReadLeafData(pObjStrm);
113 ReadTimeTable(pObjStrm);
114 delete pObjStrm;
116 else
118 ReadRootData(pObjStrm);
119 delete pObjStrm;
121 for (sal_uInt16 k = 0; k < m_nLeafCount; k++)
123 //Read leaf
124 pStrm->Seek(m_ChildIndex[k]+LwpSvStream::LWP_STREAM_BASE);
127 //Old Code
128 //ReadLeafIndex(pStrm);
129 //New Code
130 ReadObjIndex( pStrm );
133 //Read object in root, these objects are between the leaf objects
134 if(k!=m_nLeafCount-1)
136 m_ObjectKeys.push_back(m_RootObjs[k]);
137 m_nKeyCount ++;
140 m_RootObjs.clear();
145 * @descr Read data in VO_ROOTOBJINDEX
147 void LwpIndexManager::ReadRootData(LwpObjectStream* pObjStrm)
150 sal_uInt16 KeyCount = 0;
151 pObjStrm->QuickRead(&KeyCount, sizeof(KeyCount));
152 m_nLeafCount = KeyCount + 1;
154 if(KeyCount)
156 //read object keys
157 LwpKey* akey = new LwpKey();
158 akey->id.Read(pObjStrm);
159 m_RootObjs.push_back(akey);
162 //sal_uInt8 k = 0;
163 sal_uInt16 k = 0;
165 for (k = 1; k < KeyCount; k++)
167 akey = new LwpKey();
168 akey->id.ReadCompressed(pObjStrm, m_RootObjs[k-1]->id);
169 m_RootObjs.push_back(akey);
172 for (k = 0; k < KeyCount; k++)
174 pObjStrm->QuickRead(&(m_RootObjs[k]->offset), sizeof(sal_uInt32));
177 //read leaf index offset
178 for (k = 0; k < m_nLeafCount; k++)
180 pObjStrm->QuickRead(&(m_ChildIndex[k]), sizeof(sal_uInt32));
184 ReadTimeTable(pObjStrm);
189 //Add new method to handle ObjIndex data
191 * @descr Read data in VO_OBJINDEX
193 void LwpIndexManager::ReadObjIndexData(LwpObjectStream* pObjStrm)
195 sal_uInt16 KeyCount = 0;
196 sal_uInt16 LeafCount = 0;
197 pObjStrm->QuickRead(&KeyCount, sizeof(KeyCount));
198 LeafCount = KeyCount + 1;
200 std::vector<LwpKey*> vObjIndexs;
202 if(KeyCount)
204 LwpKey* akey = new LwpKey();
205 akey->id.Read(pObjStrm);
206 vObjIndexs.push_back(akey);
208 sal_uInt16 k = 0;
210 for (k = 1; k < KeyCount; k++)
212 akey = new LwpKey();
213 akey->id.ReadCompressed(pObjStrm, vObjIndexs[k-1]->id);
214 vObjIndexs.push_back(akey);
217 for (k = 0; k < KeyCount; k++)
218 pObjStrm->QuickRead(&(vObjIndexs[k]->offset), sizeof(sal_uInt32));
220 for (k = 0; k < LeafCount; k++)
221 pObjStrm->QuickRead(&(m_TempVec[k]), sizeof(sal_uInt32));
224 for( sal_uInt16 j=0; j<LeafCount; j++ )
226 pObjStrm->GetStream()->Seek( m_TempVec[j]+LwpSvStream::LWP_STREAM_BASE);
227 ReadLeafIndex(pObjStrm->GetStream());
229 if(j!=LeafCount-1)
231 m_ObjectKeys.push_back(vObjIndexs[j]);
233 m_nKeyCount ++;
237 vObjIndexs.clear();
238 m_TempVec.clear();
243 * @descr Read VO_OBJINDEX
245 void LwpIndexManager::ReadObjIndex( LwpSvStream *pStrm )
248 LwpObjectHeader ObjHdr;
249 ObjHdr.Read(*pStrm);
250 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 );
257 else if( (sal_uInt32)VO_LEAFOBJINDEX == ObjHdr.GetTag() )
259 ReadLeafData(pObjStrm);
262 delete pObjStrm;
267 * @descr Read VO_LEAFOBJINDEX
269 void LwpIndexManager::ReadLeafIndex( LwpSvStream *pStrm )
271 LwpObjectHeader ObjHdr;
272 ObjHdr.Read(*pStrm);
273 LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
274 static_cast<sal_uInt16>(ObjHdr.GetSize()) );
276 ReadLeafData(pObjStrm);
278 delete pObjStrm;
281 * @descr Read data in VO_LEAFOBJINDEX
283 void LwpIndexManager::ReadLeafData( LwpObjectStream *pObjStrm )
285 sal_uInt16 KeyCount;
286 pObjStrm->QuickRead(&KeyCount, sizeof(KeyCount));
288 if(KeyCount)
290 LwpKey* akey = new LwpKey();
291 //read object keys: id & offset
292 akey->id.Read(pObjStrm);
293 m_ObjectKeys.push_back(akey);
295 for (sal_uInt8 k = 1; k < KeyCount; k++)
297 akey = new LwpKey();
298 akey->id.ReadCompressed(pObjStrm, m_ObjectKeys[m_nKeyCount+k-1]->id);
299 m_ObjectKeys.push_back(akey);
302 for (sal_uInt8 j = 0; j < KeyCount; j++)
304 pObjStrm->QuickRead(&(m_ObjectKeys[m_nKeyCount+j]->offset), sizeof(sal_uInt32));
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;
315 pObjStrm->QuickRead(&nTimeCount, sizeof(nTimeCount));
317 sal_uInt32 atime;
318 for(sal_uInt16 i=0; i<nTimeCount; i++)
320 pObjStrm->QuickRead( &atime, sizeof(atime) );
321 m_TimeTable.push_back(atime);
325 * @descr get object offset per the object id
327 sal_uInt32 LwpIndexManager::GetObjOffset( LwpObjectID objid )
330 //sal_uInt16 L, U, M;
331 sal_uInt32 L, U, M;
334 L = 0;
335 U = m_nKeyCount;
336 while (L != U)
338 M = (L + U) >> 1;
340 if (objid.GetLow() > m_ObjectKeys[M]->id.GetLow())
341 L = M + 1;
342 else if (objid.GetLow() < m_ObjectKeys[M]->id.GetLow())
343 U = M;
344 else if (objid.GetHigh() > m_ObjectKeys[M]->id.GetHigh())
345 L = M + 1;
346 else if (objid.GetHigh() < m_ObjectKeys[M]->id.GetHigh())
347 U = M;
348 else
350 return(m_ObjectKeys[M]->offset);
353 return BAD_OFFSET;