LongPar.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00065 if ( is_indirrect( str ) )
00066 return;
00067
00068 char msg[ 256 ];
00069
00070
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
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 }