RDBColumn.cc

00001 // --8<--8<--8<--8<--
00002 //
00003 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00004 //
00005 // This file is part of RDB
00006 //
00007 // RDB is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // RDB is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the 
00019 //       Free Software Foundation, Inc. 
00020 //       51 Franklin Street, Fifth Floor
00021 //       Boston, MA  02110-1301, USA
00022 //
00023 // -->8-->8-->8-->8--
00024 
00025 #include <cstdlib>
00026 #include "RDBColumn.h"
00027 
00037 istream&
00038 operator>>( 
00039   istream& is, 
00040   RDBColumn& col 
00041   ) throw ( RDBErr ) {
00042 
00043   try { 
00044     col.read( is );
00045     
00046   } catch ( RDBErr& rdberr ) {
00047     rdberr.set_message( "operator>>(istream&,RDBColumn&):  " );
00048     throw( rdberr );
00049 
00050   } catch ( ... ) {
00051     throw( RDBErr( "operator>>(istream&,RDBColumn&):  unexpected exception caught" ) );
00052 
00053   }
00054 
00055   return is;
00056 
00057 }
00058 
00068 istream&
00069 operator>>( 
00070   istream& is, 
00071   RDBColumn* col 
00072   ) throw ( RDBErr ) {
00073 
00074   try { 
00075     col->read( is );
00076 
00077   } catch ( RDBErr& rdberr ) {
00078     rdberr.set_message( "operator>>(istream&,RDBColumn*):  " );
00079     throw( rdberr );
00080 
00081   } catch ( ... ) {
00082     throw( RDBErr( "operator>>(istream&,RDBColumn*):  unexpected exception caught" ) );
00083 
00084   }
00085 
00086   return is;
00087 
00088 }
00089 
00099 ostream&
00100 operator<<( 
00101   ostream& os, 
00102   const RDBColumn& col 
00103   ) {
00104 
00105   return col.write( os );
00106 
00107 }
00108 
00118 ostream&
00119 operator<<( 
00120   ostream& os, 
00121   const RDBColumn* col 
00122   ) {
00123 
00124   return col->write( os );
00125 
00126 }
00127   
00136 RDBColumn::RDBColumn( 
00137   const string& name,
00138   const string& def
00139   ) throw ( RDBErr )
00140   : _name(name), _def(def), _width(-1), _type(STRING), _just(LEFT),
00141     _desc(""), _changed(false), _throw(true), _errno(0),
00142     _precision(DBL_DIG), _group(false), _initgroup(true) {
00143 
00144   if ( !_def.empty( ) ) {
00145     try { 
00146       setDef( _def );
00147 
00148     } catch ( RDBErr& rdberr ) {
00149       rdberr.set_message( "RDBColumn::RDBColumn(string&,string&):  " );
00150       throw( rdberr );
00151 
00152     } catch ( ... ) { 
00153       throw( RDBErr( "RDBColumn::RDBColumn(string&,string&):  unexpected exception caught" ) );
00154 
00155     }
00156   }
00157 }
00158 
00165 RDBColumn::RDBColumn( 
00166   const RDBColumn& col
00167   ) 
00168   : _name(col._name), _def(col._def), _width(col._width),
00169     _type(col._type), _just(col._just), _desc(col._desc),
00170     _changed(col._changed), _throw(col._throw), _errno(col._errno),
00171     _precision(col._precision), _group(col._group),
00172     _initgroup(col._initgroup) {
00173 
00174 }
00175 
00180 RDBColumn::~RDBColumn( 
00181   void 
00182   ) {
00183 
00184 }
00185 
00190 RDBColumn& 
00191 RDBColumn::operator=( 
00192   const RDBColumn& col 
00193   ) {
00194 
00195   if ( this != &col ) {
00196     _name      = col._name;
00197     _def       = col._def;
00198     _width     = col._width;
00199     _type      = col._type;
00200     _just      = col._just;
00201     _desc      = col._desc;
00202     _throw     = col._throw;
00203     _errno     = col._errno;
00204     _precision = col._precision;
00205     _changed   = col._changed;
00206     _group     = col._group;
00207 
00208   }
00209 
00210   return *this;
00211 
00212 }
00213 
00218 void
00219 RDBColumn::setGroup( 
00220   bool group
00221   ) {
00222 
00223   _group = group;
00224 
00225 }
00226 
00231 bool
00232 RDBColumn::getGroup( 
00233   void
00234   ) const {
00235 
00236   return _group;
00237 
00238 }
00239 
00246 void
00247 RDBColumn::setName( 
00248   const string& name
00249   ) {
00250 
00251   _name = name;
00252 
00253 }
00254 
00265 void
00266 RDBColumn::setDef(
00267   const string& def
00268   ) throw ( RDBErr ) {
00269 
00270   _def = def;
00271   
00272   _width = -1;
00273   _type  = STRING;
00274   _just  = LEFT;
00275   _desc.erase( );
00276 
00277   size_t defsize = def.size( );
00278   char* cdef = new char[defsize+1];
00279   strcpy( cdef, def.c_str() );
00280   //  cdef[defsize] = '\0';
00281   int n = 0;
00282 
00283   if ( isdigit( cdef[0] ) ) {
00284     char* ptr = NULL;
00285     _width = strtol( cdef, &ptr, 10 );
00286     if ( ptr ) {
00287       n = ptr - cdef;
00288 
00289     }
00290   }
00291 
00292   if ( NUMERIC == cdef[n] || STRING == cdef[n] || MONTH == cdef[n] ) {
00293     _type = Type(cdef[n++]);
00294 
00295   } else if ( '-' == cdef[n] ) {
00296     _type = STRING;
00297     while ( '-' == cdef[n] ) {
00298       n++;
00299 
00300     }
00301   } else {
00302     delete [] cdef;
00303     throw( RDBErr( string("RDBColumn::setDef(string&):  Bad type in column defintion '") + def + "' for '" + _name + "'" ) );
00304 
00305   }  
00306 
00307   if ( '\0' != cdef[n] && ( LEFT == cdef[n] || RIGHT == cdef[n] ) ) {
00308     _just = Just(cdef[n++]);
00309 
00310   }
00311 
00312   if ( '\0' != cdef[n] ) {
00313     if ( ' ' == cdef[n] ) {
00314       while ( ' ' == cdef[n] ) { 
00315         n++; 
00316 
00317       }
00318       _desc = &cdef[n];
00319     }
00320     else {
00321       delete [] cdef;
00322       throw( RDBErr( string("RDBColumn::setDef(string&):  Bad just in column defintion '") + def + "' for '" + _name + "'" ) );
00323 
00324     }
00325   }
00326 
00327   _changed = false;
00328   delete [] cdef;
00329 
00330 }
00331 
00339 void
00340 RDBColumn::setWidth(
00341   const long width
00342   ) {
00343 
00344   _width   = width;
00345   _changed = true;
00346 
00347 }
00348 
00356 void
00357 RDBColumn::setType( 
00358   const RDBColumn::Type type
00359   ) {
00360 
00361   _type    = type;
00362   _changed = true;
00363 
00364 }
00365 
00374 void
00375 RDBColumn::setJust( 
00376   const RDBColumn::Just just
00377   ) {
00378 
00379   _just    = just;
00380   _changed = true;
00381 
00382 }
00383 
00390 void
00391 RDBColumn::setDesc( 
00392   const string& desc 
00393   ) {
00394 
00395   _desc    = desc;
00396   _changed = true;
00397 
00398 }
00399 
00407 void
00408 RDBColumn::setPrecision( 
00409   const int precision
00410   ) {
00411 
00412   _precision = precision;
00413 
00414 }
00415 
00422 void
00423 RDBColumn::setThrow( 
00424   const bool t
00425   ) {
00426 
00427   _throw = t;
00428 
00429 }
00430 
00437 void
00438 RDBColumn::setErrNo(
00439   const int no
00440   ) {
00441 
00442   _errno = no;
00443 
00444 }
00445 
00447 void
00448 RDBColumn::mapData( 
00449   double data[], 
00450   const size_t nelems 
00451   ) throw ( RDBErr ) {
00452 
00453   throw( RDBErr( string("RDBColumn::mapData(double[],size_t):  Wrong data type:  double[] in column '") + _name + "'" ) );
00454 
00455 }
00456 
00458 void
00459 RDBColumn::mapData( 
00460   long data[], 
00461   const size_t nelems
00462   ) throw ( RDBErr ) {
00463 
00464   throw( RDBErr( string("RDBColumn::mapData(long[],size_t):  Wrong data type:  long[] in column '") + _name + "'" ) );
00465 
00466 }
00467 
00469 void
00470 RDBColumn::mapData( 
00471   string data[], 
00472   const size_t nelems
00473   ) throw ( RDBErr ) {
00474 
00475   throw( RDBErr( string("RDBColumn::mapData(stringdouble[],size_t):  Wrong data type:  string[] in column '") + _name + "'" ) );
00476 
00477 }
00478 
00483 string 
00484 RDBColumn::getName( 
00485   void
00486   ) const {
00487 
00488   return _name;
00489 
00490 }
00491 
00497 string
00498 RDBColumn::getDef(
00499   void
00500   ) {
00501 
00502   if ( _changed ) {
00503     _def.erase( );
00504     _strstrm.seekp( 0 );
00505     if ( ! _strstrm.good( ) )
00506       _strstrm.clear( );
00507     if ( -1 != _width )
00508       _strstrm << _width;
00509 
00510     if ( RDBColumn::NUMERIC == _type )
00511       _strstrm << 'N';
00512     else if ( RDBColumn::STRING == _type ) 
00513       _strstrm << 'S';
00514     else 
00515       _strstrm << 'M';
00516 
00517     if ( RDBColumn::LEFT == _just )
00518       _strstrm << '<';
00519     else 
00520       _strstrm << '>';
00521 
00522     if ( 0 != _desc.length( ) )
00523       _strstrm << ' ' << _desc;
00524 
00525     _strstrm << '\0';
00526     _def     = _strstrm.str( ).c_str( );
00527     _changed = false;
00528 
00529   }
00530 
00531   return _def;
00532   
00533 }
00534 
00539 long 
00540 RDBColumn::getWidth( 
00541   void
00542   ) const {
00543 
00544   return _width;
00545 
00546 }
00547 
00552 RDBColumn::Type
00553 RDBColumn::getType(
00554   void
00555   ) const {
00556 
00557   return _type;
00558 
00559 }
00560 
00565 RDBColumn::Just
00566 RDBColumn::getJust(
00567   void
00568   ) const {
00569 
00570   return _just;
00571 
00572 }
00573 
00578 string
00579 RDBColumn::getDesc(
00580   void
00581   ) const {
00582 
00583   return _desc;
00584   
00585 }
00586 
00591 int
00592 RDBColumn::getPrecision(
00593   void
00594   ) const {
00595 
00596   return _precision;
00597   
00598 }
00599 
00604 bool
00605 RDBColumn::getThrow(
00606   void
00607   ) const {
00608 
00609   return _throw;
00610   
00611 }
00612 
00617 char*
00618 RDBColumn::getErr(
00619   void
00620   ) const {
00621 
00622   return strerror( _errno );
00623 
00624 }
00625 
00630 int
00631 RDBColumn::getErrNo(
00632   void
00633   ) const {
00634 
00635   return _errno;  
00636 
00637 }
00638 
00646 void
00647 RDBColumn::convert(
00648   const double& idata,
00649   double& odata
00650   ) throw ( RDBErr ) {
00651 
00652   odata = idata;
00653 
00654 }
00655 
00663 void
00664 RDBColumn::convert(
00665   const double& idata,
00666   long& odata
00667   ) throw ( RDBErr ) {
00668 
00669   odata = (long) idata;
00670 
00671 }
00672 
00683 void
00684 RDBColumn::convert(
00685   const double& idata,
00686   string& odata
00687   ) throw ( RDBErr ) {
00688 
00689   _strstrm.seekp( 0 );
00690   if ( ! _strstrm.good( ) )
00691     _strstrm.clear( );
00692   _strstrm << setprecision( _precision ) << idata << '\0';
00693   odata = _strstrm.str( ).c_str( );
00694 
00695 }
00696 
00704 void
00705 RDBColumn::convert(
00706   const long& idata,
00707   double& odata
00708   ) throw ( RDBErr ) {
00709 
00710   odata = (double) idata;  
00711 
00712 }
00713 
00721 void
00722 RDBColumn::convert(
00723   const long& idata,
00724   long& odata
00725   ) throw ( RDBErr ) {
00726   
00727   odata = idata;
00728 
00729 }
00730 
00740 void
00741 RDBColumn::convert(
00742   const long& idata,
00743   string& odata
00744   ) throw ( RDBErr ) {
00745 
00746   _strstrm.seekp( 0 );
00747   if ( ! _strstrm.good( ) )
00748     _strstrm.clear( );
00749   _strstrm << idata << '\0';
00750   odata = _strstrm.str( ).c_str( );  
00751 
00752 }
00753 
00763 void
00764 RDBColumn::convert(
00765   const string& idata,
00766   double& odata
00767   ) throw ( RDBErr ) {
00768 
00769   errno = 0;
00770   char* endptr = NULL;
00771   odata = strtod( idata.length( ) ? idata.c_str( ) : "NaN", &endptr );
00772   _errno = errno;
00773   if ( ( endptr && ( *endptr != '\0' ) ) ) {
00774     if ( _throw ) {
00775       throw( RDBErr( string("RDBColumn::convert(string&,double&):  Non numeric data in column '") + _name + "'  at '" + endptr + "'" ) );
00776         
00777     } else {
00778       _errno = RDBColumn::OUTOFRANGE;
00779         
00780     }
00781   }
00782 
00783 }
00784 
00795 void
00796 RDBColumn::convert(
00797   const string& idata,
00798   long& odata
00799   ) throw ( RDBErr ) {
00800 
00801   errno = 0;
00802   char* endptr = NULL;
00803   odata = strtol( idata.length( ) ? idata.c_str( ) : "NaN", &endptr, 10 );
00804   _errno = errno;
00805   if ( ( endptr && *endptr != '\0' ) ) {
00806     if ( *endptr != '.' ) {
00807       if ( _throw ) {
00808         throw( RDBErr( string("RDBColumn::convert(string&,long&):  Non numeric data in column '") + _name + "' at '" + endptr + "'" ) );
00809           
00810       } else {
00811         _errno = RDBColumn::NONNUMERIC;
00812           
00813       }
00814     } else {
00815       char* beginptr = endptr+1;
00816       endptr = NULL;
00817       strtol( beginptr, &endptr, 10 );
00818       _errno = errno;
00819       if ( ( endptr && *endptr != '\0' ) ) {
00820         if ( _throw ) {
00821           throw( RDBErr( string("RDBColumn::convert(string&,long&):  Non numeric data in column '") + _name + "'at '" + endptr + "'" ) );
00822           
00823         } else {
00824           _errno = RDBColumn::NONNUMERIC;
00825           
00826         }
00827       } else {
00828         if ( _throw ) {
00829           throw( RDBErr( string("RDBColumn::convert(string&,long&):  Lost precision in column '") + _name + "'" ) );
00830           
00831         } else {
00832           _errno = RDBColumn::LOSTPRECISION;
00833           
00834         }
00835       }
00836     }
00837   }
00838 }
00839 
00847 void
00848 RDBColumn::convert(
00849   const string& idata,
00850   string& odata
00851   ) throw ( RDBErr ) {
00852 
00853   odata = idata;
00854 
00855 }
00856 
00866 istream& 
00867 RDBColumn::extract( 
00868   istream& is,
00869   double& data
00870   ) throw ( RDBErr ) {
00871 
00872   is >> data;
00873   if ( ! is.good( ) ) {
00874     throw( RDBErr( string("RDBColumn::extract(istream&,double&):  Non numeric data in column '") + _name + "'" ) );
00875   }
00876 
00877   return is;
00878 
00879 }
00880 
00890 istream& 
00891 RDBColumn::extract( 
00892   istream& is,
00893   long& data
00894   ) throw ( RDBErr ) {
00895 
00896   is >> data;
00897   if ( ! is.good( ) ) {
00898     throw( RDBErr( string("RDBColumn::extract(istream&,long&):  Non numeric data in column '") + _name + "'" ) );
00899 
00900   }
00901 
00902   return is;
00903 
00904 }
00905 
00916 istream& 
00917 RDBColumn::extract( 
00918   istream& is,
00919   string& data
00920   ) throw ( RDBErr ) {
00921 
00922   string tmp;
00923   is >> data;
00924   if ( ! is.good( ) ) {
00925     throw( RDBErr( string("RDBColumn::extract(stream&,string&):  Row format error in column '") + _name + "'" ) );
00926 
00927   }
00928 
00929   while ( ' ' == is.peek( ) ) {
00930     is >> tmp;
00931     if ( ! is.good( ) ) {
00932       throw( RDBErr( string("RDBColumn::extract(stream&,string&):  Row format error in column '") + _name + "'" ) );
00933 
00934     }
00935     data += ' ' + tmp;
00936 
00937   }
00938 
00939   return is;
00940 
00941 }
00942 
00952 ostream& 
00953 RDBColumn::insert( 
00954   ostream& os,
00955   double& data
00956   ) const {
00957 
00958   return os << setprecision( _precision ) << data;
00959 
00960 }
00961 
00971 ostream& 
00972 RDBColumn::insert( 
00973   ostream& os,
00974   long& data
00975   ) const {
00976 
00977   return os << setprecision( _precision ) << data;
00978 
00979 }
00980 
00990 ostream& 
00991 RDBColumn::insert( 
00992   ostream& os,
00993   string& data
00994   ) const {
00995 
00996   return os << setprecision( _precision ) << data;
00997 
00998 }

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