Par.h

00001 #ifndef PAR_H
00002 #define PAR_H
00003 
00004 // --8<--8<--8<--8<--
00005 //
00006 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00007 //
00008 // This file is part of paramxx
00009 //
00010 // paramxx is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public License
00012 // as published by the Free Software Foundation; either version 2
00013 // of the License, or (at your option) any later version.
00014 //
00015 // paramxx is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the 
00022 //       Free Software Foundation, Inc. 
00023 //       51 Franklin Street, Fifth Floor
00024 //       Boston, MA  02110-1301, USA
00025 //
00026 // -->8-->8-->8-->8--
00027 
00028 #include <iostream>
00029 #include <string>
00030 #include <cstring>
00031 #include <vector>
00032 
00033 using namespace std;
00034 
00035 #include "ParFileException.h"
00036 #include "ParTxt.h"
00037 
00041 class Par {
00042 
00046   friend ostream& operator << ( ostream& os, Par& par ) {
00047     par.print( os );
00048     return os;
00049   }
00050 
00054   friend ostream& operator << ( ostream& os, Par* par ) {
00055     par->print( os );
00056     return os;
00057   }
00058   
00059 public:
00060 
00064   enum ParFileDelimit { DELIMIT=',' };
00065 
00069   enum ParType { PARNAME,    
00070                  PARTYPE,    
00071                  PARMODE,    
00072                  PARVALUE,   
00073                  PARMINIMUM, 
00074                  PARMAXIMUM, 
00075                  PARPROMPT   
00076   };
00077 
00078   enum ParamMode { AUTOS='a', BATCH='b', hIDDEN='h',
00079                    HIDDEN='H', QUERRY='q', LEARN='l' };
00080 
00081   enum ParamType { BOOLEAN='b', COMMENT='c', INTEGER='i', 
00082                    REAL='r',  STRING='s' };
00083 
00084   enum NumTokens { NUMTOKENS=7 };
00085 
00089   virtual ~Par( void ) { };
00090 
00094   Par( void ) { }
00095 
00096   Par( ParTxt& par );
00097 
00098   int check_value( const char* str ) const;
00099 
00105   virtual void plist( ostream& os ) const;
00106 
00110   string get_name( void ) const { return parameter[ PARNAME ]; }
00111 
00115   string get_mode( void ) const { return parameter[ PARMODE ]; }
00116 
00120   string get_prompt( void ) const { return parameter[ PARPROMPT ]; }
00121   
00122   string get_value( void ) const { return parameter[ PARVALUE ]; }
00123   
00124   static int my_tokenize( char* str, char* delimit, char*** tokens )
00125     throw ( Exception );
00126   static void delete_tokens( char** ptr );
00127 
00132   virtual bool pgetb( void ) const throw ( ParFileException );
00134 
00139   virtual double pgetd( void ) const throw ( ParFileException );
00141 
00142   virtual int pgeti( void ) const throw ( ParFileException );
00144   
00145   virtual long pgetl( void ) const throw ( ParFileException );
00147   
00148   virtual void pgetstr( char result[], size_t size ) const
00149         throw ( ParFileException );
00151 
00152   virtual string pgetstring( void ) const throw ( ParFileException );
00154 
00158   virtual void print( ostream& os ) const;
00159 
00160   virtual void set_val( const string& str )
00161     throw ( ParFileException, Exception );
00162 
00163   static bool is_indirrect( const string& str, char delimit=')' );
00164 
00165 protected:
00166 
00167   vector< string > parameter;
00168 
00169   void not_between_limits( char str[], 
00170                            const char* left, const string& left_val,
00171                            const char* right, const string& right_val ) const;
00172 
00173 private:
00174 
00175   static int count_tokens( char* str, char* delimit ) throw ( Exception );
00176   static void print_tokens( char* str, char** str_argv, ostream& os );
00177   static char* save_string( char* str ) throw ( Exception );
00178 
00183   virtual void between_limits( const string& str ) const
00184     throw ( ParFileException, Exception );
00185 
00190   void pget_wrong_type( char[], const char* ) const;
00191 
00192   virtual void validate( const string& ) const throw ( ParFileException );
00194 
00195 };
00196 
00197 #endif

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