summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-reader/Reb.cpp65
1 files changed, 0 insertions, 65 deletions
diff --git a/noncore/apps/opie-reader/Reb.cpp b/noncore/apps/opie-reader/Reb.cpp
index 2e6c1fc..65de9f6 100644
--- a/noncore/apps/opie-reader/Reb.cpp
+++ b/noncore/apps/opie-reader/Reb.cpp
@@ -1,789 +1,724 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <string.h> 2#include <string.h>
3#include <qimage.h> 3#include <qimage.h>
4#include "decompress.h" 4#include "decompress.h"
5#include "Reb.h" 5#include "Reb.h"
6#include "my_list.h" 6#include "my_list.h"
7#include "Bkmks.h" 7#include "Bkmks.h"
8#include "Model.h" 8#include "Model.h"
9/*
10#ifdef offsetof
11#define OffsetOf(type, field) ((int) offsetof(type, field))
12#else
13#define OffsetOf(type, field) ((int) ((char *) &((type *) 0)->field))
14#endif
15
16template<class T>
17UInt32 binarychop(T* data, UInt32 n, T val)
18{
19 UInt32 jl = 0,jh = n-1,jm = (jl+jh)/2;
20 while (jh > jl+1)
21 {
22 if (data[jm] > val)
23 {
24 jh = jm;
25 }
26 else
27 {
28 jl = jm;
29 }
30 jm = (jl+jh)/2;
31 }
32 return jl;
33}
34
35template<class T, class D>
36UInt32 binarychop(D* data, UInt32 n, T val, UInt32 offset)
37{
38 UInt32 jl = 0,jh = n-1,jm = (jl+jh)/2;
39 while (jh > jl+1)
40 {
41 T* d = reinterpret_cast<T*>(reinterpret_cast<char*>(data+jm)+offset);
42 if (*d > val)
43 {
44 jh = jm;
45 }
46 else
47 {
48 jl = jm;
49 }
50 jm = (jl+jh)/2;
51 }
52 return jl;
53}
54*/
55 9
56CReb::CReb() 10CReb::CReb()
57: 11:
58 fin(NULL), m_indexpages(NULL), m_pagedetails(NULL),tagoffset(0), 12 fin(NULL), m_indexpages(NULL), m_pagedetails(NULL),tagoffset(0),
59 tags(NULL), paras(NULL), noparas(0), joins(NULL), nojoins(0) 13 tags(NULL), paras(NULL), noparas(0), joins(NULL), nojoins(0)
60{ 14{
61} 15}
62 16
63CReb::~CReb() 17CReb::~CReb()
64{ 18{
65 if (fin != NULL) fclose(fin); 19 if (fin != NULL) fclose(fin);
66 if (m_indexpages != NULL) delete [] m_indexpages; 20 if (m_indexpages != NULL) delete [] m_indexpages;
67 if (m_pagedetails != NULL) delete [] m_pagedetails; 21 if (m_pagedetails != NULL) delete [] m_pagedetails;
68 if (tags != NULL) delete [] tags; 22 if (tags != NULL) delete [] tags;
69 if (paras != NULL) delete [] paras; 23 if (paras != NULL) delete [] paras;
70 if (joins != NULL) delete [] joins; 24 if (joins != NULL) delete [] joins;
71} 25}
72 26
73unsigned int CReb::locate() 27unsigned int CReb::locate()
74{ 28{
75 return m_pagedetails[currentpage.pageno()].pagestart+currentpage.offset(); 29 return m_pagedetails[currentpage.pageno()].pagestart+currentpage.offset();
76} 30}
77 31
78void CReb::locate(unsigned int n) 32void CReb::locate(unsigned int n)
79{ 33{
80 /*
81 UInt32 cp = nopages-1;
82 for (int i = 0; i < nopages; ++i)
83 {
84 if (m_pagedetails[i].pagestart > n)
85 {
86 cp = i-1;
87 break;
88 }
89 }
90 qDebug("Requesting %u from page %u [%u]", n, cp, n - m_pagedetails[cp].pagestart);
91 */
92 //UInt32 jl = binarychop<UInt32, Page_detail>(m_pagedetails, nopages, n, OffsetOf(Page_detail, pagestart));
93
94 UInt32 jl = 0,jh = nopages-1,jm = (jl+jh)/2; 34 UInt32 jl = 0,jh = nopages-1,jm = (jl+jh)/2;
95 while (jh > jl+1) 35 while (jh > jl+1)
96 { 36 {
97 if (m_pagedetails[jm].pagestart > n) 37 if (m_pagedetails[jm].pagestart > n)
98 { 38 {
99 jh = jm; 39 jh = jm;
100 } 40 }
101 else 41 else
102 { 42 {
103 jl = jm; 43 jl = jm;
104 } 44 }
105 jm = (jl+jh)/2; 45 jm = (jl+jh)/2;
106 } 46 }
107 47
108 unsuspend(); 48 unsuspend();
109 Page_detail rs = m_pagedetails[jl]; 49 Page_detail rs = m_pagedetails[jl];
110 UInt32 val = n - rs.pagestart; 50 UInt32 val = n - rs.pagestart;
111 if (jl != currentpage.pageno()) readindex(jl); 51 if (jl != currentpage.pageno()) readindex(jl);
112 currentpage.setoffset(page2pos(jl), jl, ((rs.flags & 8) != 0), rs.len, val); 52 currentpage.setoffset(page2pos(jl), jl, ((rs.flags & 8) != 0), rs.len, val);
113 if (noparas > 0) 53 if (noparas > 0)
114 { 54 {
115 //jl = binarychop<int, ParaRef>(paras, noparas, val, OffsetOf(ParaRef, pos));
116
117 UInt32 jl = 0,jh = noparas-1,jm = (jl+jh)/2; 55 UInt32 jl = 0,jh = noparas-1,jm = (jl+jh)/2;
118 while (jh > jl+1) 56 while (jh > jl+1)
119 { 57 {
120 if (paras[jm].pos > val) 58 if (paras[jm].pos > val)
121 { 59 {
122 jh = jm; 60 jh = jm;
123 } 61 }
124 else 62 else
125 { 63 {
126 jl = jm; 64 jl = jm;
127 } 65 }
128 jm = (jl+jh)/2; 66 jm = (jl+jh)/2;
129 } 67 }
130 68
131 qDebug("TAGS:%s", (const char*)tags[paras[jl].tag]); 69 qDebug("TAGS:%s", (const char*)tags[paras[jl].tag]);
132 tagstring = tags[paras[jl].tag]+"<br>"; // Add br to set extra space to 0 70 tagstring = tags[paras[jl].tag]+"<br>"; // Add br to set extra space to 0
133 tagoffset = 0; 71 tagoffset = 0;
134 } 72 }
135 unsigned long current = locate(); 73 unsigned long current = locate();
136 if (m_currentstart > current || current > m_currentend) 74 if (m_currentstart > current || current > m_currentend)
137 { 75 {
138 start2endSection(); 76 start2endSection();
139 } 77 }
140 if (current != n) qDebug("ERROR:Ended up at %u", current); 78 if (current != n) qDebug("ERROR:Ended up at %u", current);
141} 79}
142 80
143bool CReb::getFile(const QString& href, const QString& nm) 81bool CReb::getFile(const QString& href, const QString& nm)
144{ 82{
145 qDebug("File:%s, Name:%s", (const char*)href, (const char*)nm); 83 qDebug("File:%s, Name:%s", (const char*)href, (const char*)nm);
146 QMap<QString, UInt32>::Iterator iter = m_index.find(href); 84 QMap<QString, UInt32>::Iterator iter = m_index.find(href);
147 if (iter != m_index.end()) 85 if (iter != m_index.end())
148 { 86 {
149 qDebug("REB:BEFORE:%u", locate()); 87 qDebug("REB:BEFORE:%u", locate());
150 startpage(iter.data()); 88 startpage(iter.data());
151 qDebug("REB:AFTER:%u", locate()); 89 qDebug("REB:AFTER:%u", locate());
152 return true; 90 return true;
153 } 91 }
154 else 92 else
155 { 93 {
156 return false; 94 return false;
157 } 95 }
158} 96}
159 97
160QImage* CReb::getPicture(const QString& ref) 98QImage* CReb::getPicture(const QString& ref)
161{ 99{
162 QMap<QString, UInt32>::Iterator iter = m_index.find(ref); 100 QMap<QString, UInt32>::Iterator iter = m_index.find(ref);
163 if (iter != m_index.end()) 101 if (iter != m_index.end())
164 { 102 {
165 unsuspend(); 103 unsuspend();
166 Page_detail rs = m_pagedetails[iter.data()]; 104 Page_detail rs = m_pagedetails[iter.data()];
167 char* imgbuffer = new char[rs.len]; 105 char* imgbuffer = new char[rs.len];
168 fseek(fin, page2pos(iter.data()), SEEK_SET); 106 fseek(fin, page2pos(iter.data()), SEEK_SET);
169 fread(imgbuffer, rs.len, 1, fin); 107 fread(imgbuffer, rs.len, 1, fin);
170 QByteArray arr; 108 QByteArray arr;
171 arr.assign((const char*)imgbuffer, rs.len); 109 arr.assign((const char*)imgbuffer, rs.len);
172 QImage* qimage = new QImage(arr); 110 QImage* qimage = new QImage(arr);
173 return qimage; 111 return qimage;
174 } 112 }
175 else 113 else
176 { 114 {
177 return NULL; 115 return NULL;
178 } 116 }
179} 117}
180 118
181CList<Bkmk>* CReb::getbkmklist() { return NULL; } 119CList<Bkmk>* CReb::getbkmklist() { return NULL; }
182 120
183void CReb::home() 121void CReb::home()
184{ 122{
185 startpage(m_homepage); 123 startpage(m_homepage);
186} 124}
187 125
188int CReb::OpenFile(const char *src) 126int CReb::OpenFile(const char *src)
189{ 127{
190 m_binary = false; 128 m_binary = false;
191 if (fin != NULL) fclose(fin); 129 if (fin != NULL) fclose(fin);
192 fin = fopen(src, "r"); 130 fin = fopen(src, "r");
193 if (fin == NULL) 131 if (fin == NULL)
194 { 132 {
195 return -1; 133 return -1;
196 } 134 }
197 UInt32 type; 135 UInt32 type;
198 fseek(fin, 6, SEEK_SET); 136 fseek(fin, 6, SEEK_SET);
199 fread(&type, 1, sizeof(type), fin); 137 fread(&type, 1, sizeof(type), fin);
200 qDebug("CREB:Okay %x", type); 138 qDebug("CREB:Okay %x", type);
201 139
202 if (type == 0x4f56554e || type == 0x574d4954 || type == 0x576d6954) 140 if (type == 0x4f56554e || type == 0x574d4954 || type == 0x576d6954)
203 { 141 {
204 struct stat _stat; 142 struct stat _stat;
205 stat(src, &_stat); 143 stat(src, &_stat);
206 file_length = _stat.st_size; 144 file_length = _stat.st_size;
207 fread(&m_blocksize, 1, sizeof(m_blocksize), fin); 145 fread(&m_blocksize, 1, sizeof(m_blocksize), fin);
208 if (type == 0x574d4954 || type == 0x576d6954) 146 if (type == 0x574d4954 || type == 0x576d6954)
209 { 147 {
210 if (type == 0x576d6954) m_binary = true; 148 if (type == 0x576d6954) m_binary = true;
211 qDebug("Blocksize(1) %x", m_blocksize); 149 qDebug("Blocksize(1) %x", m_blocksize);
212 unsigned char ct = (m_blocksize >> 24) & 0xff; 150 unsigned char ct = (m_blocksize >> 24) & 0xff;
213 qDebug("Compress type:%x", ct); 151 qDebug("Compress type:%x", ct);
214 switch (ct) 152 switch (ct)
215 { 153 {
216 case 0: 154 case 0:
217 m_decompress = UnZip; 155 m_decompress = UnZip;
218 break; 156 break;
219 case 3: 157 case 3:
220 m_decompress = getdecompressor("PluckerDecompress3"); 158 m_decompress = getdecompressor("PluckerDecompress3");
221 break; 159 break;
222 case 4: 160 case 4:
223 m_decompress = getdecompressor("PluckerDecompress4"); 161 m_decompress = getdecompressor("PluckerDecompress4");
224 break; 162 break;
225 } 163 }
226 if (m_decompress == NULL) return -1; 164 if (m_decompress == NULL) return -1;
227 m_blocksize = 1024*(m_blocksize & 0xffffff); 165 m_blocksize = 1024*(m_blocksize & 0xffffff);
228 } 166 }
229 else 167 else
230 { 168 {
231 m_blocksize = 4096; 169 m_blocksize = 4096;
232 m_decompress = UnZip; 170 m_decompress = UnZip;
233 } 171 }
234 qDebug("Blocksize %u", m_blocksize); 172 qDebug("Blocksize %u", m_blocksize);
235 currentpage.init(fin, m_blocksize, m_decompress); 173 currentpage.init(fin, m_blocksize, m_decompress);
236 qDebug("Its a REB!!!!"); 174 qDebug("Its a REB!!!!");
237 fseek(fin, 0x18, SEEK_SET); 175 fseek(fin, 0x18, SEEK_SET);
238 fread(&toc, 1, sizeof(toc), fin); 176 fread(&toc, 1, sizeof(toc), fin);
239 qDebug("Expect this to be 128 or 20:%x", toc); 177 qDebug("Expect this to be 128 or 20:%x", toc);
240 fread(&type, 1, sizeof(type), fin); 178 fread(&type, 1, sizeof(type), fin);
241 qDebug("File length:%u", type); 179 qDebug("File length:%u", type);
242 fseek(fin, toc, SEEK_SET); 180 fseek(fin, toc, SEEK_SET);
243 fread(&nopages, 1, sizeof(nopages), fin); 181 fread(&nopages, 1, sizeof(nopages), fin);
244 m_indexpages = new UInt32[nopages]; 182 m_indexpages = new UInt32[nopages];
245 m_pagedetails = new Page_detail[nopages]; 183 m_pagedetails = new Page_detail[nopages];
246 qDebug("There are %u pages", nopages); 184 qDebug("There are %u pages", nopages);
247 UInt32 loc = 0; 185 UInt32 loc = 0;
248 UInt32 homeguess = nopages-1; 186 UInt32 homeguess = nopages-1;
249 QString homeurl; 187 QString homeurl;
250 for (int i = 0; i < nopages; ++i) 188 for (int i = 0; i < nopages; ++i)
251 { 189 {
252 char name[32]; 190 char name[32];
253 UInt32 len, pos, flags; 191 UInt32 len, pos, flags;
254 fread(name, 1, 32, fin); 192 fread(name, 1, 32, fin);
255 fread(&len, 1, 4, fin); 193 fread(&len, 1, 4, fin);
256 fread(&pos, 1, 4, fin); 194 fread(&pos, 1, 4, fin);
257 fread(&flags, 1, 4, fin); 195 fread(&flags, 1, 4, fin);
258 //qDebug("Page %u (%s) is %u bytes at %u (%u) of type %u", i, name, len, pos, loc, flags); 196 //qDebug("Page %u (%s) is %u bytes at %u (%u) of type %u", i, name, len, pos, loc, flags);
259 m_index[name] = i; 197 m_index[name] = i;
260 m_pagedetails[i] = Page_detail(loc, len, flags); 198 m_pagedetails[i] = Page_detail(loc, len, flags);
261 199
262 if (QString(name).find(".htm", 0, false) >= 0) 200 if (QString(name).find(".htm", 0, false) >= 0)
263 { 201 {
264 if (homeguess > i) homeguess = i; 202 if (homeguess > i) homeguess = i;
265 if ((flags & 8) != 0) 203 if ((flags & 8) != 0)
266 { 204 {
267 UInt32 lastpos = ftell(fin); 205 UInt32 lastpos = ftell(fin);
268 loc += pagelength(i); 206 loc += pagelength(i);
269 fseek(fin, lastpos, SEEK_SET); 207 fseek(fin, lastpos, SEEK_SET);
270 } 208 }
271 else 209 else
272 { 210 {
273 loc += len; 211 loc += len;
274 } 212 }
275 } 213 }
276 if ((flags & 2) != 0) 214 if ((flags & 2) != 0)
277 { 215 {
278 UInt32 lastpos = ftell(fin); 216 UInt32 lastpos = ftell(fin);
279 RBPage* idx = new RBPage(); 217 RBPage* idx = new RBPage();
280 idx->init(fin, m_blocksize, m_decompress); 218 idx->init(fin, m_blocksize, m_decompress);
281 idx->startpage(page2pos(i), i, ((flags & 8) != 0), len); 219 idx->startpage(page2pos(i), i, ((flags & 8) != 0), len);
282 int c = 0; 220 int c = 0;
283 while (c != EOF) 221 while (c != EOF)
284 { 222 {
285 QString s(""); 223 QString s("");
286 while (1) 224 while (1)
287 { 225 {
288 c = idx->getch(this); 226 c = idx->getch(this);
289 if (c == 10 || c == EOF) break; 227 if (c == 10 || c == EOF) break;
290 s += c; 228 s += c;
291 } 229 }
292 if (s.left(5) == "BODY=") 230 if (s.left(5) == "BODY=")
293 { 231 {
294 homeurl = s.right(s.length()-5); 232 homeurl = s.right(s.length()-5);
295 qDebug("Home:%s", (const char*)homeurl); 233 qDebug("Home:%s", (const char*)homeurl);
296 } 234 }
297 else 235 else
298 { 236 {
299 qDebug("Info:%s", (const char*)s); 237 qDebug("Info:%s", (const char*)s);
300 } 238 }
301 } 239 }
302 delete idx; 240 delete idx;
303 fseek(fin, lastpos, SEEK_SET); 241 fseek(fin, lastpos, SEEK_SET);
304 } 242 }
305 } 243 }
306 text_length = loc; 244 text_length = loc;
307 qDebug("Looking for homepage"); 245 qDebug("Looking for homepage");
308 if (homeurl.isEmpty()) 246 if (homeurl.isEmpty())
309 { 247 {
310 m_homepage = homeguess; 248 m_homepage = homeguess;
311 } 249 }
312 else 250 else
313 { 251 {
314 QMap<QString, UInt32>::Iterator iter = m_index.find(homeurl); 252 QMap<QString, UInt32>::Iterator iter = m_index.find(homeurl);
315 if (iter != m_index.end()) 253 if (iter != m_index.end())
316 { 254 {
317 m_homepage = iter.data(); 255 m_homepage = iter.data();
318 } 256 }
319 else 257 else
320 { 258 {
321 m_homepage = homeguess; 259 m_homepage = homeguess;
322 } 260 }
323 } 261 }
324 m_homepos = m_pagedetails[m_homepage].pagestart; 262 m_homepos = m_pagedetails[m_homepage].pagestart;
325 qDebug("Finding indices"); 263 qDebug("Finding indices");
326 for (QMap<QString, UInt32>::Iterator iter = m_index.begin(); iter != m_index.end(); ++iter) 264 for (QMap<QString, UInt32>::Iterator iter = m_index.begin(); iter != m_index.end(); ++iter)
327 { 265 {
328 QString href = iter.key(); 266 QString href = iter.key();
329 if (href.find(".htm", 0, false) >= 0) 267 if (href.find(".htm", 0, false) >= 0)
330 { 268 {
331 QString hind = href.left(href.find(".htm", 0, false))+".hidx"; 269 QString hind = href.left(href.find(".htm", 0, false))+".hidx";
332 //qDebug("Index is %s", (const char*)hind); 270 //qDebug("Index is %s", (const char*)hind);
333 QMap<QString, UInt32>::Iterator iter2 = m_index.find(hind); 271 QMap<QString, UInt32>::Iterator iter2 = m_index.find(hind);
334 if (iter2 != m_index.end()) 272 if (iter2 != m_index.end())
335 { 273 {
336 m_indexpages[iter.data()] = iter2.data(); 274 m_indexpages[iter.data()] = iter2.data();
337 } 275 }
338 } 276 }
339 } 277 }
340 qDebug("Going home"); 278 qDebug("Going home");
341 home(); 279 home();
342 return 0; 280 return 0;
343 } 281 }
344 else 282 else
345 { 283 {
346 char * tmp = (char*)(&type); 284 char * tmp = (char*)(&type);
347 for (int i = 0; i < 4; ++i) qDebug("%d:%c", i, tmp[i]); 285 for (int i = 0; i < 4; ++i) qDebug("%d:%c", i, tmp[i]);
348 return -1; 286 return -1;
349 } 287 }
350} 288}
351 289
352UInt32 CReb::page2pos(UInt32 page) 290UInt32 CReb::page2pos(UInt32 page)
353{ 291{
354 fseek(fin, toc+40+44*page, SEEK_SET); 292 fseek(fin, toc+40+44*page, SEEK_SET);
355 UInt32 pos; 293 UInt32 pos;
356 fread(&pos, 1, 4, fin); 294 fread(&pos, 1, 4, fin);
357 return pos; 295 return pos;
358} 296}
359 297
360UInt32 CReb::pagelength(UInt32 pagenum) 298UInt32 CReb::pagelength(UInt32 pagenum)
361{ 299{
362 fseek(fin, toc+40+44*pagenum, SEEK_SET); 300 fseek(fin, toc+40+44*pagenum, SEEK_SET);
363 UInt32 pos; 301 UInt32 pos;
364 fread(&pos, 1, 4, fin); 302 fread(&pos, 1, 4, fin);
365 fseek(fin, pos+4, SEEK_SET); 303 fseek(fin, pos+4, SEEK_SET);
366 UInt32 len; 304 UInt32 len;
367 fread(&len, 1, sizeof(len), fin); 305 fread(&len, 1, sizeof(len), fin);
368 return len; 306 return len;
369} 307}
370 308
371void CReb::readindex(UInt32 cp) 309void CReb::readindex(UInt32 cp)
372{ 310{
373 if (joins != NULL) 311 if (joins != NULL)
374 { 312 {
375 delete [] joins; 313 delete [] joins;
376 joins = NULL; 314 joins = NULL;
377 } 315 }
378 if (tags != NULL) 316 if (tags != NULL)
379 { 317 {
380 delete [] tags; 318 delete [] tags;
381 tags = NULL; 319 tags = NULL;
382 } 320 }
383 if (paras != NULL) 321 if (paras != NULL)
384 { 322 {
385 delete [] paras; 323 delete [] paras;
386 paras = NULL; 324 paras = NULL;
387 } 325 }
388 noparas = 0; 326 noparas = 0;
389 nojoins = 0; 327 nojoins = 0;
390 names.clear(); 328 names.clear();
391 329
392 UInt32 rspage = m_indexpages[cp]; 330 UInt32 rspage = m_indexpages[cp];
393 if (rspage != 0) 331 if (rspage != 0)
394 { 332 {
395 Page_detail rs = m_pagedetails[rspage]; 333 Page_detail rs = m_pagedetails[rspage];
396 int count = 0; 334 int count = 0;
397 RBPage* idx = new RBPage(); 335 RBPage* idx = new RBPage();
398 idx->init(fin, m_blocksize, m_decompress); 336 idx->init(fin, m_blocksize, m_decompress);
399 idx->startpage(page2pos(rspage), rspage, ((rs.flags & 8) != 0), rs.len); 337 idx->startpage(page2pos(rspage), rspage, ((rs.flags & 8) != 0), rs.len);
400 int c = 0; 338 int c = 0;
401 int phase = 0; 339 int phase = 0;
402 int i; 340 int i;
403 if (m_binary) 341 if (m_binary)
404 { 342 {
405 count = idx->getuint(this); 343 count = idx->getuint(this);
406 qDebug("tag count:%d", count); 344 qDebug("tag count:%d", count);
407 tags = new QString[count]; 345 tags = new QString[count];
408 for (int i = 0; i < count; ++i) 346 for (int i = 0; i < count; ++i)
409 { 347 {
410 QString s; 348 QString s;
411 while (1) 349 while (1)
412 { 350 {
413 c = idx->getch(this); 351 c = idx->getch(this);
414 if (c == 0 || c == EOF) break; 352 if (c == 0 || c == EOF) break;
415 s += c; 353 s += c;
416 } 354 }
417 unsigned short val = idx->getuint(this); 355 unsigned short val = idx->getuint(this);
418 if (val != 0xffff) 356 if (val != 0xffff)
419 { 357 {
420 tags[i] = tags[val]+s; 358 tags[i] = tags[val]+s;
421 } 359 }
422 else 360 else
423 { 361 {
424 tags[i] = s; 362 tags[i] = s;
425 } 363 }
426 //qDebug("tags[%d](%d) = %s", i, val, (const char*)tags[i]); 364 //qDebug("tags[%d](%d) = %s", i, val, (const char*)tags[i]);
427 } 365 }
428 noparas = idx->getint(this); 366 noparas = idx->getint(this);
429 qDebug("Para count %d", noparas); 367 qDebug("Para count %d", noparas);
430 paras = new ParaRef[noparas]; 368 paras = new ParaRef[noparas];
431 for (int i = 0; i < noparas; ++i) 369 for (int i = 0; i < noparas; ++i)
432 { 370 {
433 paras[i] = ParaRef(idx->getint(this), idx->getuint(this)); 371 paras[i] = ParaRef(idx->getint(this), idx->getuint(this));
434 } 372 }
435 count = idx->getint(this); 373 count = idx->getint(this);
436 qDebug("Name count %d", count); 374 qDebug("Name count %d", count);
437 for (int i = 0; i < count; ++i) 375 for (int i = 0; i < count; ++i)
438 { 376 {
439 QString s; 377 QString s;
440 while (1) 378 while (1)
441 { 379 {
442 c = idx->getch(this); 380 c = idx->getch(this);
443 if (c == 0 || c == EOF) break; 381 if (c == 0 || c == EOF) break;
444 s += c; 382 s += c;
445 } 383 }
446 int val = idx->getint(this); 384 int val = idx->getint(this);
447 names[s.mid(1,s.length()-2)] = val; 385 names[s.mid(1,s.length()-2)] = val;
448 qDebug("names[%s] = %d", (const char*)s, val); 386 qDebug("names[%s] = %d", (const char*)s, val);
449 } 387 }
450 count = idx->getint(this); 388 count = idx->getint(this);
451 qDebug("Join count %d", count); 389 qDebug("Join count %d", count);
452 if (count > 0) 390 if (count > 0)
453 { 391 {
454 nojoins = count+2; 392 nojoins = count+2;
455 joins = new UInt32[count+2]; 393 joins = new UInt32[count+2];
456 joins[0] = 0; 394 joins[0] = 0;
457 joins[count+1] = currentpage.length(); 395 joins[count+1] = currentpage.length();
458 for (int i = 1; i < count+1; ++i) 396 for (int i = 1; i < count+1; ++i)
459 { 397 {
460 joins[i] = idx->getint(this); 398 joins[i] = idx->getint(this);
461 } 399 }
462 } 400 }
463 } 401 }
464 else 402 else
465 { 403 {
466 while (c != EOF) 404 while (c != EOF)
467 { 405 {
468 QString s(""); 406 QString s("");
469 while (1) 407 while (1)
470 { 408 {
471 c = idx->getch(this); 409 c = idx->getch(this);
472 if (c == 10 || c == EOF) break; 410 if (c == 10 || c == EOF) break;
473 s += c; 411 s += c;
474 } 412 }
475 //qDebug("%s", (const char*)s); 413 //qDebug("%s", (const char*)s);
476 if (count > 0) 414 if (count > 0)
477 { 415 {
478 --count; 416 --count;
479 int sp = s.findRev(' '); 417 int sp = s.findRev(' ');
480 QString l = s.left(sp); 418 QString l = s.left(sp);
481 int val = s.right(s.length()-sp).toInt(); 419 int val = s.right(s.length()-sp).toInt();
482 switch (phase) 420 switch (phase)
483 { 421 {
484 case 4: 422 case 4:
485 //qDebug("Join %d is at offset %d", i, val); 423 //qDebug("Join %d is at offset %d", i, val);
486 joins[i++] = val; 424 joins[i++] = val;
487 break; 425 break;
488 case 3: 426 case 3:
489 //qDebug("Name %s is at offset %d", (const char*)l.mid(1,l.length()-2), val+m_pagedetails[cp].pagestart); 427 //qDebug("Name %s is at offset %d", (const char*)l.mid(1,l.length()-2), val+m_pagedetails[cp].pagestart);
490 names[l.mid(1,l.length()-2)] = val; 428 names[l.mid(1,l.length()-2)] = val;
491 break; 429 break;
492 case 1: 430 case 1:
493 //qDebug("%s:%d [%d]", (const char*)l, val, i); 431 //qDebug("%s:%d [%d]", (const char*)l, val, i);
494 if (val >= 0) 432 if (val >= 0)
495 { 433 {
496 tags[i++] = tags[val]+l; 434 tags[i++] = tags[val]+l;
497 } 435 }
498 else 436 else
499 { 437 {
500 tags[i++] = l; 438 tags[i++] = l;
501 } 439 }
502 //qDebug("TAG:%s", (const char*)tags[i-1]); 440 //qDebug("TAG:%s", (const char*)tags[i-1]);
503 break; 441 break;
504 case 2: 442 case 2:
505 paras[i++] = ParaRef(QString(l).toInt(), val); 443 paras[i++] = ParaRef(QString(l).toInt(), val);
506 //qDebug("Para:%u - %u (%s)", QString(l).toInt(), val, (const char*)s); 444 //qDebug("Para:%u - %u (%s)", QString(l).toInt(), val, (const char*)s);
507 break; 445 break;
508 default: 446 default:
509 qDebug("%s:%d", (const char*)l, val); 447 qDebug("%s:%d", (const char*)l, val);
510 break; 448 break;
511 } 449 }
512 } 450 }
513 else 451 else
514 { 452 {
515 QString key = "[tags "; 453 QString key = "[tags ";
516 if (s.left(key.length()) == key) 454 if (s.left(key.length()) == key)
517 { 455 {
518 phase = 1; 456 phase = 1;
519 i = 0; 457 i = 0;
520 count = s.mid(key.length(),s.length()-key.length()-1).toInt(); 458 count = s.mid(key.length(),s.length()-key.length()-1).toInt();
521 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count); 459 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count);
522 tags = new QString[count]; 460 tags = new QString[count];
523 } 461 }
524 key = "[paragraphs "; 462 key = "[paragraphs ";
525 if (s.left(key.length()) == key) 463 if (s.left(key.length()) == key)
526 { 464 {
527 phase = 2; 465 phase = 2;
528 i = 0; 466 i = 0;
529 count = s.mid(key.length(),s.length()-key.length()-1).toInt(); 467 count = s.mid(key.length(),s.length()-key.length()-1).toInt();
530 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count); 468 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count);
531 paras = new ParaRef[count]; 469 paras = new ParaRef[count];
532 noparas = count; 470 noparas = count;
533 } 471 }
534 key = "[names "; 472 key = "[names ";
535 if (s.left(key.length()) == key) 473 if (s.left(key.length()) == key)
536 { 474 {
537 phase = 3; 475 phase = 3;
538 count = s.mid(key.length(),s.length()-key.length()-1).toInt(); 476 count = s.mid(key.length(),s.length()-key.length()-1).toInt();
539 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count); 477 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count);
540 } 478 }
541 key = "[joins "; 479 key = "[joins ";
542 if (s.left(key.length()) == key) 480 if (s.left(key.length()) == key)
543 { 481 {
544 phase = 4; 482 phase = 4;
545 count = s.mid(key.length(),s.length()-key.length()-1).toInt(); 483 count = s.mid(key.length(),s.length()-key.length()-1).toInt();
546 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count); 484 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count);
547 nojoins = count+2; 485 nojoins = count+2;
548 i = 1; 486 i = 1;
549 joins = new UInt32[count+2]; 487 joins = new UInt32[count+2];
550 joins[0] = 0; 488 joins[0] = 0;
551 joins[count+1] = currentpage.length(); 489 joins[count+1] = currentpage.length();
552 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count); 490 qDebug("%s:%s:%d", (const char*)key, (const char*)s, count);
553 } 491 }
554 qDebug("ZC:%s", (const char*)s); 492 qDebug("ZC:%s", (const char*)s);
555 } 493 }
556 } 494 }
557 } 495 }
558 // for (int i = 0; i < nojoins; ++i) qDebug("JOINS:%u %u", i, joins[i]); 496 // for (int i = 0; i < nojoins; ++i) qDebug("JOINS:%u %u", i, joins[i]);
559 delete idx; 497 delete idx;
560 } 498 }
561} 499}
562 500
563bool CReb::findanchor(const QString& _info) 501bool CReb::findanchor(const QString& _info)
564{ 502{
565 QMap<QString, int>::Iterator iter = names.find(_info); 503 QMap<QString, int>::Iterator iter = names.find(_info);
566 if (iter != names.end()) 504 if (iter != names.end())
567 { 505 {
568 locate(iter.data()+m_pagedetails[currentpage.pageno()].pagestart); 506 locate(iter.data()+m_pagedetails[currentpage.pageno()].pagestart);
569 return true; 507 return true;
570 } 508 }
571 return false; 509 return false;
572} 510}
573 511
574#ifdef USEQPE 512#ifdef USEQPE
575void CReb::suspend() 513void CReb::suspend()
576{ 514{
577 CExpander::suspend(fin); 515 CExpander::suspend(fin);
578} 516}
579void CReb::unsuspend() 517void CReb::unsuspend()
580{ 518{
581 CExpander::unsuspend(fin); 519 CExpander::unsuspend(fin);
582} 520}
583#endif 521#endif
584 522
585#ifndef __STATIC 523#ifndef __STATIC
586extern "C" 524extern "C"
587{ 525{
588 CExpander* newcodec() { return new CReb; } 526 CExpander* newcodec() { return new CReb; }
589} 527}
590#endif 528#endif
591 529
592void CReb::startpage(UInt32 pgno) 530void CReb::startpage(UInt32 pgno)
593{ 531{
594 Page_detail rs = m_pagedetails[pgno]; 532 Page_detail rs = m_pagedetails[pgno];
595 unsuspend(); 533 unsuspend();
596 readindex(pgno); 534 readindex(pgno);
597 currentpage.startpage(page2pos(pgno), pgno, ((rs.flags & 8) != 0), rs.len); 535 currentpage.startpage(page2pos(pgno), pgno, ((rs.flags & 8) != 0), rs.len);
598} 536}
599 537
600void CReb::startpage(UInt32 _cp, bool _isCompressed, UInt32 _len) 538void CReb::startpage(UInt32 _cp, bool _isCompressed, UInt32 _len)
601{ 539{
602 unsuspend(); 540 unsuspend();
603 readindex(_cp); 541 readindex(_cp);
604 currentpage.startpage(page2pos(_cp), _cp, _isCompressed, _len); 542 currentpage.startpage(page2pos(_cp), _cp, _isCompressed, _len);
605} 543}
606 544
607void RBPage::initpage(UInt32 pos, size_t _cp, bool _isCompressed, UInt32 _len) 545void RBPage::initpage(UInt32 pos, size_t _cp, bool _isCompressed, UInt32 _len)
608{ 546{
609 filepos = pos; 547 filepos = pos;
610 m_pageno = _cp; 548 m_pageno = _cp;
611 m_Compressed = _isCompressed; 549 m_Compressed = _isCompressed;
612 m_pagelen = _len; 550 m_pagelen = _len;
613 currentchunk = 0; 551 currentchunk = 0;
614 pageoffset = 0; 552 pageoffset = 0;
615 553
616 if (chunklist != NULL) delete [] chunklist; 554 if (chunklist != NULL) delete [] chunklist;
617 555
618 fseek(fin, filepos, SEEK_SET); 556 fseek(fin, filepos, SEEK_SET);
619 if (m_Compressed) 557 if (m_Compressed)
620 { 558 {
621 fread(&nochunks, 1, sizeof(nochunks), fin); 559 fread(&nochunks, 1, sizeof(nochunks), fin);
622 fread(&m_pagelen, 1, sizeof(m_pagelen), fin); 560 fread(&m_pagelen, 1, sizeof(m_pagelen), fin);
623 chunklist = new UInt32[nochunks]; 561 chunklist = new UInt32[nochunks];
624 fread(chunklist, nochunks, 4, fin); 562 fread(chunklist, nochunks, 4, fin);
625 } 563 }
626 else 564 else
627 { 565 {
628 chunklist = NULL; 566 chunklist = NULL;
629 nochunks = (_len+m_blocksize-1)/m_blocksize; 567 nochunks = (_len+m_blocksize-1)/m_blocksize;
630 } 568 }
631 m_startoff = 0; 569 m_startoff = 0;
632 m_endoff = m_pagelen; 570 m_endoff = m_pagelen;
633 chunkpos = ftell(fin); 571 chunkpos = ftell(fin);
634 qDebug("Compressed:%u Expanded:%u", _len, m_pagelen); 572 qDebug("Compressed:%u Expanded:%u", _len, m_pagelen);
635} 573}
636 574
637void RBPage::startpage(UInt32 pos, UInt32 _cp, bool _isCompressed, UInt32 _len) 575void RBPage::startpage(UInt32 pos, UInt32 _cp, bool _isCompressed, UInt32 _len)
638{ 576{
639 initpage(pos, _cp, _isCompressed, _len); 577 initpage(pos, _cp, _isCompressed, _len);
640 readchunk(); 578 readchunk();
641} 579}
642 580
643int CReb::getch() 581int CReb::getch()
644{ 582{
645 if (tagoffset < tagstring.length()) 583 if (tagoffset < tagstring.length())
646 return tagstring[tagoffset++].unicode(); 584 return tagstring[tagoffset++].unicode();
647 else 585 else
648 return currentpage.getch(this); 586 return currentpage.getch(this);
649} 587}
650 588
651int RBPage::getch(CReb* parent) 589int RBPage::getch(CReb* parent)
652{ 590{
653 if (chunkoffset >= chunklen) 591 if (chunkoffset >= chunklen)
654 { 592 {
655 if (++currentchunk >= nochunks) 593 if (++currentchunk >= nochunks)
656 { 594 {
657 --currentchunk; 595 --currentchunk;
658 return EOF; 596 return EOF;
659 } 597 }
660 pageoffset += chunklen; 598 pageoffset += chunklen;
661 parent->unsuspend(); 599 parent->unsuspend();
662 readchunk(); 600 readchunk();
663 } 601 }
664 if (offset() == m_endoff) return EOF; 602 if (offset() == m_endoff) return EOF;
665 return chunk[chunkoffset++]; 603 return chunk[chunkoffset++];
666} 604}
667 605
668unsigned short int RBPage::getuint(CReb* parent) 606unsigned short int RBPage::getuint(CReb* parent)
669{ 607{
670 unsigned short int ret = 0; 608 unsigned short int ret = 0;
671 char *buffer = (char*)(&ret); 609 char *buffer = (char*)(&ret);
672 for (int i = 0; i < 2; ++i) 610 for (int i = 0; i < 2; ++i)
673 { 611 {
674 int ch = getch(parent); 612 int ch = getch(parent);
675 if (ch == EOF) return 0; 613 if (ch == EOF) return 0;
676 buffer[i] = ch; 614 buffer[i] = ch;
677 } 615 }
678 return ret; 616 return ret;
679} 617}
680 618
681int RBPage::getint(CReb* parent) 619int RBPage::getint(CReb* parent)
682{ 620{
683 int ret = 0; 621 int ret = 0;
684 char *buffer = (char*)(&ret); 622 char *buffer = (char*)(&ret);
685 for (int i = 0; i < 4; ++i) 623 for (int i = 0; i < 4; ++i)
686 { 624 {
687 int ch = getch(parent); 625 int ch = getch(parent);
688 if (ch == EOF) return 0; 626 if (ch == EOF) return 0;
689 buffer[i] = ch; 627 buffer[i] = ch;
690 } 628 }
691 return ret; 629 return ret;
692} 630}
693 631
694void RBPage::readchunk() 632void RBPage::readchunk()
695{ 633{
696 if (m_Compressed) 634 if (m_Compressed)
697 { 635 {
698 chunkoffset = 0; 636 chunkoffset = 0;
699 fseek(fin, chunkpos, SEEK_SET); 637 fseek(fin, chunkpos, SEEK_SET);
700 UInt8* inbuf = new UInt8[chunklist[currentchunk]]; 638 UInt8* inbuf = new UInt8[chunklist[currentchunk]];
701 fread(inbuf, 1, chunklist[currentchunk], fin); 639 fread(inbuf, 1, chunklist[currentchunk], fin);
702 chunklen = (*m_decompress)(inbuf, chunklist[currentchunk], chunk, m_blocksize); 640 chunklen = (*m_decompress)(inbuf, chunklist[currentchunk], chunk, m_blocksize);
703 delete [] inbuf; 641 delete [] inbuf;
704 chunkpos = ftell(fin); 642 chunkpos = ftell(fin);
705 } 643 }
706 else 644 else
707 { 645 {
708 chunkoffset = 0; 646 chunkoffset = 0;
709 chunklen = m_blocksize; 647 chunklen = m_blocksize;
710 if (m_blocksize*(currentchunk+1) > m_pagelen) 648 if (m_blocksize*(currentchunk+1) > m_pagelen)
711 { 649 {
712 chunklen = m_pagelen - currentchunk*m_blocksize; 650 chunklen = m_pagelen - currentchunk*m_blocksize;
713 } 651 }
714 fseek(fin, chunkpos, SEEK_SET); 652 fseek(fin, chunkpos, SEEK_SET);
715 chunklen = fread(chunk, 1, chunklen, fin); 653 chunklen = fread(chunk, 1, chunklen, fin);
716 chunkpos = ftell(fin); 654 chunkpos = ftell(fin);
717 } 655 }
718} 656}
719 657
720void RBPage::setoffset(UInt32 pos, size_t _cp, bool _isCompressed, UInt32 _len, UInt32 _offset) 658void RBPage::setoffset(UInt32 pos, size_t _cp, bool _isCompressed, UInt32 _len, UInt32 _offset)
721{ 659{
722 if (m_pageno != _cp) 660 if (m_pageno != _cp)
723 { 661 {
724 initpage(pos, _cp, _isCompressed, _len); 662 initpage(pos, _cp, _isCompressed, _len);
725 } 663 }
726 else 664 else
727 { 665 {
728 if (m_Compressed) 666 if (m_Compressed)
729 { 667 {
730 chunkpos = filepos + sizeof(nochunks) + sizeof(m_pagelen) + 4*nochunks; 668 chunkpos = filepos + sizeof(nochunks) + sizeof(m_pagelen) + 4*nochunks;
731 } 669 }
732 else 670 else
733 { 671 {
734 chunkpos = filepos; 672 chunkpos = filepos;
735 } 673 }
736 } 674 }
737 675
738 currentchunk = _offset/m_blocksize; 676 currentchunk = _offset/m_blocksize;
739 pageoffset = m_blocksize*currentchunk; 677 pageoffset = m_blocksize*currentchunk;
740 if (m_Compressed) 678 if (m_Compressed)
741 { 679 {
742 for (int i = 0; i < currentchunk; ++i) 680 for (int i = 0; i < currentchunk; ++i)
743 { 681 {
744 chunkpos += chunklist[i]; 682 chunkpos += chunklist[i];
745 } 683 }
746 } 684 }
747 else 685 else
748 { 686 {
749 chunkpos += pageoffset; 687 chunkpos += pageoffset;
750 } 688 }
751 readchunk(); 689 readchunk();
752 chunkoffset = _offset - pageoffset; 690 chunkoffset = _offset - pageoffset;
753} 691}
754 692
755void CReb::start2endSection() 693void CReb::start2endSection()
756{ 694{
757 if (m_pagedetails != NULL) 695 if (m_pagedetails != NULL)
758 { 696 {
759 if (nojoins > 0) 697 if (nojoins > 0)
760 { 698 {
761 //UInt32 jl = binarychop<UInt32>(joins, nojoins, currentpage.offset());
762
763 UInt32 jl = 0,jh = nojoins-1,jm = (jl+jh)/2; 699 UInt32 jl = 0,jh = nojoins-1,jm = (jl+jh)/2;
764 while (jh > jl+1) 700 while (jh > jl+1)
765 { 701 {
766 if (joins[jm] > currentpage.offset()) 702 if (joins[jm] > currentpage.offset())
767 { 703 {
768 jh = jm; 704 jh = jm;
769 } 705 }
770 else 706 else
771 { 707 {
772 jl = jm; 708 jl = jm;
773 } 709 }
774 jm = (jl+jh)/2; 710 jm = (jl+jh)/2;
775 } 711 }
776 712
777 currentpage.m_startoff = joins[jl]; 713 currentpage.m_startoff = joins[jl];
778 currentpage.m_endoff = joins[jl+1]-1; 714 currentpage.m_endoff = joins[jl+1]-1;
779 //currentpage.m_endoff = joins[jh]-1;
780 } 715 }
781 m_currentstart = m_pagedetails[currentpage.pageno()].pagestart+currentpage.m_startoff; 716 m_currentstart = m_pagedetails[currentpage.pageno()].pagestart+currentpage.m_startoff;
782 m_currentend = m_pagedetails[currentpage.pageno()].pagestart+currentpage.m_endoff; 717 m_currentend = m_pagedetails[currentpage.pageno()].pagestart+currentpage.m_endoff;
783 } 718 }
784 else 719 else
785 { 720 {
786 m_currentstart = m_currentend = 0; 721 m_currentstart = m_currentend = 0;
787 } 722 }
788 qDebug("s2e:[%u, %u, %u]", m_currentstart, locate(), m_currentend); 723 qDebug("s2e:[%u, %u, %u]", m_currentstart, locate(), m_currentend);
789} 724}