rdbxx  1.0.10
RDB.h
1 #ifndef RDB_h
2 #define RDB_h
3 
4 /* --8<--8<--8<--8<--
5  *
6  * Copyright (C) 2006 Smithsonian Astrophysical Observatory
7  *
8  * This file is part of RDB
9  *
10  * RDB is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * RDB is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the
22  * Free Software Foundation, Inc.
23  * 51 Franklin Street, Fifth Floor
24  * Boston, MA 02110-1301, USA
25  *
26  * -->8-->8-->8-->8-- */
27 
28 #include <fstream>
29 #include <iosfwd>
30 #include <iostream>
31 #include <string>
32 #include <vector>
33 
34 #include <suplibxx/str.h>
35 
36 #include <rdbxx/RDBErr.h>
37 
38 #include <rdbxx/RDBComment.h>
39 #include <rdbxx/RDBColumnTmplt.h>
40 
42 class RDB {
44 
45  friend std::istream& operator>>( std::istream& is, RDB& rdb );
48  friend std::ostream& operator<<( std::ostream& os, RDB& rdb );
50 
51  public:
53 
54  enum Status { REOF=0x00, REOL=0x01, REOG=0x02, RBOG=0x04 };
57 
59 
60  RDB( const std::string& name="", std::ios::openmode mode=std::ios::in );
63  RDB( std::istream* isptr );
65  RDB( std::ostream* osptr );
67  RDB( const RDB& rdb );
69  ~RDB( void );
71 
73 
74  void open( const std::string& name, std::ios::openmode mode=std::ios::in );
77  void open( std::istream* isptr );
79  void open( std::ostream* osptr );
81  void open( const RDB& rdb );
83  void close( void );
84 
86  int read( void );
88  bool write( void );
90  bool rewind( void );
92 
94 
95  bool autoIdx( void ) const;
98  void autoIdx( const bool on );
100  void advanceIdx( void );
102 
104 
105  void setGroup( const std::string& name, bool group=true );
108  void setGroup( const int idx, bool group=true );
110  bool getGroup( const std::string& name );
112  bool getGroup( const int idx );
114  bool newGroup( void );
116 
118 
119  void setComment( const std::string& comm, const int idx=-1 );
122  void setComment( RDBComment& comm, const int idx=-1 );
124  void setComment( RDBComment& comm, const std::string& name, const size_t idx=0 );
126  void setComment( const RDB& rdb );
127 
129  RDBComment& getComment( const size_t idx );
131  RDBComment& getComment( const std::string& name, const size_t idx=0 );
133 
135 
136  void setColumn( const std::string& name, const std::string& def, const int idx=-1 );
139  void setColumn( RDBColumn* col, const int idx=-1 );
141  void setColumn( RDBColumn* col, const std::string& name, const size_t idx=0 );
143  void setColumn( const RDB& rdb );
144 
146  RDBColumn* getColumn( const size_t idx );
148  RDBColumn* getColumn( const std::string& name, const size_t idx=0 );
150 
152 
153  void setName( const size_t idx, const std::string& name );
156  void setDef( const size_t idx, const std::string& def );
158  void setWidth( const size_t idx, const long width );
160  void setType( const size_t idx, const RDBColumn::Type type );
162  void setJust( const size_t idx, const RDBColumn::Just just );
164  void setDesc( const size_t idx, const std::string& desc );
166  void mapData( const size_t idx, double data[], const size_t nelems=1 );
168  void mapData( const size_t idx, long data[], const size_t nelems=1 );
170  void mapData( const size_t idx, std::string data[], const size_t nelems=1 );
172  void setData( const size_t idx, const double data );
174  void setData( const size_t idx, const long data );
176  void setData( const size_t idx, const std::string& data );
177 
179  void getName( const size_t idx, std::string& name ) const;
181  void getDef( const size_t idx, std::string& def );
183  void getWidth( const size_t idx, long& width ) const;
185  void getType( const size_t idx, RDBColumn::Type& type ) const;
187  void getJust( const size_t idx, RDBColumn::Just& just ) const;
189  void getDesc( const size_t idx, std::string& desc ) const;
191  void getData( const size_t idx, double& data );
193  void getData( const size_t idx, long& data );
195  void getData( const size_t idx, std::string& data );
196 
198  std::string getName( const size_t idx ) const;
200  std::string getDef( const size_t idx );
202  long getWidth( const size_t idx ) const;
204  RDBColumn::Type getType( const size_t idx ) const;
206  RDBColumn::Just getJust( const size_t idx ) const;
208  std::string getDesc( const size_t idx ) const;
210  double getDataDouble( const size_t idx );
212  long getDataLong( const size_t idx );
214  std::string getDataString( const size_t idx );
216 
218 
219  void setName( const std::string& name, const std::string& newname );
222  void setDef( const std::string& name, const std::string& def );
224  void setWidth( const std::string& name, const long width );
226  void setType( const std::string& name, const RDBColumn::Type type );
228  void setJust( const std::string& name, const RDBColumn::Just just );
230  void setDesc( const std::string& name, const std::string& desc );
232  void mapData( const std::string& name, double data[], const size_t nelems=1 );
234  void mapData( const std::string& name, long data[], const size_t nelems=1 );
236  void mapData( const std::string& name, std::string data[], const size_t nelems=1 );
238  void setData( const std::string& name, const double data );
240  void setData( const std::string& name, const long data );
242  void setData( const std::string& name, const std::string& data );
243 
245  void getName( const std::string& name, std::string& namefound ) const;
247  void getDef( const std::string& name, std::string& def );
249  void getWidth( const std::string& name, long& width ) const;
251  void getType( const std::string& name, RDBColumn::Type& type ) const;
253  void getJust( const std::string& name, RDBColumn::Just& just ) const;
255  void getDesc( const std::string& name, std::string& desc ) const;
257  void getData( const std::string& name, double& data );
259  void getData( const std::string& name, long& data );
261  void getData( const std::string& name, std::string& data );
262 
264  std::string getName( const std::string& name ) const;
266  std::string getDef( const std::string& name );
268  long getWidth( const std::string& name ) const;
270  RDBColumn::Type getType( const std::string& name ) const;
272  RDBColumn::Just getJust( const std::string& name ) const;
274  std::string getDesc( const std::string& name ) const;
276  double getDataDouble( const std::string& name );
278  long getDataLong( const std::string& name );
280  std::string getDataString( const std::string& name );
282 
284 
285  size_t nComments( void ) const;
288  size_t nColumns( void ) const;
290  size_t nRows( void ); // Not const since it requires scanning the entire file...
292 
293  protected:
295  void parseHeader( void );
297  std::vector<std::string> parseLine( const std::string& line ) const;
299  size_t parseLine( bool& newgroup );
300 
302  std::string _filename;
304  std::ios::openmode _mode;
305 
307  std::istream* _isptr;
309  std::ostream* _osptr;
311  bool _myisptr;
313  bool _myosptr;
314 
316  size_t _rewindto;
318  size_t _ncomms;
320  size_t _ncols;
322  size_t _nrows;
324  bool _knowrows;
326  long _rownum;
328  long _frownum;
329 
331  bool _autoidx;
335  bool _lastread;
337  bool _writehdr;
338 
346  bool* _mycols;
347 
349  std::string _line;
350 
351 
353  // char* _line;
355  // size_t _linesize;
356 
357 };
358 
359 #endif
360 
size_t _ncols
Number of columns.
Definition: RDB.h:320
void getData(const size_t idx, double &data)
Return the data of the RDBColumn at idx, converting if necessary.
Definition: RDB.cc:1932
size_t nColumns(void) const
Return number of columns in RDB object.
Definition: RDB.cc:3132
std::ios::openmode _mode
Open mode of the associated stream.
Definition: RDB.h:304
void setComment(const std::string &comm, const int idx=-1)
Add RDBComment in header of RDB object.
Definition: RDB.cc:1014
Just
Acceptable column justifications.
Definition: RDBColumn.h:58
std::string _line
Line from RDB table.
Definition: RDB.h:349
size_t _nrows
Number of rows.
Definition: RDB.h:322
bool _knowrows
Indicates if associated file must be scanned to determine number of rows.
Definition: RDB.h:324
bool rewind(void)
Rewind the stream to the beginning of the first row of data.
Definition: RDB.cc:771
bool _myosptr
Indicates if RDB object is responsible for deallocating the ostream.
Definition: RDB.h:313
RDBComment & getComment(const size_t idx)
Return RDBComment at given index.
Definition: RDB.cc:1147
bool _myisptr
Indicates if RDB object is responsible for deallocating the istream.
Definition: RDB.h:311
Status
Acceptable column justifications.
Definition: RDB.h:55
bool _firstread
Indicates if this is the first call to RDB::read.
Definition: RDB.h:333
friend std::ostream & operator<<(std::ostream &os, RDB &rdb)
Write table to output stream.
Definition: RDB.cc:75
void getDesc(const size_t idx, std::string &desc) const
Return the description of the RDBColumn at idx.
Definition: RDB.cc:1912
bool _writehdr
Indicates if the header has been output.
Definition: RDB.h:337
void close(void)
Closes the stream attached to RDB object.
Definition: RDB.cc:525
std::istream * _isptr
Istream attached to data file.
Definition: RDB.h:307
RDBLongColumn _nrcol
Hidden column, containing row number.
Definition: RDB.h:344
void getName(const size_t idx, std::string &name) const
Return the name of the RDBColumn at idx.
Definition: RDB.cc:1817
int read(void)
Read a line of data from the istream.
Definition: RDB.cc:588
void parseHeader(void)
Parse header, i.e. comments and column names and definitions.
Definition: RDB.cc:3184
~RDB(void)
Deletes resources allocated by the RDB object.
Definition: RDB.cc:270
RDBColumn ** _cols
Array of RDBColumns.
Definition: RDB.h:342
bool write(void)
Write a line of data to the ostream.
Definition: RDB.cc:711
void setWidth(const size_t idx, const long width)
Modify the width of the RDBColumn at idx.
Definition: RDB.cc:1536
std::ostream * _osptr
Ostream attached to data file.
Definition: RDB.h:309
void setType(const size_t idx, const RDBColumn::Type type)
Modify the type of the RDBColumn at idx.
Definition: RDB.cc:1564
void setDesc(const size_t idx, const std::string &desc)
Modify the description of the RDBColumn at idx.
Definition: RDB.cc:1620
Provides interface for manipulating RDB comments.
Definition: RDBComment.h:33
void setData(const size_t idx, const double data)
Sets the data value of RDBColumn, converting as necessary.
Definition: RDB.cc:1734
void setGroup(const std::string &name, bool group=true)
Turn on/off group status for the named column.
Definition: RDB.cc:877
RDBColumn * getColumn(const size_t idx)
Return pointer to RDBColumn at given index.
Definition: RDB.cc:1432
size_t nRows(void)
Return number of rows in RDB object.
Definition: RDB.cc:3150
bool autoIdx(void) const
Indicates if auto-indexing is activated.
Definition: RDB.cc:842
RDBComment * _comms
Array of RDBComments.
Definition: RDB.h:340
RDB(const std::string &name="", std::ios::openmode mode=std::ios::in)
Attaches RDB object to a file.
Definition: RDB.cc:117
bool newGroup(void)
Checks if any column indicates a new group.
Definition: RDB.cc:988
size_t _rewindto
Position of beginning of first row of data.
Definition: RDB.h:316
Provides interface for manipulating RDB tables.
Definition: RDB.h:42
size_t _ncomms
Number of comments.
Definition: RDB.h:318
void getType(const size_t idx, RDBColumn::Type &type) const
Return the type of the RDBColumn at idx.
Definition: RDB.cc:1874
Type
Acceptable column types.
Definition: RDBColumn.h:60
void getWidth(const size_t idx, long &width) const
Return the width of the RDBColumn at idx.
Definition: RDB.cc:1855
void mapData(const size_t idx, double data[], const size_t nelems=1)
Map RDBColumn data to user-supplied memory.
Definition: RDB.cc:1649
long getDataLong(const size_t idx)
Return the data of the RDBColumn at idx, converting if necessary.
Definition: RDB.cc:2166
void getDef(const size_t idx, std::string &def)
Return the definition of the RDBColumn at idx.
Definition: RDB.cc:1836
size_t nComments(void) const
Return number of comments in RDB object.
Definition: RDB.cc:3119
void advanceIdx(void)
Increments the indices in the RDBColumn data elements.
Definition: RDB.cc:856
void open(const std::string &name, std::ios::openmode mode=std::ios::in)
Attaches RDB object to a file.
Definition: RDB.cc:315
void setColumn(const std::string &name, const std::string &def, const int idx=-1)
Add an RDBColumn in RDB object.
Definition: RDB.cc:1207
std::vector< std::string > parseLine(const std::string &line) const
Parse fields in a row.
Definition: RDB.cc:3283
void setName(const size_t idx, const std::string &name)
Modify the name of the RDBColumn at idx.
Definition: RDB.cc:1487
void setDef(const size_t idx, const std::string &def)
Modify the definition of the RDBColumn at idx.
Definition: RDB.cc:1508
std::string getDataString(const size_t idx)
Return the data of the RDBColumn at idx, converting if necessary.
Definition: RDB.cc:2195
bool _autoidx
Indicates if RDBColumn data elements should be advanced.
Definition: RDB.h:331
friend std::istream & operator>>(std::istream &is, RDB &rdb)
Read table from input stream.
Definition: RDB.cc:37
std::string _filename
Name of RDB file.
Definition: RDB.h:302
bool _lastread
Indicates if this is the last call to RDB::read.
Definition: RDB.h:335
double getDataDouble(const size_t idx)
Return the data of the RDBColumn at idx, converting if necessary.
Definition: RDB.cc:2137
bool getGroup(const std::string &name)
Returns group status, true if its a new group, for the named column.
Definition: RDB.cc:937
long _rownum
Current table row number.
Definition: RDB.h:326
long _frownum
Current file row number.
Definition: RDB.h:328
Provides interface for general column related methods.
Definition: RDBColumn.h:41
bool * _mycols
Indicates if RDB object is responsible for deallocating given RDBColumn.
Definition: RDB.h:346
void getJust(const size_t idx, RDBColumn::Just &just) const
Return the just of the RDBColumn at idx.
Definition: RDB.cc:1893
void setJust(const size_t idx, const RDBColumn::Just just)
Modify the justification of the RDBColumn at idx.
Definition: RDB.cc:1592