gui
[lbook_fbreader.git] / fbreader / src / formats / pdb / PalmDocStream.cpp
blobe69eb92ba0edc4d5bd482d5a1eaa77593b154191
1 /*
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
17 * 02110-1301, USA.
20 #include <ZLFile.h>
22 #include "PalmDocStream.h"
23 #include "DocDecompressor.h"
25 PalmDocStream::PalmDocStream(ZLFile &file) : PdbStream(file) {
28 PalmDocStream::~PalmDocStream() {
29 close();
32 bool PalmDocStream::open() {
33 if (!PdbStream::open()) {
34 return false;
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) {
46 return false;
48 myBuffer = new char[myMaxRecordSize];
50 myRecordIndex = 0;
52 return true;
55 bool PalmDocStream::fillBuffer() {
56 while (myBufferOffset == myBufferLength) {
57 if (myRecordIndex + 1 > myMaxRecordIndex) {
58 return false;
60 ++myRecordIndex;
61 size_t currentOffset = myHeader.Offsets[myRecordIndex];
62 if (currentOffset < myBase->offset()) {
63 return false;
65 myBase->seek(currentOffset, true);
66 size_t nextOffset =
67 (myRecordIndex + 1 < myHeader.Offsets.size()) ?
68 myHeader.Offsets[myRecordIndex + 1] : myBase->sizeOfOpened();
69 if (nextOffset < currentOffset) {
70 return false;
72 if (myIsCompressed) {
73 myBufferLength = DocDecompressor().decompress(*myBase, myBuffer, nextOffset - currentOffset, myMaxRecordSize);
74 } else {
75 myBase->read(myBuffer, nextOffset - currentOffset);
76 myBufferLength = nextOffset - currentOffset;
78 myBufferOffset = 0;
80 return true;