source: main/branches/64_bit_Greenstone/greenstone2/common-src/indexers/mgpp/text/comp_dict.cpp@ 23508

Last change on this file since 23508 was 23508, checked in by sjm84, 13 years ago

Committing 64 bit changes into the branch

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 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 **************************************************************************/
21
22#include "sysfuncs.h"
23
24#include "huffman.h"
25#include "local_strings.h"
26#include "memlib.h"
27#include "messages.h"
28
29#include "mg.h"
30#include "hash.h"
31#include "text.h"
32#include "comp_dict.h"
33#include "locallib.h"
34#include "mg_files.h"
35
36compression_dict_header cdh;
37compressed_text_header cth;
38comp_frags_header cfh[2];
39
40dict_hash_table *ht[2];
41
42huff_data char_huff[2];
43huff_data lens_huff[2];
44mg_u_long *char_codes[2], *lens_codes[2];
45mg_u_long Words_disk = 0;
46mg_u_long Chars_disk = 0;
47
48
49static dict_hash_table *
50ReadInWords (FILE * dict, comp_frags_header * cfh,
51 int esc)
52{
53 int i;
54 u_char *allwords, *prev = NULL;
55 dict_hash_table *ht;
56 u_char **words;
57 mg_u_long ht_size;
58
59 ht_size = prime (cfh->hd.num_codes * HASH_RATIO);
60 if (!(ht = (dict_hash_table *) Xmalloc (sizeof (dict_hash_table) +
61 (ht_size - 1) * sizeof (ht->table[0]))))
62 {
63 Message ("no memory for hash_table\n");
64 return NULL;
65 }
66
67 ht->size = ht_size;
68 ht->hd = &cfh->hd;
69
70 if (!(ht->codes = Generate_Huffman_Codes (&cfh->hd, NULL)))
71 {
72 Message ("no memory for huffman codes\n");
73 return NULL;
74 }
75
76 if (!(ht->words = (u_char **) Xmalloc (sizeof (u_char *) * cfh->hd.num_codes)))
77 {
78 Message ("no memory for word pointers\n");
79 return NULL;
80 }
81 words = ht->words;
82
83 memset (ht->table, '\0', ht_size * sizeof (ht->table[0]));
84
85 if (!(allwords = (u_char *) Xmalloc (sizeof (u_char) * cfh->uncompressed_size)))
86 {
87 Message ("no memory for words\n");
88 return NULL;
89 }
90
91 for (i = 0; i < cfh->hd.num_codes; ++i, ++words)
92 {
93 register int val, copy;
94 val = fgetc (dict);
95 copy = (val >> 4) & 0xf;
96 val &= 0xf;
97
98 *words = allwords;
99
100 memcpy (allwords + 1, prev + 1, copy);
101 size_t numbytes = fread (allwords + copy + 1, sizeof (u_char), val, dict);
102 if (numbytes != val)
103 {
104#ifdef DEBUG
105 fprintf(stderr, "[mgpp/text/comp_dict.cpp L104] number of bytes read by fread does not match the requested amount\n");
106#endif
107 }
108 *allwords = val + copy;
109
110 Words_disk += val + 1;
111
112 /* insert into the hash table */
113 if (i < cfh->hd.num_codes - esc)
114 {
115 register u_char **wptr;
116 register mg_u_long tsize = ht->size;
117 register mg_u_long hashval, step;
118
119 HASH (hashval, step, allwords, tsize);
120
121 wptr = ht->table[hashval];
122 for (; wptr;)
123 {
124 hashval += step;
125 if (hashval >= tsize)
126 hashval -= tsize;
127 wptr = ht->table[hashval];
128 }
129 ht->table[hashval] = words;
130 }
131 prev = allwords;
132 allwords += *allwords + 1;
133 }
134 return ht;
135}
136
137inline int LoadWords(FILE *dict, int which)
138{
139 if (Read_cfh (dict, &cfh[which], NULL, &Words_disk) == -1)
140 return 1;
141
142 if (!(ht[which] = ReadInWords (dict, &cfh[which], 0)))
143 return 1;
144
145 return 0;
146}
147
148inline int LoadHuffmanCodes(FILE *dict, int which)
149{
150 if (Read_Huffman_Data (dict, &char_huff[which], NULL,
151 &Chars_disk) == -1)
152 return 1;
153 if (!(char_codes[which] =
154 Generate_Huffman_Codes (&char_huff[which], NULL)))
155 return 2;
156 if (Read_Huffman_Data (dict, &lens_huff[which], NULL,
157 &Chars_disk) == -1)
158 return 3;
159 if (!(lens_codes[which] =
160 Generate_Huffman_Codes (&lens_huff[which], NULL)))
161 return 4;
162
163 return 0;
164}
165
166int
167LoadCompressionDictionary (char *dict_file_name)
168{
169 FILE *dict;
170 int which;
171 if (!(dict = open_named_file (dict_file_name, "rb", MAGIC_DICT, MG_MESSAGE))) /* [RPAP - Feb 97: WIN32 Port] */
172 return COMPERROR;
173
174 Words_disk = sizeof (mg_u_long);
175
176 if (Read_cdh (dict, &cdh, NULL, &Words_disk) == -1)
177 goto error;
178
179 for (which = 0; which < 2; ++which)
180 switch (cdh.dict_type)
181 {
182 case MG_COMPLETE_DICTIONARY:
183 {
184 if (LoadWords(dict, which) != 0)
185 goto error;
186 }
187 break;
188 case MG_PARTIAL_DICTIONARY:
189 {
190 if (cdh.num_words[which])
191 {
192 if (LoadWords(dict, which) != 0)
193 goto error;
194 }
195 else
196 ht[which] = NULL;
197 if (LoadHuffmanCodes(dict, which) != 0)
198 goto error;
199 }
200 break;
201 case MG_SEED_DICTIONARY:
202 {
203 if (cdh.num_words[which])
204 {
205 if (LoadWords(dict, which) != 0)
206 goto error;
207 }
208 else
209 ht[which] = NULL;
210 switch (cdh.novel_method)
211 {
212 case MG_NOVEL_HUFFMAN_CHARS:
213 if (LoadHuffmanCodes(dict, which) != 0)
214 goto error;
215 break;
216 case MG_NOVEL_DELTA:
217 break;
218 case MG_NOVEL_HYBRID:
219 break;
220 default:
221 FatalError (1, "Bad novel method");
222 }
223 }
224 break;
225 default:
226 FatalError (1, "Bad dictionary kind\n");
227 }
228
229 return (COMPALLOK);
230
231
232error:
233 fclose (dict);
234 return (COMPERROR);
235}
Note: See TracBrowser for help on using the repository browser.