CMS 3D CMS Logo

Classes | Public Member Functions | Protected Types | Static Protected Member Functions | Protected Attributes | Friends

ConfigFile Class Reference

#include <ConfigFile.h>

List of all members.

Classes

struct  file_not_found
struct  key_not_found

Public Member Functions

template<class T >
void add (std::string key, const T &value)
 ConfigFile ()
 ConfigFile (std::string filename, std::string delimiter="=", std::string comment="#", std::string sentry="EndConfigFile")
std::string getComment () const
std::string getDelimiter () const
std::string getSentry () const
bool keyExists (const std::string &key) const
template<class T >
T read (const std::string &key, const T &value) const
template<class T >
T read (const std::string &key) const
template<class T >
bool readInto (T &var, const std::string &key) const
template<class T >
bool readInto (T &var, const std::string &key, const T &value) const
void remove (const std::string &key)
std::string setComment (const std::string &s)
std::string setDelimiter (const std::string &s)

Protected Types

typedef std::map< std::string,
std::string >::const_iterator 
mapci
typedef std::map< std::string,
std::string >::iterator 
mapi

Static Protected Member Functions

template<class T >
static T string_as_T (const std::string &s)
template<class T >
static std::string T_as_string (const T &t)
static void trim (std::string &s)

Protected Attributes

std::string myComment
std::map< std::string,
std::string > 
myContents
std::string myDelimiter
std::string mySentry

Friends

std::ostream & operator<< (std::ostream &os, const ConfigFile &cf)
std::istream & operator>> (std::istream &is, ConfigFile &cf)

Detailed Description

Definition at line 52 of file ConfigFile.h.


Member Typedef Documentation

typedef std::map<std::string,std::string>::const_iterator ConfigFile::mapci [protected]

Definition at line 61 of file ConfigFile.h.

typedef std::map<std::string,std::string>::iterator ConfigFile::mapi [protected]

Definition at line 60 of file ConfigFile.h.


Constructor & Destructor Documentation

ConfigFile::ConfigFile ( std::string  filename,
std::string  delimiter = "=",
std::string  comment = "#",
std::string  sentry = "EndConfigFile" 
)

Definition at line 7 of file ConfigFile.cc.

References recoMuon::in.

        : myDelimiter(delimiter), myComment(comment), mySentry(sentry)
{
        // Construct a ConfigFile, getting keys and values from given file
        
        std::ifstream in( filename.c_str() );
        
        if( !in ) throw file_not_found( filename ); 
        
        in >> (*this);
}
ConfigFile::ConfigFile ( )

Definition at line 21 of file ConfigFile.cc.

        : myDelimiter( std::string(1,'=') ), myComment( std::string(1,'#') )
{
        // Construct a ConfigFile without a file; empty
}

Member Function Documentation

template<class T >
void ConfigFile::add ( std::string  key,
const T value 
)

Definition at line 233 of file ConfigFile.h.

References combine::key, myContents, AlCaHLTBitMon_QueryRunRegistry::string, T_as_string(), trim(), and v.

{
        // Add a key with given value
        std::string v = T_as_string( value );
        trim(key);
        trim(v);
        myContents[key] = v;
        return;
}
std::string ConfigFile::getComment ( ) const [inline]

Definition at line 88 of file ConfigFile.h.

References myComment.

{ return myComment; }
std::string ConfigFile::getDelimiter ( ) const [inline]

Definition at line 87 of file ConfigFile.h.

References myDelimiter.

{ return myDelimiter; }
std::string ConfigFile::getSentry ( ) const [inline]

Definition at line 89 of file ConfigFile.h.

References mySentry.

{ return mySentry; }
bool ConfigFile::keyExists ( const std::string &  key) const

Definition at line 36 of file ConfigFile.cc.

References myContents, and AlCaHLTBitMon_ParallelJobs::p.

{
        // Indicate whether key is found
        mapci p = myContents.find( key );
        return ( p != myContents.end() );
}
template<class T >
T ConfigFile::read ( const std::string &  key) const

Definition at line 173 of file ConfigFile.h.

References myContents, and AlCaHLTBitMon_ParallelJobs::p.

Referenced by ManipHist::configBlockDivide(), FitHist::configBlockFit(), CompHist::configBlockHist(), CompHist::configBlockIO(), CalibClosureTest::configBlockSpecific(), and ManipHist::configBlockSum().

{
        // Read the value corresponding to key
        mapci p = myContents.find(key);
        if( p == myContents.end() ) throw key_not_found(key);
        return string_as_T<T>( p->second );
}
template<class T >
T ConfigFile::read ( const std::string &  key,
const T value 
) const

Definition at line 183 of file ConfigFile.h.

References myContents, and AlCaHLTBitMon_ParallelJobs::p.

{
        // Return the value corresponding to key or given default value
        // if key is not found
        mapci p = myContents.find(key);
        if( p == myContents.end() ) return value;
        return string_as_T<T>( p->second );
}
template<class T >
bool ConfigFile::readInto ( T var,
const std::string &  key,
const T value 
) const

Definition at line 217 of file ConfigFile.h.

References newFWLiteAna::found, myContents, AlCaHLTBitMon_ParallelJobs::p, and relativeConstraints::value.

{
        // Get the value corresponding to key and store in var
        // Return true if key is found
        // Otherwise set var to given default
        mapci p = myContents.find(key);
        bool found = ( p != myContents.end() );
        if( found )
                var = string_as_T<T>( p->second );
        else
                var = value;
        return found;
}
template<class T >
bool ConfigFile::readInto ( T var,
const std::string &  key 
) const

Definition at line 204 of file ConfigFile.h.

