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
23 #include <ZLZDecompressor.h>
25 #include "PluckerTextStream.h"
26 #include "PdbReader.h"
27 #include "DocDecompressor.h"
29 PluckerTextStream::PluckerTextStream(ZLFile
&file
) : PdbStream(file
) {
33 PluckerTextStream::~PluckerTextStream() {
37 bool PluckerTextStream::open() {
38 if (!PdbStream::open()) {
42 PdbUtil::readUnsignedShort(*myBase
, myCompressionVersion
);
44 myBuffer
= new char[65536];
45 myFullBuffer
= new char[65536];
52 bool PluckerTextStream::fillBuffer() {
53 while (myBufferOffset
== myBufferLength
) {
54 if (myRecordIndex
+ 1 > myHeader
.Offsets
.size() - 1) {
58 size_t currentOffset
= myHeader
.Offsets
[myRecordIndex
];
59 if (currentOffset
< myBase
->offset()) {
62 myBase
->seek(currentOffset
, true);
64 (myRecordIndex
+ 1 < myHeader
.Offsets
.size()) ?
65 myHeader
.Offsets
[myRecordIndex
+ 1] : myBase
->sizeOfOpened();
66 if (nextOffset
< currentOffset
) {
69 processRecord(nextOffset
- currentOffset
);
74 void PluckerTextStream::close() {
75 if (myFullBuffer
!= 0) {
76 delete[] myFullBuffer
;
82 void PluckerTextStream::processRecord(size_t recordSize
) {
83 myBase
->seek(2, false);
85 unsigned short paragraphs
;
86 PdbUtil::readUnsignedShort(*myBase
, paragraphs
);
89 PdbUtil::readUnsignedShort(*myBase
, size
);
92 myBase
->read((char*)&type
, 1);
93 if (type
> 1) { // this record is not text record
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;
109 doProcess
= myBase
->read(myFullBuffer
, size
) == size
;
110 } else if (myCompressionVersion
== 1) {
112 DocDecompressor().decompress(*myBase
, myFullBuffer
, recordSize
- 8 - 4 * paragraphs
, size
) == size
;
113 } else if (myCompressionVersion
== 2) {
114 myBase
->seek(2, false);
116 ZLZDecompressor(recordSize
- 10 - 4 * paragraphs
).decompress(*myBase
, myFullBuffer
, size
) == size
;
122 char *start
= myFullBuffer
;
123 char *end
= myFullBuffer
;
125 for (std::vector
<int>::const_iterator it
= pars
.begin(); it
!= pars
.end(); ++it
) {
128 if (end
> myFullBuffer
+ size
) {
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
) {
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) {
153 functionFlag
= false;
157 if (end
!= textStart
) {
158 memcpy(myBuffer
+ myBufferLength
, textStart
, end
- textStart
);
159 myBufferLength
+= end
- textStart
;