gui
[lbook_fbreader.git] / fbreader / src / formats / tcr / TcrStream.cpp
blob751bb7355131b97430f87f1c7590577b2c0cd118
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 <string.h>
22 #include <algorithm>
24 #include <ZLFile.h>
25 #include <ZLZDecompressor.h>
27 #include "TcrStream.h"
29 TcrStream::TcrStream(ZLFile &file) : myBase(file.inputStream()) {
32 TcrStream::~TcrStream() {
33 close();
36 bool TcrStream::open() {
37 close();
38 if (myBase.isNull() || !myBase->open()) {
39 return false;
42 char header[9];
43 if ((myBase->read(header, 9) != 9) || (strncmp(header, "!!8-Bit!!", 9) != 0)) {
44 myBase->close();
45 return false;
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))) {
53 myBase->close();
54 return false;
56 if (entryLength > 0) {
57 myDictionary[i].append(entryBuffer, entryLength);
61 return true;
64 void TcrStream::close() {
65 if (!myBase.isNull()) {
66 myBase->close();
68 for (int i = 0; i < 256; ++i) {
69 myDictionary[i].erase();
71 myBuffer.erase();
74 size_t TcrStream::read(char *buffer, size_t maxSize) {
75 size_t size = 0;
76 if (myBuffer.length() > 0) {
77 size += std::min(maxSize, myBuffer.length());
78 if (buffer != 0) {
79 strncpy(buffer, myBuffer.data(), size);
81 myBuffer.erase(0, size);
83 while (size < maxSize) {
84 unsigned char index;
85 if (myBase->read((char*)&index, 1) != 1) {
86 break;
88 size_t len = myDictionary[index].length();
89 if (len > 0) {
90 size_t freeSize = maxSize - size;
91 if (buffer != 0) {
92 strncpy(buffer + size, myDictionary[index].data(), std::min(len, freeSize));
94 size += std::min(len, freeSize);
95 if (len > freeSize) {
96 myBuffer = myDictionary[index].substr(freeSize);
100 myOffset += size;
101 return size;
104 void TcrStream::seek(int offset, bool absoluteOffset) {
105 if (absoluteOffset) {
106 offset -= this->offset();
108 if (offset > 0) {
109 read(0, offset);
110 } else if (offset < 0) {
111 offset += this->offset();
112 open();
113 if (offset >= 0) {
114 read(0, offset);
119 size_t TcrStream::offset() const {
120 return myOffset;
123 size_t TcrStream::sizeOfOpened() {
124 // TODO: implement
125 return 0;