GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GCsv Class Reference

Comma-separated values table class. More...

#include <GCsv.hpp>

Inheritance diagram for GCsv:
GBase

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...
 
GCsvoperator= (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...
 
GCsvclone (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...
 

Detailed Description

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);

Definition at line 57 of file GCsv.hpp.

Constructor & Destructor Documentation

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 
)

Table constructor.

Parameters
[in]nrowsNumber of rows.
[in]ncolsNumber of columns.

Definition at line 75 of file GCsv.cpp.

References init_members(), m_cols, m_data, m_rows, ncols(), and nrows().

GCsv::GCsv ( const GFilename filename,
const std::string &  sep = " " 
)

File constructor.

Parameters
[in]filenameFilename.
[in]sepColumn separator (default is whitespace).

Definition at line 101 of file GCsv.cpp.

References init_members(), and load().

GCsv::GCsv ( const GCsv csv)

Copy constructor.

Parameters
[in]csvComma-separated values table.

Definition at line 119 of file GCsv.cpp.

References copy_members(), and init_members().

GCsv::~GCsv ( void  )
virtual

Destructor.

Definition at line 135 of file GCsv.cpp.

References free_members().

Member Function Documentation

void GCsv::append ( const std::vector< std::string > &  list)

Append list of strings.

Parameters
[in]listList of strings.
Exceptions
GException::invalid_argumentInvalid 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 267 of file GCsv.cpp.

References G_APPEND, m_cols, m_data, m_rows, and gammalib::str().

std::string GCsv::classname ( void  ) const
inlinevirtual

Return class name.

Returns
String containing the class name ("GCsv").

Implements GBase.

Definition at line 113 of file GCsv.hpp.

void GCsv::clear ( void  )
virtual

Clear CSV table.

This method properly resets the object to an initial state.

Implements GBase.

Definition at line 229 of file GCsv.cpp.

References free_members(), and init_members().

Referenced by load().

GCsv * GCsv::clone ( void  ) const
virtual

Clone CSV table.

Returns
Pointer to deep copy of CSV table.

Implements GBase.

Definition at line 247 of file GCsv.cpp.

References GCsv().

void GCsv::copy_members ( const GCsv csv)
protected

Copy class members.

Parameters
[in]csvComma-separated values table.

Definition at line 615 of file GCsv.cpp.

References m_cols, m_data, m_precision, and m_rows.

Referenced by GCsv(), and operator=().

void GCsv::free_members ( void  )
protected

Delete class members.

Definition at line 631 of file GCsv.cpp.

Referenced by clear(), operator=(), and ~GCsv().

void GCsv::init_members ( void  )
protected

Initialise class members.

Definition at line 597 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.

Parameters
[in]rowTable row.
[in]colTable column.

Returns value of specified row and column as integer.

Definition at line 336 of file GCsv.cpp.

References gammalib::toint().

void GCsv::integer ( const int &  row,
const int &  col,
const int &  value 
)

Get integer value.

Parameters
[in]rowTable row.
[in]colTable column.
[in]valueInteger value.

Set value of specified row and column as integer.

Definition at line 393 of file GCsv.cpp.

References gammalib::str().

void GCsv::load ( const GFilename filename,
const std::string &  sep = " " 
)

Load CSV table.

Parameters
[in]filenameFilename.
[in]sepColumn separator (default is whitespace).
Exceptions
GException::file_not_foundCSV table file not found.
GException::csv_bad_columnsInconsistent 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 417 of file GCsv.cpp.

References clear(), gammalib::expand_env(), G_LOAD, m_cols, m_data, m_rows, gammalib::split(), gammalib::strip_chars(), gammalib::strip_whitespace(), and GFilename::url().

Referenced by GCsv().

const int & GCsv::ncols ( void  ) const
inline

Return number of columns.

Returns
Number of columns.

Definition at line 137 of file GCsv.hpp.

References m_cols.

