Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Types | Public Member Functions | Static Public Member Functions
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 >

List of all members.

Public Types

enum  OpenMode {
  readBinary = 0, readText = 1, writeBinary = 2, writeText = 3,
  notOpen = 4
}
 mode to open files More...

Public Member Functions

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

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
static std::vector< std::string > read_lines (const std::string &filename)
 static utility method that reads a whole file as lines (text-mode only)
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
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)

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

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

Create a null file.

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

Create a file with given filename.

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

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

Destructor (enshures that the file is closed)


Member Function Documentation

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

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

returns whether this file was opened with read access

returns whether this file was opened with write access

closes the file

erases the file

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

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

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

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

returns the pointer to the internal file buffer at current position

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

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

returns the pointer to the internal file buffers begin

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

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

returns the file handle

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

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

returns the current OpenMode of this file of notOpen

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

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

returns whether more lines can be read from this file

returns if the file was opened for binary data transfer

returns whether the file is a directory

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

returns whether the file is already opened

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

Throws a FileOpenException if file can not be opened

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

writes a character into the file

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

writes an unsigned character into the file

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

writes an integer into the file

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

writes a unsigned integer into the file

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

writes a float into the file

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

writes a double into the file

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

writes a string into the file

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

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

reads max. len bytes into the destination pointer

Returns:
number of bytes actually read
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

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)

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

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

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

reads the next line of the file (buffered)

only available in non-binary mode

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

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

Throws a FileOpenException if file can not be opened

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

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

sets floating point precision

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

writes len bytes from data into the file

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

writes a string into the 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

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)

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:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines