ConfigFile.h

Go to the documentation of this file.
00001 // Contents: Definition of ConfigFile class
00002 //
00003 // Principal Author:   Alexei Miasnikov
00004 // Copyright (C) 2005
00005 //
00006 // Status: in progress
00007 //
00008 // Revision History:
00009 //
00010 
00011 
00012 #ifndef _WSET_CONFIG_H_
00013 #define _WSET_CONFIG_H_
00014 
00015 #include <string>
00016 #include <sstream>
00017 #include <iostream>
00018 #include <map>
00019 
00020 using namespace std;
00021 
00023 struct ltstr
00024 {
00025   bool operator()(const string& s1, const string& s2) const
00026   {
00027     return strcmp(s1.c_str(), s2.c_str()) < 0;
00028   }
00029 };
00030 
00031 
00033 
00037 class Value
00038 {
00039 public:
00041   Value() {}
00043 
00046   Value( const char* v) : theValue(v) { }       
00048 
00051   Value( int n ) {
00052     stringstream s;
00053     s << n << '\0' << flush;
00054     theValue = string(s.str());
00055   }
00056   
00058 
00061   operator int( ) const { return atoi(theValue.c_str()); }
00063 
00066   operator double( ) const { return atof(theValue.c_str()); }
00068 
00071   operator string( ) const { return theValue; }
00073   friend istream& operator >> (istream& in, Value& v){
00074     in >> v.theValue;
00075     return in;
00076   }
00077   
00078 private:
00079   string theValue;
00080 };
00081 
00082 
00084 typedef map<string, Value, ltstr> ParameterType;
00085 
00086 
00087 //------------------------------ ConfigFile ----------------------------------//
00088 
00089 
00091 
00110 class ConfigFile
00111 {
00112   
00113 public:
00114   
00116   //                                                                     //
00117   // Constructors:                                                       //
00118   //                                                                     //
00120   
00122   ConfigFile();
00124 
00129   ConfigFile(const string& f_name);
00130   
00131   // copy constructor supplied by compiler.
00132   // destructor supplied by compiler.
00133   
00134   
00136   //                                                                     //
00137   // Accessors/Modifiers:                                                          //
00138   //                                                                     //
00140   
00142 
00149   void setVariable( const char* p_name, const Value& p_value );
00150   // used by readFrom() to set variables
00151   
00152   
00154 
00163   const Value& getValue(const char* p_name);
00164   // returns value of the parameter
00165 
00167   //                                                                     //
00168   // OI:                                                                 //
00169   //                                                                     //
00171   
00172   // assignment operator supplied by compiler
00173 
00175 
00178   friend ostream& operator << ( ostream& ostr, const ConfigFile& C )
00179   {
00180     C.printOn(ostr);
00181     return ostr;
00182   }
00183     
00184 
00185 private:
00186 
00188   //                                                                     //
00189   // Private functions:                                                  //
00190   //                                                                     //
00192 
00193   void readFrom( istream& istr );
00194 
00195   void printOn( ostream& ostr ) const;
00196 
00197   void rtrim(char* ch);
00198 
00200   //                                                                     //
00201   // Data members:                                                       //
00202   //                                                                     //
00204 
00205   ParameterType parameters;
00206 
00207   
00208 };
00209 #endif

Generated on Mon Feb 27 22:47:04 2006 for CRyptography And Groups (CRAG) by  doxygen 1.4.4