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
25 #include <ZLZDecompressor.h>
27 #include "TcrStream.h"
29 TcrStream::TcrStream(ZLFile
&file
) : myBase(file
.inputStream()) {
32 TcrStream::~TcrStream() {
36 bool TcrStream::open() {
38 if (myBase
.isNull() || !myBase
->open()) {
43 if ((myBase
->read(header
, 9) != 9) || (strncmp(header
, "!!8-Bit!!", 9) != 0)) {
48 unsigned char entryLength
;
49 char entryBuffer
[255];
50 for (int i
= 0; i
< 256; ++i
) {
51 if ((myBase
->read((char*)&entryLength
, 1) != 1) ||
52 ((entryLength
> 0) && (myBase
->read(entryBuffer
, entryLength
) != entryLength
))) {
56 if (entryLength
> 0) {
57 myDictionary
[i
].append(entryBuffer
, entryLength
);
64 void TcrStream::close() {
65 if (!myBase
.isNull()) {
68 for (int i
= 0; i
< 256; ++i
) {
69 myDictionary
[i
].erase();
74 size_t TcrStream::read(char *buffer
, size_t maxSize
) {
76 if (myBuffer
.length() > 0) {
77 size
+= std::min(maxSize
, myBuffer
.length());
79 strncpy(buffer
, myBuffer
.data(), size
);
81 myBuffer
.erase(0, size
);
83 while (size
< maxSize
) {
85 if (myBase
->read((char*)&index
, 1) != 1) {
88 size_t len
= myDictionary
[index
].length();
90 size_t freeSize
= maxSize
- size
;
92 strncpy(buffer
+ size
, myDictionary
[index
].data(), std::min(len
, freeSize
));
94 size
+= std::min(len
, freeSize
);
96 myBuffer
= myDictionary
[index
].substr(freeSize
);
104 void TcrStream::seek(int offset
, bool absoluteOffset
) {
105 if (absoluteOffset
) {
106 offset
-= this->offset();
110 } else if (offset
< 0) {
111 offset
+= this->offset();
119 size_t TcrStream::offset() const {
123 size_t TcrStream::sizeOfOpened() {