CARMA C++
|
Simple ASCII Table format, fully memory based. More...
#include <carma/services/Table.h>
Public Member Functions | |
void | addRow (const std::string &data) |
int | find (const std::string &query, const int column=0, bool caseSensitive=false) |
Find the row that matches the queried name and column number. More... | |
int | find (const std::string &query, const std::string &columnName, bool caseSensitive=false) |
Find the row that matches the queried name and column number. More... | |
std::vector< bool > | getBoolColumn (const std::string &column) const |
get a boolean column More... | |
std::vector< bool > | getBoolColumn (const int column) const |
get boolean column data by index More... | |
std::vector< bool > | getBoolColumn (const int col1, const int col2) const |
std::vector< std::string > | getColumn (const std::string &column) const |
get column data by name More... | |
std::vector< std::string > | getColumn (const int column) const |
get column data by index More... | |
std::vector< std::string > | getColumn (const int col1, const int col2) const |
Get multiple column data by index, returned as a string. More... | |
std::string | getColumnName (const int column=-1) const |
std::vector< std::string > | getColumnNames () const |
int | getColumnNumber (const std::string &colname) const |
std::string | getColumnType (const int column=-1) const |
std::vector< std::string > | getColumnTypes () const |
std::string | getColumnUnit (const int column=-1) const |
std::vector< std::string > | getColumnUnits () const |
std::vector< std::string > | getCommentColumn (const std::string &column) const |
this special case grabs the last set of columns in a verbatim style (i.e preserving the embedded spaces through the last non-space). More... | |
std::vector< std::string > | getCommentColumn (const int column) const |
std::vector< double > | getDMSColumn (const std::string &column) const |
std::vector< double > | getDMSColumn (const int column) const |
std::vector< double > | getDMSColumn (const std::string &hcolumn, const std::string &mcolumn, const std::string &scolumn) const |
std::vector< double > | getDMSColumn (const int dcolumn, const int mcolumn, const int scolumn) const |
std::vector< double > | getDoubleColumn (const std::string &column) const |
Read a column from the Table as doubles. More... | |
std::vector< double > | getDoubleColumn (const int column) const |
Read a column from the Table as doubles. More... | |
std::vector< double > | getDoubleColumn (const int col1, const int col2) const |
std::vector< double > | getDoubleColumnAndVerify (const std::string &column) const |
Read a column from the Table as doubles. More... | |
std::vector< double > | getDoubleColumnAndVerify (const int column) const |
Read a column from the Table as doubles. More... | |
std::vector< double > | getDoubleRow (const int row) const |
std::vector< double > | getHMSColumn (const std::string &column) const |
std::vector< double > | getHMSColumn (const int column) const |
std::vector< double > | getHMSColumn (const std::string &hcolumn, const std::string &mcolumn, const std::string &scolumn) const |
std::vector< double > | getHMSColumn (const int hcolumn, const int mcolumn, const int scolumn) const |
std::vector< int > | getIntColumn (const std::string &column) const |
Read a column from the Table as integers. More... | |
std::vector< int > | getIntColumn (const int column) const |
Read a column from the Table as integers No checking of the values is done; data that fail string to integer conversion are returned as zeroes. More... | |
std::vector< int > | getIntColumn (const int col1, const int col2) const |
std::vector< int > | getIntRow (const int row) const |
int | getNcols (void) const |
int | getNrows (void) const |
::std::string | getPathAndFileName () |
Return the current full path+filename. More... | |
std::string | getRow (const int row) const |
std::vector< std::string > | getStringRow (const int row) const |
bool | hasBeenModified () |
hasBeenModified checks if the currently open file has been modified Returns true|false depending on if the currently open file has been modified since it was opened. More... | |
void | open (const std::string &fileName, int maxRows=0) |
open and read table This munges the whole table, reads the header and stores the data in ASCII. More... | |
void | putRow (const int row, const std::string &data) |
void | removeRow (const int row) |
void | reRead () |
reRead calls open on whatever file is currently open More... | |
void | setColumnName (const int column, const std::string &name) |
void | setColumnType (const int column, const std::string &name) |
void | setColumnUnit (const int column, const std::string &name) |
struct stat | status (const std::string &fileName) |
Sanity-check a file by calling stat(). More... | |
Table () | |
Default constructor, creates an empty table. More... | |
Table (const std::string &fileName, int maxRows=0) | |
Table (int ncols) | |
Create an empty table with fixed number of columns. More... | |
void | test (void) |
~Table () | |
Destructor. More... | |
Simple ASCII Table format, fully memory based.
1) comments lines are lines that start with '#' 2) all columns of a row must be on one line, no line wrapping 3) each row must contain the same number of columns (this may be relaxed) 4) a magic marker '#|' is needed to denote the column names and registered types, columns are separated with a '|' symbol e.g.
#| name1 | name2 | name3 | #| type1 | type2 | type3 | #| unit1 | unit2 | unit3 |
in that order! Column names/types/units should NOT have embedded spaces. Valid types are:
r real i integer s string hms special-string (hh:mm:ss.ss) - internally stored as radians dms special-string (dd:mm:ss.ss) - internally stored as radians
Units should be taken from the carma::services::Units class.
Example usage:
1) munge the whole data, and grab some (row or column) slice
Table t("filename"); vector<double> ra,dec, col, row; ra = t.getHMSColumn(2,3,4); // 3 columns for HH, MM and SS combined as radians typ = t.getColumn("type"); // single column, by name ppm = t.getIntColumn(2); // column by number sig = t.getDoubleColumn(14,20); // column by fixed location
2) Add data, and write it to a file
Table t(3); // create a table with 3 columns t.addRow("Add silly data"); t.addRow("1 2 3"); // set the column names t.setColumnName(0,"Column 0"); t.setColumnName(1,"Column 1"); t.setColumnName(2,"Column 2"); // set the type to string t.setColumnType(0,"s"); t.setColumnType(1,"s"); t.setColumnType(2,"s"); os << t;
The Table class represents a simple view into the ASCII table with additional meta-data to describe how many columns and rows exist.
carma::services::Table::Table | ( | ) |
Default constructor, creates an empty table.
carma::services::Table::Table | ( | const std::string & | fileName, |
int | maxRows = 0 |
||
) |
fileName | file to read the table from |
maxRows | maximum number of data rows to read from the table (0=all) |
carma::util::FileNotFoundException | if the file is not found |
carma::services::Table::Table | ( | int | ncols | ) |
Create an empty table with fixed number of columns.
ncols | Number of columns in this table |
carma::services::Table::~Table | ( | ) |
Destructor.
void carma::services::Table::addRow | ( | const std::string & | data | ) |
@return |
int carma::services::Table::find | ( | const std::string & | query, |
const int | column = 0 , |
||
bool | caseSensitive = false |
||
) |
Find the row that matches the queried name and column number.
This method looks for an instance of a string value in the given column number that matches the query. It will return the row number of the first matching string.
query | The string to compare |
column | The column number in which to look |
caseSensitive | Whether or not the comparison should be case-sensitive. true means a case-sensitive search |
carma::util::NotFoundException | if a matching entry could not be found. |
carma::util::ErrorException | if the column number was outside the range in this Table |
int carma::services::Table::find | ( | const std::string & | query, |
const std::string & | columnName, | ||
bool | caseSensitive = false |
||
) |
Find the row that matches the queried name and column number.
This method looks for an instance of a string value in the given named column that matches the query. It will return the row number of the first matching string.
query | The string to compare |
columnName | The name of the column in which to look |
caseSensitive | Whether or not the comparison should be case-sensitive. true means a case-sensitive search NOTE: Column names are always case-sensitive: the boolean parameter value does not apply to the column name. |
carma::util::NotFoundException | if a matching entry could not be found. Note: If this table is backed by a file, the file must support intelligence (named columns, delimited by "|") |
carma::util::ErrorException | if the column number was outside the range in this Table or if the file does not support intelligence. |
std::vector<bool> carma::services::Table::getBoolColumn | ( | const std::string & | column | ) | const |
get a boolean column
column | ASCII name of the column @return vector containing column boolean data |
std::vector<bool> carma::services::Table::getBoolColumn | ( | const int | column | ) | const |
get boolean column data by index
column | integer column index (beginning at zero) |
std::vector<bool> carma::services::Table::getBoolColumn | ( | const int | col1, |
const int | col2 | ||
) | const |
@return |
std::vector<std::string> carma::services::Table::getColumn | ( | const std::string & | column | ) | const |
get column data by name
column | ASCII name of the column |
std::vector<std::string> carma::services::Table::getColumn | ( | const int | column | ) | const |
get column data by index
column | integer column index (beginning at zero) |
std::vector<std::string> carma::services::Table::getColumn | ( | const int | col1, |
const int | col2 | ||
) | const |
Get multiple column data by index, returned as a string.
For instance if a Table has 4 columns:
#| col0 | col1 | col2| col3 | #| s | r | | r | #| | | | | foo 3 bar 1 aunt 2 sally 5
then v = getColumn(0,2) will return a vector containing:
v[0] = "foo 3 bar" v[1] = "aunt 2 sally"
col1 | start column |
col2 | end column, inclusive |
std::string carma::services::Table::getColumnName | ( | const int | column = -1 | ) | const |
@return |
std::vector<std::string> carma::services::Table::getColumnNames | ( | ) | const |
int carma::services::Table::getColumnNumber | ( | const std::string & | colname | ) | const |
@return |
std::string carma::services::Table::getColumnType | ( | const int | column = -1 | ) | const |
@return |
std::string carma::services::Table::getColumnUnit | ( | const int | column = -1 | ) | const |
@return |
std::vector<std::string> carma::services::Table::getColumnUnits | ( | ) | const |
std::vector<std::string> carma::services::Table::getCommentColumn | ( | const std::string & | column | ) | const |
this special case grabs the last set of columns in a verbatim style (i.e preserving the embedded spaces through the last non-space).
Although this function raises an exception when column-0 does not exist, it is allowed to have a non-existent (blank) column, in which case the returned element will be a null string.
std::vector<std::string> carma::services::Table::getCommentColumn | ( | const int | column | ) | const |
@return |
std::vector<double> carma::services::Table::getDMSColumn | ( | const std::string & | column | ) | const |
@return |
std::vector<double> carma::services::Table::getDMSColumn | ( | const int | column | ) | const |
@return |
std::vector<double> carma::services::Table::getDMSColumn | ( | const std::string & | hcolumn, |
const std::string & | mcolumn, | ||
const std::string & | scolumn | ||
) | const |
@return |
std::vector<double> carma::services::Table::getDMSColumn | ( | const int | dcolumn, |
const int | mcolumn, | ||
const int | scolumn | ||
) | const |
@return |
std::vector<double> carma::services::Table::getDoubleColumn | ( | const std::string & | column | ) | const |
Read a column from the Table as doubles.
No checking of the values is done; data that fail string to double conversion are returned as zeroes.
column | ASCII name of the column. |
std::vector<double> carma::services::Table::getDoubleColumn | ( | const int | column | ) | const |
Read a column from the Table as doubles.
No checking of the values is done; data that fail string to double conversion are returned as zeroes.
column | The column number, zero-based |
std::vector<double> carma::services::Table::getDoubleColumn | ( | const int | col1, |
const int | col2 | ||
) | const |
@return |
std::vector<double> carma::services::Table::getDoubleColumnAndVerify | ( | const std::string & | column | ) | const |
Read a column from the Table as doubles.
The values are checked as they are read; if string to double conversion fails, a util::ErrorException is thrown.
column | ASCII name of the column. |
std::vector<double> carma::services::Table::getDoubleColumnAndVerify | ( | const int | column | ) | const |
Read a column from the Table as doubles.
The values are checked as they are read; if string to double conversion fails, a util::ErrorException is thrown.
column | The column number, zero-based |
std::vector<double> carma::services::Table::getDoubleRow | ( | const int | row | ) | const |
@return |
std::vector<double> carma::services::Table::getHMSColumn | ( | const std::string & | column | ) | const |
@return |
std::vector<double> carma::services::Table::getHMSColumn | ( | const int | column | ) | const |
@return |
std::vector<double> carma::services::Table::getHMSColumn | ( | const std::string & | hcolumn, |
const std::string & | mcolumn, | ||
const std::string & | scolumn | ||
) | const |
@return |
std::vector<double> carma::services::Table::getHMSColumn | ( | const int | hcolumn, |
const int | mcolumn, | ||
const int | scolumn | ||
) | const |
@return |
std::vector<int> carma::services::Table::getIntColumn | ( | const std::string & | column | ) | const |
Read a column from the Table as integers.
No checking of the values is done; data that fail string to integer conversion are returned as zeroes.
column | ASCII name of the column. |
std::vector<int> carma::services::Table::getIntColumn | ( | const int | column | ) | const |
Read a column from the Table as integers No checking of the values is done; data that fail string to integer conversion are returned as zeroes.
column | The column number, zero-based |
std::vector<int> carma::services::Table::getIntColumn | ( | const int | col1, |
const int | col2 | ||
) | const |
@return |
std::vector<int> carma::services::Table::getIntRow | ( | const int | row | ) | const |
@return |
int carma::services::Table::getNcols | ( | void | ) | const |
int carma::services::Table::getNrows | ( | void | ) | const |
::std::string carma::services::Table::getPathAndFileName | ( | ) |
std::string carma::services::Table::getRow | ( | const int | row | ) | const |
@return |
std::vector<std::string> carma::services::Table::getStringRow | ( | const int | row | ) | const |
@return |
bool carma::services::Table::hasBeenModified | ( | ) |
hasBeenModified checks if the currently open file has been modified Returns true|false depending on if the currently open file has been modified since it was opened.
Generally, code calling this would immediately call reRead() if the return is true.
void carma::services::Table::open | ( | const std::string & | fileName, |
int | maxRows = 0 |
||
) |
open and read table This munges the whole table, reads the header and stores the data in ASCII.
Column and Row extractors will do the interpretation. Repeated calls to open() on a given Table instance will clear any data loaded from previous opens.
fileName | file to read the table from |
maxRows | maximum number of data rows to read from the table (0=all) |
carma::util::FileNotFoundException | if the file is not found |
void carma::services::Table::putRow | ( | const int | row, |
const std::string & | data | ||
) |
@return |
void carma::services::Table::removeRow | ( | const int | row | ) |
@return |
void carma::services::Table::reRead | ( | ) |
reRead calls open on whatever file is currently open
void carma::services::Table::setColumnName | ( | const int | column, |
const std::string & | name | ||
) |
@return |
void carma::services::Table::setColumnType | ( | const int | column, |
const std::string & | name | ||
) |
@return |
void carma::services::Table::setColumnUnit | ( | const int | column, |
const std::string & | name | ||
) |
@return |
struct stat carma::services::Table::status | ( | const std::string & | fileName | ) |
Sanity-check a file by calling stat().
fileName | file to read the table from |
Exception | if file is not a regular file. |