| 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 "FileArray.h" |
| 23 |
#include <cassert> |
| 24 |
|
| 25 |
AiksaurusImpl::FileArray::FileArray(const char* file, long structsize, bool& ok) |
| 26 |
: d_file(fopen(file, "r")), |
| 27 |
d_structsize(structsize), |
| 28 |
d_bytes_cached(false) |
| 29 |
{ |
| 30 |
assert(d_structsize > 0); |
| 31 |
ok = (d_file != 0); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
AiksaurusImpl::FileArray::~FileArray() |
| 36 |
{ |
| 37 |
// close the file if it's still open. |
| 38 |
if (d_file != 0) |
| 39 |
fclose(d_file); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
// |
| 44 |
// getStructSize() |
| 45 |
// Just return the structsize as was specified in the constructor. |
| 46 |
// The user really should know what this is on their own. |
| 47 |
// |
| 48 |
long |
| 49 |
AiksaurusImpl::FileArray::getStructSize() const |
| 50 |
{ |
| 51 |
return d_structsize; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
// |
| 56 |
// getBytes() |
| 57 |
// Returns the size of the file (if able to calculate this). |
| 58 |
// Returns -1 if unable to calculate the file's size. |
| 59 |
// |
| 60 |
long |
| 61 |
AiksaurusImpl::FileArray::getBytes() const |
| 62 |
{ |
| 63 |
// return cached answer if available. |
| 64 |
if (d_bytes_cached) return d_bytes; |
| 65 |
|
| 66 |
// return failure if file was unable to be opened. |
| 67 |
if (!d_file) return -1; |
| 68 |
|
| 69 |
// otherwise, answer not cached yet. mark that we |
| 70 |
// will cache this answer. |
| 71 |
d_bytes_cached = true; |
| 72 |
|
| 73 |
// jump to the end of the file. |
| 74 |
fseek(d_file, 0L, SEEK_END); |
| 75 |
|
| 76 |
// figure out where we're at. ftell() might return -1 |
| 77 |
// on failure, which is fine because that's what we |
| 78 |
// return on failure too. |
| 79 |
d_bytes = ftell(d_file); |
| 80 |
|
| 81 |
return d_bytes; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
// |
| 87 |
// FileArray::at |
| 88 |
// Return the element at a particular position. |
| 89 |
// No bounds checking is done. |
| 90 |
// |
| 91 |
bool |
| 92 |
AiksaurusImpl::FileArray::at(long position, unsigned char* buf) const |
| 93 |
{ |
| 94 |
assert(position >= 0); |
| 95 |
|
| 96 |
// ensure file is open before we start trying to read from it. |
| 97 |
if (!d_file) |
| 98 |
{ |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
// Jump to the right position in the file. |
| 103 |
if (fseek(d_file, position * d_structsize, SEEK_SET) == -1) |
| 104 |
{ |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
// Read structsize bytes from the file. |
| 109 |
if (fread(buf, 1, d_structsize, d_file) != |
| 110 |
static_cast<unsigned int>(d_structsize)) |
| 111 |
{ |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
|