BinFileHelper

Search for usage in LXR

#include <binfilehelper.h>

Public Types

enum  Errors {
  ERR_NULL, ERR_FILEOPEN, ERR_FD_TRUNC, ERR_INDEX_TRUNC,
  ERR_INDEX_BADID, ERR_INDEX_IDMISMATCH, ERR_INDEX_BADOFFSET, ERR_BADSEEK
}
 

Public Member Functions

 BinFileHelper ()
 
 ~BinFileHelper ()
 
void closeFile ()
 
bool getByteSwap () const
 
long getDataOffset () const
 
QString getError ()
 
int getErrorNumber ()
 
struct dataElement getField (const QString &fieldName) const
 
int getFieldCount () const
 
FILE * getFileHandle () const
 
QString getHeaderText () const
 
long getIndexTableOffset () const
 
long getOffset (int id) const
 
unsigned long getRecordCount () const
 
unsigned int getRecordCount (int id) const
 
int guessRecordSize () const
 
bool isField (const QString &FieldName) const
 
FILE * openFile (const QString &fileName)
 
bool propertiesUpdated () const
 
bool readHeader ()
 
void setRecordSize (int rs)
 

Static Public Member Functions

static bool testFileExists (const QString &fileName)
 
static int unsigned_KDE_fseek (FILE *stream, quint32 offset, int whence)
 

Detailed Description

Implements an interface to handle binary data files used by KStars.

This class provides utility functions to handle binary data files in the format prescribed by KStars. The file format is designed specifically to support data that has the form of an array of structures. See data/README.fileformat for details. The methods use primitive C file I/O routines defined in stdio.h to obtain efficiency

Author
Akarsh Simha
Version
1.0

Definition at line 38 of file binfilehelper.h.

Member Enumeration Documentation

◆ Errors

An enum providing user-friendly names for errors encountered.

Enumerator
ERR_NULL 

No error occurred

ERR_FILEOPEN 

File could not be opened

ERR_FD_TRUNC 

Field descriptor table is truncated

ERR_INDEX_TRUNC 

File ends prematurely, before expected end of index table

ERR_INDEX_BADID 

Index table has an invalid ID entry

ERR_INDEX_IDMISMATCH 

Index table has a mismatched ID entry [ID found in the wrong place]

ERR_INDEX_BADOFFSET 

Offset / Record count specified in the Index table is bad

ERR_BADSEEK 

Premature end of file / bad seek while reading index table

Definition at line 201 of file binfilehelper.h.

Constructor & Destructor Documentation

◆ BinFileHelper()

BinFileHelper::BinFileHelper ( )

Constructor.

Definition at line 16 of file binfilehelper.cpp.

◆ ~BinFileHelper()

BinFileHelper::~BinFileHelper ( )

Destructor.

Definition at line 21 of file binfilehelper.cpp.

Member Function Documentation

◆ closeFile()

void BinFileHelper::closeFile ( )

Close the binary data file.

Definition at line 276 of file binfilehelper.cpp.

◆ getByteSwap()

bool BinFileHelper::getByteSwap ( ) const
inline

Should we do byte swapping?

Note
To be called only after the header has been parsed
Returns
true if we must do byte swapping, false if not

Definition at line 136 of file binfilehelper.h.

◆ getDataOffset()

long BinFileHelper::getDataOffset ( ) const
inline

Returns the offset at which the data begins.

Returns
The value of dataOffset if indexUpdated, else zero

Definition at line 175 of file binfilehelper.h.

◆ getError()

QString BinFileHelper::getError ( )

Get error string.

Returns
A QString containing the error message to be displayed

Definition at line 289 of file binfilehelper.cpp.

◆ getErrorNumber()

int BinFileHelper::getErrorNumber ( )

Get error number.

Returns
A number corresponding to the error

Definition at line 282 of file binfilehelper.cpp.

◆ getField()

struct dataElement BinFileHelper::getField ( const QString fieldName) const

Get field by name.

Parameters
fieldNameName of the field
Returns
A dataElement structure containing field data if the field exists, containing junk values if the field doesn't exist

Definition at line 296 of file binfilehelper.cpp.

◆ getFieldCount()

int BinFileHelper::getFieldCount ( ) const
inline

Returns the number of fields as suggested by the field descriptor in the header.

Returns
Number of fields, or zero if the FD hasn't been read yet

Definition at line 163 of file binfilehelper.h.

