configurationfile.cpp
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
00029 #include "configurationfile.h"
00035 ConfigurationFile::ConfigurationFile(string fname)
00036 {
00037 this->file = fname ;
00038 this->config.clear();
00039 this->protectedKeys.clear();
00040 }
00041
00045 ConfigurationFile::~ConfigurationFile()
00046 {
00047
00048 }
00049
00058 bool ConfigurationFile::load()
00059 {
00060 this->config.clear();
00061 vector<string> temp ;
00062 ifstream ifs(this->file.c_str());
00063 if ( ifs ) {
00064 string line;
00065 while ( getline( ifs, line ) ) {
00066 if ( (line[0] != '#')&&(line!="") ) {
00067 if ( line.find("=") == string::npos) {
00068 return false;
00069 }
00070 else {
00071 temp = Tools::stringToVector(line,"=",0) ;
00072 if ( temp.size()>1) {
00073 if ( this->getValue(temp[0]) == "") {
00074 this->config[temp[0]] = temp[1];
00075 }
00076 else {
00077 this->config[temp[0]] += (","+temp[1]);
00078 }
00079 }
00080 else
00081 {
00082 if ( this->getValue(temp[0]) == "") {
00083 this->config[temp[0]] = "";
00084 }
00085 }
00086 }
00087 }
00088 }
00089 ifs.close();
00090 return true;
00091 }
00092 else {
00093 return false;
00094 }
00095 }
00096
00104 bool ConfigurationFile::flush()
00105 {
00106 ofstream ofs(this->file.c_str(),ios_base::trunc);
00107 if(ofs) {
00108 for (map<string,string>::const_iterator iter = this->config.begin(); iter!=this->config.end();++iter)
00109 {
00110 ofs << iter->first<<"="<<iter->second<<endl;
00111 }
00112 ofs.flush();
00113 ofs.close();
00114 return true;
00115 }
00116 else {
00117 return false;
00118 }
00119 }
00120
00126 map<string,string> ConfigurationFile::getConfig()
00127 {
00128 return this->config;
00129 }
00130
00135 void ConfigurationFile::addProtectedKey(string key)
00136 {
00137 this->protectedKeys.push_back(key);
00138 }
00139
00147 string ConfigurationFile::getValue(string key,bool displayProtected)
00148 {
00149 if(!displayProtected && Tools::isInVector(this->protectedKeys,key)) {
00150 return "";
00151 }
00152 map<string,string>::iterator fter = this->config.find(key);
00153 if ( fter != this->config.end() ) {
00154 return fter->second ;
00155 }
00156 else {
00157 return "";
00158 }
00159 }
00160
00168 void ConfigurationFile::setValue(string key,string value) {
00169 this->config[key] = value;
00170 }
00171
00176 bool ConfigurationFile::delKey(string key)
00177 {
00178 map<string,string>::iterator fter = this->config.find(key);
00179 if ( fter != this->config.end() ) {
00180 this->config.erase(fter);
00181 return true;
00182 }
00183 else
00184 {
00185 return false;
00186 }
00187 }
00188
00193 string ConfigurationFile::getFilePath() {
00194 return this->file;
00195 }