commonlib
FileHandler< T > Class Template Reference

Wrapper of operations on file. More...

#include <disk_buffer.h>

Public Member Functions

 FileHandler (const std::string &filename, bool deleteFileAfterClose=true)
 The constructor creates an instance. More...
 
void open ()
 Opens file as binary in write/update mode.
 
void open (char const *mode)
 Opens file as binary in given file access mode. More...
 
bool close ()
 Closes the file and frees the file pointer. More...
 
void removeFile ()
 Deletes the file.
 
void rewind ()
 Places file pointer at start of the file.
 
size_t write (const T *data, size_t count)
 Writes data on disk at the current file pointer position. More...
 
size_t writeAt (const T *data, size_t count, size_t pos)
 Writes data on disk. More...
 
size_t read (T *data, size_t count)
 Reads data from disk at the current file pointer position. More...
 
size_t readAt (T *data, size_t count, size_t pos)
 Reads data from disk. More...
 
void seekPos (size_t offset)
 Repositions file pointer relative to beginning of the file. More...
 
size_t getSizeInBytes ()
 Returns the size of the file in bytes. More...
 
size_t getSize ()
 Returns the size of the file in type T elements. More...
 
void reset ()
 Clears file.
 

Protected Member Functions

size_t write (const void *data, size_t size, size_t count)
 
size_t read (void *data, size_t size, size_t count)
 

Protected Attributes

std::fstream file
 
std::string filename
 
bool deleteFileAfterClose
 

Detailed Description

template<typename T>
class FileHandler< T >

Wrapper of operations on file.

Constructor & Destructor Documentation

◆ FileHandler()

template<typename T>
FileHandler< T >::FileHandler ( const std::string &  filename,
bool  deleteFileAfterClose = true 
)
inlineexplicit

The constructor creates an instance.

Parameters
filenamefull path + name.
deleteFileAfterClosethe file is deleted after object is freed or close is called.

Member Function Documentation

◆ close()

template<typename T>
bool FileHandler< T >::close ( )
inline

Closes the file and frees the file pointer.

Returns
True if success else false.

◆ getSize()

template<typename T>
size_t FileHandler< T >::getSize ( )
inline

Returns the size of the file in type T elements.

Returns
Size of file in elements.

◆ getSizeInBytes()

template<typename T>
size_t FileHandler< T >::getSizeInBytes ( )
inline

Returns the size of the file in bytes.

Returns
Size of file.

◆ open()

template<typename T>
void FileHandler< T >::open ( char const *  mode)
inline

Opens file as binary in given file access mode.

Parameters
modenull-terminated character string determining file access mode.

◆ read()

template<typename T>
size_t FileHandler< T >::read ( T *  data,
size_t  count 
)
inline

Reads data from disk at the current file pointer position.

Parameters
datadata to be read.
countnumber of elements of type T.
Returns
The total number of elements successfully read.

◆ readAt()

template<typename T>
size_t FileHandler< T >::readAt ( T *  data,
size_t  count,
size_t  pos 
)
inline

Reads data from disk.

Parameters
datadata to be read.
countnumber of elements of type T.
posposition of read.
Returns
The total number of elements successfully read.

◆ seekPos()

template<typename T>
void FileHandler< T >::seekPos ( size_t  offset)
inline

Repositions file pointer relative to beginning of the file.

Parameters
offsetnumber of elements for reposition from origin.
Returns
If successful, the function returns zero.

◆ write()

template<typename T>
size_t FileHandler< T >::write ( const T *  data,
size_t  count 
)
inline

Writes data on disk at the current file pointer position.

Parameters
datadata to be written.
countnumber of elements of type T.
Returns
The total number of elements successfully written.

◆ writeAt()

template<typename T>
size_t FileHandler< T >::writeAt ( const T *  data,
size_t  count,
size_t  pos 
)
inline

Writes data on disk.

Parameters
datadata to be written.
countnumber of elements of type T.
posposition of write.
Returns
The total number of elements successfully written.

The documentation for this class was generated from the following file: