Parameters.cc

00001 // --8<--8<--8<--8<--
00002 //
00003 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00004 //
00005 // This file is part of paramxx
00006 //
00007 // paramxx 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 // paramxx 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 <ParFile.h>
00026 
00027 #include "Parameters.h"
00028 
00029 typedef map< string, bool, less< string > > BoolMap;
00030 typedef BoolMap::iterator BoolMapIterator;
00031 typedef BoolMap::const_iterator ConstBoolMapIterator;
00032 
00033 typedef map< string, double, less< string > > DoubleMap;
00034 typedef DoubleMap::iterator DoubleMapIterator;
00035 typedef DoubleMap::const_iterator ConstDoubleMapIterator;
00036 
00037 typedef map< string, int, less< string > > IntMap;
00038 typedef IntMap::iterator IntMapIterator;
00039 typedef IntMap::const_iterator ConstIntMapIterator;
00040 
00041 typedef map< string, string, less< string > > StringMap;
00042 typedef StringMap::iterator StringMapIterator;
00043 typedef StringMap::const_iterator ConstStringMapIterator;
00044 
00045 static char mymsg[ 128 ];
00046 
00047 Parameters::Parameters( int argc, char* argv[], const char* fname,
00048                         const char* boolpars[], const char* doublepars[],
00049                         const char* intpars[], const char* stringpars[] )
00050   throw ( ParFileException ) {
00051 
00052     try {
00053 
00054       ParFile parfile( argc, argv, fname );
00055 
00056       init_boolpars( parfile, boolpars );
00057 
00058       init_doublepars( parfile, doublepars );
00059 
00060       init_intpars( parfile, intpars );
00061 
00062       init_stringpars( parfile, stringpars );
00063 
00064     } catch( ParFileException& pfe ) {
00065 
00066     throw;
00067     
00068   }
00069 
00070 }
00071 
00072 void Parameters::init_boolpars( ParFile& pf, const char* boolpars[] )
00073   throw ( ParFileException ) {
00074 
00075   try {
00076 
00077     if ( boolpars ) {
00078       size_t ii = 0;
00079       while( boolpars[ ii ] ) {
00080         bool_parameters[ boolpars[ ii ] ] = pf.pgetb( boolpars[ ii ] );
00081         ++ii;
00082       }
00083     }
00084 
00085   } catch( ParFileException& pfe ) {
00086 
00087     throw;
00088     
00089   }
00090 
00091 }
00092 
00093 void Parameters::init_doublepars( ParFile& pf, const char* doublepars[] )
00094   throw ( ParFileException ) {
00095 
00096   try {
00097 
00098     if ( doublepars ) {
00099       size_t ii = 0;
00100       while( doublepars[ ii ] ) {
00101         double_parameters[ doublepars[ ii ] ] = pf.pgetd( doublepars[ ii ] );
00102         ++ii;
00103       }
00104     }
00105 
00106 
00107   } catch( ParFileException& pfe ) {
00108 
00109     throw;
00110     
00111   }
00112 
00113 }
00114 
00115 void Parameters::init_intpars( ParFile& pf, const char* intpars[] )
00116   throw ( ParFileException ) {
00117 
00118   try {
00119 
00120     if ( intpars ) {
00121       size_t ii = 0;
00122       while( intpars[ ii ] ) {
00123         int_parameters[ intpars[ ii ] ] = pf.pgeti( intpars[ ii ] );
00124         ++ii;
00125       }
00126     }
00127 
00128   } catch( ParFileException& pfe ) {
00129 
00130     throw;
00131     
00132   }
00133 
00134 }
00135 
00136 void Parameters::init_stringpars( ParFile& pf, const char* stringpars[] )
00137   throw ( ParFileException ) {
00138 
00139   try {
00140 
00141     if ( stringpars ) {
00142       size_t ii = 0;
00143       while( stringpars[ ii ] ) {
00144         string_parameters[ stringpars[ ii ] ] =
00145           pf.pgetstring( stringpars[ ii ] );
00146         ++ii;
00147       }
00148     }
00149 
00150   } catch( ParFileException& pfe ) {
00151 
00152     throw;
00153     
00154   }
00155 
00156 }
00157 
00158 void Parameters::init_Parameters( int argc, char* argv[], const char* fname,
00159                                   const char* boolpars[],
00160                                   const char* doublepars[],
00161                                   const char* intpars[],
00162                                   const char* stringpars[] )
00163   throw ( ParFileException ) {
00164 
00165     try {
00166 
00167       ParFile pf ( argc, argv, fname );
00168 
00169       init_boolpars( pf, boolpars );
00170 
00171       init_doublepars( pf, doublepars );
00172 
00173       init_intpars( pf, intpars );
00174 
00175       init_stringpars( pf, stringpars );
00176 
00177     } catch( ParFileException& pfe ) {
00178 
00179       throw;
00180 
00181     }
00182 
00183 }
00184 
00185 bool Parameters::pgetb( const char* par ) const
00186   throw ( ParFileException ) {
00187 
00188     ConstBoolMapIterator iter = bool_parameters.find( par );
00189 
00190     if ( iter == bool_parameters.end( ) ) {
00191 
00192       const char* format =
00193         "Parameters::pgetb( %s ) : Unable to find parameter\n";
00194 
00195       sprintf( mymsg, format, par );
00196 
00197       throw ParFileException( mymsg );
00198 
00199     }
00200 
00201     return iter->second;
00202 
00203 }
00204 
00205 double Parameters::pgetd( const char* par ) const
00206   throw ( ParFileException ) {
00207 
00208     ConstDoubleMapIterator iter = double_parameters.find( par );
00209 
00210     if ( iter == double_parameters.end( ) ) {
00211 
00212       const char* format =
00213         "Parameters::pgetd( %s ) : Unable to find parameter\n";
00214 
00215       sprintf( mymsg, format, par );
00216 
00217       throw ParFileException( mymsg );
00218 
00219     }
00220 
00221     return iter->second;
00222 
00223 }
00224 
00225 int Parameters::pgeti( const char* par ) const
00226   throw ( ParFileException ) {
00227 
00228     ConstIntMapIterator iter = int_parameters.find( par );
00229 
00230     if ( iter == int_parameters.end( ) ) {
00231 
00232       const char* format =
00233         "Parameters::pgeti( %s ) : Unable to find parameter\n";
00234 
00235       sprintf( mymsg, format, par );
00236 
00237       throw ParFileException( mymsg );
00238 
00239     }
00240 
00241     return iter->second;
00242 
00243 }
00244 
00245 string Parameters::pgetstring( const char* par ) const
00246   throw ( ParFileException ) {
00247 
00248     ConstStringMapIterator iter = string_parameters.find( par );
00249 
00250     if ( iter == string_parameters.end( ) ) {
00251 
00252       const char* format =
00253         "Parameters::pgetstring( %s ) : Unable to find parameter\n";
00254 
00255       sprintf( mymsg, format, par );
00256 
00257       throw ParFileException( mymsg );
00258 
00259     }
00260 
00261     return iter->second;
00262 
00263 }
00264 
00265 void Parameters::print( ostream& os, const char* prefix ) const {
00266 
00267   // boolean
00268   {
00269     ConstBoolMapIterator iter_begin = bool_parameters.begin( );
00270     ConstBoolMapIterator iter_end = bool_parameters.end( );
00271     
00272     for ( ConstBoolMapIterator iter = iter_begin; iter != iter_end; iter++ )
00273       os << prefix << iter->first << " = " << iter->second << endl;
00274   }
00275 
00276   // int
00277   {
00278     ConstIntMapIterator iter_begin = int_parameters.begin( );
00279     ConstIntMapIterator iter_end = int_parameters.end( );
00280     
00281     for ( ConstIntMapIterator iter = iter_begin; iter != iter_end; iter++ )
00282       os << prefix << iter->first << " = " << iter->second << endl;
00283   }
00284 
00285   // double
00286   {
00287     ConstDoubleMapIterator iter_begin = double_parameters.begin( );
00288     ConstDoubleMapIterator iter_end = double_parameters.end( );
00289     
00290     for ( ConstDoubleMapIterator iter = iter_begin; iter != iter_end; iter++ )
00291       os << prefix << iter->first << " = " << iter->second << endl;
00292   }
00293 
00294   // string
00295   {
00296     ConstStringMapIterator iter_begin = string_parameters.begin( );
00297     ConstStringMapIterator iter_end = string_parameters.end( );
00298 
00299     for ( ConstStringMapIterator iter = iter_begin; iter != iter_end; iter++ )
00300       os << prefix << iter->first << " = " << iter->second << endl;
00301   }
00302 
00303 }

Generated on Thu Oct 2 17:54:19 2008 for paramxx by  doxygen 1.5.6