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
21 #include <ZLZDecompressor.h>
23 #include "ZTXTStream.h"
25 ZTXTStream::ZTXTStream(ZLFile
&file
) : PdbStream(file
) {
28 ZTXTStream::~ZTXTStream() {
32 bool ZTXTStream::open() {
33 if (!PdbStream::open()) {
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) {
46 myBuffer
= new char[myMaxRecordSize
];
53 bool ZTXTStream::fillBuffer() {
54 while (myBufferOffset
== myBufferLength
) {
55 if (myRecordIndex
+ 1 > myMaxRecordIndex
) {
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) {
65 if (currentOffset
< myBase
->offset()) {
68 myBase
->seek(currentOffset
, true);
70 (myRecordIndex
+ 1 < myHeader
.Offsets
.size()) ?
71 myHeader
.Offsets
[myRecordIndex
+ 1] : myBase
->sizeOfOpened();
72 if (nextOffset
< currentOffset
) {
75 myBufferLength
= ZLZDecompressor(nextOffset
- currentOffset
).decompress(*myBase
, myBuffer
, myMaxRecordSize
);