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,
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 /**********************************************************************************************************************
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"
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()
86 std::vector
<LwpKey
*>::iterator it
;
88 for( it
= m_ObjectKeys
.begin(); it
!= m_ObjectKeys
.end(); ++it
)
101 * @descr Read all index records, VO_ROOTLEAFOBJINDEX, VO_ROOTOBJINDEX, VO_LEAFOBJINDEX
103 void LwpIndexManager::Read(LwpSvStream
* pStrm
)
106 LwpObjectHeader ObjHdr
;
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
);
119 ReadRootData(pObjStrm
);
122 for (sal_uInt16 k
= 0; k
< m_nLeafCount
; k
++)
125 sal_Int64 nPos
= m_ChildIndex
[k
]+LwpSvStream::LWP_STREAM_BASE
;
126 sal_Int64 nActualPos
= pStrm
->Seek(nPos
);
128 if (nPos
!= nActualPos
)
132 //ReadLeafIndex(pStrm);
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
]);
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");
162 LwpKey
* akey
= new LwpKey();
163 akey
->id
.Read(pObjStrm
);
164 m_RootObjs
.push_back(akey
);
168 for (k
= 1; k
< KeyCount
; k
++)
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
;
200 LwpKey
* akey
= new LwpKey();
201 akey
->id
.Read(pObjStrm
);
202 vObjIndexs
.push_back(akey
);
206 for (k
= 1; k
< KeyCount
; k
++)
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
)
228 ReadLeafIndex(pObjStrm
->GetStream());
232 m_ObjectKeys
.push_back(vObjIndexs
[j
]);
243 * @descr Read VO_OBJINDEX
245 void LwpIndexManager::ReadObjIndex( LwpSvStream
*pStrm
)
248 LwpObjectHeader ObjHdr
;
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
;
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();
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
++)
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;
329 if (objid
.GetLow() > m_ObjectKeys
[M
]->id
.GetLow())
331 else if (objid
.GetLow() < m_ObjectKeys
[M
]->id
.GetLow())
333 else if (objid
.GetHigh() > m_ObjectKeys
[M
]->id
.GetHigh())
335 else if (objid
.GetHigh() < m_ObjectKeys
[M
]->id
.GetHigh())
339 return(m_ObjectKeys
[M
]->offset
);
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */