Parse text files with columns. More...
#include <ReadAscii.h>
Public Member Functions | |
| bool | AddRow (const string newline) |
| Adds a new row at the end of the row list. | |
| bool | GetCol (vector< string > &col, const int co) |
| Dumps the content of a string column in a vector. | |
| bool | GetCol (vector< double > &col, const int co) |
| Dumps the content of a double column in a vector. | |
| bool | GetCol (vector< int > &col, const int co) |
| Dumps the content of an integer column in a vector. | |
| double | GetColMax (const int co) |
| Returns the maximum value of a column. | |
| double | GetColMin (const int co) |
| Returns the minimum value of a column. | |
| bool | GetElement (string &element, const int li, const int co) |
| Returns the string data value at a given line and column position. | |
| bool | GetElement (double &element, const int li, const int co) |
| Returns the double data value at a given line and column position. | |
| bool | GetElement (int &element, const int li, const int co) |
| Returns the integer data value at a given line and column position. | |
| string | GetFormat (const int co) |
| Returns the format string ('s', 'd' or 'i') of a column. | |
| string | GetFormat (void) |
| Returns the format string with the convetion decribed in ReadAscii(). | |
| bool | GetLine (string &line, const int li) |
| Dumps the content of a line as a string. | |
| int | GetNCol (void) |
| Returns the number of columns. | |
| int | GetNLine (void) |
| Returns the number of rows/lines. | |
| int | GetNRow (void) |
| Returns the number of rows/lines. | |
| string | GetRow (const int li) |
| Dumps the content of a row/line in a string. | |
| bool | ModifyElement (const string newelement, const int li, const int co) |
| Modify an element at a given line and column position. | |
| bool | ModifyElement (const double newelement, const int li, const int co) |
| Modify an element at a given line and column position. | |
| bool | ModifyElement (const int newelement, const int li, const int co) |
| Modify an element at a given line and column position. | |
| bool | RemoveRow (const int li) |
| Removes a row/line. | |
| bool | SetFormat (const int co, const string aformat) |
| Defines a column format ('s', 'd' or 'i'). | |
| bool | WriteFile (const string filename) |
| Writes the data in a new ouput text file. | |
| bool | WriteFile (const char *filename) |
| Writes the data in a new ouput text file. | |
Constructors and destructors | |
| ReadAscii (const string afilename, const string aformat="UNKNOWN") | |
| Constructor of the ReadAscii class. | |
| virtual | ~ReadAscii (void) |
| Destructor of the ReadAscii class. | |
Parse text files with columns.
Many operations are then available to access and modify the data elements.
| ReadAscii::ReadAscii | ( | const string | afilename, | |
| const string | aformat = "UNKNOWN" | |||
| ) |
Constructor of the ReadAscii class.
With this constructor, the text file is loaded and parsed. The text file must be composed of a fix number of data columns. This file should not contain more than 50 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 formats are supported:
{.cpp}
ReadAscii("/path/to/my/file.txt", "s;s;i;i");
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.
| afilename | path to the text file | |
| aformat | string format to be expected in the file |
| ReadAscii::~ReadAscii | ( | void | ) | [virtual] |
Destructor of the ReadAscii class.
| bool ReadAscii::AddRow | ( | const string | newline | ) |
Adds a new row at the end of the row list.
The new row should be given as a string with as many columns as defined in the constructor. The format of each column should be respected as well.
true is returned in case of success.
| newline | new line of data to be added. |
| bool ReadAscii::GetCol | ( | vector< string > & | col, | |
| const int | co | |||
| ) |
Dumps the content of a string column in a vector.
true is returned in case of success.
| col | returned vector of string data. | |
| co | selected column number (indexing starts at 0). |
| bool ReadAscii::GetCol | ( | vector< double > & | col, | |
| const int | co | |||
| ) |
Dumps the content of a double column in a vector.
true is returned in case of success.
| col | returned vector of double data | |
| co | selected column number (indexing starts at 0) |
| bool ReadAscii::GetCol | ( | vector< int > & | col, | |
| const int | co | |||
| ) |
Dumps the content of an integer column in a vector.
true is returned in case of success.
| col | returned vector of integer data | |
| co | selected column number (indexing starts at 0) |
| double ReadAscii::GetColMax | ( | const int | co | ) |
Returns the maximum value of a column.
This function is only available for integer and double columns. For integers, the returned value is casted as double.
-1 is returned in case of failure.
| co | selected column number (indexing starts at 0). |
| double ReadAscii::GetColMin | ( | const int | co | ) |
Returns the minimum value of a column.
This function is only available for integer and double columns. For integers, the returned value is casted as double.
-1 is returned in case of failure.
| co | selected column number (indexing starts at 0). |
| bool ReadAscii::GetElement | ( | string & | element, | |
| const int | li, | |||
| const int | co | |||
| ) |
Returns the string data value at a given line and column position.
true is returned in case of success.
| element | returned data value. | |
| co | selected column number (indexing starts at 0). | |
| li | selected line number (indexing starts at 0). |
| bool ReadAscii::GetElement | ( | double & | element, | |
| const int | li, | |||
| const int | co | |||
| ) |
Returns the double data value at a given line and column position.
true is returned in case of success.
| element | returned data value. | |
| co | selected column number (indexing starts at 0). | |
| li | selected line number (indexing starts at 0). |
| bool ReadAscii::GetElement | ( | int & | element, | |
| const int | li, | |||
| const int | co | |||
| ) |
Returns the integer data value at a given line and column position.
true is returned in case of success.
| element | returned data value. | |
| co | selected column number (indexing starts at 0). | |
| li | selected line number (indexing starts at 0). |
| string ReadAscii::GetFormat | ( | const int | co | ) |
Returns the format string ('s', 'd' or 'i') of a column.
"NONE" is returned in case of failure.
| co | selected column number (indexing starts at 0). |
| string ReadAscii::GetFormat | ( | void | ) |
Returns the format string with the convetion decribed in ReadAscii().
"NONE" is returned in case of failure.
| bool ReadAscii::GetLine | ( | string & | line, | |
| const int | li | |||
| ) |
Dumps the content of a line as a string.
true is returned in case of success.
| line | returned line. | |
| li | selected line number (indexing starts at 0) |
| int ReadAscii::GetNCol | ( | void | ) | [inline] |
Returns the number of columns.
| int ReadAscii::GetNLine | ( | void | ) | [inline] |
Returns the number of rows/lines.
| int ReadAscii::GetNRow | ( | void | ) | [inline] |
Returns the number of rows/lines.
| string ReadAscii::GetRow | ( | const int | li | ) |
Dumps the content of a row/line in a string.
"NONE" is returned in case of failure.
| li | selected line number (indexing starts at 0) |
| bool ReadAscii::ModifyElement | ( | const string | newelement, | |
| const int | li, | |||
| const int | co | |||
| ) |
Modify an element at a given line and column position.
the orignal string format should be respected.
true is returned in case of success.
| newelement | new element string value. | |
| li | selected line number (indexing starts at 0). | |
| co | selected column number (indexing starts at 0). |
| bool ReadAscii::ModifyElement | ( | const double | newelement, | |
| const int | li, | |||
| const int | co | |||
| ) |
Modify an element at a given line and column position.
the orignal double format should be respected.
true is returned in case of success.
| newelement | new element double value. | |
| li | selected line number (indexing starts at 0). | |
| co | selected column number (indexing starts at 0). |
| bool ReadAscii::ModifyElement | ( | const int | newelement, | |
| const int | li, | |||
| const int | co | |||
| ) |
Modify an element at a given line and column position.
the orignal integer format should be respected.
true is returned in case of success.
| newelement | new element integer value. | |
| li | selected line number (indexing starts at 0). | |
| co | selected column number (indexing starts at 0). |
| bool ReadAscii::RemoveRow | ( | const int | li | ) |
Removes a row/line.
true is returned in case of success.
| li | selected line number (indexing starts at 0). |
| bool ReadAscii::SetFormat | ( | const int | co, | |
| const string | aformat | |||
| ) |
Defines a column format ('s', 'd' or 'i').
This function can only be used in the case where the column format was set as "UNKNOWN" in the constructor.
true is returned in case of success.
| co | selected column number (indexing starts at 0). | |
| aformat | string format ('s', 'd' or 'i'). |
| bool ReadAscii::WriteFile | ( | const string | filename | ) | [inline] |
Writes the data in a new ouput text file.
It takes into account all the modifications performed on the ReadAscii object. If filename already exists, it is erased first.
true is returned in case of success.
| filename | output file name. |
| bool ReadAscii::WriteFile | ( | const char * | filename | ) |
Writes the data in a new ouput text file.
It takes into account all the modifications performed on the ReadAscii object. If filename already exists, it is erased first.
true is returned in case of success.
| filename | output file name. |
1.6.1