GammaLib
2.1.0.dev
|
Comma-separated values table class. More...
#include <GCsv.hpp>
Public Member Functions | |
GCsv (void) | |
Void constructor. More... | |
GCsv (const int &nrows, const int &ncols) | |
Table constructor. More... | |
GCsv (const GFilename &filename, const std::string &sep=" ") | |
File constructor. More... | |
GCsv (const GCsv &csv) | |
Copy constructor. More... | |
virtual | ~GCsv (void) |
Destructor. More... | |
GCsv & | operator= (const GCsv &csv) |
Assignment operator. More... | |
std::string & | operator() (const int &row, const int &col) |
Table element access operator. More... | |
const std::string & | operator() (const int &row, const int &col) const |
Table element access operator (const version) More... | |
void | clear (void) |
Clear CSV table. More... | |
GCsv * | clone (void) const |
Clone CSV table. More... | |
std::string | classname (void) const |
Return class name. More... | |
int | size (void) const |
Return table size (columns times rows) More... | |
const int & | ncols (void) const |
Return number of columns. More... | |
const int & | nrows (void) const |
Return number of rows. More... | |
const int & | precision (void) const |
Return fixed field floating point precision. More... | |
void | precision (const int &precision) |
Set fixed field floating point precision. More... | |
void | append (const std::vector< std::string > &list) |
Append list of strings. More... | |
std::string | string (const int &row, const int &col) const |
Get string value. More... | |
double | real (const int &row, const int &col) const |
Get double precision value. More... | |
int | integer (const int &row, const int &col) const |
Get integer value. More... | |
void | string (const int &row, const int &col, const std::string &value) |
Get string value. More... | |
void | real (const int &row, const int &col, const double &value) |
Get double precision value. More... | |
void | integer (const int &row, const int &col, const int &value) |
Get integer value. More... | |
void | load (const GFilename &filename, const std::string &sep=" ") |
Load CSV table. More... | |
void | save (const GFilename &filename, const std::string &sep=" ", const bool &clobber=false) const |
Save CSV table. More... | |
std::string | print (const GChatter &chatter=NORMAL) const |
Print column separated values information. More... | |
Public Member Functions inherited from GBase | |
virtual | ~GBase (void) |
Destructor. More... | |
Protected Member Functions | |
void | init_members (void) |
Initialise class members. More... | |
void | copy_members (const GCsv &csv) |
Copy class members. More... | |
void | free_members (void) |
Delete class members. More... | |
Protected Attributes | |
int | m_cols |
Number of columns. More... | |
int | m_rows |
Number of rows. More... | |
std::vector< std::vector < std::string > > | m_data |
CSV table data. More... | |
int | m_precision |
Precision for floats. More... | |
Comma-separated values table class.
This class implements a table of std::string elements that is loaded from a comma-separated value ASCII file. The comma-separation string can be specified upon loading of the file (by default the class assumes that elements are separated by a white space).
The class provides operators for string element access, and methods for conversion of the string values:
double real = csv.real(row,col); int integer = csv.integer(row,col); std::string string = csv.string(row,col);
GCsv::GCsv | ( | void | ) |
Void constructor.
Definition at line 59 of file GCsv.cpp.
References init_members().
Referenced by clone().
GCsv::GCsv | ( | const int & | nrows, |
const int & | ncols | ||
) |
GCsv::GCsv | ( | const GFilename & | filename, |
const std::string & | sep = " " |
||
) |
File constructor.
[in] | filename | Filename. |
[in] | sep | Column separator (default is whitespace). |
Definition at line 101 of file GCsv.cpp.
References init_members(), and load().
GCsv::GCsv | ( | const GCsv & | csv | ) |
Copy constructor.
[in] | csv | Comma-separated values table. |
Definition at line 119 of file GCsv.cpp.
References copy_members(), and init_members().
|
virtual |
void GCsv::append | ( | const std::vector< std::string > & | list | ) |
Append list of strings.
[in] | list | List of strings. |
GException::invalid_argument | Invalid number of elements in list. |
Appends a list of strings to the CSV table. If the table is empty, the number of elements in the list will determine the number of columns of the table. If a table exists already, the method will throw an exception if the number of elements in the list does not correspond to the number of table columns.
Definition at line 273 of file GCsv.cpp.
References G_APPEND, m_cols, m_data, m_rows, and gammalib::str().
|
inlinevirtual |
|
virtual |
Clear CSV table.
This method properly resets the object to an initial state.
Implements GBase.
Definition at line 235 of file GCsv.cpp.
References free_members(), and init_members().
Referenced by load().
|
virtual |
|
protected |
Copy class members.
[in] | csv | Comma-separated values table. |
Definition at line 627 of file GCsv.cpp.
References m_cols, m_data, m_precision, and m_rows.
Referenced by GCsv(), and operator=().
|
protected |
Delete class members.
Definition at line 643 of file GCsv.cpp.
Referenced by clear(), operator=(), and ~GCsv().
|
protected |
Initialise class members.
Definition at line 609 of file GCsv.cpp.
References m_cols, m_data, m_precision, and m_rows.
Referenced by clear(), GCsv(), and operator=().
int GCsv::integer | ( | const int & | row, |
const int & | col | ||
) | const |
Get integer value.
[in] | row | Table row. |
[in] | col | Table column. |
Returns value of specified row and column as integer.
Definition at line 342 of file GCsv.cpp.
References gammalib::toint().
void GCsv::integer | ( | const int & | row, |
const int & | col, | ||
const int & | value | ||
) |
Get integer value.
[in] | row | Table row. |
[in] | col | Table column. |
[in] | value | Integer value. |
Set value of specified row and column as integer.
Definition at line 399 of file GCsv.cpp.
References gammalib::str().
void GCsv::load | ( | const GFilename & | filename, |
const std::string & | sep = " " |
||
) |
Load CSV table.
[in] | filename | Filename. |
[in] | sep | Column separator (default is whitespace). |
GException::file_error | CSV table file not found. |
GException::csv_bad_columns | Inconsistent columns encountered in CSV table file. |
Load CSV table from ASCII file. Any environment variable present in the filename will be expanded.
Definition at line 423 of file GCsv.cpp.
References clear(), gammalib::expand_env(), G_LOAD, m_cols, m_data, m_rows, gammalib::split(), gammalib::str(), gammalib::strip_chars(), gammalib::strip_whitespace(), and GFilename::url().
Referenced by GCsv().
|
inline |
Return number of columns.
Definition at line 137 of file GCsv.hpp.
References m_cols.
Referenced by GCsv(), and GModelSpectralFunc::load_nodes().
|
inline |
Return number of rows.
Definition at line 149 of file GCsv.hpp.
References m_rows.
Referenced by GCsv(), GModelSpectralFunc::load_nodes(), GDaemon::update_countries_data(), GDaemon::update_countries_header(), GDaemon::update_daily(), GDaemon::update_dates(), and GDaemon::update_versions_data().
std::string & GCsv::operator() | ( | const int & | row, |
const int & | col | ||
) |
const std::string & GCsv::operator() | ( | const int & | row, |
const int & | col | ||
) | const |
Assignment operator.
[in] | csv | Comma-separated values table. |
Definition at line 157 of file GCsv.cpp.
References copy_members(), free_members(), and init_members().
|
inline |
Return fixed field floating point precision.
Returns the precision for floating point values when setting values using the real() method. Any value >0 indicates the number of decimal places that the floating point value will have.
Definition at line 165 of file GCsv.hpp.
References m_precision.
Referenced by precision().
|
inline |
Set fixed field floating point precision.
[in] | precision | Fixed field floating point precision. |
Set the precision for floating point values when setting values using the real() method. Any value >0 indicates the number of decimal places that the floating point value will have.
Definition at line 181 of file GCsv.hpp.
References m_precision, and precision().
Print column separated values information.
[in] | chatter | Chattiness (defaults to NORMAL). |
Implements GBase.
Definition at line 569 of file GCsv.cpp.
References m_cols, m_precision, m_rows, gammalib::parformat(), SILENT, and gammalib::str().
double GCsv::real | ( | const int & | row, |
const int & | col | ||
) | const |
Get double precision value.
[in] | row | Table row. |
[in] | col | Table column. |
Returns value of specified row and column as double precision.
Definition at line 324 of file GCsv.cpp.
References gammalib::todouble().
Referenced by GModelSpectralFunc::load_nodes(), GObservations::likelihood::save_csv(), GDaemon::update_countries_data(), GDaemon::update_daily(), and GDaemon::update_versions_data().
void GCsv::real | ( | const int & | row, |
const int & | col, | ||
const double & | value | ||
) |
Get double precision value.
[in] | row | Table row. |
[in] | col | Table column. |
[in] | value | Double precision floating point value. |
Set value of specified row and column as double precision floating point.
Definition at line 380 of file GCsv.cpp.
References m_precision, and gammalib::str().
void GCsv::save | ( | const GFilename & | filename, |
const std::string & | sep = " " , |
||
const bool & | clobber = false |
||
) | const |
Save CSV table.
[in] | filename | Filename. |
[in] | sep | Column separator. |
[in] | clobber | Overwrite existing file? |
GException::invalid_value | Attempt to overwrite existing file. |
GException::file_error | Unable to create file. |
Save CSV table into ASCII file.
Definition at line 519 of file GCsv.cpp.
References GFilename::exists(), G_SAVE, m_cols, m_data, m_rows, and GFilename::url().
Referenced by GModelSpectralFunc::save(), and GObservations::likelihood::save_csv().
|
inline |
std::string GCsv::string | ( | const int & | row, |
const int & | col | ||
) | const |
Get string value.
[in] | row | Table row. |
[in] | col | Table column. |
Returns value of specified row and column as string.
Definition at line 309 of file GCsv.cpp.
Referenced by GObservations::likelihood::save_csv(), GDaemon::update_countries_data(), GDaemon::update_countries_header(), GDaemon::update_daily(), GDaemon::update_dates(), and GDaemon::update_versions_data().
void GCsv::string | ( | const int & | row, |
const int & | col, | ||
const std::string & | value | ||
) |
|
protected |
Number of columns.
Definition at line 100 of file GCsv.hpp.
Referenced by append(), copy_members(), GCsv(), init_members(), load(), ncols(), operator()(), print(), save(), and size().
|
protected |
CSV table data.
Definition at line 102 of file GCsv.hpp.
Referenced by append(), copy_members(), GCsv(), init_members(), load(), operator()(), and save().
|
protected |
Precision for floats.
Definition at line 103 of file GCsv.hpp.
Referenced by copy_members(), init_members(), precision(), print(), and real().
|
protected |
Number of rows.
Definition at line 101 of file GCsv.hpp.
Referenced by append(), copy_members(), GCsv(), init_members(), load(), nrows(), operator()(), print(), save(), and size().