| 1 |
/* |
| 2 |
* Aiksaurus - An open source thesaurus library |
| 3 |
* Copyright (C) 2001 by Jared Davis |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 18 |
* 02111-1307, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "MeaningsFile.h" |
| 23 |
#include "MeaningsHash.h" |
| 24 |
|
| 25 |
|
| 26 |
////////////////////////////////////////////////////////////////////////// |
| 27 |
// |
| 28 |
// MeaningStream Class |
| 29 |
// |
| 30 |
////////////////////////////////////////////////////////////////////////// |
| 31 |
|
| 32 |
namespace AiksaurusImpl |
| 33 |
{ |
| 34 |
|
| 35 |
// |
| 36 |
// BUF_SIZE: number of links to read from the meanings file |
| 37 |
// per read. Note: will read (BUF_SIZE * 2) bytes per read |
| 38 |
// since each link is 2 bytes long. |
| 39 |
// |
| 40 |
// This constant is completely arbitrary. Other values may |
| 41 |
// provide better performance, but this seems to work well. |
| 42 |
// |
| 43 |
static const int BUF_SIZE = 256; |
| 44 |
|
| 45 |
|
| 46 |
// |
| 47 |
// MeaningStream Class |
| 48 |
// Purpose: read input from the Meanings file. |
| 49 |
// |
| 50 |
class MeaningStream |
| 51 |
{ |
| 52 |
private: |
| 53 |
|
| 54 |
// No copying or assignment, please. |
| 55 |
MeaningStream(MeaningStream& rhs); |
| 56 |
MeaningStream& operator=(MeaningStream& rhs); |
| 57 |
|
| 58 |
// d_file_ptr: pointer to the meanings file stream. |
| 59 |
FILE* d_file_ptr; |
| 60 |
|
| 61 |
// d_position: our current position in our read buffer. |
| 62 |
int d_position; |
| 63 |
|
| 64 |
// d_end: the size of our read buffer. |
| 65 |
int d_end; |
| 66 |
|
| 67 |
// d_buffer: our read buffer itself. |
| 68 |
unsigned char d_buffer[BUF_SIZE * 2]; |
| 69 |
|
| 70 |
|
| 71 |
public: |
| 72 |
|
| 73 |
// MeaningStream Constructor: |
| 74 |
// Initialize d_file_ptr to the correct FILE* structure. |
| 75 |
// Initialize d_position and d_end so that the stream will |
| 76 |
// be read at first call to read(). |
| 77 |
MeaningStream(FILE* file_ptr); |
| 78 |
|
| 79 |
|
| 80 |
// MeaningStream::read |
| 81 |
// Put the next integer in the stream into x. |
| 82 |
// Return true normally, false if cannot read any more. |
| 83 |
bool read(int& x, AiksaurusError& err); |
| 84 |
}; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
// |
| 90 |
// MeaningStream Constructor |
| 91 |
// Store pointer to FILE structure, and tell our buffer that it is |
| 92 |
// empty to begin with. |
| 93 |
// |
| 94 |
inline |
| 95 |
AiksaurusImpl::MeaningStream::MeaningStream(FILE* file_ptr) |
| 96 |
: d_file_ptr(file_ptr), |
| 97 |
d_position(0), |
| 98 |
d_end(0) |
| 99 |
{ |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
// |
| 105 |
// MeaningStream Read Function |
| 106 |
// |
| 107 |
// Read a single link from the meaning stream. We actually read |
| 108 |
// chunks of the file at a time, and then return pieces of those |
| 109 |
// chunks. |
| 110 |
// |
| 111 |
// Returns TRUE if able to read a link, FALSE if unable to read |
| 112 |
// a link. (EOF or problem reading file). |
| 113 |
// |
| 114 |
inline bool |
| 115 |
AiksaurusImpl::MeaningStream::read(int& x, AiksaurusError& err) |
| 116 |
{ |
| 117 |
bool ret = true; |
| 118 |
|
| 119 |
// If position is less than end, there is still some buffer left |
| 120 |
// that we can read from. We want to read the next two bytes |
| 121 |
// from the buffer into x. |
| 122 |
if (d_position < d_end) |
| 123 |
{ |
| 124 |
x = d_buffer[d_position++] << 8; |
| 125 |
x |= d_buffer[d_position++]; |
| 126 |
} |
| 127 |
|
| 128 |
// Otherwise, we have reached the end of our buffer. We need |
| 129 |
// to read a new buffer in. |
| 130 |
else |
| 131 |
{ |
| 132 |
// Attempt to read BUF_SIZE * 2 bytes from the file. |
| 133 |
d_end = fread(d_buffer, 1, BUF_SIZE * 2, d_file_ptr); |
| 134 |
|
| 135 |
// Make sure there weren't any errors reading from the |
| 136 |
// file. We don't want to continue if there was an error. |
| 137 |
if (ferror(d_file_ptr)) |
| 138 |
{ |
| 139 |
err.setDescription(AiksaurusError::CORRUPT_MEANINGS_FILE); |
| 140 |
ret = false; |
| 141 |
} |
| 142 |
|
| 143 |
// If we were able to read data, go ahead and reset |
| 144 |
// position to 0 and we read two bytes as above. |
| 145 |
else if (d_end) |
| 146 |
{ |
| 147 |
d_position = 0; |
| 148 |
x = d_buffer[d_position++] << 8; |
| 149 |
x |= d_buffer[d_position++]; |
| 150 |
} |
| 151 |
|
| 152 |
// Otherwise, we were unable to read anything more. |
| 153 |
// We've most likely reached the end of file. Return |
| 154 |
// false to indicate failure. |
| 155 |
else |
| 156 |
{ |
| 157 |
ret = false; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return ret; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
////////////////////////////////////////////////////////////////////////// |
| 166 |
// |
| 167 |
// MeaningsFile Class Implementation |
| 168 |
// |
| 169 |
////////////////////////////////////////////////////////////////////////// |
| 170 |
|
| 171 |
// |
| 172 |
// MeaningsFile Constructor |
| 173 |
// All we need to do is get a handle to the file. |
| 174 |
// We'll also check to make sure our meanings file opens ok. |
| 175 |
// |
| 176 |
AiksaurusImpl::MeaningsFile::MeaningsFile(const char* fname) |
| 177 |
: d_file_ptr(fopen(fname, "r")) |
| 178 |
{ |
| 179 |
if (!d_file_ptr) |
| 180 |
{ |
| 181 |
d_error.setDescription(AiksaurusError::CANNOT_OPEN_MEANINGS_FILE); |
| 182 |
return; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
// |
| 188 |
// MeaningsFile Destructor |
| 189 |
// Close our file if we were able to open it successfully. |
| 190 |
// |
| 191 |
AiksaurusImpl::MeaningsFile::~MeaningsFile() |
| 192 |
{ |
| 193 |
if (d_file_ptr) |
| 194 |
fclose(d_file_ptr); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
// |
| 199 |
// _readline() |
| 200 |
// Reads an entire line from the meanings file into an array. |
| 201 |
// Observe that the array must have enough space to hold the |
| 202 |
// line, plus one slot for end-of-links (-1). |
| 203 |
// |
| 204 |
int |
| 205 |
AiksaurusImpl::MeaningsFile::_readline(MeaningStream& s, int* buffer) |
| 206 |
{ |
| 207 |
int i = 0; |
| 208 |
int x; |
| 209 |
|
| 210 |
while(s.read(x, d_error)) |
| 211 |
{ |
| 212 |
if (x == 0xFFFF) // 0xFFFF is the end-of-line marker. |
| 213 |
break; |
| 214 |
|
| 215 |
buffer[i++] = x; |
| 216 |
} |
| 217 |
|
| 218 |
buffer[i] = -1; // we terminate with -1 to say end of links. |
| 219 |
|
| 220 |
return i; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
// |
| 225 |
// getWords() |
| 226 |
// |
| 227 |
// Return array of all links in a particular meaning set. |
| 228 |
// The array will be terminated with -1 at the end so you |
| 229 |
// will know where to stop. |
| 230 |
// |
| 231 |
// NOTE: This array is only valid until the next time |
| 232 |
// the function is called. |
| 233 |
// |
| 234 |
int* |
| 235 |
AiksaurusImpl::MeaningsFile::getWords(int id) |
| 236 |
{ |
| 237 |
// First we need to create our buffer to return. |
| 238 |
// We know that there are at most s_dataMaxLineLength |
| 239 |
// bytes on any given line. However, since each link |
| 240 |
// is composed of two bytes, we only need half this |
| 241 |
// in order to store everything we need. |
| 242 |
|
| 243 |
// TO DO: why doesn't this have a +1 at the end? |
| 244 |
static int buffer[s_dataMaxLineLength / 2]; |
| 245 |
|
| 246 |
// If we have not successfully opened a file, we need to go ahead |
| 247 |
// and just return nothing here. |
| 248 |
if (!d_file_ptr) |
| 249 |
{ |
| 250 |
buffer[0] = -1; |
| 251 |
return buffer; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
// Divide by our offset modulus to get the start of the |
| 256 |
// correct partition. |
| 257 |
long partition_start = id / s_offsetModulus; |
| 258 |
|
| 259 |
// Jump to the offset pointed to by our partition. |
| 260 |
if (fseek(d_file_ptr, s_offsetData[partition_start], SEEK_SET) != 0) |
| 261 |
{ |
| 262 |
// There was an error jumping to that part of the file. |
| 263 |
// Set an error and return an empty list of links. |
| 264 |
d_error.setDescription(AiksaurusError::CORRUPT_MEANINGS_FILE); |
| 265 |
buffer[0] = -1; |
| 266 |
return buffer; |
| 267 |
} |
| 268 |
|
| 269 |
// Create a MeaningStream to read the file. |
| 270 |
MeaningStream s(d_file_ptr); |
| 271 |
|
| 272 |
// Read until we get the correct line. |
| 273 |
int line = partition_start * s_offsetModulus; |
| 274 |
for(; line <= id; line++) |
| 275 |
{ |
| 276 |
if (!_readline(s, buffer)) |
| 277 |
{ |
| 278 |
// we failed to read the correct lines. |
| 279 |
// return an empty set of words. |
| 280 |
buffer[0] = -1; |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return buffer; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
// |
| 290 |
// getError() |
| 291 |
// Simple function to just return the AiksaurusError object that we |
| 292 |
// are using to report errors. |
| 293 |
// |
| 294 |
const AiksaurusImpl::AiksaurusError& |
| 295 |
AiksaurusImpl::MeaningsFile::getError() const |
| 296 |
{ |
| 297 |
return d_error; |
| 298 |
} |
| 299 |
|
| 300 |
|