38 #define G_ACCESS "GCsv::operator()(int&, int&)"
39 #define G_APPEND "GCsv::append(std::vector<std::string>&)"
40 #define G_LOAD "GCsv::load(GFilename&, std::string&)"
41 #define G_SAVE "GCsv::save(GFilename&, std::string&, bool&)"
81 std::vector<std::string> row(ncols,
"");
187 #if defined(G_RANGE_CHECK)
188 if (row < 0 || row >=
m_rows) {
191 if (col < 0 || col >=
m_cols) {
210 #if defined(G_RANGE_CHECK)
211 if (row < 0 || row >=
m_rows) {
214 if (col < 0 || col >=
m_cols) {
255 return new GCsv(*
this);
277 if (
m_cols != list.size()) {
278 std::string msg =
"Invalid attempt to append "+
312 return (*
this)(row, col);
361 void GCsv::string(
const int& row,
const int& col,
const std::string& value)
364 (*this)(row, col) = value;
380 void GCsv::real(
const int& row,
const int& col,
const double& value)
436 FILE* fptr = std::fopen(fname.c_str(),
"r");
438 std::string msg =
"Unable to open file \""+fname+
"\" for read access. "
439 "Please specify a readable file.";
445 while (std::fgets(line, n, fptr) != NULL) {
455 if (sline.length() == 0) {
460 if (sline[0] ==
'#') {
466 for (
int i = 0; i < elements.size(); ++i) {
472 m_data.push_back(elements);
479 if (
m_cols != elements.size()) {
480 std::string msg =
"Number of "+
gammalib::str(elements.size())+
482 " is not consistent with number of "+
484 "specify a correctly formatted CSV file.";
489 m_data.push_back(elements);
520 const std::string& sep,
521 const bool& clobber)
const
524 if (!clobber && filename.
exists()) {
525 std::string msg =
"File \""+filename+
"\" exists already but "
526 "the clobber flag is set to \"false\". Set the "
527 "clobber flag to true to overwrite the existing "
528 "file or specify another file name.";
533 FILE* fptr = std::fopen(filename.
url().c_str(),
"w");
535 std::string msg =
"Unable to create file \""+filename+
"\".";
540 for (
int row = 0; row <
m_rows; ++row) {
543 for (
int col = 0; col <
m_cols; ++col) {
544 std::fputs(
m_data[row][col].c_str(), fptr);
545 if (col < m_cols-1) {
546 std::fputs(sep.c_str(), fptr);
551 std::fputs(
"\n", fptr);
578 result.append(
"=== GCsv ===");
587 result.append(
"default");
void append(const std::vector< std::string > &list)
Append list of strings.
void free_members(void)
Delete class members.
void save(const GFilename &filename, const std::string &sep=" ", const bool &clobber=false) const
Save CSV table.
int m_precision
Precision for floats.
GCsv(void)
Void constructor.
GCsv & operator=(const GCsv &csv)
Assignment operator.
void clear(void)
Clear CSV table.
Comma-separated values table class.
std::string strip_chars(const std::string &arg, const std::string &chars)
Strip leading and trailing character from string.
std::vector< std::string > split(const std::string &s, const std::string &sep)
Split string.
virtual ~GCsv(void)
Destructor.
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
int m_cols
Number of columns.
std::string string(const int &row, const int &col) const
Get string value.
const int & ncols(void) const
Return number of columns.
void init_members(void)
Initialise class members.
std::string & operator()(const int &row, const int &col)
Table element access operator.
GCsv * clone(void) const
Clone CSV table.
bool exists(void) const
Checks whether file exists.
int integer(const int &row, const int &col) const
Get integer value.
const int & nrows(void) const
Return number of rows.
double real(const int &row, const int &col) const
Get double precision value.
int m_rows
Number of rows.
std::string url(void) const
Return Uniform Resource Locator (URL)
std::vector< std::vector< std::string > > m_data
CSV table data.
Exception handler interface definition.
void load(const GFilename &filename, const std::string &sep=" ")
Load CSV table.
std::string expand_env(const std::string &arg)
Expand environment variables in string.
int toint(const std::string &arg)
Convert string into integer value.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
std::string print(const GChatter &chatter=NORMAL) const
Print column separated values information.
void copy_members(const GCsv &csv)
Copy class members.
Filename class interface definition.
Comma-separated values table class definition.
double todouble(const std::string &arg)
Convert string into double precision value.
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.