LTL  2.0.x
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ltl::AscFile Class Reference

Columns based interface to an ASCII data file. More...

Public Member Functions

 AscFile (const string &fname, const char delim=0, const string &comment="#")
 Constructs an AsciiFile object. More...
 
 AscFile (const string &fname, const int b, int e=-1, const char delim=0, const string &comment="#")
 
virtual ~AscFile ()
 
void close ()
 
bool readNextLine (string &buf)
 
void rewind ()
 
MArray< int, 1 > readIntColumn (const int col, const int start=1, int nrows=-1) throw (IOException)
 
MArray< long long, 1 > readLongLongColumn (const int col, const int start=1, int nrows=-1) throw (IOException)
 
MArray< float, 1 > readFloatColumn (const int col, const int start=1, int nrows=-1) throw (IOException)
 
MArray< double, 1 > readDoubleColumn (const int col, const int start=1, int nrows=-1) throw (IOException)
 
MArray< int, 2 > readIntColumns (const int first=0, const int last=0, const int start=1, int nrows=-1) throw (IOException)
 
MArray< long long, 2 > readLongLongColumns (const int first=0, const int last=0, const int start=1, int nrows=-1) throw (IOException)
 
MArray< float, 2 > readFloatColumns (const int first=0, const int last=0, const int start=1, int nrows=-1) throw (IOException)
 
MArray< double, 2 > readDoubleColumns (const int first=0, const int last=0, const int start=1, int nrows=-1) throw (IOException)
 
MArray< float, 2 > readFloatColumns (const int *cols, const int ncols, const int start=1, int nrows=-1)
 read arbitrary list of columns More...
 
template<class T >
int readColumn (const int col, T &cont, const int start=1, int nrows=-1) throw (IOException)
 High level interface for standard STL containers. More...
 
template<class T >
int readColumn (const int col, vector< T > &cont, const int start=1, int nrows=-1) throw (IOException)
 High level interface for STL vectors. More...
 
template<class T >
int readColumn (const int col, T *&cont, const int start=1, int nrows=-1) throw (IOException)
 High level interface for C-style arrays. More...
 
template<class T >
void replaceColumn (const int col, const T &cont, ostream &os)
 Replace one column and write result to ostream. More...
 
template<class T >
void replaceColumns (const int col1, const int col2, const T &cont, ostream &os)
 Replace consecutive columns and write result to ostream. More...
 
int getHeader (vector< string > &, bool keepcs=false)
 
int rows () throw (IOException)
 High level interface to ltl::AscFile::rows_. More...
 
int cols () throw (IOException)
 High level interface to ltl::AscFile::cols_. More...
 
template<>
int readColumn (const int col, vector< string > &cont, const int start, int nrows) throw (IOException)
 

Protected Member Functions

string readColumnFromLine__ (const int col, const string &line)
 Internal function to extract one column from a given string. More...
 
string replaceColumnsInLine__ (const int col1, const int col2, string &line, const string &rep)
 Internal function to replace one column in a line. More...
 
bool readNextLine__ (string &buf)
 Internal function used to read on line from the file stream. More...
 
char * getNextColumn__ (char *&str)
 Internal function to fast read the next column in a WS delimited file. More...
 
void rewind__ ()
 
bool eof__ ()
 
void countRows__ ()
 Internal function used to count the number of rows. More...
 
void countCols__ ()
 Internal function used to count the number of columns. More...
 
template<class T >
MArray< T, 1 > readSingleCol__ (const int col, const int start=1, int nrows=-1)
 
MArray< float, 1 > readSingleFloatCol__ (const int col, const int start=1, int nrows=-1)
 
MArray< int, 1 > readSingleIntCol__ (const int col, const int start=1, int nrows=-1)
 
template<class T >
MArray< T, 2 > readCols__ (int first=0, int last=0, const int start=1, int nrows=-1)
 
MArray< float, 2 > readFloatCols__ (int first=0, int last=0, const int start=1, int nrows=-1)
 
MArray< int, 2 > readIntCols__ (int first=0, int last=0, const int start=1, int nrows=-1)
 

Protected Attributes

string filename_
 The internal storage of the represented file. More...
 
fstream in_
 The filestream. More...
 
streampos begin_
 Internal storage of the position of the filestream. More...
 
streampos end_
 
int rows_
 Internal storage of the number of rows in the file. More...
 
int cols_
 Internal storage of the number of columns in the file. More...
 
char delim_
 Internal storage of delimiter character. More...
 
string comment_
 Internal storage of the comment string. More...
 
int currline_
 Internal storage of next line to be read. More...
 

Detailed Description

Columns based interface to an ASCII data file.

A high level interface class to read columns of data from an ASCII-file Supports arbitrary delimiter characters and whitespace seperated columns. Comments can begin at any point of a line. An arbitrary string can be used for the beginning of the comment.

Constructor & Destructor Documentation

ltl::AscFile::AscFile ( const string &  fname,
const char  delim = 0,
const string &  comment = "#" 
)

Constructs an AsciiFile object.

: in_(fname.c_str()), rows_(0), cols_(0), filename_(fname), delim_(delim), comment_(comment)

Constructs an ltl::AscFile object corresponding to the file fname.

ltl::AscFile::AscFile ( const string &  fname,
const int  b,
int  e = -1,
const char  delim = 0,
const string &  comment = "#" 
)
virtual ltl::AscFile::~AscFile ( )
virtual

Member Function Documentation

string ltl::AscFile::readColumnFromLine__ ( const int  col,
const string &  line 
)
protected

Internal function to extract one column from a given string.

Extracts the col'th column from the string buff, and returns it as a string.

Exceptions
IOExceptionif an EOF is encountered before the requested colum was read.
IOExceptionif the file cannot be opened.

