Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef CONFIG_H
00053 #define CONFIG_H
00054
00060
00061 static const char header_config_rcsid[] = "$Id: config.h,v 1.18 2006/05/16 19:24:26 gourdeau Exp $";
00062
00063
00064 #ifdef _MSC_VER // Microsoft
00065 #pragma warning (disable:4786) // Disable decorated name truncation warnings
00066 #pragma warning (disable:4503) // Disable decorated name truncation warnings
00067 #endif
00068 #include <iostream>
00069 #include <string>
00070 #include <iomanip>
00071 #include <fstream>
00072
00073 #include <boost/lexical_cast.hpp>
00074
00075 #include <sstream>
00076 #include <vector>
00077
00078
00080 #define CAN_NOT_OPEN_FILE -1
00081
00083 #define CAN_NOT_CREATE_FILE -2
00084
00085
00087 typedef struct Data{
00088 std::string section;
00089 std::string parameter;
00090 std::string value;
00091 } Data;
00092
00094 typedef std::vector< Data > Conf_data;
00095
00097 class Config {
00098
00099 public:
00100 Config(const bool bPrintErrorMessages = true);
00101 short read_conf(std::ifstream & inconffile);
00102 void clear();
00103 void print();
00104
00105 bool section_exists(const std::string & section) const;
00106 bool parameter_exists(const std::string & section, const std::string & parameter) const;
00107
00108
00109 template<typename T> bool select(const std::string & section, const std::string & parameter,
00110 T & value) const
00115 {
00116 for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
00117 {
00118 if( (iter->section == section) && (iter->parameter == parameter) )
00119 {
00120 try
00121 {
00122 value = boost::lexical_cast<T>(iter->value);
00123 }
00124 catch (boost::bad_lexical_cast & e)
00125 {
00126 return false;
00127 }
00128 return true;
00129 }
00130 }
00131 return false;
00132 }
00133
00134 short write_conf(std::ofstream & outconffile, const std::string & file_title,
00135 const int space_between_column);
00136
00137 template <typename T> bool add(const std::string & section, const std::string & parameter,
00138 const T & value)
00144 {
00145 Data dataSet;
00146 dataSet.section = section;
00147 dataSet.parameter = parameter;
00148 try
00149 {
00150 dataSet.value = boost::lexical_cast<std::string>(value);
00151 }
00152 catch (boost::bad_lexical_cast & e)
00153 {
00154 return false;
00155 }
00156
00157 for(Conf_data::iterator iterConf = conf.begin(); iterConf != conf.end(); ++iterConf)
00158 {
00159 if(section == iterConf->section)
00160 {
00161 if(parameter == iterConf->parameter)
00162 {
00163 try
00164 {
00165 iterConf->value = boost::lexical_cast<std::string>(value);
00166 }
00167 catch (boost::bad_lexical_cast & e)
00168 {
00169 return false;
00170 }
00171 }
00172
00173 for(Conf_data::iterator iterConf2 = iterConf; iterConf2 != conf.end(); ++iterConf2)
00174 {
00175 if(section != iterConf2->section)
00176 {
00177 conf.insert(iterConf2, dataSet);
00178 return true;
00179 }
00180 }
00181 }
00182 }
00183
00184 conf.push_back(dataSet);
00185 return true;
00186 }
00187
00188 private:
00189 Conf_data conf;
00190 bool bPrintErrorMessages;
00191 };
00192
00193 #endif
00194