RealPar.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 <suplibxx/str.h>
00028 
00029 #include "RealPar.h"
00030 #include "ParFile.h"
00031 
00032 RealPar::RealPar( ParTxt& par ) throw ( ParFileException, Exception ) :
00033   Par( par ) {
00034 
00035     try {
00036 
00037       validate( parameter[ PARMINIMUM ] );
00038   
00039       validate( parameter[ PARMAXIMUM ] );
00040   
00041       validate( parameter[ PARVALUE ] );
00042 
00043       between_limits( parameter[ PARVALUE ] );
00044 
00045     } catch( ParFileException& pfe ) {
00046 
00047       throw;
00048 
00049     } catch( Exception& e ) {
00050 
00051       throw;
00052 
00053     }
00054 
00055 }
00056 
00057 void RealPar::between_limits( const string& str ) const
00058   throw ( ParFileException, Exception ) {
00059 
00060     try {
00061 
00062       // if the value field has an indirrection then do not check for limits
00063       if ( is_indirrect( str ) )
00064         return;
00065 
00066       char msg[ 256 ];
00067 
00068       // if a minimum value was entered by user then make sure that min <= val
00069       if ( "" != parameter[ PARMINIMUM ] ) {
00070         double min =
00071           suplib::str2d( (const char*) parameter[ PARMINIMUM ].c_str( ) );
00072         double val = suplib::str2d( (const char*) str.c_str( ) );
00073         if ( val < min ) {
00074           not_between_limits( msg, "min", parameter[ PARMINIMUM ], "val",
00075                               str );
00076           throw ParFileException( msg );
00077         }
00078       }
00079 
00080       // if a minimum value was entered by user then make sure that val <= max
00081       if ( "" !=  parameter[ PARMAXIMUM ] ) {
00082         double max =
00083           suplib::str2d( (const char*) parameter[ PARMAXIMUM ].c_str( ) );
00084         double val = suplib::str2d( (const char*) str.c_str( ) );
00085         if ( val > max ) {
00086           not_between_limits( msg, "val", str, "max",
00087                               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 void RealPar::set_val( const string& str )
00101   throw ( ParFileException, Exception ) {
00102 
00103     try {
00104 
00105       string tmp( str );
00106 
00107       validate( tmp );
00108 
00109       between_limits( tmp );
00110 
00111       parameter[ PARVALUE ] = str;
00112 
00113     } catch ( ParFileException& pfe ) {
00114 
00115       throw;
00116 
00117     } catch( Exception& e ) {
00118 
00119       throw;
00120 
00121     }
00122 
00123 }
00124 
00125 void RealPar::validate( const string& str ) const throw ( ParFileException ) {
00126 
00127   try {
00128 
00129     if ( "" == str )
00130       return;
00131 
00132     if ( Par::is_indirrect( str ) )
00133       return;
00134 
00135     const char* ptr = (const char*) str.c_str( );
00136 
00137     double result = suplib::str2d( ptr );
00138 
00139   } catch( Exception& e ) {
00140 
00141     string msg = e.get_message( );
00142     throw ParFileException( msg );
00143 
00144   }
00145 
00146 }

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