References newFWLiteAna::found, myContents, and AlCaHLTBitMon_ParallelJobs::p.

{
        // Get the value corresponding to key and store in var
        // Return true if key is found
        // Otherwise leave var untouched
        mapci p = myContents.find(key);
        bool found = ( p != myContents.end() );
        if( found ) var = string_as_T<T>( p->second );
        return found;
}
void ConfigFile::remove ( const std::string &  key)

Definition at line 28 of file ConfigFile.cc.

References myContents.

{
        // Remove key and its value
        myContents.erase( myContents.find( key ) );
        return;
}
std::string ConfigFile::setComment ( const std::string &  s) [inline]

Definition at line 92 of file ConfigFile.h.

References myComment, alignCSCRings::s, and AlCaHLTBitMon_QueryRunRegistry::string.

                { std::string old = myComment;  myComment = s;  return old; }
std::string ConfigFile::setDelimiter ( const std::string &  s) [inline]

Definition at line 90 of file ConfigFile.h.

References myDelimiter, alignCSCRings::s, and AlCaHLTBitMon_QueryRunRegistry::string.

                { std::string old = myDelimiter;  myDelimiter = s;  return old; }  
bool ConfigFile::string_as_T< bool > ( const std::string &  s) [inline, static, protected]

Definition at line 132 of file ConfigFile.h.

References lumiQTWidget::t.

{
        // Convert from a std::string to a T
        // Type T must support >> operator
        T t;
        std::istringstream ist(s);
        ist >> t;
        return t;
}
template<class T >
std::string ConfigFile::T_as_string ( const T t) [static, protected]

Definition at line 120 of file ConfigFile.h.

References lumiQTWidget::t.

Referenced by add().

{
        // Convert from a T to a std::string
        // Type T must support << operator
        std::ostringstream ost;
        ost << t;
        return ost.str();
}
void ConfigFile::trim ( std::string &  s) [static, protected]

Definition at line 45 of file ConfigFile.cc.

Referenced by add(), and operator>>().

{
        // Remove leading and trailing whitespace
        static const char whitespace[] = " \n\t\v\r\f";
        s.erase( 0, s.find_first_not_of(whitespace) );
        s.erase( s.find_last_not_of(whitespace) + 1U );
}

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const ConfigFile cf 
) [friend]

Definition at line 54 of file ConfigFile.cc.

{
        // Save a ConfigFile to os
        for( ConfigFile::mapci p = cf.myContents.begin();
             p != cf.myContents.end();
                 ++p )
        {
                os << p->first << " " << cf.myDelimiter << " ";
                os << p->second << std::endl;
        }
        return os;
}
std::istream& operator>> ( std::istream &  is,
ConfigFile cf 
) [friend]

Definition at line 68 of file ConfigFile.cc.

{
        // Load a ConfigFile from is
        // Read in keys and values, keeping internal whitespace
        typedef std::string::size_type pos;
        const std::string& delim  = cf.myDelimiter;  // separator
        const std::string& comm   = cf.myComment;    // comment
        const std::string& sentry = cf.mySentry;     // end of file sentry
        const pos skip = delim.length();        // length of separator
        
        string nextline = "";  // might need to read ahead to see where value ends
        
        while( is || nextline.length() > 0 )
        {
                // Read an entire line at a time
                string line;
                if( nextline.length() > 0 )
                {
                        line = nextline;  // we read ahead; use it now
                        nextline = "";
                }
                else
                {
                        std::getline( is, line );
                }
                
                // Ignore comments
                line = line.substr( 0, line.find(comm) );
                
                // Check for end of file sentry
                if( sentry != "" && line.find(sentry) != std::string::npos ) return is;
                
                // Parse the line if it contains a delimiter
                pos delimPos = line.find( delim );
                if( delimPos < std::string::npos )
                {
                        // Extract the key
                        string key = line.substr( 0, delimPos );
                        line.replace( 0, delimPos+skip, "" );
                        
                        // See if value continues on the next line
                        // Stop at blank line, next line with a key, end of stream,
                        // or end of file sentry
                        bool terminate = false;
                        while( !terminate && is )
                        {
                                std::getline( is, nextline );
                                terminate = true;
                                
                                string nlcopy = nextline;
                                ConfigFile::trim(nlcopy);
                                if( nlcopy == "" ) continue;
                                
                                nextline = nextline.substr( 0, nextline.find(comm) );
                                if( nextline.find(delim) != std::string::npos )
                                        continue;
                                if( sentry != "" && nextline.find(sentry) != std::string::npos )
                                        continue;
                                
                                nlcopy = nextline;
                                ConfigFile::trim(nlcopy);
                                if( nlcopy != "" ) line += "\n";
                                line += nextline;
                                terminate = false;
                        }
                        
                        // Store key and value
                        ConfigFile::trim(key);
                        ConfigFile::trim(line);
                        cf.myContents[key] = line;  // overwrites if key is repeated
                }
        }
        
        return is;
}

Member Data Documentation

std::string ConfigFile::myComment [protected]

Definition at line 56 of file ConfigFile.h.

Referenced by getComment(), operator>>(), and setComment().

std::map<std::string,std::string> ConfigFile::myContents [protected]

Definition at line 58 of file ConfigFile.h.

Referenced by add(), keyExists(), operator<<(), operator>>(), read(), readInto(), and remove().

std::string ConfigFile::myDelimiter [protected]

Definition at line 55 of file ConfigFile.h.

Referenced by getDelimiter(), operator<<(), operator>>(), and setDelimiter().

std::string ConfigFile::mySentry [protected]

Definition at line 57 of file ConfigFile.h.

Referenced by getSentry(), and operator>>().