rdbxx  1.0.7_02
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 #include <rdbxx/RDBComment.h>
38 #include <rdbxx/RDBColumnTmplt.h>
39 
40 using namespace std;
41 
43 class RDB {
45 
46  friend istream& operator>>( istream& is, RDB& rdb );
49  friend ostream& operator<<( ostream& os, RDB& rdb );
51 
52  public:
54 
55  enum Status { REOF=0x00, REOL=0x01, REOG=0x02, RBOG=0x04 };
58 
60 
61  RDB( const string& name="", ios::openmode mode=ios::in );
64  RDB( istream* isptr );
66  RDB( ostream* osptr );
68  RDB( const RDB& rdb );
70  ~RDB( void );
72 
74 
75  void open( const string& name, ios::openmode mode=ios::in );
78  void open( istream* isptr );
80  void open( ostream* osptr );
82  void open( const RDB& rdb );
84  void close( void );
85 
87  int read( void );
89  bool write( void );
91  bool rewind( void );
93 
95 
96  bool autoIdx( void ) const;
99  void autoIdx( const bool on );
101  void advanceIdx( void );
103 
105 
106  void setGroup( const string& name, bool group=true );
109  void setGroup( const int idx, bool group=true );
111  bool getGroup( const string& name );
113  bool getGroup( const int idx );
115  bool newGroup( void );
117 
119 
120  void setComment( const string& comm, const int idx=-1 );
123  void setComment( RDBComment& comm, const int idx=-1 );
125  void setComment( RDBComment& comm, const string& name, const size_t idx=0 );
127  void setComment( const RDB& rdb );
128 
130  RDBComment& getComment( const size_t idx );
132  RDBComment& getComment( const string& name, const size_t idx=0 );
134 
136 
137  void setColumn( const string& name, const string& def, const int idx=-1 );
140  void setColumn( RDBColumn* col, const int idx=-1 );
142  void setColumn( RDBColumn* col, const string& name, const size_t idx=0 );
144  void setColumn( const RDB& rdb );
145 
147  RDBColumn* getColumn( const size_t idx );
149  RDBColumn* getColumn( const string& name, const size_t idx=0 );
151 
153 
154  void setName( const size_t idx, const string& name );
157  void setDef( const size_t idx, const string& def );
159  void setWidth( const size_t idx, const long width );
161  void setType( const size_t idx, const RDBColumn::Type type );
163  void setJust( const size_t idx, const RDBColumn::Just just );
165  void setDesc( const size_t idx, const string& desc );
167  void mapData( const size_t idx, double data[], const size_t nelems=1 );
169  void mapData( const size_t idx, long data[], const size_t nelems=1 );
171  void mapData( const size_t idx, string data[], const size_t nelems=1 );
173  void setData( const size_t idx, const double data );
175  void setData( const size_t idx, const long data );
177  void setData( const size_t idx, const string& data );
178 
180  void getName( const size_t idx, string& name ) const;
182  void getDef( const size_t idx, string& def );
184  void getWidth( const size_t idx, long& width ) const;
186  void getType( const size_t idx, RDBColumn::Type& type ) const;
188  void getJust( const size_t idx, RDBColumn::Just& just ) const;
190  void getDesc( const size_t idx, string& desc ) const;
192  void getData( const size_t idx, double& data );
194  void getData( const size_t idx, long& data );
196  void getData( const size_t idx, string& data );
197 
199  string getName( const size_t idx ) const;
201  string getDef( const size_t idx );
203  long getWidth( const size_t idx ) const;
205  RDBColumn::Type getType( const size_t idx ) const;
207  RDBColumn::Just getJust( const size_t idx ) const;
209  string getDesc( const size_t idx ) const;
211  double getDataDouble( const size_t idx );
213  long getDataLong( const size_t idx );
215  string getDataString( const size_t idx );
217 
219 
220  void setName( const string& name, const string& newname );
223  void setDef( const string& name, const string& def );
225  void setWidth( const string& name, const long width );
227  void setType( const string& name, const RDBColumn::Type type );
229  void setJust( const string& name, const RDBColumn::Just just );
231  void setDesc( const string& name, const string& desc );
233  void mapData( const string& name, double data[], const size_t nelems=1 );
235  void mapData( const string& name, long data[], const size_t nelems=1 );
237  void mapData( const string& name, string data[], const size_t nelems=1 );
239  void setData( const string& name, const double data );
241  void setData( const string& name, const long data );
243  void setData( const string& name, const string& data );
244 
246  void getName( const string& name, string& namefound ) const;
248  void getDef( const string& name, string& def );
250  void getWidth( const string& name, long& width ) const;
252  void getType( const string& name, RDBColumn::Type& type ) const;
254  void getJust( const string& name, RDBColumn::Just& just ) const;
256  void getDesc( const string& name, string& desc ) const;
258  void getData( const string& name, double& data );
260  void getData( const string& name, long& data );
262  void getData( const string& name, string& data );
263 
265  string getName( const string& name ) const;
267  string getDef( const string& name );
269  long getWidth( const string& name ) const;
271  RDBColumn::Type getType( const string& name ) const;
273  RDBColumn::Just getJust( const string& name ) const;
275  string getDesc( const string& name ) const;
277  double getDataDouble( const string& name );
279  long getDataLong( const string& name );
281  string getDataString( const string& name );
283 
285 
286  size_t nComments( void ) const;
289  size_t nColumns( void ) const;
291  size_t nRows( void ); // Not const since it requires scanning the entire file...
293 
294  protected:
296  void parseHeader( void );
298  vector<string> parseLine( const string& line ) const;
300  size_t parseLine( bool& newgroup );
301 
303  string _filename;
305  ios::openmode _mode;
306 
308  istream* _isptr;
310  ostream* _osptr;
312  bool _myisptr;
314  bool _myosptr;
315 
317  size_t _rewindto;
319  size_t _ncomms;
321  size_t _ncols;
323  size_t _nrows;
325  bool _knowrows;
327  long _rownum;
329  long _frownum;
330 
332  bool _autoidx;
336  bool _lastread;
338  bool _writehdr;
339 
347  bool* _mycols;
348 
350  string _line;
351 
352 
354  // char* _line;
356  // size_t _linesize;
357 
358 };
359 
360 #endif
361 
size_t _ncols
Number of columns.
Definition: RDB.h:321
string _filename
Name of RDB file.
Definition: RDB.h:303
Just
Acceptable column justifications.
Definition: RDBColumn.h:60
size_t _nrows
Number of rows.
Definition: RDB.h:323
bool _knowrows
Indicates if associated file must be scanned to determine number of rows.
Definition: RDB.h:325
bool _myosptr
Indicates if RDB object is responsible for deallocating the ostream.
Definition: RDB.h:314
bool _myisptr
Indicates if RDB object is responsible for deallocating the istream.
Definition: RDB.h:312
Status
Acceptable column justifications.
Definition: RDB.h:56
bool _firstread
Indicates if this is the first call to RDB::read.
Definition: RDB.h:334
bool _writehdr
Indicates if the header has been output.
Definition: RDB.h:338
RDBLongColumn _nrcol
Hidden column, containing row number.
Definition: RDB.h:345
RDBColumn ** _cols
Array of RDBColumns.
Definition: RDB.h:343
Provides interface for manipulating RDB comments.
Definition: RDBComment.h:35
ostream * _osptr
Ostream attached to data file.
Definition: RDB.h:310
RDBComment * _comms
Array of RDBComments.
Definition: RDB.h:341
istream * _isptr
Istream attached to data file.
Definition: RDB.h:308
size_t _rewindto
Position of beginning of first row of data.
Definition: RDB.h:317
Provides interface for manipulating RDB tables.
Definition: RDB.h:43
size_t _ncomms
Number of comments.
Definition: RDB.h:319
Type
Acceptable column types.
Definition: RDBColumn.h:62
string _line
Line from RDB table.
Definition: RDB.h:350
ios::openmode _mode
Open mode of the associated stream.
Definition: RDB.h:305
bool _autoidx
Indicates if RDBColumn data elements should be advanced.
Definition: RDB.h:332
bool _lastread
Indicates if this is the last call to RDB::read.
Definition: RDB.h:336
long _rownum
Current table row number.
Definition: RDB.h:327
long _frownum
Current file row number.
Definition: RDB.h:329
Provides interface for general column related methods.
Definition: RDBColumn.h:43
bool * _mycols
Indicates if RDB object is responsible for deallocating given RDBColumn.
Definition: RDB.h:347