• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

/Users/jmonk/Physics/ForIA/ForIA/ConfigurationFile.hh

Go to the documentation of this file.
00001 // ConfigFile.hh
00002 // Class for reading named values from configuration files
00003 // Richard J. Wagner  v2.1  24 May 2004  wagnerr@umich.edu
00004 
00005 // Copyright (c) 2004 Richard J. Wagner
00006 // 
00007 // Permission is hereby granted, free of charge, to any person obtaining a copy
00008 // of this software and associated documentation files (the "Software"), to
00009 // deal in the Software without restriction, including without limitation the
00010 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00011 // sell copies of the Software, and to permit persons to whom the Software is
00012 // furnished to do so, subject to the following conditions:
00013 // 
00014 // The above copyright notice and this permission notice shall be included in
00015 // all copies or substantial portions of the Software.
00016 // 
00017 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00023 // IN THE SOFTWARE.
00024 
00025 // Typical usage
00026 // -------------
00027 // 
00028 // Given a configuration file "settings.inp":
00029 //   atoms  = 25
00030 //   length = 8.0  # nanometers
00031 //   name = Reece Surcher
00032 // 
00033 // Named values are read in various ways, with or without default values:
00034 //   ConfigFile config( "settings.inp" );
00035 //   int atoms = config.read<int>( "atoms" );
00036 //   double length = config.read( "length", 10.0 );
00037 //   string author, title;
00038 //   config.readInto( author, "name" );
00039 //   config.readInto( title, "title", string("Untitled") );
00040 // 
00041 // See file example.cpp for more examples.
00042 //
00043 // Robindra Prabhu: code snippet adapted from: http://www-personal.umich.edu/~wagnerr/ConfigFile.html
00044 
00045 #ifndef FORIA_CONFIGURATIONFILE_HH
00046 #define FORIA_CONFIGURATIONFILE_HH
00047 
00048 #include <string>
00049 #include <map>
00050 #include <iostream>
00051 #include <fstream>
00052 #include <sstream>
00053 
00054 using std::string;
00055 
00056 namespace ForIA{
00057   
00065   class ConfigurationFile {
00066     // Data
00067   protected:
00068     string myDelimiter;  // separator between key and value
00069     string myComment;    // separator between value and comments
00070     string mySentry;     // optional string to signal end of file
00071     std::map<string,string> myContents;  // extracted keys and values
00072     
00073     typedef std::map<string,string>::iterator mapi;
00074     typedef std::map<string,string>::const_iterator mapci;
00075     
00076     // Methods
00077   public:
00078     /*
00079     ConfigurationFile( string filename,
00080                        string delimiter = "=",
00081                        string comment = "#",
00082                        string sentry = "EndConfigurationFile" );
00083      */
00084     ConfigurationFile();
00085     
00086     void init(const string &filename, const string &delimiter = "=", 
00087               const string &comment = "#", const string &sentry= "EndConfigurationFile");
00088         
00089     // Search for key and read value or optional default value
00090     template<class T> T read( const string& key ) const;  // call as read<T>
00091     template<class T> T read( const string& key, const T& value ) const;
00092     template<class T> bool readInto( T& var, const string& key ) const;
00093     template<class T>
00094     bool readInto( T& var, const string& key, const T& value ) const;
00095     
00096     // Modify keys and values
00097     template<class T> void add( string key, const T& value );
00098     void remove( const string& key );
00099     
00100     // Check whether key exists in configuration
00101     bool keyExists( const string& key ) const;
00102     
00103     // Check or change configuration syntax
00104     string getDelimiter() const { return myDelimiter; }
00105     string getComment() const { return myComment; }
00106     string getSentry() const { return mySentry; }
00107     string setDelimiter( const string& s )
00108     { string old = myDelimiter;  myDelimiter = s;  return old; }  
00109     string setComment( const string& s )
00110     { string old = myComment;  myComment = s;  return old; }
00111     
00112     // Write or read configuration
00113     friend std::ostream& operator<<( std::ostream& os, const ConfigurationFile& cf );
00114     friend std::istream& operator>>( std::istream& is, ConfigurationFile& cf );
00115     
00116   protected:
00117     template<class T> static string T_as_string( const T& t );
00118     template<class T> static T string_as_T( const string& s );
00119     static void trim( string& s );
00120     
00121     
00122     // Exception types
00123   public:
00124     struct file_not_found {
00125       string filename;
00126       file_not_found( const string& filename_ = string() )
00127         : filename(filename_) {} };
00128     struct key_not_found {  // thrown only by T read(key) variant of read()
00129       string key;
00130       key_not_found( const string& key_ = string() )
00131         : key(key_) {} };
00132   };
00133   
00134   
00135   /* static */
00136   template<class T>
00137   string ConfigurationFile::T_as_string( const T& t )
00138   {
00139     // Convert from a T to a string
00140     // Type T must support << operator
00141     std::ostringstream ost;
00142     ost << t;
00143     return ost.str();
00144   }
00145   
00146   
00147   /* static */
00148   template<class T>
00149   T ConfigurationFile::string_as_T( const string& s )
00150   {
00151     // Convert from a string to a T
00152     // Type T must support >> operator
00153     T t;
00154     std::istringstream ist(s);
00155     ist >> t;
00156     return t;
00157   }
00158   
00159   
00160   /* static */
00161   template<>
00162   inline string ConfigurationFile::string_as_T<string>( const string& s )
00163   {
00164     // Convert from a string to a string
00165     // In other words, do nothing
00166     return s;
00167   }
00168   
00169   
00170   /* static */
00171   template<>
00172   inline bool ConfigurationFile::string_as_T<bool>( const string& s )
00173   {
00174     // Convert from a string to a bool
00175     // Interpret "false", "F", "no", "n", "0" as false
00176     // Interpret "true", "T", "yes", "y", "1", "-1", or anything else as true
00177     bool b = true;
00178     string sup = s;
00179     for( string::iterator p = sup.begin(); p != sup.end(); ++p )
00180       *p = toupper(*p);  // make string all caps
00181     if( sup==string("FALSE") || sup==string("F") ||
00182         sup==string("NO") || sup==string("N") ||
00183         sup==string("0") || sup==string("NONE") )
00184       b = false;
00185     return b;
00186   }
00187   
00188   
00189   template<class T>
00190   T ConfigurationFile::read( const string& key ) const
00191   {
00192     // Read the value corresponding to key
00193     mapci p = myContents.find(key);
00194     if( p == myContents.end() ) throw key_not_found(key);
00195     return string_as_T<T>( p->second );
00196   }
00197   
00198   
00199   template<class T>
00200   T ConfigurationFile::read( const string& key, const T& value ) const
00201   {
00202     // Return the value corresponding to key or given default value
00203     // if key is not found
00204     mapci p = myContents.find(key);
00205     if( p == myContents.end() ) return value;
00206     return string_as_T<T>( p->second );
00207   }
00208   
00209   
00210   template<class T>
00211   bool ConfigurationFile::readInto( T& var, const string& key ) const
00212   {
00213     // Get the value corresponding to key and store in var
00214     // Return true if key is found
00215     // Otherwise leave var untouched
00216     mapci p = myContents.find(key);
00217     bool found = ( p != myContents.end() );
00218     if( found ) var = string_as_T<T>( p->second );
00219     return found;
00220   }
00221   
00222   
00223   template<class T>
00224   bool ConfigurationFile::readInto( T& var, const string& key, const T& value ) const
00225   {
00226     // Get the value corresponding to key and store in var
00227     // Return true if key is found
00228     // Otherwise set var to given default
00229     mapci p = myContents.find(key);
00230     bool found = ( p != myContents.end() );
00231     if( found )
00232       var = string_as_T<T>( p->second );
00233     else
00234       var = value;
00235     return found;
00236   }
00237   
00238   
00239   template<class T>
00240   void ConfigurationFile::add( string key, const T& value )
00241   {
00242     // Add a key with given value
00243     string v = T_as_string( value );
00244     trim(key);
00245     trim(v);
00246     myContents[key] = v;
00247     return;
00248   }
00249 }
00250 #endif  //FORIA_CONFIGURATIONFILE_HH
00251 
00252 // Release notes:
00253 // v1.0  21 May 1999
00254 //   + First release
00255 //   + Template read() access only through non-member readConfigFile()
00256 //   + ConfigurationFileBool is only built-in helper class
00257 // 
00258 // v2.0  3 May 2002
00259 //   + Shortened name from ConfigurationFile to ConfigFile
00260 //   + Implemented template member functions
00261 //   + Changed default comment separator from % to #
00262 //   + Enabled reading of multiple-line values
00263 // 
00264 // v2.1  24 May 2004
00265 //   + Made template specializations inline to avoid compiler-dependent linkage
00266 //   + Allowed comments within multiple-line values
00267 //   + Enabled blank line termination for multiple-line values
00268 //   + Added optional sentry to detect end of configuration file
00269 //   + Rewrote messy trimWhitespace() function as elegant trim()

Generated on Mon Jul 30 2012 16:56:35 for ForIA by  doxygen 1.7.2