Diffstat (limited to 'noncore/apps/opie-reader/ztxt.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-reader/ztxt.cpp | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/noncore/apps/opie-reader/ztxt.cpp b/noncore/apps/opie-reader/ztxt.cpp new file mode 100644 index 0000000..c30e4fd --- a/dev/null +++ b/noncore/apps/opie-reader/ztxt.cpp | |||
@@ -0,0 +1,159 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <string.h> | ||
3 | #include "ztxt.h" | ||
4 | |||
5 | ztxt::ztxt() : bInit(false), expandedtextbuffer(NULL), compressedtextbuffer(NULL) { /*printf("constructing:%x\n",fin);*/ } | ||
6 | |||
7 | |||
8 | int ztxt::openfile(const char *src) | ||
9 | { | ||
10 | if (!Cpdb::openfile(src)) | ||
11 | { | ||
12 | return -1; | ||
13 | } | ||
14 | |||
15 | //printf("Okay %u\n", 4); | ||
16 | |||
17 | if (head.type != ZTXT_ID) return -1; | ||
18 | |||
19 | gotorecordnumber(0); | ||
20 | fread(&hdr0, 1, sizeof(hdr0), fin); | ||
21 | //printf("Okay %u\n", 5); | ||
22 | buffersize = ntohl(hdr0.size); | ||
23 | compressedtextbuffer = new UInt8[buffersize]; | ||
24 | expandedtextbuffer = new UInt8[buffersize]; | ||
25 | //printf("Okay %u\n", 6); | ||
26 | |||
27 | home(); | ||
28 | //printf("Okay %u\n", 7); | ||
29 | |||
30 | // printf("Returning 0\n"); | ||
31 | |||
32 | return 0; | ||
33 | } | ||
34 | |||
35 | int ztxt::getch() | ||
36 | { | ||
37 | if (bufferpos >= buffercontent) | ||
38 | { | ||
39 | size_t reclen = recordlength(++bufferrec); | ||
40 | if (reclen == 0) return -1; | ||
41 | gotorecordnumber(bufferrec); | ||
42 | fread(compressedtextbuffer, reclen, sizeof(char), fin); | ||
43 | |||
44 | zstream.next_in = compressedtextbuffer; | ||
45 | zstream.next_out = expandedtextbuffer; | ||
46 | zstream.avail_out = buffersize; | ||
47 | zstream.avail_in = reclen; | ||
48 | |||
49 | int ret = inflate(&zstream, Z_SYNC_FLUSH); | ||
50 | buffercontent = buffersize - zstream.avail_out; | ||
51 | bufferpos = 0; | ||
52 | |||
53 | } | ||
54 | currentpos++; | ||
55 | return expandedtextbuffer[bufferpos++]; | ||
56 | } | ||
57 | |||
58 | unsigned int ztxt::locate() | ||
59 | { | ||
60 | return currentpos; | ||
61 | } | ||
62 | |||
63 | void ztxt::locate(unsigned int n) | ||
64 | { | ||
65 | |||
66 | if (hasrandomaccess()) | ||
67 | { | ||
68 | bufferrec = n / ntohs(hdr0.recordSize) + 1; | ||
69 | if (bufferrec == 1) | ||
70 | { | ||
71 | inflateEnd(&zstream); | ||
72 | } | ||
73 | size_t reclen = recordlength(bufferrec); | ||
74 | if (reclen == 0) return; | ||
75 | gotorecordnumber(bufferrec); | ||
76 | fread(compressedtextbuffer, reclen, sizeof(char), fin); | ||
77 | |||
78 | zstream.next_in = compressedtextbuffer; | ||
79 | zstream.next_out = expandedtextbuffer; | ||
80 | zstream.avail_out = buffersize; | ||
81 | zstream.avail_in = reclen; | ||
82 | |||
83 | if (bufferrec == 1) | ||
84 | { | ||
85 | zstream.zalloc = Z_NULL; | ||
86 | zstream.zfree = Z_NULL; | ||
87 | zstream.opaque = Z_NULL; | ||
88 | |||
89 | inflateInit(&zstream); | ||
90 | } | ||
91 | |||
92 | |||
93 | int ret = inflate(&zstream, Z_SYNC_FLUSH); | ||
94 | buffercontent = buffersize - zstream.avail_out; | ||
95 | bufferpos = 0; | ||
96 | currentpos = n - n % ntohs(hdr0.recordSize); | ||
97 | while (currentpos < n) getch(); | ||
98 | } | ||
99 | else | ||
100 | { | ||
101 | home(); | ||
102 | while (currentpos < n && getch() != EOF); | ||
103 | } | ||
104 | } | ||
105 | |||
106 | void ztxt::home() | ||
107 | { | ||
108 | if (bInit) | ||
109 | { | ||
110 | inflateEnd(&zstream); | ||
111 | } | ||
112 | bInit = true; | ||
113 | size_t reclen = recordlength(1); | ||
114 | gotorecordnumber(1); | ||
115 | fread(compressedtextbuffer, reclen, sizeof(char), fin); | ||
116 | |||
117 | zstream.next_in = compressedtextbuffer; | ||
118 | zstream.next_out = expandedtextbuffer; | ||
119 | zstream.avail_out = buffersize; | ||
120 | zstream.avail_in = reclen; | ||
121 | |||
122 | zstream.zalloc = Z_NULL; | ||
123 | zstream.zfree = Z_NULL; | ||
124 | zstream.opaque = Z_NULL; | ||
125 | |||
126 | // printf("Initialising\n"); | ||
127 | |||
128 | inflateInit(&zstream); | ||
129 | |||
130 | int ret = inflate(&zstream, Z_SYNC_FLUSH); | ||
131 | // printf("Inflate : %d\n", ret); | ||
132 | bufferpos = 0; | ||
133 | bufferrec = 1; | ||
134 | currentpos = 0; | ||
135 | buffercontent = buffersize - zstream.avail_out; | ||
136 | //printf("buffercontent:%u\n", buffercontent); | ||
137 | } | ||
138 | |||
139 | CList<Bkmk>* ztxt::getbkmklist() | ||
140 | { | ||
141 | UInt16 recno = ntohs(hdr0.bookmarkRecord); | ||
142 | |||
143 | // printf("Bookmarks - record %d contains %d\n", recno, ntohs(hdr0.numBookmarks)); | ||
144 | |||
145 | if (recno == 0) return NULL; | ||
146 | |||
147 | CList<Bkmk>* t = new CList<Bkmk>; | ||
148 | size_t cur = ftell(fin); | ||
149 | gotorecordnumber(recno); | ||
150 | for (int i = 0; i < ntohs(hdr0.numBookmarks); i++) | ||
151 | { | ||
152 | zTXTbkmk bkmk; | ||
153 | if (fread(&bkmk, sizeof(bkmk), 1, fin) != 1) break; | ||
154 | // printf("Bookmark number:%d:%.20s\n", i, bkmk.title); | ||
155 | t->push_back(Bkmk(bkmk.title, ntohl(bkmk.offset))); | ||
156 | } | ||
157 | fseek(fin, cur, SEEK_SET); | ||
158 | return t; | ||
159 | } | ||