thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
ThePEG::CFileLineReader Class Reference

CFileLineReader is a wrapper around a standard C FILE stream. More...

#include <CFileLineReader.h>

Public Member Functions

Standard constructors and destructors.
 CFileLineReader ()
 The default constructor.
 
 CFileLineReader (string filename, int len=defsize)
 Constructor taking a filename as argument.
 
 ~CFileLineReader ()
 The destructor.
 
Initialization functions.
void open (string filename)
 Initialize with a filename.
 
void close ()
 If the file was opened from within this object, close it.
 
bool readline ()
 Read a line from the underlying c-file into the line buffer.
 
void resetline ()
 Undo reading from the current line, ie.
 
string getline () const
 Return a string containing what is left of the line buffer.
 
CFile cFile () const
 Return the underlying c-file.
 
 operator void * ()
 Return null if a previous read failed.
 
bool operator! ()
 Return true if a previous read failed.
 
bool skip (char c)
 Scan forward up and until the first occurrence of the given character.
 
bool find (string str) const
 Check if a given string is present in the current line buffer.
 

Operators to read from the line buffer.

CFile file
 The c-file to be read from.
 
int bufflen
 The length of the line buffer.
 
char * buff
 The line buffer.
 
char * pos
 The current position in the line buffer.
 
bool bad
 The current state is bad if a read has failed.
 
static const int defsize = 1024
 The default size of the buffer.
 
char getc ()
 Return the next character of the line-buffer.
 
CFileLineReaderoperator>> (long &l)
 Read a long from the line buffer.
 
CFileLineReaderoperator>> (int &i)
 Read an int from the line buffer.
 
CFileLineReaderoperator>> (unsigned long &l)
 Read an unsigned long from the line buffer.
 
CFileLineReaderoperator>> (unsigned int &i)
 Read an unsigned int from the line buffer.
 
CFileLineReaderoperator>> (double &d)
 Read a double from the line buffer.
 
CFileLineReaderoperator>> (float &f)
 Read a float from the line buffer.
 
CFileLineReaderoperator>> (std::string &s)
 Read a (whitespace delimited) string from the line buffer.
 
 CFileLineReader (const CFileLineReader &)
 The copy constructor is private and not implemented.
 
CFileLineReaderoperator= (const CFileLineReader &)=delete
 The assignment operator is private and must never be called.
 

Detailed Description

CFileLineReader is a wrapper around a standard C FILE stream.

With it one reads one line at the time (with readline()) into an internal buffer from which one can then read as from a standard std::istream with a limited set of operator>> functions. It can be thought of as an std::ifstream where the internal buffer must be filled by hand one line at the time.

Contrary to std::ifstream the CFileLineReader can also handle gipped files and pipes. Gzipped files are automatically handles by a pipe using the zcat command if the file name ends with .gz. Also if a file name ends with a | sign, the preceding string is interpreted as a command defining a pipe from which to read.

Since CFileLineReader is very close to the standard C FILE stream it is in many cases much faster than eg. reading from lines via std::istringstream.

Definition at line 42 of file CFileLineReader.h.

Constructor & Destructor Documentation

◆ CFileLineReader()

ThePEG::CFileLineReader::CFileLineReader ( string  filename,
int  len = defsize 
)

Constructor taking a filename as argument.

Optionally the size len of the line buffer can be specified. If filename ends with .gz a pipe is opened where the file is read by zcat. If filename ends with a | sign, the preceding string is interpreted as a command defining a pipe from which to read.

Member Function Documentation

◆ open()

void ThePEG::CFileLineReader::open ( string  filename)

Initialize with a filename.

If filename ends with .gz a pipe is opened where the file is read by zcat. If filename ends with a | sign, the preceding string is interpreted as a command defining a pipe from which to read.

◆ operator=()

CFileLineReader & ThePEG::CFileLineReader::operator= ( const CFileLineReader )
privatedelete

The assignment operator is private and must never be called.

In fact, it should not even be implemented.

◆ resetline()

void ThePEG::CFileLineReader::resetline ( )

Undo reading from the current line, ie.

the next read will be from the beginning of the current line. Afterwards the state will be not bad.

◆ skip()

bool ThePEG::CFileLineReader::skip ( char  c)

Scan forward up and until the first occurrence of the given character.

Returns
true if the given character was found.

Member Data Documentation

◆ bad

bool ThePEG::CFileLineReader::bad
private

The current state is bad if a read has failed.

Definition at line 198 of file CFileLineReader.h.

◆ buff

char* ThePEG::CFileLineReader::buff
private

The line buffer.

Definition at line 188 of file CFileLineReader.h.

◆ bufflen

int ThePEG::CFileLineReader::bufflen
private

The length of the line buffer.

Definition at line 183 of file CFileLineReader.h.

◆ defsize

const int ThePEG::CFileLineReader::defsize = 1024
staticprivate

The default size of the buffer.

Definition at line 203 of file CFileLineReader.h.

◆ file

CFile ThePEG::CFileLineReader::file
private

The c-file to be read from.

Definition at line 178 of file CFileLineReader.h.

◆ pos

char* ThePEG::CFileLineReader::pos
private

The current position in the line buffer.

Definition at line 193 of file CFileLineReader.h.


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