GWOLLUM  4.2.0
Tools for gravitational-wave analyses
ReadAscii Class Reference

Parse text files with columns. More...

#include <ReadAscii.h>

Public Member Functions

bool GetCol (vector< double > &col, const unsigned int co)
 Returns the content of a double column in a vector. More...
 
bool GetCol (vector< int > &col, const unsigned int co)
 Returns the content of an integer column in a vector. More...
 
bool GetCol (vector< string > &col, const unsigned int co)
 Returns the content of a string column in a vector. More...
 
bool GetCol (vector< unsigned int > &col, const unsigned int co)
 Returns the content of an unsigned integer column in a vector. More...
 
bool GetElement (double &element, const unsigned int li, const unsigned int co)
 Returns the double data value at a given line and column position. More...
 
bool GetElement (int &element, const unsigned int li, const unsigned int co)
 Returns the integer data value at a given line and column position. More...
 
bool GetElement (string &element, const unsigned int li, const unsigned int co)
 Returns the string data value at a given line and column position. More...
 
bool GetElement (unsigned int &element, const unsigned int li, const unsigned int co)
 Returns the unsigned integer data value at a given line and column position. More...
 
bool GetLine (string &line, const unsigned int li)
 Returns the content of a line as a string. More...
 
unsigned int GetNCol (void)
 Returns the number of columns. More...
 
unsigned int GetNLine (void)
 Returns the number of rows/lines. More...
 
unsigned int GetNRow (void)
 Returns the number of rows/lines. More...
 
bool SetFormat (const unsigned int co, const char aformat)
 Defines a new column format ('s', 'd', 'u' or 'i'). More...
 
Constructors and destructors
 ReadAscii (const string afilename, const string aformat="UNKNOWN")
 Constructor of the ReadAscii class. More...
 
virtual ~ReadAscii (void)
 Destructor of the ReadAscii class. More...
 

Private Member Functions

void ExtractCol (void)
 Extract columns. More...
 
void ExtractFormat (void)
 Extract format vector. More...
 
void ExtractLines (void)
 Extract lines. More...
 
void GetMinMax (const unsigned int co)
 Extract min/max values. More...
 

Private Attributes

double colmax [NCOLMAX]
 Maximum in a given column. More...
 
double colmin [NCOLMAX]
 Minimum in a given column. More...
 
vector< double > dcol [NCOLMAX]
 Column of double. More...
 
string ffilename
 Input file name. More...
 
string fformat
 Input format string. More...
 
vector< int > icol [NCOLMAX]
 Column of int. More...
 
vector< string > mylines
 Lines of the files. More...
 
vector< string > scol [NCOLMAX]
 Column of string. More...
 
vector< unsigned int > ucol [NCOLMAX]
 Column of unsigned int. More...
 
vector< string > Vformat
 Vector of formats. More...
 

Detailed Description

Parse text files with columns.

Many operations are then available to access and modify the data elements.

Author
Florent Robinet

Constructor & Destructor Documentation

◆ ReadAscii()

ReadAscii::ReadAscii ( const string  afilename,
const string  aformat = "UNKNOWN" 
)

Constructor of the ReadAscii class.

With this constructor, the text file is loaded and data are extracted.

  • The text file must be composed of a fix number of data columns.
  • This file should not contain more than NCOLMAX columns.
  • Columns should be separated by white spaces.
  • Empty lines and commented lines ('//', '#' or '%') are ignored.
  • If known, the format of each column should be provided in argument. The format syntax is the folowing: "t;t;t", where ';' separates each column definition and 't' refers to the type of data which is expected. Three data types are supported:
    • 's' for a string type
    • 'i' for a int type
    • 'u' for an unsigned int type
    • 'd' for a double/float type
ReadAscii("/path/to/my/file.txt", "s;s;i;i");
ReadAscii(const string afilename, const string aformat="UNKNOWN")
Constructor of the ReadAscii class.
Definition: ReadAscii.cc:9

This example means that the text file is composed of 4 columns, the 2 first ones are string fields and the 2 last ones are integer fields.

If the input format is unknown, 'aformat' should be set to "UNKNOWN" which it is by default. In that case, all columns are assumed to have a string format. This default format can be changed later on by using the SetFormat() function.

Lines with a wrong number of columns (as defined by aformat or by the first line) are skipped.

Parameters
[in]afilenamePath to the text file.
[in]aformatString format to be expected in the file.

◆ ~ReadAscii()

ReadAscii::~ReadAscii ( void  )
virtual

Destructor of the ReadAscii class.

Member Function Documentation

◆ ExtractCol()

void ReadAscii::ExtractCol ( void  )
private

Extract columns.

◆ ExtractFormat()

void ReadAscii::ExtractFormat ( void  )
private

Extract format vector.

◆ ExtractLines()

void ReadAscii::ExtractLines ( void  )
private

