gui
[lbook_fbreader.git] / fbreader / src / formats / pdb / ZTXTStream.cpp
blobd8c41b255a9aa04b540c6a593d2dc97f338c72e2
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>
21 #include <ZLZDecompressor.h>
23 #include "ZTXTStream.h"
25 ZTXTStream::ZTXTStream(ZLFile &file) : PdbStream(file) {
28 ZTXTStream::~ZTXTStream() {
29 close();
32 bool ZTXTStream::open() {
33 if (!PdbStream::open()) {
34 return false;
37 myBase->seek(2, false);
38 unsigned short records;
39 PdbUtil::readUnsignedShort(*myBase, records);
40 myMaxRecordIndex = std::min(records, (unsigned short)(myHeader.Offsets.size() - 1));
41 myBase->seek(4, false);
42 PdbUtil::readUnsignedShort(*myBase, myMaxRecordSize);
43 if (myMaxRecordSize == 0) {
44 return false;
46 myBuffer = new char[myMaxRecordSize];
48 myRecordIndex = 0;
50 return true;
53 bool ZTXTStream::fillBuffer() {
54 while (myBufferOffset == myBufferLength) {
55 if (myRecordIndex + 1 > myMaxRecordIndex) {
56 return false;
58 ++myRecordIndex;
59 size_t currentOffset = myHeader.Offsets[myRecordIndex];
60 // Hmm, this works on examples from manybooks.net,
61 // but I don't what thi code means :((
62 if (myRecordIndex == 1) {
63 currentOffset += 2;
65 if (currentOffset < myBase->offset()) {
66 return false;
68 myBase->seek(currentOffset, true);
69 size_t nextOffset =
70 (myRecordIndex + 1 < myHeader.Offsets.size()) ?
71 myHeader.Offsets[myRecordIndex + 1] : myBase->sizeOfOpened();
72 if (nextOffset < currentOffset) {
73 return false;
75 myBufferLength = ZLZDecompressor(nextOffset - currentOffset).decompress(*myBase, myBuffer, myMaxRecordSize);
76 myBufferOffset = 0;
78 return true;