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
00058
00059 static const char rcsid[] = "$Id: config.cpp,v 1.20 2006/05/16 19:24:26 gourdeau Exp $";
00060
00061
00062 #include "config.h"
00063
00064 using namespace std;
00065
00066 Config::Config(const bool bPrintErrorMessages_)
00067
00068 {
00069 bPrintErrorMessages = bPrintErrorMessages_;
00070 }
00071
00072 short Config::read_conf(ifstream & inconffile)
00089 {
00090
00091
00092 if(inconffile)
00093 {
00094 string temp;
00095 unsigned int tmpPos;
00096 Data data;
00097 getline(inconffile, temp);
00098
00099 while( !inconffile.eof() )
00100 {
00101
00102 if(temp.substr(0,1) != "#")
00103 {
00104
00105 if(temp.substr(0,1) == "[")
00106 {
00107
00108 tmpPos = temp.find("]");
00109 if (tmpPos != string::npos)
00110 {
00111 data.section = temp.substr(1, tmpPos-1);
00112
00113 getline(inconffile, temp);
00114
00115 while( (temp.substr(0,1) != "[") &&
00116 (!inconffile.eof()) )
00117 {
00118 if(temp.substr(0,1) != "#")
00119 {
00120 if(temp.find(":") != string::npos)
00121 {
00122 istringstream inputString(temp);
00123 inputString >> data.parameter >> data.value;
00124 string tmp_value;
00125 while(inputString >> tmp_value)
00126 {
00127 data.value.append(" ");
00128 data.value.append(tmp_value);
00129 }
00130
00131 tmpPos = data.parameter.find(":");
00132 if (tmpPos != string::npos)
00133
00134 data.parameter = data.parameter.substr(0, tmpPos);
00135 else
00136 {
00137 inputString >> data.value;
00138 string tmp_value;
00139 while(inputString >> tmp_value)
00140 {
00141 data.value.append(" ");
00142 data.value.append(tmp_value);
00143 }
00144 }
00145
00146
00147 conf.push_back(data);
00148 }
00149
00150
00151 getline(inconffile, temp);
00152 }
00153 else
00154 {
00155
00156 getline(inconffile, temp);
00157 }
00158 }
00159 }
00160 }
00161 }
00162 if(temp.substr(0,1) != "[") {
00163 getline(inconffile, temp);
00164 }
00165 }
00166 }
00167 else
00168 {
00169 if (bPrintErrorMessages)
00170 {
00171 cerr << "Config::read_conf: invalid input ifstream " << endl;
00172 }
00173 return CAN_NOT_OPEN_FILE;
00174 }
00175 return 0;
00176 }
00177
00178 void Config::clear()
00180 {
00181 conf.clear();
00182 }
00183
00184 void Config::print()
00186 {
00187 string tmpSection;
00188 for(Conf_data::iterator iter = conf.begin(); iter != conf.end(); ++iter)
00189 {
00190 if (tmpSection != iter->section)
00191 {
00192
00193 tmpSection = iter->section;
00194 cout << "\n[" << tmpSection << "]" << endl;
00195 cout << iter->parameter+":" << " " << iter->value << endl;
00196 }
00197 else
00198 cout << iter->parameter+":" << " " << iter->value << endl;
00199 }
00200 }
00201
00202 bool Config::section_exists(const string& section) const
00207 {
00208 for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
00209 if(iter->section == section)
00210 return true;
00211 return false;
00212 }
00213
00214 bool Config::parameter_exists(const string& section, const string& parameter) const
00219 {
00220 for(Conf_data::const_iterator iter = conf.begin(); iter != conf.end(); ++iter)
00221 if( (iter->section == section) && (iter->parameter == parameter) )
00222 return true;
00223 return false;
00224 }
00225
00226 short Config::write_conf(ofstream & outconffile, const string & file_title,
00227 const int space_between_column)
00234 {
00235 if(outconffile)
00236 {
00237 outconffile << "# ---------------------------------------------------" << endl;
00238 outconffile << "# " << file_title << endl;
00239 outconffile << "# ---------------------------------------------------" << endl;
00240 outconffile << endl;
00241
00242 string section = "";
00243
00244 for(Conf_data::iterator iterConf = conf.begin(); iterConf != conf.end(); ++iterConf)
00245 {
00246 if(section != iterConf->section)
00247 {
00248 section = iterConf->section;
00249 outconffile << "\n[" << section << "]\n" << endl;
00250 }
00251 outconffile << setw(space_between_column-iterConf->parameter.size())
00252 << iterConf->parameter + ":" << " " << iterConf->value << endl;
00253 }
00254 return 0;
00255 }
00256 else
00257 {
00258 if (bPrintErrorMessages)
00259 {
00260 cerr << "Config::write_conf: invalid input ofstream " << endl;
00261 }
00262 return CAN_NOT_CREATE_FILE;
00263 }
00264 }
00265