source: trunk/gsdl/src/mgpp/text/comp_dict.cpp@ 879

Last change on this file since 879 was 856, checked in by sjboddie, 24 years ago

Rodgers new C++ mg

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/**************************************************************************
2 *
3 * comp_dict.cpp -- Functions for loading the compression dictionary
4 * Copyright (C) 1994 Neil Sharman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id: comp_dict.cpp 856 2000-01-14 02:26:25Z sjboddie $
21 *
22 **************************************************************************/
23
24#include "sysfuncs.h"
25
26#include "huffman.h"
27#include "local_strings.h"
28#include "memlib.h"
29#include "messages.h"
30
31#include "mg.h"
32#include "hash.h"
33#include "text.h"
34#include "comp_dict.h"
35#include "locallib.h"
36#include "mg_files.h"
37
38/*
39 $Log$
40 Revision 1.1 2000/01/14 02:26:04 sjboddie
41 Rodgers new C++ mg
42
43 Revision 1.1 1999/10/11 02:57:10 cs025
44 Base install of MG-PP
45
46 Revision 1.1 1999/08/10 21:17:48 sjboddie
47 renamed mg-1.3d directory mg
48
49 Revision 1.1 1998/11/17 09:34:34 rjmcnab
50 *** empty log message ***
51
52 * Revision 1.3 1994/10/20 03:56:41 tes
53 * I have rewritten the boolean query optimiser and abstracted out the
54 * components of the boolean query.
55 *
56 * Revision 1.2 1994/09/20 04:41:24 tes
57 * For version 1.1
58 *
59 */
60
61
62compression_dict_header cdh;
63compressed_text_header cth;
64comp_frags_header cfh[2];
65
66dict_hash_table *ht[2];
67
68huff_data char_huff[2];
69huff_data lens_huff[2];
70u_long *char_codes[2], *lens_codes[2];
71u_long Words_disk = 0;
72u_long Chars_disk = 0;
73
74
75static dict_hash_table *
76ReadInWords (FILE * dict, comp_frags_header * cfh,
77 int esc)
78{
79 int i;
80 u_char *allwords, *prev = NULL;
81 dict_hash_table *ht;
82 u_char **words;
83 u_long ht_size;
84
85 ht_size = prime (cfh->hd.num_codes * HASH_RATIO);
86 if (!(ht = (dict_hash_table *) Xmalloc (sizeof (dict_hash_table) +
87 (ht_size - 1) * sizeof (ht->table[0]))))
88 {
89 Message ("no memory for hash_table\n");
90 return NULL;
91 }
92
93 ht->size = ht_size;
94 ht->hd = &cfh->hd;
95
96 if (!(ht->codes = Generate_Huffman_Codes (&cfh->hd, NULL)))
97 {
98 Message ("no memory for huffman codes\n");
99 return NULL;
100 }
101
102 if (!(ht->words = (u_char **) Xmalloc (sizeof (u_char *) * cfh->hd.num_codes)))
103 {
104 Message ("no memory for word pointers\n");
105 return NULL;
106 }
107 words = ht->words;
108
109 bzero ((char *) ht->table, ht_size * sizeof (ht->table[0]));
110
111 if (!(allwords = (u_char *) Xmalloc (sizeof (u_char) * cfh->uncompressed_size)))
112 {
113 Message ("no memory for words\n");
114 return NULL;
115 }
116
117 for (i = 0; i < cfh->hd.num_codes; i++, words++)
118 {
119 register int val, copy;
120 val = fgetc (dict);
121 copy = (val >> 4) & 0xf;
122 val &= 0xf;
123
124 *words = allwords;
125
126 memcpy (allwords + 1, prev + 1, copy);
127 fread (allwords + copy + 1, sizeof (u_char), val, dict);
128 *allwords = val + copy;
129
130 Words_disk += val + 1;
131
132 /* insert into the hash table */
133 if (i < cfh->hd.num_codes - esc)
134 {
135 register u_char **wptr;
136 register unsigned long tsize = ht->size;
137 register unsigned long hashval, step;
138
139 HASH (hashval, step, allwords, tsize);
140
141 wptr = ht->table[hashval];
142 for (; wptr;)
143 {
144 hashval += step;
145 if (hashval >= tsize)
146 hashval -= tsize;
147 wptr = ht->table[hashval];
148 }
149 ht->table[hashval] = words;
150 }
151 prev = allwords;
152 allwords += *allwords + 1;
153 }
154 return ht;
155}
156
157inline int LoadWords(FILE *dict, int which)
158{
159 if (Read_cfh (dict, &cfh[which], NULL, &Words_disk) == -1)
160 return 1;
161
162 if (!(ht[which] = ReadInWords (dict, &cfh[which], 0)))
163 return 1;
164
165 return 0;
166}
167
168inline int LoadHuffmanCodes(FILE *dict, int which)
169{
170 if (Read_Huffman_Data (dict, &char_huff[which], NULL,
171 &Chars_disk) == -1)
172 return 1;
173 if (!(char_codes[which] =
174 Generate_Huffman_Codes (&char_huff[which], NULL)))
175 return 2;
176 if (Read_Huffman_Data (dict, &lens_huff[which], NULL,
177 &Chars_disk) == -1)
178 return 3;
179 if (!(lens_codes[which] =
180 Generate_Huffman_Codes (&lens_huff[which], NULL)))
181 return 4;
182
183 return 0;
184}
185
186int
187LoadCompressionDictionary (char *dict_file_name)
188{
189 FILE *dict;
190 int which;
191 if (!(dict = open_named_file (dict_file_name, "rb", MAGIC_DICT, MG_MESSAGE))) /* [RPAP - Feb 97: WIN32 Port] */
192 return COMPERROR;
193
194 Words_disk = sizeof (u_long);
195
196 if (Read_cdh (dict, &cdh, NULL, &Words_disk) == -1)
197 goto error;
198
199 for (which = 0; which < 2; which++)
200 switch (cdh.dict_type)
201 {
202 case MG_COMPLETE_DICTIONARY:
203 {
204 if (LoadWords(dict, which) != 0)
205 goto error;
206 }
207 break;
208 case MG_PARTIAL_DICTIONARY:
209 {
210 if (cdh.num_words[which])
211 {
212 if (LoadWords(dict, which) != 0)
213 goto error;
214 }
215 else
216 ht[which] = NULL;
217 if (LoadHuffmanCodes(dict, which) != 0)
218 goto error;
219 }
220 break;
221 case MG_SEED_DICTIONARY:
222 {
223 if (cdh.num_words[which])
224 {
225 if (LoadWords(dict, which) != 0)
226 goto error;
227 }
228 else
229 ht[which] = NULL;
230 switch (cdh.novel_method)
231 {
232 case MG_NOVEL_HUFFMAN_CHARS:
233 if (LoadHuffmanCodes(dict, which) != 0)
234 goto error;
235 break;
236 case MG_NOVEL_DELTA:
237 break;
238 case MG_NOVEL_HYBRID:
239 break;
240 default:
241 FatalError (1, "Bad novel method");
242 }
243 }
244 break;
245 default:
246 FatalError (1, "Bad dictionary kind\n");
247 }
248
249 return (COMPALLOK);
250
251
252error:
253 fclose (dict);
254 return (COMPERROR);
255}
Note: See TracBrowser for help on using the repository browser.