Image Component Library (ICL)
Public Types | Public Member Functions | Static Public Member Functions | List of all members
icl::utils::File Class Reference

Utility class for file handling (reading files is buffered) More...

#include <File.h>

Inheritance diagram for icl::utils::File:
icl::utils::ShallowCopyable< FileImpl, FileImplDelOp >

Public Types

enum  OpenMode {
  readBinary = 0, readText = 1, writeBinary = 2, writeText = 3,
  notOpen = 4
}
 mode to open files More...
 
- Public Types inherited from icl::utils::ShallowCopyable< FileImpl, FileImplDelOp >
typedef ShallowCopyable< FileImpl, FileImplDelOp > ParentSC
 

Public Member Functions

 File ()
 Create a null file. More...
 
 File (const std::string &name)
 Create a file with given filename. More...
 
 File (const std::string &name, OpenMode om)
 Create a file with given filename which is opened imediately with given openmode. More...
 
 ~File ()
 Destructor (enshures that the file is closed) More...
 
bool exists () const
 returns whether the file is open, or can be opened for reading More...
 
bool isDirectory () const
 returns whether the file is a directory More...
 
bool isOpen () const
 returns whether the file is already opened More...
 
bool isBinary () const
 returns if the file was opened for binary data transfer More...
 
std::string getDir () const
 returns the path postfix of the files url ( "../data.txt" -> "../") More...
 
std::string getBaseName () const
 returns the files basename ( "../data.txt" -> "data") More...
 
std::string getSuffix () const
 returns the files suffix ( "../data.txt" -> ".txt") More...
 
std::string getName () const
 returns whole file url ( "../data.txt" -> "../data.txt") More...
 
void write (const void *data, int len)
 writes len bytes from data into the file More...
 
void writeLine (const void *data, int len)
 writes len bytes from data into the file and an additional Line-Break More...
 
void write (const std::string &text)
 writes a string into the file More...
 
void setPrecision (const std::string &p="%8f")
 sets floating point precision More...
 
Fileoperator<< (char c)
 writes a character into the file More...
 
Fileoperator<< (unsigned char uc)
 writes an unsigned character into the file More...
 
Fileoperator<< (int i)
 writes an integer into the file More...
 
Fileoperator<< (unsigned int ui)
 writes a unsigned integer into the file More...
 
Fileoperator<< (float f)
 writes a float into the file More...
 
Fileoperator<< (double d)
 writes a double into the file More...
 
Fileoperator<< (const std::string &s)
 writes a string into the file More...
 
std::string readLine () const
 reads the next line of the file (buffered) More...
 
std::string & readLine (std::string &dst) const
 reads the next line of the file into a given string More...
 
const std::vector< icl8u > & readAll () const
 reads the whole data of the file into an internal buffer and returns it More...
 
const icl8ugetCurrentDataPointer () const
 returns the pointer to the internal file buffer at current position More...
 
const icl8ugetFileDataPointer () const
 returns the pointer to the internal file buffers begin More...
 
std::vector< icl8uread (int len) const
 reads len bytes from the file (buffered) More...
 
int read (int len, void *dst) const
 reads max. len bytes into the destination pointer More...
 
bool hasMoreLines () const
 returns whether more lines can be read from this file More...
 
int bytesAvailable () const
 returns the number of to-be-read bytes in the file More...
 
int getFileSize () const
 returns the number of bytes in this file only in read-mode More...
 
void open (OpenMode om)
 opens the file with given openmode ("rw" is not yet supported!) More...
 
void reopen (OpenMode om)
 re-opens the file with given openmode ("rw" is not yet supported!) More...
 
void close ()
 closes the file More...
 
void erase ()
 erases the file More...
 
void reset ()
 jumps to the beginning of the file (only in read mode) More...
 
void * getHandle () const
 returns the file handle More...
 
bool canRead () const
 returns whether this file was opened with read access More...
 
bool canWrite () const
 returns whether this file was opened with write access More...
 
OpenMode getOpenMode () const
 returns the current OpenMode of this file of notOpen More...
 
