RDB.h

00001 #ifndef RDB_h
00002 #define RDB_h
00003 
00004 /* --8<--8<--8<--8<--
00005  *
00006  * Copyright (C) 2006 Smithsonian Astrophysical Observatory
00007  *
00008  * This file is part of RDB
00009  *
00010  * RDB is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU General Public License
00012  * as published by the Free Software Foundation; either version 2
00013  * of the License, or (at your option) any later version.
00014  *
00015  * RDB is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the 
00022  *       Free Software Foundation, Inc. 
00023  *       51 Franklin Street, Fifth Floor
00024  *       Boston, MA  02110-1301, USA
00025  *
00026  * -->8-->8-->8-->8-- */
00027 
00028 #include <fstream>
00029 #include <iosfwd>
00030 #include <iostream>
00031 #include <string>
00032 #include <vector>
00033 
00034 #include <suplibxx/str.h>
00035 
00036 #include <rdbxx/RDBErr.h>
00037 #include <rdbxx/RDBComment.h>
00038 #include <rdbxx/RDBColumnTmplt.h>
00039 
00040 using namespace std;
00041 
00043 class RDB {
00045 
00046 
00047   friend istream& operator>>( istream& is, RDB& rdb ) throw ( RDBErr );
00049   friend ostream& operator<<( ostream& os, RDB& rdb ) throw ( RDBErr );
00051   
00052  public:
00054 
00055 
00056   enum Status { REOF=0x00, REOL=0x01, REOG=0x02, RBOG=0x04 };
00058 
00060 
00061 
00062   RDB( const string& name="", ios::openmode mode=ios::in ) throw ( RDBErr );
00064   RDB( istream* isptr ) throw ( RDBErr );
00066   RDB( ostream* osptr ) throw ( RDBErr );
00068   RDB( const RDB& rdb ) throw ( RDBErr );
00070   ~RDB( void );
00072   
00074 
00075 
00076   void open( const string& name, ios::openmode mode=ios::in ) throw ( RDBErr );
00078   void open( istream* isptr ) throw ( RDBErr );
00080   void open( ostream* osptr ) throw ( RDBErr );
00082   void open( const RDB& rdb ) throw ( RDBErr );
00084   void close( void );
00085   
00087   int read( void ) throw ( RDBErr );
00089   bool write( void ) throw ( RDBErr );
00091   bool rewind( void ) throw ( RDBErr );
00093 
00095 
00096 
00097   bool autoIdx( void ) const;
00099   void autoIdx( const bool on );
00101   void advanceIdx( void );
00103 
00105 
00106 
00107   void setGroup( const string& name, bool group=true ) throw ( RDBErr );
00109   void setGroup( const int idx, bool group=true ) throw ( RDBErr );
00111   bool getGroup( const string& name ) throw ( RDBErr );
00113   bool getGroup( const int idx ) throw ( RDBErr );
00115   bool newGroup( void );
00117   
00119 
00120 
00121   void setComment( const string& comm, const int idx=-1 );
00123   void setComment( RDBComment& comm, const int idx=-1 );
00125   void setComment( RDBComment& comm, const string& name, const size_t idx=0 );
00127   void setComment( const RDB& rdb );
00128   
00130   RDBComment& getComment( const size_t idx ) throw ( RDBErr );
00132   RDBComment& getComment( const string& name, const size_t idx=0 ) throw ( RDBErr );
00134   
00136 
00137 
00138   void setColumn( const string& name, const string& def, const int idx=-1 );
00140   void setColumn( RDBColumn* col, const int idx=-1 );
00142   void setColumn( RDBColumn* col, const string& name, const size_t idx=0 );
00144   void setColumn( const RDB& rdb );
00145   
00147   RDBColumn* getColumn( const size_t idx ) throw ( RDBErr );
00149   RDBColumn* getColumn( const string& name, const size_t idx=0 ) throw ( RDBErr );
00151 
00153 
00154 
00155   void setName( const size_t idx, const string& name ) throw ( RDBErr );
00157   void setDef( const size_t idx, const string& def  ) throw ( RDBErr );
00159   void setWidth( const size_t idx, const long width ) throw ( RDBErr );
00161   void setType( const size_t idx, const RDBColumn::Type type ) throw ( RDBErr );
00163   void setJust( const size_t idx, const RDBColumn::Just just ) throw ( RDBErr );
00165   void setDesc( const size_t idx, const string& desc ) throw ( RDBErr );
00167   void mapData( const size_t idx, double data[], const size_t nelems=1 ) throw ( RDBErr );
00169   void mapData( const size_t idx, long data[], const size_t nelems=1 ) throw ( RDBErr );
00171   void mapData( const size_t idx, string data[], const size_t nelems=1 ) throw ( RDBErr );
00173   void setData( const size_t idx, const double data ) throw ( RDBErr );
00175   void setData( const size_t idx, const long data ) throw ( RDBErr );
00177   void setData( const size_t idx, const string& data ) throw ( RDBErr );
00178 
00180   void getName( const size_t idx, string& name ) const throw ( RDBErr );
00182   void getDef( const size_t idx, string& def  ) throw ( RDBErr );
00184   void getWidth( const size_t idx, long& width ) const throw ( RDBErr );
00186   void getType( const size_t idx, RDBColumn::Type& type ) const throw ( RDBErr );
00188   void getJust( const size_t idx, RDBColumn::Just& just ) const throw ( RDBErr );
00190   void getDesc( const size_t idx, string& desc ) const throw ( RDBErr );
00192   void getData( const size_t idx, double& data ) throw ( RDBErr );
00194   void getData( const size_t idx, long& data ) throw ( RDBErr );
00196   void getData( const size_t idx, string& data ) throw ( RDBErr );
00197 
00199   string getName( const size_t idx ) const throw ( RDBErr );
00201   string getDef( const size_t idx  ) throw ( RDBErr );
00203   long getWidth( const size_t idx ) const throw ( RDBErr );
00205   RDBColumn::Type getType( const size_t idx ) const throw ( RDBErr );
00207   RDBColumn::Just getJust( const size_t idx ) const throw ( RDBErr );
00209   string getDesc( const size_t idx ) const throw ( RDBErr );
00211   double getDataDouble( const size_t idx ) throw ( RDBErr );
00213   long getDataLong( const size_t idx ) throw ( RDBErr );
00215   string getDataString( const size_t idx ) throw ( RDBErr );
00217 
00219 
00220 
00221   void setName( const string& name, const string& newname ) throw ( RDBErr );
00223   void setDef( const string& name, const string& def  ) throw ( RDBErr );
00225   void setWidth( const string& name, const long width ) throw ( RDBErr );
00227   void setType( const string& name, const RDBColumn::Type type ) throw ( RDBErr );
00229   void setJust( const string& name, const RDBColumn::Just just ) throw ( RDBErr );
00231   void setDesc( const string& name, const string& desc ) throw ( RDBErr );
00233   void mapData( const string& name, double data[], const size_t nelems=1 ) throw ( RDBErr );
00235   void mapData( const string& name, long data[], const size_t nelems=1 ) throw ( RDBErr );
00237   void mapData( const string& name, string data[], const size_t nelems=1 ) throw ( RDBErr );
00239   void setData( const string& name, const double data ) throw ( RDBErr );
00241   void setData( const string& name, const long data ) throw ( RDBErr );
00243   void setData( const string& name, const string& data ) throw ( RDBErr );
00244 
00246   void getName( const string& name, string& namefound ) const throw ( RDBErr );
00248   void getDef( const string& name, string& def  ) throw ( RDBErr );
00250   void getWidth( const string& name, long& width ) const throw ( RDBErr );
00252   void getType( const string& name, RDBColumn::Type& type ) const throw ( RDBErr );
00254   void getJust( const string& name, RDBColumn::Just& just ) const throw ( RDBErr );
00256   void getDesc( const string& name, string& desc ) const throw ( RDBErr );
00258   void getData( const string& name, double& data ) throw ( RDBErr );
00260   void getData( const string& name, long& data ) throw ( RDBErr );
00262   void getData( const string& name, string& data ) throw ( RDBErr );
00263 
00265   string getName( const string& name ) const throw ( RDBErr );
00267   string getDef( const string& name  ) throw ( RDBErr );
00269   long getWidth( const string& name ) const throw ( RDBErr );
00271   RDBColumn::Type getType( const string& name ) const throw ( RDBErr );
00273   RDBColumn::Just getJust( const string& name ) const throw ( RDBErr );
00275   string getDesc( const string& name ) const throw ( RDBErr );
00277   double getDataDouble( const string& name ) throw ( RDBErr );
00279   long getDataLong( const string& name ) throw ( RDBErr );
00281   string getDataString( const string& name ) throw ( RDBErr );
00283   
00285 
00286 
00287   size_t nComments( void ) const;
00289   size_t nColumns( void ) const;
00291   size_t nRows( void ); // Not const since it requires scanning the entire file...
00293   
00294  protected:
00296   void parseHeader( void ) throw ( RDBErr );
00298   vector<string> parseLine( const string& line ) const;
00300   size_t parseLine( bool& newgroup ) throw ( RDBErr );
00301 
00303   string _filename;
00305   ios::openmode _mode;
00306 
00308   istream* _isptr;
00310   ostream* _osptr;
00312   bool _myisptr;
00314   bool _myosptr; 
00315   
00317   size_t _rewindto;
00319   size_t _ncomms;
00321   size_t _ncols;
00323   size_t _nrows;
00325   bool _knowrows;
00327   long _rownum;
00329   long _frownum;
00330   
00332   bool _autoidx;
00334   bool _firstread;
00336   bool _lastread;
00338   bool _writehdr;
00339 
00341   RDBComment* _comms;
00343   RDBColumn** _cols;
00345   RDBLongColumn _nrcol;
00347   bool* _mycols;
00348 
00350   string _line;
00351   
00352   
00354     //  char* _line;
00356     //  size_t _linesize;
00357   
00358 };
00359 
00360 #endif
00361 

Generated on Tue Sep 15 11:07:16 2009 for rdbxx by  doxygen 1.5.6