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