BoolPar.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 <stdlib.h>
00026 
00027 #include "BoolPar.h"
00028 
00029 static const char* yep[]  = { "yes", "y", "1", NULL };
00030 static const char* nope[] = { "no",  "n", "0", NULL };
00031 
00032 BoolPar::BoolPar( ParTxt& par ) throw ( ParFileException ) : Par( par ) {
00033 
00034   try {
00035 
00036     validate( parameter[ PARMINIMUM ] );
00037   
00038     validate( parameter[ PARMAXIMUM ] );
00039   
00040     validate( parameter[ PARVALUE ] );
00041 
00042   } catch( ParFileException& pfe ) {
00043     throw;
00044   }
00045   
00046 }
00047   
00048 void BoolPar::between_limits( const string& str ) const
00049   throw ( ParFileException, Exception ) {
00050 
00051     try {
00052       validate( str );
00053     } catch( ParFileException& pfe ) {
00054       throw;
00055     }
00056 
00057 }
00058 
00059 bool BoolPar::pgetb( void ) const throw ( ParFileException ) {
00060 
00061   size_t ii = 0;
00062   while( yep[ ii ] )
00063     if ( parameter[ PARVALUE ] == yep[ ii++ ] )
00064       return true;
00065 
00066   ii = 0;
00067   while( nope[ ii ] ) {
00068     if ( parameter[ PARVALUE ] == nope[ ii++ ] ) {
00069       return false;
00070     }
00071   }
00072 
00073   throw ParFileException( "BoolPar::pgetb( ) : Unable to get the value "
00074                           "for parameter " + parameter[ PARNAME ] );
00075 
00076 }
00077 
00078 void BoolPar::set_val( const string& str ) throw ( ParFileException,
00079                                                    Exception ) {
00080 
00081   try {
00082     validate( str );
00083     parameter[ PARVALUE ] = str;
00084   } catch( ParFileException& pfe ) {
00085     throw;
00086   } catch( Exception& e ) {
00087     throw;
00088   }
00089 
00090 }
00091 
00092 void BoolPar::validate( const string& str ) const throw ( ParFileException ) {
00093 
00094   if ( Par::is_indirrect( str ) )
00095     return;
00096 
00097   if ( "" == str )
00098     return;
00099 
00100   size_t ii = 0;
00101   while( yep[ ii ] )
00102     if ( str == yep[ ii++ ] )
00103       return;
00104 
00105   ii = 0;
00106   while( nope[ ii ] )
00107     if ( str == nope[ ii++ ] )
00108       return;
00109 
00110   throw ParFileException( "BoolPar::validate( " + str + " ) : Ivalid arg\n" );
00111 
00112 }

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