Referenced by GCsv(), and GModelSpectralFunc::load_nodes().

const int & GCsv::nrows ( void  ) const
inline

Return number of rows.

Returns
Number of rows.

Definition at line 149 of file GCsv.hpp.

References m_rows.

Referenced by GCsv(), and GModelSpectralFunc::load_nodes().

std::string & GCsv::operator() ( const int &  row,
const int &  col 
)

Table element access operator.

Parameters
[in]rowTable row.
[in]colTable column.

Definition at line 184 of file GCsv.cpp.

References G_ACCESS, m_cols, m_data, and m_rows.

const std::string & GCsv::operator() ( const int &  row,
const int &  col 
) const

Table element access operator (const version)

Parameters
[in]rowTable row.
[in]colTable column.

Definition at line 204 of file GCsv.cpp.

References G_ACCESS, m_cols, m_data, and m_rows.

GCsv & GCsv::operator= ( const GCsv csv)

Assignment operator.

Parameters
[in]csvComma-separated values table.
Returns
Comma-separated values table.

Definition at line 157 of file GCsv.cpp.

References copy_members(), free_members(), and init_members().

const int & GCsv::precision ( void  ) const
inline

Return fixed field floating point precision.

Returns
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().

void GCsv::precision ( const int &  precision)
inline

Set fixed field floating point precision.

Parameters
[in]precisionFixed 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().

std::string GCsv::print ( const GChatter chatter = NORMAL) const
virtual

Print column separated values information.

Parameters
[in]chatterChattiness (defaults to NORMAL).
Returns
String containing column separated values information.

Implements GBase.

Definition at line 557 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.

Parameters
[in]rowTable row.
[in]colTable column.

Returns value of specified row and column as double precision.

Definition at line 318 of file GCsv.cpp.

References gammalib::todouble().

Referenced by GModelSpectralFunc::load_nodes(), and GObservations::likelihood::save_csv().

void GCsv::real ( const int &  row,
const int &  col,
const double &  value 
)

Get double precision value.

Parameters
[in]rowTable row.
[in]colTable column.
[in]valueDouble precision floating point value.

Set value of specified row and column as double precision floating point.

Definition at line 374 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.

Parameters
[in]filenameFilename.
[in]sepColumn separator (default: whitespace).
[in]clobberOverwrite existing file? (default: false).
Exceptions
GException::invalid_valueAttempt to overwrite existing file.
GException::file_errorUnable to create file.

Save CSV table into ASCII file.

Definition at line 507 of file GCsv.cpp.

References GFilename::exists(), G_SAVE, m_cols, m_data, m_rows, and GFilename::url().

Referenced by GObservations::likelihood::save_csv().

int GCsv::size ( void  ) const
inline

Return table size (columns times rows)

Returns
Table size.

Definition at line 125 of file GCsv.hpp.

References m_cols, and m_rows.

std::string GCsv::string ( const int &  row,
const int &  col 
) const

Get string value.

Parameters
[in]rowTable row.
[in]colTable column.

Returns value of specified row and column as string.

Definition at line 303 of file GCsv.cpp.

Referenced by GObservations::likelihood::save_csv().

void GCsv::string ( const int &  row,
const int &  col,
const std::string &  value 
)

Get string value.

Parameters
[in]rowTable row.
[in]colTable column.
[in]valueString value.

Set value of specified row and column as string.

Definition at line 355 of file GCsv.cpp.

Member Data Documentation

int GCsv::m_cols
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().

std::vector<std::vector<std::string> > GCsv::m_data
protected

CSV table data.

Definition at line 102 of file GCsv.hpp.

Referenced by append(), copy_members(), GCsv(), init_members(), load(), operator()(), and save().

int GCsv::m_precision
protected

Precision for floats.

Definition at line 103 of file GCsv.hpp.

Referenced by copy_members(), init_members(), precision(), print(), and real().

int GCsv::m_rows
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().


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