conversion to LF

This commit is contained in:
James Turk 2005-02-03 20:38:56 +00:00
parent a7567de2df
commit 29f2d3c45c

View File

@ -1,247 +1,250 @@
//This file is part of Photon (http://photon.sourceforge.net) //This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk //Copyright (C) 2004-2005 James Turk
// //
// Author: // Author:
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: ConfigFile.h,v 1.2 2005/01/31 15:44:13 cozman Exp $ // $Id: ConfigFile.h,v 1.3 2005/02/03 20:38:56 cozman Exp $
// //
// Revisions: // Revisions:
// $Log: ConfigFile.h,v $ // $Log: ConfigFile.h,v $
// Revision 1.3 2005/02/03 20:38:56 cozman
// conversion to LF
//
// Revision 1.2 2005/01/31 15:44:13 cozman // Revision 1.2 2005/01/31 15:44:13 cozman
// ConfigFile rewrite // ConfigFile rewrite
// //
// Revision 1.1 2005/01/27 03:35:23 cozman // Revision 1.1 2005/01/27 03:35:23 cozman
// initial import (exceptions,types, and logging,oh my!) // initial import (exceptions,types, and logging,oh my!)
// //
// //
#ifndef PHOTON_UTIL_CONFIGFILE_H #ifndef PHOTON_UTIL_CONFIGFILE_H
#define PHOTON_UTIL_CONFIGFILE_H #define PHOTON_UTIL_CONFIGFILE_H
#include <string> #include <string>
#include <list> #include <list>
#include <sstream> #include <sstream>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
namespace photon namespace photon
{ {
namespace util namespace util
{ {
// Class: ConfigFile // Class: ConfigFile
// ConfigFile class, for reading/writing INI-style config files. // ConfigFile class, for reading/writing INI-style config files.
// //
// File format is fairly flexible, whitespace and comments beginning with # or // File format is fairly flexible, whitespace and comments beginning with # or
// ; are ignored & left intact. // ; are ignored & left intact.
// //
// Use []'s to denote sections. // Use []'s to denote sections.
// //
// Variables are defined in var=val format. // Variables are defined in var=val format.
class ConfigFile class ConfigFile
{ {
public: // types used in ConfigFile public: // types used in ConfigFile
//predicate for search //predicate for search
template<class pairT> template<class pairT>
class StrPairEq class StrPairEq
{ {
public: public:
typedef pairT first_argument_type; typedef pairT first_argument_type;
typedef std::string second_argument_type; typedef std::string second_argument_type;
typedef bool result_type; typedef bool result_type;
bool operator()(const pairT& lhs, const std::string& rhs) const; bool operator()(const pairT& lhs, const std::string& rhs) const;
}; };
typedef std::pair<std::string,std::string> Variable; typedef std::pair<std::string,std::string> Variable;
typedef std::list< Variable > Section; typedef std::list< Variable > Section;
typedef std::pair< std::string, std::list< Variable > > NamedSection; typedef std::pair< std::string, std::list< Variable > > NamedSection;
typedef std::list< NamedSection > Layout; typedef std::list< NamedSection > Layout;
// Group: (Con/De)structors // Group: (Con/De)structors
public: public:
// Function: ConfigFile // Function: ConfigFile
// Default constructor for ConfigFile. // Default constructor for ConfigFile.
ConfigFile(); ConfigFile();
// Function: ConfigFile // Function: ConfigFile
// Constructor for ConfigFile, calls <open>. // Constructor for ConfigFile, calls <open>.
// //
// Parameters: // Parameters:
// filename - Name of ConfigFile to open. // filename - Name of ConfigFile to open.
ConfigFile(const std::string& filename); ConfigFile(const std::string& filename);
// Function: ~ConfigFile // Function: ~ConfigFile
// Calls close upon the ConfigFile. // Calls close upon the ConfigFile.
virtual ~ConfigFile(); virtual ~ConfigFile();
// Group: File Access // Group: File Access
public: public:
// Function: open // Function: open
// open a file, processing it as an INI-like config file. // open a file, processing it as an INI-like config file.
// //
// Parameters: // Parameters:
// filename - Name of ConfigFile to open. // filename - Name of ConfigFile to open.
void open(const std::string& filename); void open(const std::string& filename);
// Function: flush // Function: flush
// Flushes the data written to the config file to disk, generally needs // Flushes the data written to the config file to disk, generally needs
// not be called. // not be called.
void flush(); void flush();
// Function: close // Function: close
// Flushes the data and closes, open must be called again before using same // Flushes the data and closes, open must be called again before using same
// ConfigFile. // ConfigFile.
void close(); void close();
// Function: setVariable // Function: setVariable
// Template function for setting variables in the config file. // Template function for setting variables in the config file.
// //
// WARNING: Do not try to use this with user-defined types, numeric types // WARNING: Do not try to use this with user-defined types, numeric types
// and strings work fine, and this is all that should be contained in // and strings work fine, and this is all that should be contained in
// an INI. // an INI.
// //
// Parameters: // Parameters:
// sec - section name within config file // sec - section name within config file
// var - variable name within section // var - variable name within section
// value - value to set variable equal to // value - value to set variable equal to
template<class varType> template<class varType>
void setVariable(const std::string& sec, void setVariable(const std::string& sec,
const std::string& var, const std::string& var,
varType value); varType value);
// Function: getVariable // Function: getVariable
// Template function for getting values from the config file. Supports // Template function for getting values from the config file. Supports
// returning a default value if the desired variable was not found. // returning a default value if the desired variable was not found.
// //
// WARNING: Do not try to use this with user-defined types, numeric types // WARNING: Do not try to use this with user-defined types, numeric types
// and strings work fine, and this is all that should be contained in // and strings work fine, and this is all that should be contained in
// an INI. // an INI.
// //
// Parameters: // Parameters:
// sec - section name within config file // sec - section name within config file
// var - variable name within section // var - variable name within section
// defVal - value to return if variable does not exist // defVal - value to return if variable does not exist
// //
// Returns: // Returns:
// Value of variable within config file or defVal if value was not found. // Value of variable within config file or defVal if value was not found.
template<class varType> template<class varType>
varType getVariable(const std::string& sec, varType getVariable(const std::string& sec,
const std::string& var, const std::string& var,
varType defVal) const; varType defVal) const;
private: private:
static std::string cleanString(const std::string& str); static std::string cleanString(const std::string& str);
static std::string bracketString(const std::string& str); static std::string bracketString(const std::string& str);
private: private:
Layout layout_; Layout layout_;
std::string filename_; std::string filename_;
}; };
//predicate for search //predicate for search
template<class pairT> template<class pairT>
bool ConfigFile::StrPairEq<pairT>::operator()(const pairT& lhs, bool ConfigFile::StrPairEq<pairT>::operator()(const pairT& lhs,
const std::string& rhs) const const std::string& rhs) const
{ {
return ConfigFile::cleanString(lhs.first) == ConfigFile::cleanString(rhs); return ConfigFile::cleanString(lhs.first) == ConfigFile::cleanString(rhs);
} }
//Template implementation //Template implementation
template<class varType> template<class varType>
void void
ConfigFile::setVariable(const std::string& sec, ConfigFile::setVariable(const std::string& sec,
const std::string& var, const std::string& var,
varType value) varType value)
{ {
std::string secBrac(bracketString(sec)); std::string secBrac(bracketString(sec));
Layout::iterator secIter; Layout::iterator secIter;
Section::iterator varIter; Section::iterator varIter;
std::ostringstream ss; std::ostringstream ss;
ss << value; //write value to string ss << value; //write value to string
//search for section //search for section
secIter = std::find_if( layout_.begin(), secIter = std::find_if( layout_.begin(),
layout_.end(), layout_.end(),
std::bind2nd(StrPairEq<NamedSection>(), secBrac) ); std::bind2nd(StrPairEq<NamedSection>(), secBrac) );
// add the section if it does not exist // add the section if it does not exist
if(secIter == layout_.end()) if(secIter == layout_.end())
{ {
layout_.push_back( NamedSection( secBrac, Section() ) ); layout_.push_back( NamedSection( secBrac, Section() ) );
//search again, assert that it now exists //search again, assert that it now exists
secIter = std::find_if( layout_.begin(), secIter = std::find_if( layout_.begin(),
layout_.end(), layout_.end(),
std::bind2nd(StrPairEq<NamedSection>(), secBrac) ); std::bind2nd(StrPairEq<NamedSection>(), secBrac) );
assert(secIter != layout_.end()); assert(secIter != layout_.end());
} }
//search for variable //search for variable
varIter = std::find_if( secIter->second.begin(), varIter = std::find_if( secIter->second.begin(),
secIter->second.end(), secIter->second.end(),
std::bind2nd(StrPairEq<Variable>(), var) ); std::bind2nd(StrPairEq<Variable>(), var) );
// add the variable if it does not exist // add the variable if it does not exist
if(varIter == secIter->second.end()) if(varIter == secIter->second.end())
{ {
secIter->second.push_back( Variable(var, ss.str()) ); secIter->second.push_back( Variable(var, ss.str()) );
} }
else else
{ {
varIter->second = ss.str(); varIter->second = ss.str();
} }
} }
//template specialization for setVariable<std::string> //template specialization for setVariable<std::string>
/*template<> /*template<>
void void
ConfigFile::setVariable(std::string sec, std::string var, std::string value) ConfigFile::setVariable(std::string sec, std::string var, std::string value)
{ {
sec = "[" + sec + "]"; //add []s to section name sec = "[" + sec + "]"; //add []s to section name
value = "\"" + value + "\""; //add ""s to value value = "\"" + value + "\""; //add ""s to value
layout_[sec][var] = value; //actually set it layout_[sec][var] = value; //actually set it
}*/ }*/
template<class varType> template<class varType>
varType varType
ConfigFile::getVariable(const std::string& sec, ConfigFile::getVariable(const std::string& sec,
const std::string& var, const std::string& var,
varType defVal) const varType defVal) const
{ {
std::string secBrac(bracketString(sec)); std::string secBrac(bracketString(sec));
std::stringstream ss; std::stringstream ss;
varType ret(defVal); varType ret(defVal);
Layout::const_iterator secIter; Layout::const_iterator secIter;
Section::const_iterator varIter; Section::const_iterator varIter;
secIter = std::find_if( layout_.begin(), secIter = std::find_if( layout_.begin(),
layout_.end(), layout_.end(),
std::bind2nd(StrPairEq<NamedSection>(), secBrac) ); std::bind2nd(StrPairEq<NamedSection>(), secBrac) );
if(secIter != layout_.end()) if(secIter != layout_.end())
{ {
varIter = std::find_if( secIter->second.begin(), varIter = std::find_if( secIter->second.begin(),
secIter->second.end(), secIter->second.end(),
std::bind2nd(StrPairEq<Variable>(), var) ); std::bind2nd(StrPairEq<Variable>(), var) );
if(varIter != secIter->second.end()) if(varIter != secIter->second.end())
{ {
ss.str(varIter->second); ss.str(varIter->second);
ss >> ret; ss >> ret;
} }
} }
return ret; return ret;
} }
} }
} }
#endif //PHOTON_UTIL_CONFIGFILE_H #endif //PHOTON_UTIL_CONFIGFILE_H