Extract lines.

◆ GetCol() [1/4]

bool ReadAscii::GetCol ( vector< double > &  col,
const unsigned int  co 
)

Returns the content of a double column in a vector.

true is returned in case of success.

Parameters
[out]colReturned vector of double data.
[in]coSelected column number (indexing starts at 0).

◆ GetCol() [2/4]

bool ReadAscii::GetCol ( vector< int > &  col,
const unsigned int  co 
)

Returns the content of an integer column in a vector.

true is returned in case of success.

Parameters
[out]colReturned vector of integer data.
[in]coSelected column number (indexing starts at 0).

◆ GetCol() [3/4]

bool ReadAscii::GetCol ( vector< string > &  col,
const unsigned int  co 
)

Returns the content of a string column in a vector.

true is returned in case of success.

Parameters
[out]colReturned vector of string data.
[in]coSelected column number (indexing starts at 0).

◆ GetCol() [4/4]

bool ReadAscii::GetCol ( vector< unsigned int > &  col,
const unsigned int  co 
)

Returns the content of an unsigned integer column in a vector.

true is returned in case of success.

Parameters
[out]colReturned vector of integer data.
[in]coSelected column number (indexing starts at 0).

◆ GetElement() [1/4]

bool ReadAscii::GetElement ( double &  element,
const unsigned int  li,
const unsigned int  co 
)

Returns the double data value at a given line and column position.

true is returned in case of success.

Parameters
[out]elementReturned data value.
[in]coSelected column number (indexing starts at 0).
[in]liSelected line number (indexing starts at 0).

◆ GetElement() [2/4]

bool ReadAscii::GetElement ( int &  element,
const unsigned int  li,
const unsigned int  co 
)

Returns the integer data value at a given line and column position.

true is returned in case of success.

Parameters
[out]elementReturned data value.
[in]coSelected column number (indexing starts at 0).
[in]liSelected line number (indexing starts at 0).

◆ GetElement() [3/4]

bool ReadAscii::GetElement ( string &  element,
const unsigned int  li,
const unsigned int  co 
)

Returns the string data value at a given line and column position.

true is returned in case of success.

Parameters
[out]elementReturned data value.
[in]coSelected column number (indexing starts at 0).
[in]liSelected line number (indexing starts at 0).

◆ GetElement() [4/4]

bool ReadAscii::GetElement ( unsigned int &  element,
const unsigned int  li,
const unsigned int  co 
)

Returns the unsigned integer data value at a given line and column position.

true is returned in case of success.

Parameters
[out]elementReturned data value.
[in]coSelected column number (indexing starts at 0).
[in]liSelected line number (indexing starts at 0).

◆ GetLine()

bool ReadAscii::GetLine ( string &  line,
const unsigned int  li 
)

Returns the content of a line as a string.

true is returned in case of success.

Parameters
[out]lineReturned line.
[in]liSelected line number (indexing starts at 0).

◆ GetMinMax()

void ReadAscii::GetMinMax ( const unsigned int  co)
private

Extract min/max values.

◆ GetNCol()

unsigned int ReadAscii::GetNCol ( void  )
inline

Returns the number of columns.

◆ GetNLine()

unsigned int ReadAscii::GetNLine ( void  )
inline

Returns the number of rows/lines.

◆ GetNRow()

unsigned int ReadAscii::GetNRow ( void  )
inline

Returns the number of rows/lines.

◆ SetFormat()

bool ReadAscii::SetFormat ( const unsigned int  co,
const char  aformat 
)

Defines a new column format ('s', 'd', 'u' or 'i').

The selected column is converted to a new format. Only one conversion is destructive: 'd' to 'i/u'. In that case, the doubles are recasted as integers. true is returned in case of success.

Parameters
[in]coSelected column number (indexing starts at 0).
[in]aformatchar new format ('s', 'd'. 'u' or 'i').

Member Data Documentation

◆ colmax

double ReadAscii::colmax[NCOLMAX]
private

Maximum in a given column.

◆ colmin

double ReadAscii::colmin[NCOLMAX]
private

Minimum in a given column.

◆ dcol

vector<double> ReadAscii::dcol[NCOLMAX]
private

Column of double.

◆ ffilename

string ReadAscii::ffilename
private

Input file name.

◆ fformat

string ReadAscii::fformat
private

Input format string.

◆ icol

vector<int> ReadAscii::icol[NCOLMAX]
private

Column of int.

◆ mylines

vector<string> ReadAscii::mylines
private

Lines of the files.

◆ scol

vector<string> ReadAscii::scol[NCOLMAX]
private

Column of string.

◆ ucol

vector<unsigned int> ReadAscii::ucol[NCOLMAX]
private

Column of unsigned int.

◆ Vformat

vector<string> ReadAscii::Vformat
private

Vector of formats.


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