rdbxx  1.0.7_02
simpleRDBTable.cc
1 // --8<--8<--8<--8<--
2 //
3 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
4 //
5 // This file is part of RDB
6 //
7 // RDB is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // RDB is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the
19 // Free Software Foundation, Inc.
20 // 51 Franklin Street, Fifth Floor
21 // Boston, MA 02110-1301, USA
22 //
23 // -->8-->8-->8-->8--
24 
25 #ifndef simpleRDBTable_cc
26 #define simpleRDBTable_cc
27 
28 #include <rdbxx/simpleRDBTable.h>
29 
30 template< class Type >
32 
33  if ( rdb_column ) {
34  delete [] rdb_column;
35  rdb_column = NULL;
36  }
37 
38 }
39 
40 template< class Type >
41 size_t simpleRDBTable< Type >::count_num_args( const char* str[] ) {
42 
43  size_t num = 0;
44  for ( num = 0; NULL != str[ num ]; num++ )
45  ;
46  return num;
47 
48 }
49 
50 template< class Type >
51 simpleRDBTable< Type >::simpleRDBTable( const string& fname,
52  const int mode,
53  const char* header[] )
54  :
55  RDB( fname, static_cast<ios_base::openmode>(mode) ){
56 
57  /*
58  The sun compiler doesn't seem to want to catch
59  the exception thrown from the base class.
60  */
61 
62  try {
63 
64  size_t num_cols = count_num_args( header );
65 
66  rdb_column = new RDBColumn*[ num_cols + 1 ];
67  for( size_t ii = 0; ii < num_cols; ii++ ) {
68  rdb_column[ ii ]= getColumn( header[ ii ] );
69  }
70  rdb_column[ num_cols ] = NULL;
71 
72  rdb_header = header;
73 
74  } catch( Exception& e ) {
75 
76  throw( e );
77 
78  } catch( ... ) {
79 
80  throw( Exception( "simpleRDBTable: unexpected exception caught" ) );
81 
82  }
83 }
84 
85 template< class Type >
86 void simpleRDBTable< Type >::getData( RDBColumn* rdbcol, string& val )
87  {
88 
89  if ( NULL == rdbcol )
90  throw Exception( "simpleRDBTable<T>::getData( RDBColumn*, string& ) :"
91  " The first argument must not be NULL\n" );
92 
93  rdbcol->getData( val );
94 
95 }
96 
97 template< class Type >
98 void simpleRDBTable< Type >::getData( RDBColumn* rdbcol, int& val )
99  {
100 
101  if ( NULL == rdbcol )
102  throw Exception( "simpleRDBTable<T>::getData( RDBColumn*, string& ) :"
103  " The first argument must not be NULL\n" );
104 
105  string tmp;
106  rdbcol->getData( tmp );
107 
108  try {
109 
110  long result = suplib::str2l( tmp.c_str( ), 10 );
111  val = (int) result;
112 
113  } catch( ... ) {
114 
115  throw;
116 
117  }
118 
119 }
120 
121 template< class Type >
122 void simpleRDBTable< Type >::getData( RDBColumn* rdbcol, long& val )
123  {
124 
125  if ( NULL == rdbcol )
126  throw Exception( "simpleRDBTable<T>::getData( RDBColumn*, string& ) : "
127  "The first argument must not be NULL\n" );
128 
129  string tmp;
130  rdbcol->getData( tmp );
131 
132  try {
133 
134  val = suplib::str2l( tmp.c_str( ), 10 );
135 
136  } catch( ... ) {
137 
138  throw;
139 
140  }
141 
142 }
143 
144 template< class Type >
145 void simpleRDBTable< Type >::getData( RDBColumn* rdbcol, double& val )
146  {
147 
148  if ( NULL == rdbcol )
149  throw Exception( "simpleRDBTable<T>::getData( RDBColumn*, string& ) : "
150  "The first argument must not be NULL\n" );
151 
152  string tmp;
153  rdbcol->getData( tmp );
154 
155  try {
156 
157  val = suplib::str2d( tmp.c_str( ) );
158 
159  } catch( ... ) {
160 
161  throw;
162 
163  }
164 
165 }
166 
167 template< class Type >
168 void simpleRDBTable< Type >::print( ostream& os ) const {
169 
170  if ( rdb_header ) {
171 
172  for( size_t num = 0; NULL != rdb_header[ num ]; num++ ) {
173  os << rdb_header[ num ];
174  if ( NULL != rdb_header[ num + 1 ] )
175  os << '\t';
176  else
177  os << '\n';
178  }
179 
180  for( size_t num = 0; NULL != rdb_column[ num ]; num++ ) {
181  os << rdb_column[ num ]->getDef( );
182  if ( NULL != rdb_column[ num + 1 ] )
183  os << '\t';
184  }
185 
186  }
187 
188 }
189 
190 template< class Type >
192  {
193 
194  if ( ! _isptr )
195  throw Exception( "The rdb table is not yet open for reading\n" );
196 
197  try {
198 
199  if ( read( ) )
200  return new Type( rdb_header, rdb_column );
201  else
202  return (Type*) NULL;
203 
204  } catch( ... ) {
205 
206  throw;
207 
208  }
209 
210 }
211 
212 #endif
Type * readRow()
Read a row of the RDB table.
virtual void * getData(void)=0
Returns a pointer to the current data element.
void getDef(const size_t idx, string &def)
Return the definition of the RDBColumn at idx.
Definition: RDB.cc:1837
Provides interface for manipulating RDB tables.
Definition: RDB.h:43
static void getData(RDBColumn *rdbcol, string &val)
Given an RDBColumn, get a string value.
A simple interface to the RDB++ read a row of an rdb table to initialize a class Type.
Provides interface for general column related methods.
Definition: RDBColumn.h:43