21 #include <kstandarddirs.h>
38 void BinFileHelper::init() {
45 preambleUpdated =
false;
51 void BinFileHelper::clearFields() {
57 QString FilePath = KStandardDirs::locate(
"appdata", fileName );
58 QByteArray b = FilePath.toAscii();
59 const char *filepath = b.data();
60 FILE *
f = KDE_fopen(filepath,
"rb");
70 QString FilePath = KStandardDirs::locate(
"appdata", fileName );
72 QByteArray b = FilePath.toAscii();
73 const char *filepath = b.data();
75 fileHandle = KDE_fopen(filepath,
"rb");
96 fread(ASCII_text, 124, 1, fileHandle);
97 ASCII_text[124] =
'\0';
98 headerText = ASCII_text;
100 fread(&endian_id, 2, 1, fileHandle);
101 if(endian_id != 0x4B53)
106 fread( &versionNumber, 1, 1, fileHandle );
108 preambleUpdated =
true;
110 fread(&nfields, 2, 1, fileHandle);
111 if( byteswap ) nfields =
bswap_16( nfields );
113 for(i = 0; i < nfields; ++i) {
115 if(!fread(de,
sizeof(
dataElement), 1, fileHandle)) {
125 for(i = 0; i < fields.size(); ++i)
126 recordSize += fields[i] -> size;
133 fread(&indexSize, 4, 1, fileHandle);
134 if( byteswap ) indexSize =
bswap_32( indexSize );
142 itableOffset = KDE_ftell(fileHandle);
151 if( indexSize == 0 ) {
152 errorMessage.sprintf(
"Zero index size!" );
155 for(i = 0; i < indexSize; ++i) {
156 if(!fread(&ID, 4, 1, fileHandle)) {
157 errorMessage.sprintf(
"Table truncated before expected! Read i = %d index entries so far", i);
161 if(ID >= indexSize) {
162 errorMessage.sprintf(
"ID %u is greater than the expected number of expected entries (%u)", ID, indexSize);
166 errorMessage.sprintf(
"Found ID %u, at the location where ID %u was expected", ID, i);
169 if(!fread(&offset, 4, 1, fileHandle)) {
170 errorMessage.sprintf(
"Table truncated before expected! Read i = %d index entries so far", i);
173 if( byteswap ) offset =
bswap_32( offset );
174 if(!fread(&nrecs, 4, 1, fileHandle)) {
175 errorMessage.sprintf(
"Table truncated before expected! Read i = %d index entries so far", i);
178 if( byteswap ) nrecs =
bswap_32( nrecs );
179 if(prev_offset != 0 && prev_nrecs != (-prev_offset + offset)/recordSize) {
180 errorMessage.sprintf(
"Expected %u = (%X - %x) / %x records, but found %u, in index entry %u",
181 (offset - prev_offset) / recordSize, offset, prev_offset, recordSize, prev_nrecs, i - 1);
185 indexOffset.append( offset );
186 indexCount.append( nrecs );
188 recordCount += nrecs;
189 prev_offset = offset;
193 dataOffset = KDE_ftell(fileHandle);
201 switch( (errnum = __readHeader()) ) {
235 QString erm = errorMessage;
242 for(
int i = 0; i < fields.size(); ++i) {
243 if(fields[i] ->
name == fieldName) {
252 for(
int i = 0; i < fields.size(); ++i) {
253 if(fields[i] -> name == fieldName)
262 if( offset <= ((quint32)1 << 31) - 1 ) {
263 ret = fseek( stream, offset, whence );
267 ret = fseek( stream, ((quint32)1 << 31) - 1, whence );
269 ret = fseek( stream, offset - ((quint32)1 << 31) + 1, SEEK_CUR );
void closeFile()
Close the binary data file.
bool isField(const QString &FieldName)
Check whether a field exists.
QString getError()
Get error string.
static bool testFileExists(const QString &fileName)
Checks if a file exists.
This class provides utility functions to handle binary data files in the format prescribed by KStars...
FILE * openFile(const QString &fileName)
WARNING: This function may not be compatible in other locales, because it calls QString::toAscii.
A structure describing a data field in the file.
int getErrorNumber()
Get error number.
struct dataElement dataElement
A structure describing a data field in the file.
~BinFileHelper()
Destructor.
bool readHeader()
Read the header and index table from the file and fill up the QVector s with the entries.
static int unsigned_KDE_fseek(FILE *stream, quint32 offset, int whence)
Wrapper around fseek for large offsets.
Errors
An enum providing user-friendly names for errors encountered.
BinFileHelper()
Constructor.