- Public Member Functions inherited from icl::utils::ShallowCopyable< FileImpl, FileImplDelOp >
bool isNull () const
 returns wheter the objects implementation holds a null pointer More...
 

Static Public Member Functions

static std::string read_file (const std::string &filename, bool textMode=true)
 static utility method that reads a whole file as single string More...
 
static std::vector< std::string > read_lines (const std::string &filename)
 static utility method that reads a whole file as lines (text-mode only) More...
 
static void write_file (const std::string &filename, const std::string &text, bool textMode=true)
 static utility method that writes a single string to a file More...
 
static void write_lines (const std::string &filename, const std::vector< std::string > &lines)
 static utility method that writes lines to a file (adding
after each line) More...
 

Additional Inherited Members

- Protected Member Functions inherited from icl::utils::ShallowCopyable< FileImpl, FileImplDelOp >
 ShallowCopyable (FileImpl *t=0)
 create a the implementation with a given T* value More...
 
- Protected Attributes inherited from icl::utils::ShallowCopyable< FileImpl, FileImplDelOp >
SmartPtrBase< FileImpl, FileImplDelOp > impl
 shared pointer for the classes implementation More...
 

Detailed Description

Utility class for file handling (reading files is buffered)

The File class implements an implicit gzip writing and reading. This feature is enabled if ICL_HAVE_LIB_Z is defined during the compilation process.
If the File's given filename has a ".gz" postfix, read and write calls are applied using gzread and gzwrite from the libz.
In addition the implementation of the File class was split into two parts: The File itself and its certain implementation, which is defined invisibly for the user. This implementation is shared by shallow copied instances using the ICL SmartPtr class. This mechanism provides save shallow copies using reference counting.

Member Enumeration Documentation

◆ OpenMode

mode to open files

Enumerator
readBinary 
readText 

!< open file for reading binary data

writeBinary 

!< open file for reading ascii data

writeText 

!< open file for reading writing binary data

notOpen 

!< open file for reading writing ascii data

Constructor & Destructor Documentation

◆ File() [1/3]

icl::utils::File::File ( )

Create a null file.

◆ File() [2/3]

icl::utils::File::File ( const std::string &  name)

Create a file with given filename.

◆ File() [3/3]

icl::utils::File::File ( const std::string &  name,
OpenMode  om 
)

Create a file with given filename which is opened imediately with given openmode.

◆ ~File()

icl::utils::File::~File ( )

Destructor (enshures that the file is closed)

Member Function Documentation

◆ bytesAvailable()

int icl::utils::File::bytesAvailable ( ) const

returns the number of to-be-read bytes in the file

◆ canRead()

bool icl::utils::File::canRead ( ) const

returns whether this file was opened with read access

◆ canWrite()

bool icl::utils::File::canWrite ( ) const

returns whether this file was opened with write access

◆ close()

void icl::utils::File::close ( )

closes the file

◆ erase()

void icl::utils::File::erase ( )

erases the file

◆ exists()

bool icl::utils::File::exists ( ) const

returns whether the file is open, or can be opened for reading

◆ getBaseName()

std::string icl::utils::File::getBaseName ( ) const

returns the files basename ( "../data.txt" -> "data")

◆ getCurrentDataPointer()

const icl8u* icl::utils::File::getCurrentDataPointer ( ) const

returns the pointer to the internal file buffer at current position

◆ getDir()

std::string icl::utils::File::getDir ( ) const

returns the path postfix of the files url ( "../data.txt" -> "../")

◆ getFileDataPointer()

const icl8u* icl::utils::File::getFileDataPointer ( ) const

returns the pointer to the internal file buffers begin

◆ getFileSize()

int icl::utils::File::getFileSize ( ) const

returns the number of bytes in this file only in read-mode

◆ getHandle()

void* icl::utils::File::getHandle ( ) const

returns the file handle

◆ getName()

std::string icl::utils::File::getName ( ) const

returns whole file url ( "../data.txt" -> "../data.txt")

◆ getOpenMode()

