CARMA C++
carma::services::Table Class Reference

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...
 

Detailed Description

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;
See Also
carma::dbms::Table
Todo:
use uint, instead of int, for columns

The Table class represents a simple view into the ASCII table with additional meta-data to describe how many columns and rows exist.

Definition at line 126 of file Table.h.

Constructor & Destructor Documentation

carma::services::Table::Table ( )

Default constructor, creates an empty table.

carma::services::Table::Table ( const std::string &  fileName,
int  maxRows = 0 
)
Parameters
fileNamefile to read the table from
maxRowsmaximum number of data rows to read from the table (0=all)
Exceptions
carma::util::FileNotFoundExceptionif the file is not found
carma::services::Table::Table ( int  ncols)

Create an empty table with fixed number of columns.

Parameters
ncolsNumber of columns in this table
carma::services::Table::~Table ( )

Destructor.

Member Function Documentation

void carma::services::Table::addRow ( const std::string &  data)
Parameters
@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.

Parameters
queryThe string to compare
columnThe column number in which to look
caseSensitiveWhether or not the comparison should be case-sensitive. true means a case-sensitive search
Returns
The row number of the matching entry.
Exceptions
carma::util::NotFoundExceptionif a matching entry could not be found.
carma::util::ErrorExceptionif 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.

Parameters
queryThe string to compare
columnNameThe name of the column in which to look
caseSensitiveWhether 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.
Returns
The row number of the matching entry.
Exceptions
carma::util::NotFoundExceptionif 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::ErrorExceptionif 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

Parameters
columnASCII 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

Parameters
columninteger column index (beginning at zero)
Returns
vector containing column data as booleans
std::vector<bool> carma::services::Table::getBoolColumn ( const int  col1,
const int  col2 
) const
Parameters
@return
std::vector<std::string> carma::services::Table::getColumn ( const std::string &  column) const

get column data by name

Parameters
columnASCII name of the column
Returns
vector containing column data as strings.
std::vector<std::string> carma::services::Table::getColumn ( const int  column) const

get column data by index

Parameters
columninteger column index (beginning at zero)
Returns
vector containing column data as strings.
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"
Parameters
col1start column
col2end column, inclusive
Returns
vector containing column data as strings.
std::string carma::services::Table::getColumnName ( const int  column = -1) const
Parameters
@return
std::vector<std::string> carma::services::Table::getColumnNames ( ) const
Returns
a vector containing all the column names

Definition at line 498 of file Table.h.

int carma::services::Table::getColumnNumber ( const std::string &  colname) const
Parameters
@return
std::string carma::services::Table::getColumnType ( const int  column = -1) const
Parameters
@return
std::string carma::services::Table::getColumnUnit ( const int  column = -1) const
Parameters
@return
std::vector<std::string> carma::services::Table::getColumnUnits ( ) const
Returns
a vector containing all the column units

Definition at line 506 of file Table.h.

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
Parameters
@return
std::vector<double> carma::services::Table::getDMSColumn ( const std::string &  column) const
Parameters
@return
std::vector<double> carma::services::Table::getDMSColumn ( const int  column) const
Parameters
@return
std::vector<double> carma::services::Table::getDMSColumn ( const std::string &  hcolumn,
const std::string &  mcolumn,
const std::string &  scolumn 
) const
Parameters
@return
std::vector<double> carma::services::Table::getDMSColumn ( const int  dcolumn,
const int  mcolumn,
const int  scolumn 
) const
Parameters
@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.

Parameters
columnASCII name of the column.
Returns
a vector of the column values as doubles.
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.

Parameters
columnThe column number, zero-based
Returns
a vector of the column values as doubles.
std::vector<double> carma::services::Table::getDoubleColumn ( const int  col1,
const int  col2 
) const
Parameters
@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.

Parameters
columnASCII name of the column.
Returns
a vector of the column values as doubles.
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.

Parameters
columnThe column number, zero-based
Returns
a vector of the column values as doubles.
std::vector<double> carma::services::Table::getDoubleRow ( const int  row) const
Parameters
@return
std::vector<double> carma::services::Table::getHMSColumn ( const std::string &  column) const
Parameters
@return
std::vector<double> carma::services::Table::getHMSColumn ( const int  column) const
Parameters
@return
std::vector<double> carma::services::Table::getHMSColumn ( const std::string &  hcolumn,
const std::string &  mcolumn,
const std::string &  scolumn 
) const
Parameters
@return
std::vector<double> carma::services::Table::getHMSColumn ( const int  hcolumn,
const int  mcolumn,
const int  scolumn 
) const
Parameters
@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.

Parameters
columnASCII name of the column.
Returns
a vector of the column values as integers.
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.

Parameters
columnThe column number, zero-based
Returns
a vector of the column values as integer
std::vector<int> carma::services::Table::getIntColumn ( const int  col1,
const int  col2 
) const
Parameters
@return
std::vector<int> carma::services::Table::getIntRow ( const int  row) const
Parameters
@return
int carma::services::Table::getNcols ( void  ) const
Parameters
@return

Definition at line 476 of file Table.h.

int carma::services::Table::getNrows ( void  ) const
Parameters
@return

Definition at line 469 of file Table.h.

::std::string carma::services::Table::getPathAndFileName ( )

Return the current full path+filename.

Returns
string with full path+filename

Definition at line 601 of file Table.h.

std::string carma::services::Table::getRow ( const int  row) const
Parameters
@return
std::vector<std::string> carma::services::Table::getStringRow ( const int  row) const
Parameters
@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.

Parameters
fileNamefile to read the table from
maxRowsmaximum number of data rows to read from the table (0=all)
Exceptions
carma::util::FileNotFoundExceptionif the file is not found
void carma::services::Table::putRow ( const int  row,
const std::string &  data 
)
Parameters
@return
void carma::services::Table::removeRow ( const int  row)
Parameters
@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 
)
Parameters
@return
void carma::services::Table::setColumnType ( const int  column,
const std::string &  name 
)
Parameters
@return
void carma::services::Table::setColumnUnit ( const int  column,
const std::string &  name 
)
Parameters
@return
struct stat carma::services::Table::status ( const std::string &  fileName)

Sanity-check a file by calling stat().

Parameters
fileNamefile to read the table from
Exceptions
Exceptionif file is not a regular file.

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