55 data.resize(data.size() + nrows);
62 if (i >= data.size()) {
63 cerr <<
"Error(" __FILE__
":" << __LINE__ <<
"): Trying to access row " << i
64 <<
" but column has only " << data.size() <<
" rows." << endl;
73 if (i >= data.size()) {
74 cerr <<
"Error(" __FILE__
":" << __LINE__ <<
"): Trying to access row " << i
75 <<
" but column has only " << data.size() <<
" rows." << endl;
100 typename vector<CSVTableColumn<Tvar> >::iterator it;
102 for (it = columns.begin(); it != columns.end(); it++) {
114 typename vector<CSVTableColumn<Tvar> >::iterator it;
115 for (it = columns.begin(); it != columns.end(); it++, colidx++) {
116 if (it->isHeader(column_name))
125 columns.push_back(col);
126 columns.back().addRows(nrows);
127 return (columns.size()-1);
136 int setCell(
unsigned row_idx,
string col_name,
string value,
bool createNewCol =
false)
138 if (nrows <= row_idx)
return -1;
140 int col_idx = getColIdx(col_name);
146 col_idx = addColumn(col_name);
150 col[row_idx] = value;
153 int setCell(
unsigned row_idx,
string col_name,
int value,
bool createNewCol =
false)
155 stringstream str; str << value;
156 return setCell(row_idx, col_name, str.str(), createNewCol);
158 int setCell(
unsigned row_idx,
string col_name,
long value,
bool createNewCol =
false)
160 stringstream str; str << value;
161 return setCell(row_idx, col_name, str.str(), createNewCol);
163 int setCell(
unsigned row_idx,
string col_name,
bool value,
bool createNewCol =
false)
165 stringstream str; str << value;
166 return setCell(row_idx, col_name, str.str(), createNewCol);
168 int setCell(
unsigned row_idx,
string col_name,
unsigned value,
bool createNewCol =
false)
170 stringstream str; str << value;
171 return setCell(row_idx, col_name, str.str(), createNewCol);
173 int setCell(
unsigned row_idx,
string col_name,
double value,
bool createNewCol =
false)
175 stringstream str; str.precision(6); str << std::fixed << value;
176 return setCell(row_idx, col_name, str.str(), createNewCol);
178 int setCell(
unsigned row_idx,
string col_name,
float value,
bool createNewCol =
false)
180 stringstream str; str << value;
181 return setCell(row_idx, col_name, str.str(), createNewCol);
191 const char* comma =
"";
192 typename vector<CSVTableColumn<Tvar> >::const_iterator it;
194 for (it = columns.begin(); it != columns.end(); it++) {
195 os << comma << it->getHeader();
199 for (
unsigned i=0; i < nrows; i++) {
202 for (it = columns.begin(); it != columns.end(); it++) {
203 os << comma << (*it)[i];
217 vector<string> tokens;
224 if (is.eof())
return -1;
228 if (line.size() == 0)
return -1;
230 split(tokens, line,
",");
231 if(tokens.size() == 0)
return -1;
234 vector<string>::iterator tit;
235 for (tit = tokens.begin(); tit != tokens.end(); tit++) {
241 unsigned ncols = columns.size();
245 vector<string> tokens;
249 split(tokens, line,
",");
251 unsigned ntokens = tokens.size();
252 if (ntokens != ncols) ret = 1;
255 for (
unsigned i=0; i<ncols; i++) {
259 string& v = tokens[i];
271 void split( vector<string> & theStringVector,
272 const string & theString,
273 const string & theDelimiter)
275 size_t start = 0, end = 0;
276 while ( end != string::npos)
278 end = theString.find( theDelimiter, start);
280 theStringVector.push_back( theString.substr( start,
281 (end == string::npos) ? string::npos : end - start));
283 start = ( ( end > (string::npos - theDelimiter.size()) )
284 ? string::npos : end + theDelimiter.size());
289 static inline string &
trim(
string &s) {
292 string::iterator
first = s.begin();
293 while((first != s.end()) && isspace(*first)) first++;
294 if (first != s.begin()) s.erase(s.begin(), first-1);
296 if (s.size() == 0)
return s;
300 string::iterator last = s.end()-1;
302 while(isspace(*last)) last--;
303 if (last != s.end()) s.erase(++last, s.end());
static string & trim(string &s)
unsigned addRows(unsigned nrows)
int setCell(unsigned row_idx, string col_name, string value, bool createNewCol=false)
int setCell(unsigned row_idx, string col_name, long value, bool createNewCol=false)
int setCell(unsigned row_idx, string col_name, float value, bool createNewCol=false)
void split(vector< string > &theStringVector, const string &theString, const string &theDelimiter)
int setCell(unsigned row_idx, string col_name, int value, bool createNewCol=false)
clarg::argBool h("-h", "help message", false)
void write(ostream &os) const
const string & getHeader() const
unsigned addColumn(const string &col_name)
int setCell(unsigned row_idx, string col_name, unsigned value, bool createNewCol=false)
T & operator[](unsigned i)
CSVTableColumn(const string &header)
unsigned addRows(unsigned n=1)
vector< CSVTableColumn< Tvar > > columns
int setCell(unsigned row_idx, string col_name, bool value, bool createNewCol=false)
void setHeader(const string &h)
int getColIdx(const string &column_name)
int setCell(unsigned row_idx, string col_name, double value, bool createNewCol=false)
const T & operator[](unsigned i) const
CSVTable< string > CSVStringTable
bool isHeader(const string &h) const