Debian Codegraph @

data/0.12-dev/src/FileArray.cpp

raw
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 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;
32 }
33
34
35 FileArray::~FileArray()
36 {
37 fclose(d_file);
38 }
39
40
41 long
42 FileArray::getStructSize() const
43 {
44 return d_structsize;
45 }
46
47
48 long
49 FileArray::getBytes() const
50 {
51 if (d_bytes_cached)
52 return d_bytes;
53
54 if (!d_file)
55 return -1;
56
57 d_bytes_cached = true;
58 fseek(d_file, 0L, SEEK_END);
59 d_bytes = ftell(d_file);
60 return d_bytes;
61 }
62
63
64 bool
65 FileArray::at(long position, unsigned char* buf) const
66 {
67 assert(position >= 0);
68
69 if (fseek(d_file, position * d_structsize, SEEK_SET) == -1)
70 {
71 return false;
72 }
73
74 if (fread(buf, 1, d_structsize, d_file) !=
75 static_cast<unsigned int>(d_structsize))
76 {
77 return false;
78 }
79
80 return true;
81 }
82
83