StringPar.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 #include <suplib/str.h>
00027 
00028 #include "StringPar.h"
00029 
00030 StringPar::StringPar( ParTxt& par ) throw ( ParFileException ) : Par( par ) {
00031 
00032   try {
00033 
00034     //
00035     // The classes BoolPar, LongPar, RealPar must be validated
00036     // but a string can be anything so there is no need to validate.
00037     //
00038 
00039     between_limits( parameter[ PARVALUE ] );
00040 
00041   } catch ( ParFileException& pfe ) {
00042     throw;
00043   }
00044 
00045 }
00046 
00047 void StringPar::between_limits( const string& str ) const
00048   throw ( ParFileException, Exception ) {
00049 
00050     // if the value field has an indirrection then do not check for limits
00051     if ( Par::is_indirrect( str ) )
00052       return;
00053 
00054     if ( "" != parameter[ PARMINIMUM ] ) {
00055 
00056       if ( string::npos != parameter[ PARMINIMUM ].find( str ) ) {
00057         char* ptr = (char*) parameter[ PARMINIMUM ].c_str( );
00058         int restore = 1;
00059         char** buffer_argv = NULL;
00060         size_t num_tokens = str_tokenize( ptr, "|", &buffer_argv, 0, restore );
00061         for ( int ii = 0; ii < num_tokens; ii++ ) {
00062           if ( 0 == str.compare( buffer_argv[ ii ] ) ) {
00063             str_tokenize_free( buffer_argv, restore );
00064             return;
00065           }
00066         }
00067         str_tokenize_free( buffer_argv, restore );
00068       } 
00069 
00070       {
00071         char tmp[ 256 ];
00072         
00073         const char* format =
00074           "StringPar::between_limits( %s ) : For parameter `%s', "
00075           "the value `%s' must be one of `";
00076 
00077         sprintf( tmp, format, str.c_str( ), parameter[ PARNAME ].c_str( ),
00078                  str.c_str( ) ); 
00079 
00080         // since parameter[ PARMINIMUM ] may contain %
00081         string msg( tmp );
00082 
00083         msg += parameter[ PARMINIMUM ];
00084         msg += "'\n";
00085 
00086         throw ParFileException( msg );
00087 
00088       }
00089 
00090     }
00091 
00092 }
00093 
00094 void StringPar::print( ostream& os ) const {
00095 
00096   for ( int i = 0; i < Par::NUMTOKENS; i++ ) {
00097     if ( i == Par::PARMODE )
00098       os << '"';
00099     os << parameter[ i ];
00100     if ( i == Par::PARMODE )
00101       os << '"';
00102     if ( i != Par::NUMTOKENS - 1 )
00103       os << char( DELIMIT );
00104   }
00105 
00106 }
00107 
00108 void StringPar::set_val( const string& str ) throw ( ParFileException ,
00109                                                      Exception ) {
00110 
00111   try {
00112 
00113     between_limits( str );
00114     parameter[ PARVALUE ] = str;
00115 
00116   } catch ( ParFileException& pfe ) {
00117 
00118     throw;
00119 
00120   } catch ( Exception& e ) {
00121 
00122     throw;
00123 
00124   } 
00125 
00126 }
00127 

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