◆ getFileHandle()

FILE* BinFileHelper::getFileHandle ( ) const
inline

Get the file handle corresponding to the currently open file.

Returns
The filehandle if a file is open, nullptr if no file is open

Definition at line 109 of file binfilehelper.h.

◆ getHeaderText()

QString BinFileHelper::getHeaderText ( ) const
inline

Returns the header text.

Returns
QString containing the header text if header has been read, blank QString otherwise

Definition at line 169 of file binfilehelper.h.

◆ getIndexTableOffset()

long BinFileHelper::getIndexTableOffset ( ) const
inline

Returns the offset at which the index table begins.

Returns
The value of itableOffset if FDUpdated, else zero

Definition at line 181 of file binfilehelper.h.

◆ getOffset()

long BinFileHelper::getOffset ( int  id) const
inline

Returns the offset in the file corresponding to the given index ID.

Parameters
idID of the index entry whose offset is required
Returns
The offset corresponding to that index ID, 0 if the index hasn't been read

Definition at line 116 of file binfilehelper.h.

◆ getRecordCount() [1/2]

unsigned long BinFileHelper::getRecordCount ( ) const
inline

Returns the total number of records in the file.

Returns
The number of records in the file, or 0 if the index has not been read

Definition at line 129 of file binfilehelper.h.

◆ getRecordCount() [2/2]

unsigned int BinFileHelper::getRecordCount ( int  id) const
inline

Returns the number of records under the given index ID.

Parameters
idID of the index entry
Returns
The number of records under index that index ID, or 0 if the index has not been read

Definition at line 123 of file binfilehelper.h.

◆ guessRecordSize()

int BinFileHelper::guessRecordSize ( ) const
inline

Return a guessed record size.

Note
The record size returned is guessed from the field descriptor table and may not be correct in many cases
Returns
A guessed size of the record, or zero if the FD hasn't been read yet

Definition at line 144 of file binfilehelper.h.

◆ isField()

bool BinFileHelper::isField ( const QString FieldName) const

Check whether a field exists.

Parameters
FieldNameName of the field
Returns
True if the field exists, false if it does not or the FD hasn't been read yet

Definition at line 311 of file binfilehelper.cpp.

◆ openFile()

FILE * BinFileHelper::openFile ( const QString fileName)

WARNING: This function may not be compatible in other locales, because it calls QString::toAscii.

Open a Binary data file and set the handle

Parameters
fileNameReference to QString containing the name of the file
Returns
Handle to the file if successful, nullptr if an error occurred, sets the error.

Definition at line 72 of file binfilehelper.cpp.

◆ propertiesUpdated()

bool BinFileHelper::propertiesUpdated ( ) const
inline

Check whether file properties are real or bogus.

Returns
The value of private variable indexUpdated, which is true if data has been updated

Definition at line 103 of file binfilehelper.h.

◆ readHeader()

bool BinFileHelper::readHeader ( )

Read the header and index table from the file and fill up the QVector s with the entries.

Returns
True if successful, false if an error occurred, sets the error.

Definition at line 250 of file binfilehelper.cpp.

◆ setRecordSize()

void BinFileHelper::setRecordSize ( int  rs)
inline

Override record size.

Note
To be used to override the guess in case the guess is wrong. This method should be called before calling readHeader(). Use this only if you are sure of the recordSize or are sure that the guess will not work.
Parameters
rsThe correct recordSize

Definition at line 153 of file binfilehelper.h.

◆ testFileExists()

bool BinFileHelper::testFileExists ( const QString fileName)
static

Checks if a file exists.

WARNING: Might be incompatible in other locales

Definition at line 57 of file binfilehelper.cpp.

◆ unsigned_KDE_fseek()

int BinFileHelper::unsigned_KDE_fseek ( FILE *  stream,
quint32  offset,
int  whence 
)
static

Wrapper around fseek for large offsets.

fseek takes a signed long for the offset, since it can be either positive or negative. If the argument is a 32-bit unsigned integer, fseek will fail in most situations, since that will be interpreted as a number of the opposite sign. This wrapper circumvents that problem by repeatedly calling fseek twice if the offset is too large. Useful when 64-bit handling is really not necessary.

Returns
Zero on success. Else, error code of the first failing fseek call.
Note
Clearly, this method can move forward only. When we need one that goes back, we'll implement that.

Definition at line 321 of file binfilehelper.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 04:04:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.