1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * IndexData.cpp -- Information needed for querying
|
---|
4 | * Copyright (C) 1999 Rodger McNab
|
---|
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: IndexData.cpp 855 2000-01-14 02:17:52Z sjboddie $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include "IndexData.h"
|
---|
25 | #include "string.h"
|
---|
26 | #include "mg_files.h"
|
---|
27 |
|
---|
28 | IndexData::IndexData () {
|
---|
29 | basePath[0] = '\0';
|
---|
30 | filename[0] = '\0';
|
---|
31 |
|
---|
32 | dictFile = NULL;
|
---|
33 |
|
---|
34 | stem1File = NULL;
|
---|
35 | stem2File = NULL;
|
---|
36 | stem3File = NULL;
|
---|
37 |
|
---|
38 | invfFile = NULL;
|
---|
39 |
|
---|
40 | approxWeightsFile = NULL;
|
---|
41 | exactWeightsFile = NULL;
|
---|
42 |
|
---|
43 | curLevelNum = 0;
|
---|
44 | }
|
---|
45 |
|
---|
46 | IndexData::~IndexData () {
|
---|
47 | UnloadData ();
|
---|
48 | }
|
---|
49 |
|
---|
50 | bool IndexData::LoadData (const char *_basePath, const char *_filename) {
|
---|
51 | if (_filename[0] == '\0') return false;
|
---|
52 |
|
---|
53 | // make sure this data has not already been loaded
|
---|
54 | if (strcmp (_basePath, basePath) == 0 &&
|
---|
55 | strcmp (_filename, filename) == 0) return true;
|
---|
56 |
|
---|
57 | // make sure there is nothing else loaded
|
---|
58 | UnloadData ();
|
---|
59 |
|
---|
60 | strcpy (basePath, _basePath);
|
---|
61 | strcpy (filename, _filename);
|
---|
62 |
|
---|
63 | set_basepath (basePath);
|
---|
64 |
|
---|
65 | // load in the level information
|
---|
66 | FILE *ivfLevelFile;
|
---|
67 | ivfLevelFile = open_file (filename, INVF_LEVEL_SUFFIX, "rb",
|
---|
68 | MAGIC_INVF_LEVELS, MG_ABORT);
|
---|
69 | levels.Read (ivfLevelFile);
|
---|
70 | fclose (ivfLevelFile);
|
---|
71 |
|
---|
72 | // blocked dictionary
|
---|
73 | dictFile = open_file (filename, INVF_DICT_BLOCKED_SUFFIX, "rb",
|
---|
74 | MAGIC_STEM, MG_ABORT);
|
---|
75 | if (!bdh.Read (dictFile)) { UnloadData (); return false; }
|
---|
76 |
|
---|
77 | fseek (dictFile, bdh.wblk_idx_start, SEEK_SET);
|
---|
78 | if (!ReadBlockIdx (dictFile, biWords)) { UnloadData (); return false; }
|
---|
79 |
|
---|
80 | fseek (dictFile, bdh.tblk_idx_start, SEEK_SET);
|
---|
81 | if (!ReadBlockIdx (dictFile, biTags)) { UnloadData (); return false; }
|
---|
82 |
|
---|
83 | // blocked stem index 1
|
---|
84 | stem1File = open_file (filename, INVF_DICT_BLOCKED_1_SUFFIX,
|
---|
85 | "rb", MAGIC_STEM_1, MG_ABORT);
|
---|
86 | if (!sih1.Read (stem1File)) { UnloadData (); return false; }
|
---|
87 |
|
---|
88 | fseek (stem1File, sih1.block_idx_start, SEEK_SET);
|
---|
89 | if (!ReadBlockIdx (stem1File, sii1)) { UnloadData (); return false; }
|
---|
90 |
|
---|
91 | // blocked stem index 2
|
---|
92 | stem2File = open_file (filename, INVF_DICT_BLOCKED_2_SUFFIX,
|
---|
93 | "rb", MAGIC_STEM_2, MG_ABORT);
|
---|
94 | if (!sih2.Read (stem2File)) { UnloadData (); return false; }
|
---|
95 |
|
---|
96 | fseek (stem2File, sih2.block_idx_start, SEEK_SET);
|
---|
97 | if (!ReadBlockIdx (stem2File, sii2)) { UnloadData (); return false; }
|
---|
98 |
|
---|
99 | // blocked stem index 3
|
---|
100 | stem3File = open_file (filename, INVF_DICT_BLOCKED_3_SUFFIX,
|
---|
101 | "rb", MAGIC_STEM_3, MG_ABORT);
|
---|
102 | if (!sih3.Read (stem3File)) { UnloadData (); return false; }
|
---|
103 |
|
---|
104 | fseek (stem3File, sih3.block_idx_start, SEEK_SET);
|
---|
105 | if (!ReadBlockIdx (stem3File, sii3)) { UnloadData (); return false; }
|
---|
106 |
|
---|
107 | // inverted file
|
---|
108 | invfFile = open_file (filename, INVF_SUFFIX, "rb",
|
---|
109 | MAGIC_INVF, MG_ABORT);
|
---|
110 | ifh.Read (invfFile);
|
---|
111 |
|
---|
112 | // weights
|
---|
113 | approxWeightsFile = open_file (filename, APPROX_WEIGHTS_SUFFIX, "rb",
|
---|
114 | MAGIC_WGHT_APPROX, MG_ABORT);
|
---|
115 | exactWeightsFile = open_file (filename, WEIGHTS_SUFFIX, "rb",
|
---|
116 | MAGIC_WGHT, MG_ABORT);
|
---|
117 |
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 |
|
---|
121 | bool IndexData::UnloadData () {
|
---|
122 | basePath[0] = '\0';
|
---|
123 | filename[0] = '\0';
|
---|
124 |
|
---|
125 | if (dictFile != NULL) {
|
---|
126 | fclose (dictFile); dictFile = NULL;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (stem1File != NULL) {
|
---|
130 | fclose (stem1File); stem1File = NULL;
|
---|
131 | }
|
---|
132 | if (stem2File != NULL) {
|
---|
133 | fclose (stem2File); stem2File = NULL;
|
---|
134 | }
|
---|
135 | if (stem3File != NULL) {
|
---|
136 | fclose (stem3File); stem3File = NULL;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (invfFile != NULL) {
|
---|
140 | fclose (invfFile); invfFile = NULL;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if (approxWeightsFile != NULL) {
|
---|
144 | fclose (approxWeightsFile); approxWeightsFile = NULL;
|
---|
145 | }
|
---|
146 | if (exactWeightsFile != NULL) {
|
---|
147 | fclose (exactWeightsFile); exactWeightsFile = NULL;
|
---|
148 | }
|
---|
149 |
|
---|
150 | UnloadLevel();
|
---|
151 |
|
---|
152 | return true;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | bool IndexData::LoadLevel (const UCArray &level) {
|
---|
157 | // make sure this level is not already loaded
|
---|
158 | if (level == curLevel) return true;
|
---|
159 |
|
---|
160 | // unload any levels currently loaded
|
---|
161 | UnloadLevel();
|
---|
162 |
|
---|
163 | // make sure the required files are open
|
---|
164 | if (dictFile == NULL || invfFile == NULL ||
|
---|
165 | approxWeightsFile == NULL || exactWeightsFile == NULL)
|
---|
166 | return false;
|
---|
167 |
|
---|
168 | // read in the information from the dictionary
|
---|
169 | block_dict_el tagEl;
|
---|
170 | unsigned long tagElNum;
|
---|
171 | if (!SearchBlockDictEl (dictFile, biTags, bdh.entries_per_tblk,
|
---|
172 | bdh.tag_dict_size, level, tagEl, tagElNum))
|
---|
173 | return false;
|
---|
174 |
|
---|
175 | // read in the level conversion information
|
---|
176 | if (!levelConverter.Read (invfFile, tagEl.invf_ptr,
|
---|
177 | bdh.num_frags, tagEl.frag_occur))
|
---|
178 | return false;
|
---|
179 |
|
---|
180 | // read in the approximate weights
|
---|
181 | if (!weightData.Read (approxWeightsFile,
|
---|
182 | levels.levelInfo[level].approxWeightsDiskPtr,
|
---|
183 | levels.levelInfo[level].numEntries))
|
---|
184 | return false;
|
---|
185 |
|
---|
186 | // find the level number
|
---|
187 | curLevelNum = 0;
|
---|
188 | IvfLevelInfoMap::const_iterator levelHere, levelEnd;
|
---|
189 | for (levelHere=levels.levelInfo.begin(), levelEnd=levels.levelInfo.end();
|
---|
190 | levelHere!=levelEnd && (*levelHere).first != level; levelHere++)
|
---|
191 | curLevelNum++;
|
---|
192 |
|
---|
193 | // make sure we found the level
|
---|
194 | if (levelHere == levelEnd) return false;
|
---|
195 |
|
---|
196 | curLevel = level;
|
---|
197 |
|
---|
198 | return true;
|
---|
199 | }
|
---|
200 |
|
---|
201 | bool IndexData::UnloadLevel () {
|
---|
202 | curLevel.erase (curLevel.begin(), curLevel.end());
|
---|
203 | curLevelNum = 0;
|
---|
204 |
|
---|
205 | weightData.Free ();
|
---|
206 |
|
---|
207 | return true;
|
---|
208 | }
|
---|
209 |
|
---|