CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/Validation/RecoJets/interface/ConfigFile.h

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