CARMA C++
Table.h
Go to the documentation of this file.
1 #ifndef CARMA_SERVICES_TABLE_H
2 #define CARMA_SERVICES_TABLE_H
3 
18 #include <iostream>
19 #include <iomanip>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 // For stat functions...
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 
29 namespace carma {
30  namespace services {
31 
43  class Column {
44  public:
45  Column();
46  ~Column();
47 
48  // by number (0..)
49  Column(int column_number);
50  // by fixed column width start..end (dangerious, TABs count as 1 character)
51  Column(int start_column, int end_column);
52  // special DMS or HMS access
53  Column(int dcol, int mcol, int scol);
54  // by name (NOT IMPLEMENTED YET)
55  Column(std::string column_name);
56  };
57 
58 
126  class Table {
127  public:
128 
133  Table();
134 
141  Table(const std::string& fileName, int maxRows=0);
142 
147  Table(int ncols);
148 
152  ~Table();
153 
154 
169  void open(const std::string& fileName, int maxRows=0);
170 
171 
177  struct stat status(const std::string & fileName );
178 
182  void reRead();
183 
191  bool hasBeenModified();
192 
193  void test(void); // we don't want to advertise this :-)
194 
195 
201  std::vector<std::string> getColumn(const std::string& column) const;
202 
209  std::vector<std::string> getColumn(const int column) const;
210 
232  std::vector<std::string> getColumn(const int col1, const int col2) const;
233 
239  std::vector<bool> getBoolColumn(const std::string& column) const;
240 
246  std::vector<bool> getBoolColumn(const int column) const;
247 
253  std::vector<bool> getBoolColumn(const int col1, const int col2) const;
254 
262  std::vector<double> getDoubleColumn(const std::string& column) const;
263 
272  std::vector<double> getDoubleColumn(const int column) const;
273 
281  std::vector<double> getDoubleColumnAndVerify(const std::string& column) const;
282 
290  std::vector<double> getDoubleColumnAndVerify(const int column) const;
291 
297  std::vector<double> getDoubleColumn(const int col1,
298  const int col2) const;
299 
307  std::vector<int> getIntColumn(const std::string& column) const;
308 
317  std::vector<int> getIntColumn(const int column) const;
318 
324  std::vector<int> getIntColumn(const int col1,
325  const int col2) const;
326 
333  std::vector<double> getDMSColumn(const std::string& column) const;
334 
341  std::vector<double> getDMSColumn(const int column) const;
342 
349  std::vector<double> getDMSColumn(const std::string& hcolumn, const std::string& mcolumn, const std::string& scolumn) const;
350 
357  std::vector<double> getDMSColumn(const int dcolumn, const int mcolumn, const int scolumn) const;
358 
365  std::vector<double> getHMSColumn(const std::string& column) const;
366 
373  std::vector<double> getHMSColumn(const int column) const;
374 
381  std::vector<double> getHMSColumn(const std::string& hcolumn, const std::string& mcolumn, const std::string& scolumn) const;
382 
389  std::vector<double> getHMSColumn(const int hcolumn, const int mcolumn, const int scolumn) const;
390 
391 
401  std::vector<std::string> getCommentColumn(const std::string& column) const;
408  std::vector<std::string> getCommentColumn(const int column) const;
409 
410 
417  std::vector<double> getDoubleRow(const int row) const;
424  std::vector<int> getIntRow(const int row) const;
431  std::vector<std::string> getStringRow(const int row) const;
432 
439  std::string getRow(const int row) const;
446  void putRow(const int row, const std::string& data);
453  void addRow(const std::string& data);
454 
461  void removeRow(const int row);
462 
469  inline int getNrows(void) const { return nrows_; }
476  inline int getNcols(void) const { return ncols_; }
477 
478 
485  int getColumnNumber(const std::string& colname) const;
486 
493  std::string getColumnName(const int column=-1) const;
494 
498  inline std::vector<std::string> getColumnNames() const
499  {
500  return colnames_;
501  }
502 
506  inline std::vector<std::string> getColumnUnits() const
507  {
508  return colunits_;
509  }
510 
511  inline std::vector<std::string> getColumnTypes() const
512  {
513  return coltypes_;
514  }
515 
522  std::string getColumnType(const int column=-1) const;
529  std::string getColumnUnit(const int column=-1) const;
530 
537  void setColumnName(const int column, const std::string& name);
544  void setColumnType(const int column, const std::string& name);
551  void setColumnUnit(const int column, const std::string& name);
552 
568  int find(const std::string& query,
569  const int column = 0,
570  bool caseSensitive = false);
571 
593  int find(const std::string& query,
594  const std::string& columnName,
595  bool caseSensitive = false);
596 
601  ::std::string getPathAndFileName() { return _filename; };
602 
603  private:
604  // NOTE: If you add any private member variables, be sure
605  // to also add them to Table::clear().
606  int ncols_; // number of columns
607  int nrows_; // number of rows
608  std::vector<std::string> header_; // header
609  std::vector<std::string> data_; // data
610 
611  std::vector<std::string> colnames_; // column names (could be absent)
612  std::vector<std::string> coltypes_; // column types (could be absent)
613  std::vector<std::string> colunits_; // column units (could be absent)
614 
618  void clear( void );
619 
624  std::string word(const std::string& line, const int n,
625  const bool end=false) const;
626 
627 
632  void Tokenize(const std::string& line,
633  std::vector<std::string>& tokens,
634  const std::string& delimiters);
635 
645  int indexToken(const std::vector<std::string>& tokens,
646  const std::string& name,
647  const bool single=true) const;
651  bool CommentKey(const std::string& line, const std::string& key);
652 
653  /*
654  * convert a sexasegimal D:M:S string into D.ddd
655  */
656  double dms(const char *s) const;
657 
658  /*
659  * convienence method for common error throwing pattern in this class
660  */
661  void throwBadColumnByNum( const int column ) const;
662 
663  /*
664  * modification time at time of open
665  */
666  time_t _mtime;
667 
668  /*
669  * local copy of opened filename
670  */
671  ::std::string _filename;
672  };
673  }
674 }
675 
676 
691 std::ostream& operator<<(std::ostream &os, const carma::services::Table& table);
692 std::ostream& operator>>(std::ostream &os, const carma::services::Table& table);
693 
694 
695 #endif // CARMA_SERVICES_TABLE_H
std::vector< double > getDMSColumn(const std::string &column) const
std::ostream & operator<<(::std::ostream &os, const carma::dbms::Table &table)
std::vector< double > getDoubleRow(const int row) const
std::vector< std::string > getColumnUnits() const
Definition: Table.h:506
std::vector< std::string > getColumnNames() const
Definition: Table.h:498
std::string getColumnUnit(const int column=-1) const
int getColumnNumber(const std::string &colname) const
void removeRow(const int row)
std::string getColumnName(const int column=-1) const
void setColumnName(const int column, const std::string &name)
std::vector< std::string > getStringRow(const int row) const
std::vector< int > getIntColumn(const std::string &column) const
Read a column from the Table as integers.
std::vector< bool > getBoolColumn(const std::string &column) const
get a boolean column
Column - an unimplemented class we could create a special class &#39;Column&#39; in which it is defined how a...
Definition: Table.h:43
int getNrows(void) const
Definition: Table.h:469
std::string getRow(const int row) const
::std::string getPathAndFileName()
Return the current full path+filename.
Definition: Table.h:601
~Table()
Destructor.
std::vector< double > getHMSColumn(const std::string &column) const
void addRow(const std::string &data)
std::vector< double > getDoubleColumnAndVerify(const std::string &column) const
Read a column from the Table as doubles.
std::vector< double > getDoubleColumn(const std::string &column) const
Read a column from the Table as doubles.
Table()
Default constructor, creates an empty table.
Simple ASCII Table format, fully memory based.
Definition: Table.h:126
std::vector< std::string > getColumn(const std::string &column) const
get column data by name
int find(const std::string &query, const int column=0, bool caseSensitive=false)
Find the row that matches the queried name and column number.
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 spac...
void putRow(const int row, const std::string &data)
std::vector< int > getIntRow(const int row) const
void reRead()
reRead calls open on whatever file is currently open
int getNcols(void) const
Definition: Table.h:476
bool hasBeenModified()
hasBeenModified checks if the currently open file has been modified Returns true|false depending on i...
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...
void setColumnUnit(const int column, const std::string &name)
void setColumnType(const int column, const std::string &name)
std::string getColumnType(const int column=-1) const