OpenMode icl::utils::File::getOpenMode ( ) const

returns the current OpenMode of this file of notOpen

◆ getSuffix()

std::string icl::utils::File::getSuffix ( ) const

returns the files suffix ( "../data.txt" -> ".txt")

◆ hasMoreLines()

bool icl::utils::File::hasMoreLines ( ) const

returns whether more lines can be read from this file

◆ isBinary()

bool icl::utils::File::isBinary ( ) const

returns if the file was opened for binary data transfer

◆ isDirectory()

bool icl::utils::File::isDirectory ( ) const

returns whether the file is a directory

◆ isOpen()

bool icl::utils::File::isOpen ( ) const

returns whether the file is already opened

◆ open()

void icl::utils::File::open ( OpenMode  om)

opens the file with given openmode ("rw" is not yet supported!)

Throws a FileOpenException if file can not be opened

◆ operator<<() [1/7]

File& icl::utils::File::operator<< ( char  c)

writes a character into the file

◆ operator<<() [2/7]

File& icl::utils::File::operator<< ( unsigned char  uc)

writes an unsigned character into the file

◆ operator<<() [3/7]

File& icl::utils::File::operator<< ( int  i)

writes an integer into the file

◆ operator<<() [4/7]

File& icl::utils::File::operator<< ( unsigned int  ui)

writes a unsigned integer into the file

◆ operator<<() [5/7]

File& icl::utils::File::operator<< ( float  f)

writes a float into the file

◆ operator<<() [6/7]

File& icl::utils::File::operator<< ( double  d)

writes a double into the file

◆ operator<<() [7/7]

File& icl::utils::File::operator<< ( const std::string &  s)

writes a string into the file

◆ read() [1/2]

std::vector<icl8u> icl::utils::File::read ( int  len) const

reads len bytes from the file (buffered)

if less than len bytes are available in the file, a zero length vector is returned

◆ read() [2/2]

int icl::utils::File::read ( int  len,
void *  dst 
) const

reads max. len bytes into the destination pointer

Returns
number of bytes actually read

◆ read_file()

static std::string icl::utils::File::read_file ( const std::string &  filename,
bool  textMode = true 
)
static

static utility method that reads a whole file as single string

◆ read_lines()

static std::vector<std::string> icl::utils::File::read_lines ( const std::string &  filename)
static

static utility method that reads a whole file as lines (text-mode only)

◆ readAll()

const std::vector<icl8u>& icl::utils::File::readAll ( ) const

reads the whole data of the file into an internal buffer and returns it

◆ readLine() [1/2]

std::string icl::utils::File::readLine ( ) const

reads the next line of the file (buffered)

only available in non-binary mode

◆ readLine() [2/2]

std::string& icl::utils::File::readLine ( std::string &  dst) const

reads the next line of the file into a given string

breaks on newlines or on eof

Returns
the given input string

◆ reopen()

void icl::utils::File::reopen ( OpenMode  om)

re-opens the file with given openmode ("rw" is not yet supported!)

Throws a FileOpenException if file can not be opened

◆ reset()

void icl::utils::File::reset ( )

jumps to the beginning of the file (only in read mode)

◆ setPrecision()

void icl::utils::File::setPrecision ( const std::string &  p = "%8f")

sets floating point precision

◆ write() [1/2]

void icl::utils::File::write ( const void *  data,
int  len 
)

writes len bytes from data into the file

◆ write() [2/2]

void icl::utils::File::write ( const std::string &  text)

writes a string into the file

◆ write_file()

static void icl::utils::File::write_file ( const std::string &  filename,
const std::string &  text,
bool  textMode = true 
)
static

static utility method that writes a single string to a file

◆ write_lines()

static void icl::utils::File::write_lines ( const std::string &  filename,
const std::vector< std::string > &  lines 
)
static

static utility method that writes lines to a file (adding
after each line)

◆ writeLine()

void icl::utils::File::writeLine ( const void *  data,
int  len 
)

writes len bytes from data into the file and an additional Line-Break

only in non-binary mode!


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