summaryrefslogtreecommitdiff
path: root/lotuswordpro/source/filter/lwpidxmgr.cxx
blob: 07ea4a1a521fa876845d169388236a66964d1f7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: IBM Corporation
 *
 *  Copyright: 2008 by IBM Corporation
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

/**********************************************************************************************************************
 * @file
 *   index manager implementation
 *  Three record types are related with index information, and two ways are used.
*  1. VO_ROOTOBJINDEX works with one or more VO_LEAFOBJINDEX records to
*  provide the map of all object ids and their offsets.
*  VO_ROOTOBJINDEX includes k (object id, offset) and timetable which is used to map index to actual low id
 *  2. VO_ROOTLEAFOBJINDEX is used when the file is so small that the number of objects is less than 256(?)
 *  VO_ROOTLEAFOBJINDEX contains directly the (object id, offset) map and time table.

 * Change History
 Jan 2005           Created
**********************************************************************************************************************/

#include "lwpidxmgr.hxx"
#include "lwptools.hxx"

const sal_uInt8 LwpIndexManager::MAXOBJECTIDS = 255;

LwpIndexManager::LwpIndexManager()
    :m_nKeyCount(0), m_nLeafCount(0)
{

    m_TempVec.resize( LwpIndexManager::MAXOBJECTIDS );

}

LwpIndexManager::~LwpIndexManager()
{
    //Clear m_ObjectKeys
    std::vector<LwpKey*>::iterator it;

    for( it = m_ObjectKeys.begin(); it != m_ObjectKeys.end(); ++it )
    {
        LwpKey* pKey = *it;
        if( pKey )
        {
            delete pKey;
            pKey = NULL;
        }
    }
    m_ObjectKeys.clear();
}

/**
 * @descr   Read all index records, VO_ROOTLEAFOBJINDEX, VO_ROOTOBJINDEX, VO_LEAFOBJINDEX
 */
void LwpIndexManager::Read(LwpSvStream* pStrm)
{
    //Read index obj
    LwpObjectHeader ObjHdr;
    ObjHdr.Read(*pStrm);
    LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
            static_cast<sal_uInt16>(ObjHdr.GetSize()) );

    if( ObjHdr.GetTag() == VO_ROOTLEAFOBJINDEX )
    {
        ReadLeafData(pObjStrm);
        ReadTimeTable(pObjStrm);
        delete pObjStrm;
    }
    else
    {
        ReadRootData(pObjStrm);
        delete pObjStrm;

        for (sal_uInt16 k = 0; k < m_nLeafCount; k++)
        {
            //Read leaf
            sal_Int64 nPos = m_ChildIndex[k]+LwpSvStream::LWP_STREAM_BASE;
            sal_Int64 nActualPos = pStrm->Seek(nPos);

            if (nPos != nActualPos)
                throw BadSeek();

            //Old Code
            //ReadLeafIndex(pStrm);
            //New Code
            ReadObjIndex( pStrm );

            //Read object in root, these objects are between the leaf objects
            if(k!=m_nLeafCount-1)
            {
                m_ObjectKeys.push_back(m_RootObjs[k]);
                m_nKeyCount ++;
            }
        }
        m_RootObjs.clear();
    }
}

/**
 * @descr   Read data in VO_ROOTOBJINDEX
 */
void LwpIndexManager::ReadRootData(LwpObjectStream* pObjStrm)
{

    sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
    m_nLeafCount = KeyCount ? KeyCount + 1 : 0;

    if(KeyCount)
    {
        //read object keys
        LwpKey* akey = new LwpKey();
        akey->id.Read(pObjStrm);
        m_RootObjs.push_back(akey);

        sal_uInt16 k = 0;

        for (k = 1; k < KeyCount; k++)
        {
            akey = new LwpKey();
            akey->id.ReadCompressed(pObjStrm, m_RootObjs[k-1]->id);
            m_RootObjs.push_back(akey);
        }

        for (k = 0; k < KeyCount; k++)
            m_RootObjs[k]->offset = pObjStrm->QuickReaduInt32();

        //read leaf index offset
        for (k = 0; k < m_nLeafCount; k++)
            m_ChildIndex[k] = pObjStrm->QuickReaduInt32();
    }

    ReadTimeTable(pObjStrm);

}

//Add new method to handle ObjIndex data
/**
 * @descr   Read data in VO_OBJINDEX
 */
