1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * text.pass1.cpp -- Text compression (Pass 1)
|
---|
4 | * Copyright (C) 1994 Neil Sharman, Gary Eddy and Alistair Moffat
|
---|
5 | * Copyright (C) 1999 Rodger McNab
|
---|
6 | *
|
---|
7 | * This program is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This program is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
20 | *
|
---|
21 | **************************************************************************/
|
---|
22 |
|
---|
23 | // need this to avoid bizarre compiler problems under VC++ 6.0
|
---|
24 | #if defined (__WIN32__) && !defined (GSDL_USE_IOS_H)
|
---|
25 | # include <iostream>
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | #include "sysfuncs.h"
|
---|
29 | #include "memlib.h"
|
---|
30 | #include "messages.h"
|
---|
31 | #include "huffman.h"
|
---|
32 | #include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
|
---|
33 | #include "mg_files.h"
|
---|
34 | #include "mg.h"
|
---|
35 | #include "build.h"
|
---|
36 | #include "locallib.h"
|
---|
37 | #include "words.h"
|
---|
38 | #include "text.h"
|
---|
39 | #include "hash.h"
|
---|
40 | #include "local_strings.h"
|
---|
41 | #include "TextEl.h"
|
---|
42 |
|
---|
43 | #define POOL_SIZE 1024*1024
|
---|
44 | #define INITIAL_HASH_SIZE 7927
|
---|
45 |
|
---|
46 |
|
---|
47 | typedef struct hash_rec
|
---|
48 | {
|
---|
49 | unsigned long wcnt; /* word frequency */
|
---|
50 | unsigned long occurance_num;
|
---|
51 | u_char *word;
|
---|
52 | }
|
---|
53 | hash_rec;
|
---|
54 |
|
---|
55 | typedef struct dict_data
|
---|
56 | {
|
---|
57 | hash_rec *HashTable;
|
---|
58 | unsigned long HashSize;
|
---|
59 | unsigned long HashUsed;
|
---|
60 | unsigned long wordnum;
|
---|
61 | unsigned long words_read;
|
---|
62 | unsigned long bytes_diff;
|
---|
63 | huff_data hd;
|
---|
64 | }
|
---|
65 | dict_data;
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | static unsigned long longestDoc = 0;
|
---|
70 | static unsigned long occurance_num = 0;
|
---|
71 | static dict_data DictData[2];
|
---|
72 |
|
---|
73 | static u_char *Pool;
|
---|
74 | static int PoolLeft;
|
---|
75 | static double inputbytes = 0; /* [RJM 07/97: 4G limit] */
|
---|
76 | static unsigned long MaxMemInUse = 0;
|
---|
77 | static unsigned long MemInUse = 0;
|
---|
78 | static compression_stats_header csh = {0, 0, 0.0}; /* [RJM 07/97: 4G limit] */
|
---|
79 |
|
---|
80 |
|
---|
81 | static void ChangeMem (int Change) {
|
---|
82 | MemInUse += Change;
|
---|
83 | if (MemInUse > MaxMemInUse) MaxMemInUse = MemInUse;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | int init_text_1 (const TagInfo &/*tagInfo*/, char * /*FileName*/) {
|
---|
88 | int which;
|
---|
89 |
|
---|
90 | if (!(Pool = (u_char *) Xmalloc (POOL_SIZE))) {
|
---|
91 | Message ("Unable to allocate memory for pool");
|
---|
92 | return (COMPERROR);
|
---|
93 | }
|
---|
94 | PoolLeft = POOL_SIZE;
|
---|
95 | ChangeMem (POOL_SIZE);
|
---|
96 |
|
---|
97 | for (which = 1; which >= 0; which--) {
|
---|
98 | u_char *word;
|
---|
99 | hash_rec *ent;
|
---|
100 | dict_data *dd = &DictData[which];
|
---|
101 |
|
---|
102 | dd->wordnum = 0;
|
---|
103 | dd->words_read = 0;
|
---|
104 | dd->bytes_diff = 0;
|
---|
105 | dd->HashSize = INITIAL_HASH_SIZE;
|
---|
106 | dd->HashUsed = 0;
|
---|
107 |
|
---|
108 | if (!(dd->HashTable = (hash_rec *) Xmalloc (sizeof (hash_rec) * dd->HashSize))) {
|
---|
109 | Message ("Unable to allocate memory for table");
|
---|
110 | return (COMPERROR);
|
---|
111 | }
|
---|
112 | ChangeMem (sizeof (hash_rec) * dd->HashSize);
|
---|
113 | bzero ((char *) (dd->HashTable), sizeof (hash_rec) * dd->HashSize);
|
---|
114 |
|
---|
115 | word = Pool;
|
---|
116 | *Pool++ = '\0';
|
---|
117 | PoolLeft--;
|
---|
118 | {
|
---|
119 | register u_char *wptr;
|
---|
120 | register int hsize = dd->HashSize;
|
---|
121 | register unsigned long hashval, step;
|
---|
122 |
|
---|
123 | HASH (hashval, step, word, hsize);
|
---|
124 | wptr = (dd->HashTable + hashval)->word;
|
---|
125 | while (wptr) {
|
---|
126 | hashval += step;
|
---|
127 | if (hashval >= (unsigned long)hsize)
|
---|
128 | hashval -= hsize;
|
---|
129 | wptr = (dd->HashTable + hashval)->word;
|
---|
130 | }
|
---|
131 | ent = dd->HashTable + hashval;
|
---|
132 | }
|
---|
133 | ent->wcnt = 1;
|
---|
134 | ent->word = word;
|
---|
135 | dd->HashUsed = 1;
|
---|
136 | }
|
---|
137 | return (COMPALLOK);
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | static int process_text_element (const u_char *s_in, int l_in) {
|
---|
142 | const u_char *end = s_in + l_in - 1;
|
---|
143 |
|
---|
144 | /*
|
---|
145 | ** Alternately parse off words and non-words from the input
|
---|
146 | ** stream beginning with a non-word. Each token is then
|
---|
147 | ** inserted into the set if it does not exist or has it's
|
---|
148 | ** frequency count incremented if it does.
|
---|
149 | */
|
---|
150 |
|
---|
151 | bool which = false; // non-word
|
---|
152 | for (; s_in <= end; which = !which) {
|
---|
153 | u_char Word[MAXWORDLEN + 1];
|
---|
154 | dict_data *dd = &DictData[which];
|
---|
155 |
|
---|
156 | /* First parse a word or non-word out of the string */
|
---|
157 | if (which) PARSE_WORD (Word, s_in, end);
|
---|
158 | else PARSE_NON_WORD (Word, s_in, end);
|
---|
159 |
|
---|
160 | dd->wordnum++;
|
---|
161 | inputbytes += *Word;
|
---|
162 | dd->words_read++;
|
---|
163 |
|
---|
164 | /* Search the hash table for Word */
|
---|
165 | {
|
---|
166 | register unsigned long hashval, step;
|
---|
167 | register int hsize = dd->HashSize;
|
---|
168 | HASH (hashval, step, Word, hsize);
|
---|
169 | for (;;) {
|
---|
170 | register u_char *s1;
|
---|
171 | register u_char *s2;
|
---|
172 | register int len;
|
---|
173 | register hash_rec *ent;
|
---|
174 | ent = dd->HashTable + hashval;
|
---|
175 | if (!ent->word) {
|
---|
176 | int len = *Word + 1;
|
---|
177 | if (len > PoolLeft) {
|
---|
178 | if (!(Pool = (u_char *) Xmalloc (POOL_SIZE))) {
|
---|
179 | Message ("Unable to allocate memory for pool");
|
---|
180 | return (COMPERROR);
|
---|
181 | }
|
---|
182 | PoolLeft = POOL_SIZE;
|
---|
183 | ChangeMem (POOL_SIZE);
|
---|
184 | }
|
---|
185 | ent->occurance_num = occurance_num++;
|
---|
186 | ent->wcnt = 1;
|
---|
187 | ent->word = Pool;
|
---|
188 | memcpy (Pool, Word, len);
|
---|
189 | Pool += len;
|
---|
190 | PoolLeft -= len;
|
---|
191 | dd->HashUsed++;
|
---|
192 | dd->bytes_diff += Word[0];
|
---|
193 | break;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /* Compare the words */
|
---|
197 | s1 = Word;
|
---|
198 | s2 = ent->word;
|
---|
199 | len = *s1 + 1;
|
---|
200 | for (; len; len--)
|
---|
201 | if (*s1++ != *s2++) break;
|
---|
202 |
|
---|
203 | if (len) {
|
---|
204 | hashval = (hashval + step);
|
---|
205 | if (hashval >= (unsigned long)hsize) hashval -= hsize;
|
---|
206 | } else {
|
---|
207 | ent->wcnt++;
|
---|
208 | break;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | if (dd->HashUsed >= dd->HashSize >> 1) {
|
---|
215 | hash_rec *ht;
|
---|
216 | unsigned long size;
|
---|
217 | unsigned long i;
|
---|
218 | size = prime (dd->HashSize * 2);
|
---|
219 | if (!(ht = (hash_rec *) Xmalloc (sizeof (hash_rec) * size))) {
|
---|
220 | Message ("Unable to allocate memory for table");
|
---|
221 | return (COMPERROR);
|
---|
222 | }
|
---|
223 | ChangeMem (sizeof (hash_rec) * size);
|
---|
224 | bzero ((char *) ht, sizeof (hash_rec) * size);
|
---|
225 |
|
---|
226 | for (i = 0; i < dd->HashSize; i++)
|
---|
227 | if (dd->HashTable[i].word) {
|
---|
228 | register u_char *wptr;
|
---|
229 | register unsigned long hashval, step;
|
---|
230 |
|
---|
231 | wptr = dd->HashTable[i].word;
|
---|
232 | HASH (hashval, step, wptr, size);
|
---|
233 | wptr = (ht + hashval)->word;
|
---|
234 | while (wptr) {
|
---|
235 | hashval += step;
|
---|
236 | if (hashval >= size) hashval -= size;
|
---|
237 | wptr = (ht + hashval)->word;
|
---|
238 | }
|
---|
239 | ht[hashval] = dd->HashTable[i];
|
---|
240 | }
|
---|
241 | Xfree (dd->HashTable);
|
---|
242 | ChangeMem (-sizeof (hash_rec) * dd->HashSize);
|
---|
243 | dd->HashTable = ht;
|
---|
244 | dd->HashSize = size;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | return COMPALLOK;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | int process_text_1 (const TagInfo &/*tagInfo*/, const TextElArray &doc) {
|
---|
253 | unsigned long textLen = 0;
|
---|
254 | unsigned long docLen = 0;
|
---|
255 | int retValue;
|
---|
256 |
|
---|
257 | // process each text element in this document
|
---|
258 | TextElArray::const_iterator here = doc.begin();
|
---|
259 | TextElArray::const_iterator end = doc.end();
|
---|
260 | while (here != end) {
|
---|
261 | textLen = (*here).text.size();
|
---|
262 | docLen += textLen;
|
---|
263 |
|
---|
264 | retValue = process_text_element ((*here).text.begin(), textLen);
|
---|
265 | if (retValue != COMPALLOK) return retValue;
|
---|
266 |
|
---|
267 | here++;
|
---|
268 | }
|
---|
269 |
|
---|
270 | // get max document length
|
---|
271 | if (docLen > longestDoc) longestDoc = docLen;
|
---|
272 |
|
---|
273 | // update header information
|
---|
274 | csh.num_docs++;
|
---|
275 | csh.num_bytes += docLen;
|
---|
276 |
|
---|
277 | return COMPALLOK;
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | static int PackHashTable (dict_data * dd) {
|
---|
282 | int s, d;
|
---|
283 | for (s = d = 0; (unsigned int)s < dd->HashSize; s++)
|
---|
284 | if (dd->HashTable[s].word)
|
---|
285 | dd->HashTable[d++] = dd->HashTable[s];
|
---|
286 |
|
---|
287 | ChangeMem (-sizeof (hash_rec) * dd->HashSize);
|
---|
288 | ChangeMem (sizeof (hash_rec) * dd->HashUsed);
|
---|
289 |
|
---|
290 | if (!(dd->HashTable = (hash_rec *) Xrealloc (dd->HashTable,
|
---|
291 | sizeof (hash_rec) * dd->HashUsed))) {
|
---|
292 | Message ("Out of memory");
|
---|
293 | return COMPERROR;
|
---|
294 | }
|
---|
295 | dd->HashSize = dd->HashUsed;
|
---|
296 | return COMPALLOK;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | static int ent_comp (const void *s1, const void *s2) {
|
---|
301 | return casecompare (((hash_rec *) s1)->word, ((hash_rec *) s2)->word);
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | static void WriteHashTable (FILE * fp, dict_data * dd) {
|
---|
306 | frags_stats_header fsh;
|
---|
307 | u_long j = 0;
|
---|
308 | u_char *curr;
|
---|
309 |
|
---|
310 | if (PackHashTable (dd) == COMPERROR) return;
|
---|
311 |
|
---|
312 | qsort (dd->HashTable, dd->HashUsed, sizeof (hash_rec), ent_comp);
|
---|
313 |
|
---|
314 | fsh.num_frags = dd->HashSize;
|
---|
315 | fsh.mem_for_frags = dd->HashSize;
|
---|
316 | for (j = 0; j < dd->HashSize; j++)
|
---|
317 | fsh.mem_for_frags += dd->HashTable[j].word[0];
|
---|
318 |
|
---|
319 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
320 | HTONUL(fsh.num_frags);
|
---|
321 | HTONUL(fsh.mem_for_frags);
|
---|
322 |
|
---|
323 | fwrite (&fsh, sizeof (fsh), 1, fp);
|
---|
324 |
|
---|
325 | for (j = 0; j < dd->HashSize; j++) {
|
---|
326 | curr = dd->HashTable[j].word;
|
---|
327 |
|
---|
328 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
329 | HTONUL(dd->HashTable[j].wcnt);
|
---|
330 | HTONUL(dd->HashTable[j].occurance_num);
|
---|
331 |
|
---|
332 | fwrite (&dd->HashTable[j].wcnt, sizeof (dd->HashTable[j].wcnt), 1, fp);
|
---|
333 | fwrite (&dd->HashTable[j].occurance_num,
|
---|
334 | sizeof (dd->HashTable[j].occurance_num), 1, fp);
|
---|
335 |
|
---|
336 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
337 | NTOHUL(dd->HashTable[j].wcnt);
|
---|
338 | NTOHUL(dd->HashTable[j].occurance_num);
|
---|
339 |
|
---|
340 | fwrite (curr, sizeof (u_char), curr[0] + 1, fp);
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 |
|
---|
345 | int done_text_1 (const TagInfo &/*tagInfo*/, char *file_name) {
|
---|
346 | char *temp_str;
|
---|
347 | FILE *fp;
|
---|
348 |
|
---|
349 | if (!(fp = create_file (file_name, TEXT_STATS_DICT_SUFFIX, "wb",
|
---|
350 | MAGIC_STATS_DICT, MG_MESSAGE))) /* [RPAP - Feb 97: WIN32 Port] */
|
---|
351 | return COMPERROR;
|
---|
352 |
|
---|
353 | temp_str = msg_prefix;
|
---|
354 | msg_prefix = "text.pass1";
|
---|
355 |
|
---|
356 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
357 | HTONUL(csh.num_docs);
|
---|
358 | HTOND(csh.num_bytes); /* [RJM 07/97: 4G limit] */
|
---|
359 |
|
---|
360 | fwrite (&csh, sizeof (csh), 1, fp);
|
---|
361 |
|
---|
362 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
363 | NTOHUL(csh.num_docs);
|
---|
364 | NTOHD(csh.num_bytes); /* [RJM 07/97: 4G limit] */
|
---|
365 |
|
---|
366 | WriteHashTable (fp, &DictData[0]);
|
---|
367 | WriteHashTable (fp, &DictData[1]);
|
---|
368 | msg_prefix = temp_str;
|
---|
369 | return COMPALLOK;
|
---|
370 | }
|
---|