Cupt
Public Member Functions
cupt::File Class Reference

high-level interface to file routines More...

#include <cupt/file.hpp>

List of all members.

Public Member Functions

 File (const string &path, const char *mode, string &error)
 constructor
virtual ~File ()
 destructor
FilerawGetLine (const char *&buffer, size_t &size)
 reads new line
FilegetLine (string &line)
 reads new line
FilegetRecord (string &record)
 reads new record
FilegetBlock (char *buffer, size_t &size)
 reads new block
void getFile (string &block)
 reads all available data from current position
void put (const string &data)
 writes data
void put (const char *data, size_t size)
 writes data
bool eof () const
void seek (size_t newPosition)
 seeks to a new position
size_t tell () const
 gets current file position
void lock (int flags)
 perform flock(2) on file

Detailed Description

high-level interface to file routines


Constructor & Destructor Documentation

cupt::File::File ( const string &  path,
const char *  mode,
string &  error 
)

constructor

Constructs new object for a regular file or reading shell pipe.

Warning:
You must not use constructed object if error is not empty.
Parameters:
pathpath to file or shell command, see mode
modeany value, accepted as mode in fopen(3); or "pr" / "pw" - special values to treat path as shell pipe with an opened handle for reading / writing, respectively
[out]errorif open fails, human readable error will be placed here

Member Function Documentation

bool cupt::File::eof ( ) const

checks for the end of file condition

File& cupt::File::getBlock ( char *  buffer,
size_t &  size 
)

reads new block

Reads up to size characters from current position to buffer.

Parameters:
bufferbuffer to read in
[in,out]sizebefore: up limit on character count, after: number of bytes read
Returns:
reference to self
void cupt::File::getFile ( string &  block)

reads all available data from current position

It's usually used just after opening the file and for small files.

Parameters:
blockcontainer for read data
File& cupt::File::getLine ( string &  line)

reads new line

Reads new line. Newline character from the end is strip if present.

End of file must be checked by querying eof right after getLine. You can use line only if eof returned false.

Parameters:
[out]linecontainer for read data
Returns:
reference to self
Example:
Reading file line by line.
string line;
while (!file.getLine(line).eof())
{
// process line
}
File& cupt::File::getRecord ( string &  record)

reads new record

Reads new record, that is, a sequence of characters which ends with double newline character ("\n\n").

If the end of file was encountered when reading, newline character(s) will be not added.

End of file must be checked by querying eof right after getRecord. You can use record only if eof returned false.

Parameters:
[out]recordcontainer for read data
Returns:
reference to self.
void cupt::File::lock ( int  flags)

perform flock(2) on file

Parameters:
flagsflags passed to flock
void cupt::File::put ( const string &  data)

writes data

Parameters:
datadata to write
void cupt::File::put ( const char *  data,
size_t  size 
)

writes data

Parameters:
datapointer to the data buffer
sizesize of the buffer
File& cupt::File::rawGetLine ( const char *&  buffer,
size_t &  size 
)

reads new line

Reads new line (that is, a sequence of characters which ends with newline character ("\n")).

If the end of file was encountered when reading, newline character will be not added.

Parameters:
[in,out]bufferwill contain a pointer to read data
[out]sizethe size (in bytes) of the buffer, a value 0 means end of file
Returns:
reference to self
void cupt::File::seek ( size_t  newPosition)

seeks to a new position

Sets new position of the file to write/read. Will throw Exception if called against File opened as pipe.

Parameters:
newPositionnew file position

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