LongPar.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 <limits.h>
00028 
00029 #include <suplibxx/str.h>
00030 
00031 #include "LongPar.h"
00032 #include "ParFile.h"
00033 
00034 LongPar::LongPar( ParTxt& par ) throw ( ParFileException, Exception ) :
00035   Par( par ) {
00036 
00037     try {
00038 
00039       validate( parameter[ PARMINIMUM ] );
00040   
00041       validate( parameter[ PARMAXIMUM ] );
00042   
00043       validate( parameter[ PARVALUE ] );
00044 
00045       between_limits( parameter[ PARVALUE ] );
00046 
00047     } catch( ParFileException& pfe ) {
00048 
00049       throw;
00050 
00051     } catch( Exception& e ) {
00052 
00053       throw;
00054 
00055     }
00056 
00057 }
00058 
00059 void LongPar::between_limits( const string& str ) const
00060   throw ( ParFileException, Exception ) {
00061 
00062   try {
00063 
00064     // if the value field has an indirrection then do not check for limits
00065     if ( is_indirrect( str ) )
00066       return;
00067 
00068     char msg[ 256 ];
00069 
00070     // if a minimum value was entered by user then make sure that min <= val
00071     if ( "" != parameter[ PARMINIMUM ] ) {
00072       long min =
00073         suplib::str2l( (const char*) parameter[ PARMINIMUM ].c_str( ) );
00074       long val = suplib::str2l( (const char*) str.c_str( ) );
00075       if ( val < min ) {
00076         not_between_limits( msg, "min", parameter[ PARMINIMUM ], "val", str );
00077         throw ParFileException( msg );
00078       }
00079     }
00080 
00081     // if a minimum value was entered by user then make sure that val <= max
00082     if ( "" !=  parameter[ PARMAXIMUM ] ) {
00083       long max =
00084         suplib::str2l( (const char*) parameter[ PARMAXIMUM ].c_str( ) );
00085       long val = suplib::str2l( (const char*) str.c_str( ) );
00086       if ( val > max ) {
00087         not_between_limits( msg, "val", str, "max", parameter[ PARMAXIMUM ] );
00088         throw ParFileException( msg );
00089       }
00090     }
00091 
00092   } catch( ParFileException& pfe ) {
00093     throw;
00094   } catch( Exception& e ) {
00095     throw;
00096   }
00097 
00098 }
00099 
00100 int LongPar::pgeti( ) const throw ( ParFileException ) {
00101 
00102   long lresult = atol( parameter[ PARVALUE ].c_str( ) );
00103 
00104   char msg[ 256 ];
00105   if ( lresult > INT_MAX ) {
00106     const char* format =
00107       "LongPar::pgeti( ) : Parameter %s (%s) > INT_MAX (%d)\n";
00108     sprintf( msg, format, parameter[ PARNAME ].c_str( ),
00109              parameter[ PARVALUE ].c_str( ), INT_MAX );
00110     throw ParFileException( msg );
00111   }
00112 
00113   if ( lresult < INT_MIN ) {
00114     const char* format =
00115       "LongPar::pgeti( ) : Parameter %s (%s) < INT_MIN (%d)\n";
00116     sprintf( msg, format, parameter[ PARNAME ].c_str( ),
00117              parameter[ PARVALUE ].c_str( ), INT_MIN );
00118     throw ParFileException( msg );
00119   }
00120 
00121   return int( lresult );
00122 
00123 }
00124 
00125 void LongPar::set_val( const string& str )
00126   throw ( ParFileException, Exception ) {
00127 
00128     try {
00129 
00130       string tmp( str );
00131 
00132       validate( tmp );
00133 
00134       between_limits( tmp );
00135 
00136       parameter[ PARVALUE ] = str;
00137 
00138     } catch ( ParFileException& pfe ) {
00139 
00140       throw;
00141 
00142     } catch ( Exception& e ) {
00143 
00144       throw;
00145 
00146     }
00147 
00148 }
00149 
00150 void LongPar::validate( const string& str ) const throw ( ParFileException ) {
00151 
00152   try {
00153 
00154     if ( "" == str )
00155       return;
00156 
00157     if ( Par::is_indirrect( str ) )
00158       return;
00159 
00160     const char* ptr = (const char*) str.c_str( );
00161 
00162     long result = suplib::str2l( ptr );
00163 
00164   } catch( Exception& e ) {
00165 
00166     string msg = e.get_message( );
00167     throw ParFileException( msg );
00168 
00169   }
00170 
00171 }

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