gui
[lbook_fbreader.git] / fbreader / src / formats / pdb / PluckerTextStream.cpp
blobcf7d28046c60b7d42bde84eb5a2f57b3d21234b1
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 <ZLFile.h>
23 #include <ZLZDecompressor.h>
25 #include "PluckerTextStream.h"
26 #include "PdbReader.h"
27 #include "DocDecompressor.h"
29 PluckerTextStream::PluckerTextStream(ZLFile &file) : PdbStream(file) {
30 myFullBuffer = 0;
33 PluckerTextStream::~PluckerTextStream() {
34 close();
37 bool PluckerTextStream::open() {
38 if (!PdbStream::open()) {
39 return false;
42 PdbUtil::readUnsignedShort(*myBase, myCompressionVersion);
44 myBuffer = new char[65536];
45 myFullBuffer = new char[65536];
47 myRecordIndex = 0;
49 return true;
52 bool PluckerTextStream::fillBuffer() {
53 while (myBufferOffset == myBufferLength) {
54 if (myRecordIndex + 1 > myHeader.Offsets.size() - 1) {
55 return false;
57 ++myRecordIndex;
58 size_t currentOffset = myHeader.Offsets[myRecordIndex];
59 if (currentOffset < myBase->offset()) {
60 return false;
62 myBase->seek(currentOffset, true);
63 size_t nextOffset =
64 (myRecordIndex + 1 < myHeader.Offsets.size()) ?
65 myHeader.Offsets[myRecordIndex + 1] : myBase->sizeOfOpened();
66 if (nextOffset < currentOffset) {
67 return false;
69 processRecord(nextOffset - currentOffset);
71 return true;
74 void PluckerTextStream::close() {
75 if (myFullBuffer != 0) {
76 delete[] myFullBuffer;
77 myFullBuffer = 0;
79 PdbStream::close();
82 void PluckerTextStream::processRecord(size_t recordSize) {
83 myBase->seek(2, false);
85 unsigned short paragraphs;
86 PdbUtil::readUnsignedShort(*myBase, paragraphs);
88 unsigned short size;
89 PdbUtil::readUnsignedShort(*myBase, size);
91 unsigned char type;
92 myBase->read((char*)&type, 1);
93 if (type > 1) { // this record is not text record
94 return;
97 myBase->seek(1, false);
99 std::vector<int> pars;
100 for (int i = 0; i < paragraphs; ++i) {
101 unsigned short pSize;
102 PdbUtil::readUnsignedShort(*myBase, pSize);
103 pars.push_back(pSize);
104 myBase->seek(2, false);
107 bool doProcess = false;
108 if (type == 0) {
109 doProcess = myBase->read(myFullBuffer, size) == size;
110 } else if (myCompressionVersion == 1) {
111 doProcess =
112 DocDecompressor().decompress(*myBase, myFullBuffer, recordSize - 8 - 4 * paragraphs, size) == size;
113 } else if (myCompressionVersion == 2) {
114 myBase->seek(2, false);
115 doProcess =
116 ZLZDecompressor(recordSize - 10 - 4 * paragraphs).decompress(*myBase, myFullBuffer, size) == size;
118 if (doProcess) {
119 myBufferLength = 0;
120 myBufferOffset = 0;
122 char *start = myFullBuffer;
123 char *end = myFullBuffer;
125 for (std::vector<int>::const_iterator it = pars.begin(); it != pars.end(); ++it) {
126 start = end;
127 end = start + *it;
128 if (end > myFullBuffer + size) {
129 break;
131 processTextParagraph(start, end);
136 void PluckerTextStream::processTextParagraph(char *start, char *end) {
137 char *textStart = start;
138 bool functionFlag = false;
139 for (char *ptr = start; ptr < end; ++ptr) {
140 if (*ptr == 0) {
141 functionFlag = true;
142 if (ptr != textStart) {
143 memcpy(myBuffer + myBufferLength, textStart, ptr - textStart);
144 myBufferLength += ptr - textStart;
146 } else if (functionFlag) {
147 int paramCounter = ((unsigned char)*ptr) % 8;
148 if (end - ptr > paramCounter + 1) {
149 ptr += paramCounter;
150 } else {
151 ptr = end - 1;
153 functionFlag = false;
154 textStart = ptr + 1;
157 if (end != textStart) {
158 memcpy(myBuffer + myBufferLength, textStart, end - textStart);
159 myBufferLength += end - textStart;