2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "PalmDocStream.h"
23 #include "DocDecompressor.h"
25 PalmDocStream::PalmDocStream(ZLFile
&file
) : PdbStream(file
) {
28 PalmDocStream::~PalmDocStream() {
32 bool PalmDocStream::open() {
33 if (!PdbStream::open()) {
37 unsigned short version
;
38 PdbUtil::readUnsignedShort(*myBase
, version
);
39 myIsCompressed
= (version
== 2);
40 myBase
->seek(6, false);
41 unsigned short records
;
42 PdbUtil::readUnsignedShort(*myBase
, records
);
43 myMaxRecordIndex
= std::min(records
, (unsigned short)(myHeader
.Offsets
.size() - 1));
44 PdbUtil::readUnsignedShort(*myBase
, myMaxRecordSize
);
45 if (myMaxRecordSize
== 0) {
48 myBuffer
= new char[myMaxRecordSize
];
55 bool PalmDocStream::fillBuffer() {
56 while (myBufferOffset
== myBufferLength
) {
57 if (myRecordIndex
+ 1 > myMaxRecordIndex
) {
61 size_t currentOffset
= myHeader
.Offsets
[myRecordIndex
];
62 if (currentOffset
< myBase
->offset()) {
65 myBase
->seek(currentOffset
, true);
67 (myRecordIndex
+ 1 < myHeader
.Offsets
.size()) ?
68 myHeader
.Offsets
[myRecordIndex
+ 1] : myBase
->sizeOfOpened();
69 if (nextOffset
< currentOffset
) {
73 myBufferLength
= DocDecompressor().decompress(*myBase
, myBuffer
, nextOffset
- currentOffset
, myMaxRecordSize
);
75 myBase
->read(myBuffer
, nextOffset
- currentOffset
);
76 myBufferLength
= nextOffset
- currentOffset
;