Referenced by readColumn().

string ltl::AscFile::replaceColumnsInLine__ ( const int  col1,
const int  col2,
string &  line,
const string &  rep 
)
protected

Internal function to replace one column in a line.

Referenced by replaceColumns().

bool ltl::AscFile::readNextLine__ ( string &  buf)
protected

Internal function used to read on line from the file stream.

Reads the next line from the input stream. The input stream is closed between subsequent calls, and the stream position is saved in the variable ltl::AscFile::filepos_.

Referenced by readColumn(), and readNextLine().

char* ltl::AscFile::getNextColumn__ ( char *&  str)
protected

Internal function to fast read the next column in a WS delimited file.

void ltl::AscFile::rewind__ ( )
protected

Referenced by readColumn(), replaceColumns(), and rewind().

bool ltl::AscFile::eof__ ( )
protected

Referenced by replaceColumns().

void ltl::AscFile::countRows__ ( )
protected

Internal function used to count the number of rows.

Counts the rows in the file. Might be expensive on large files, so the function is not called before the information is requested by calling ltl::AscFile::rows().

void ltl::AscFile::countCols__ ( )
protected

Internal function used to count the number of columns.

Counts the cols in the file. Might be expensive on large files, so the function is not called before the information is requested by calling ltl::AscFile::cols().

template<class T >
MArray<T,1> ltl::AscFile::readSingleCol__ ( const int  col,
const int  start = 1,
int  nrows = -1 
)
protected
MArray<float,1> ltl::AscFile::readSingleFloatCol__ ( const int  col,
const int  start = 1,
int  nrows = -1 
)
protected
MArray<int,1> ltl::AscFile::readSingleIntCol__ ( const int  col,
const int  start = 1,
int  nrows = -1 
)
protected
template<class T >
MArray<T,2> ltl::AscFile::readCols__ ( int  first = 0,
int  last = 0,
const int  start = 1,
int  nrows = -1 
)
protected
MArray<float,2> ltl::AscFile::readFloatCols__ ( int  first = 0,
int  last = 0,
const int  start = 1,
int  nrows = -1 
)
protected
MArray<int,2> ltl::AscFile::readIntCols__ ( int  first = 0,
int  last = 0,
const int  start = 1,
int  nrows = -1 
)
protected
void ltl::AscFile::close ( )
inline

References in_.

bool ltl::AscFile::readNextLine ( string &  buf)
inline

References readNextLine__().

void ltl::AscFile::rewind ( )
inline

References rewind__().

MArray<int,1> ltl::AscFile::readIntColumn ( const int  col,
const int  start = 1,
int  nrows = -1 
)
throw (IOException
)
MArray<long long,1> ltl::AscFile::readLongLongColumn ( const int  col,
const int  start = 1,
int  nrows = -1 
)
throw (IOException
)
MArray<float,1> ltl::AscFile::readFloatColumn ( const int  col,
const int  start = 1,
int  nrows = -1 
)
throw (IOException
)
MArray<double,1> ltl::AscFile::readDoubleColumn ( const int  col,
const int  start = 1,
int  nrows = -1 
)
throw (IOException
)
MArray<int,2> ltl::AscFile::readIntColumns ( const int  first = 0,
const int  last = 0,
const int  start = 1,
int  nrows = -1 
)
throw (IOException
)
MArray<long long,2> ltl::AscFile::readLongLongColumns ( const int  first = 0,
const int  last = 0,
const int  start = 1,
int  nrows = -1 
)
throw (IOException
)
MArray<float,2> ltl::AscFile::readFloatColumns ( const int  first = 0,
const int  last = 0,
const int  start = 1,
int  nrows = -1 
)
throw (IOException
)
MArray<double,2> ltl::AscFile::readDoubleColumns ( const int  first = 0,
const int  last = 0,
const int  start = 1,
int  nrows = -1 
)
throw (IOException
)
MArray<float,2> ltl::AscFile::readFloatColumns ( const int cols,
const int  ncols,
const int  start = 1,
int  nrows = -1 
)

read arbitrary list of columns

int ltl::AscFile::getHeader ( vector< string > &  ,
bool  keepcs = false 
)
int ltl::AscFile::rows ( )
throw (IOException
)

High level interface to ltl::AscFile::rows_.

Returns the number of rows in the file.

Referenced by readColumn().

int ltl::AscFile::cols ( )
throw (IOException
)

High level interface to ltl::AscFile::cols_.

Returns the number of columns in the file

Member Data Documentation

string ltl::AscFile::filename_
protected

The internal storage of the represented file.

fstream ltl::AscFile::in_
protected

The filestream.

Referenced by close(), and replaceColumns().

streampos ltl::AscFile::begin_
protected

Internal storage of the position of the filestream.

streampos ltl::AscFile::end_
protected
int ltl::AscFile::rows_
protected

Internal storage of the number of rows in the file.

Stores internally the the number of rows in the file. This variable is initialized with 0. The first call to ltl::AscFile::rows() updates the variable with the correct number of rows.

int ltl::AscFile::cols_
protected

Internal storage of the number of columns in the file.

Stores internally the the number of columns in the file. This variable is initialized with 0. The first call to ltl::AscFile::cols() updates the variable with the correct number of columns.

char ltl::AscFile::delim_
protected

Internal storage of delimiter character.

Stores internally the column delimiter. If the set to int(0) then it is assumed, that the columns are whitespace delimited, and any number of whitespace characters is treated as one delimiter.

Referenced by replaceColumns().

string ltl::AscFile::comment_
protected

Internal storage of the comment string.

Defaults to '#'.

Referenced by replaceColumns().

int ltl::AscFile::currline_
protected

Internal storage of next line to be read.

Referenced by readColumn().