void LwpIndexManager::ReadObjIndexData(LwpObjectStream* pObjStrm)
{
    sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
    sal_uInt16 LeafCount = KeyCount + 1;

    std::vector<LwpKey*> vObjIndexs;

    if(KeyCount)
    {
        LwpKey* akey = new LwpKey();
        akey->id.Read(pObjStrm);
        vObjIndexs.push_back(akey);

        sal_uInt16 k = 0;

        for (k = 1; k < KeyCount; k++)
        {
            akey = new LwpKey();
            akey->id.ReadCompressed(pObjStrm, vObjIndexs[k-1]->id);
            vObjIndexs.push_back(akey);
        }

        for (k = 0; k < KeyCount; k++)
            vObjIndexs[k]->offset = pObjStrm->QuickReaduInt32();

        for (k = 0; k < LeafCount; k++)
            m_TempVec[k] = pObjStrm->QuickReaduInt32();
    }

    for( sal_uInt16 j=0; j<LeafCount; j++ )
    {
        sal_Int64 nPos = m_TempVec[j]+LwpSvStream::LWP_STREAM_BASE;
        sal_Int64 nActualPos = pObjStrm->GetStream()->Seek(nPos);

        if (nPos != nActualPos)
            throw BadSeek();

        ReadLeafIndex(pObjStrm->GetStream());

        if(j!=LeafCount-1)
        {
            m_ObjectKeys.push_back(vObjIndexs[j]);

            m_nKeyCount ++;
        }
    }

    vObjIndexs.clear();
    m_TempVec.clear();
}

/**
 * @descr   Read VO_OBJINDEX
 */
void LwpIndexManager::ReadObjIndex( LwpSvStream *pStrm )
{

    LwpObjectHeader ObjHdr;
    ObjHdr.Read(*pStrm);
    LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
            static_cast<sal_uInt16>(ObjHdr.GetSize()) );

    if( (sal_uInt32)VO_OBJINDEX == ObjHdr.GetTag() )
    {
        ReadObjIndexData( pObjStrm );
    }
    else if( (sal_uInt32)VO_LEAFOBJINDEX == ObjHdr.GetTag() )
    {
        ReadLeafData(pObjStrm);
    }

    delete pObjStrm;
}

/**
 * @descr   Read VO_LEAFOBJINDEX
 */
void LwpIndexManager::ReadLeafIndex( LwpSvStream *pStrm )
{
    LwpObjectHeader ObjHdr;
    ObjHdr.Read(*pStrm);
    LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
            static_cast<sal_uInt16>(ObjHdr.GetSize()) );

    ReadLeafData(pObjStrm);

    delete pObjStrm;
}
/**
 * @descr   Read data in VO_LEAFOBJINDEX
 */
void LwpIndexManager::ReadLeafData( LwpObjectStream *pObjStrm )
{
    sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();

    if(KeyCount)
    {
        LwpKey* akey = new LwpKey();
        //read object keys: id & offset
        akey->id.Read(pObjStrm);
        m_ObjectKeys.push_back(akey);

        for (sal_uInt8 k = 1; k < KeyCount; k++)
        {
            akey = new LwpKey();
            akey->id.ReadCompressed(pObjStrm, m_ObjectKeys.at(m_nKeyCount+k-1)->id);
            m_ObjectKeys.push_back(akey);
        }

        for (sal_uInt8 j = 0; j < KeyCount; j++)
            m_ObjectKeys.at(m_nKeyCount+j)->offset = pObjStrm->QuickReaduInt32();
    }
    m_nKeyCount += KeyCount;
}
/**
 * @descr   Read time table in VO_ROOTLEAFOBJINDEX and VO_ROOTOBJINDEX
 */
void LwpIndexManager::ReadTimeTable(LwpObjectStream *pObjStrm)
{
    sal_uInt16 nTimeCount = pObjStrm->QuickReaduInt16();

    for(sal_uInt16 i=0; i<nTimeCount; ++i)
    {
        sal_uInt32 atime = pObjStrm->QuickReaduInt32();
        m_TimeTable.push_back(atime);
    }
}
/**
 * @descr       get object offset per the object id
 */
sal_uInt32 LwpIndexManager::GetObjOffset( LwpObjectID objid )
{

    //sal_uInt16 L, U, M;
    sal_uInt32 L, U, M;

    L = 0;
    U = m_nKeyCount;
    while (L != U)
    {
        M = (L + U) >> 1;

        if (objid.GetLow() > m_ObjectKeys[M]->id.GetLow())
            L = M + 1;
        else if (objid.GetLow() < m_ObjectKeys[M]->id.GetLow())
            U = M;
        else if (objid.GetHigh() > m_ObjectKeys[M]->id.GetHigh())
            L = M + 1;
        else if (objid.GetHigh() < m_ObjectKeys[M]->id.GetHigh())
            U = M;
        else
        {
            return(m_ObjectKeys[M]->offset);
        }
    }
    return BAD_OFFSET;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */