RDBComment.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 "RDBComment.h"
00026 
00037 istream&
00038 operator>>( 
00039   istream& is, 
00040   RDBComment& rdbcomment 
00041   ) throw ( RDBErr ) {
00042 
00043   string line;
00044   getline( is, line, '\n' );
00045   try { 
00046     rdbcomment.setComment( line );
00047 
00048   } catch ( RDBErr& rdberr ) {
00049     rdberr.set_message( "operator>>(istream&,RDBComment&):  " );
00050     throw( rdberr );
00051 
00052   } catch ( ... ) {
00053     throw( RDBErr( "operator>>(istream&,RDBComment&):  unexpected exception caught" ) );
00054 
00055   }
00056 
00057   return is;
00058 
00059 }
00060 
00072 ostream& 
00073 operator<<( 
00074   ostream& os, 
00075   const RDBComment& rdbcomment 
00076   ) {
00077 
00078   os <<
00079     (
00080       (
00081         0 != rdbcomment._keyword.size( ) 
00082         || 
00083         0 != rdbcomment._value.size( )
00084       )
00085       ?
00086       ("#:")
00087       :
00088       ("# ")
00089     ) << rdbcomment._comment;
00090 
00091   return os;
00092 
00093 }
00094 
00102 RDBComment::RDBComment( 
00103   const string& comment 
00104   ) throw ( RDBErr ) {
00105 
00106   try {
00107     setCommentText( comment );
00108 
00109   } catch ( RDBErr& rdberr ) {
00110     rdberr.set_message( "RDBComment::RDBComment(string&)" );
00111     throw( rdberr );
00112 
00113   } catch ( ... ) {
00114     throw( RDBErr( "RDBComment::RDBComment(string&):  unexpected exception caught" ) );
00115 
00116   }
00117 }
00118 
00125 RDBComment::RDBComment( 
00126   const RDBComment& rdbcomment
00127   ) {
00128 
00129   operator=( rdbcomment );
00130 
00131 }
00132 
00137 RDBComment::~RDBComment(
00138   void 
00139   ){
00140 
00141 }
00142 
00149 const RDBComment&
00150 RDBComment::operator=( 
00151   const RDBComment& rdbcomment
00152   ) {
00153 
00154   if ( this != &rdbcomment ) {
00155     _comment = rdbcomment._comment;
00156     _keyword = rdbcomment._keyword;
00157     _value   = rdbcomment._value;
00158 
00159   }
00160 
00161   return *this;
00162 
00163 }
00164 
00171 const RDBComment&
00172 RDBComment::operator=( 
00173   const string& comment
00174   ) {
00175 
00176   setCommentText( comment );
00177 
00178   return *this;
00179 
00180 }
00181 
00189 void
00190 RDBComment::setComment( 
00191   const string& comment
00192   ) throw ( RDBErr ) {
00193   
00194   size_t pos = comment.find_first_not_of( " " );
00195   if ( 0 == pos ) {
00196     if ( '#' != comment[0] || ' ' != comment[1] ) {
00197       throw( RDBErr( "RDBComment::setComment(string&):  Leading characters not `# '" ) );
00198 
00199     }
00200   } else if ( comment.npos != pos ) { 
00201     if ( '#' != comment[pos] ) {
00202       throw( RDBErr( "RDBComment::setComment(string&):  First non-space character not `#'" ) );
00203 
00204     }
00205   } else {
00206     throw( RDBErr( "RDBComment::setComment(string&):  No comment found" ) );
00207 
00208   }
00209 
00210   setCommentText( comment );
00211 
00212 }
00213 
00221 void
00222 RDBComment::setCommentText( 
00223   const string& comment
00224   ) {
00225 
00226   size_t pos = comment.find_first_not_of( " " );
00227   _comment = comment;
00228   _keyword.erase( );
00229   _value.erase( );
00230   
00231   // if the line has a leading '#' character, remove it...
00232   // users creating comments need not include leading '#'.
00233   if ( '#' == _comment[pos] ) {
00234     _comment.erase( 0, pos + 1 );
00235 
00236   }
00237   
00238   // see if its a keyword value pair...
00239   if ( ':' == _comment[0] ) {
00240     _comment.erase( 0, 1 );
00241     
00242     pos = _comment.find( "=", 0 );
00243     if ( pos > _comment.size( ) )
00244       pos = _comment.size( );
00245     
00246     _keyword = _comment;
00247     _keyword.erase( pos );
00248     
00249     size_t wspos = _keyword.find_first_not_of( " " );
00250     _keyword.erase( 0, wspos );
00251     wspos = _keyword.find_last_not_of( " " );
00252     _keyword.erase( wspos + 1 );
00253     
00254     _value = _comment;
00255     _value.replace( 0, pos+1, "" );
00256     
00257     wspos = _value.find_first_not_of( " " );
00258     _value.erase( 0, wspos );
00259     wspos = _value.find_last_not_of( " " );
00260     _value.erase( wspos + 1 );
00261     
00262   } else {
00263     pos = _comment.find_first_not_of( "\t " );
00264     if ( _comment.npos != pos ) {
00265       _comment.erase( 0, pos );
00266 
00267     }
00268   }
00269 }
00270 
00277 void
00278 RDBComment::setKeyword( 
00279   const string& keyword
00280   ) throw ( RDBErr ) {
00281 
00282   try {
00283     setComment( string("#:") + keyword + " = " + _value );
00284 
00285   } catch ( RDBErr& rdberr ) {
00286     rdberr.set_message( "RDBComment::setKeyword(string&):  " );
00287     throw( rdberr );
00288 
00289   } catch ( ... ) {
00290     throw( RDBErr( "RDBComment::setKeyword(string&):  unexpected exception caught" ) );
00291 
00292   }
00293 }
00294 
00301 void
00302 RDBComment::setValue(
00303   const string& value
00304   ) throw ( RDBErr ) { 
00305 
00306   try {
00307     setComment( string("#:") + _keyword + " = " + value );
00308 
00309   } catch ( RDBErr& rdberr ) {
00310     rdberr.set_message( "RDBComment::setValue(string&):  " );
00311     throw( rdberr );
00312 
00313   } catch ( ... ) {
00314     throw( RDBErr( "RDBComment::setValue(string&):  unexpected exception caught" ) );
00315 
00316   }
00317 }
00318 
00323 string
00324 RDBComment::getComment(
00325   void
00326   ) const {
00327 
00328   return 
00329     (
00330       (0 != _keyword.size( ) || 0 != _value.size( ))
00331       ?
00332       ("#:")
00333       :
00334       ("# ")
00335       +
00336       _comment
00337       );
00338   
00339 }
00340 
00345 string
00346 RDBComment::getCommentText(
00347   void
00348   ) const {
00349 
00350   return _comment;
00351 
00352 }
00353 
00358 string
00359 RDBComment::getKeyword(
00360   void
00361   ) const {
00362 
00363   return _keyword;
00364 
00365 }
00366 
00371 string
00372 RDBComment::getValue(
00373   void
00374   ) const {
00375 
00376   return _value;
00377 
00378 }

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