Conf.cc

00001 // ----------------------------------------------------------------------^
00002 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
00003 // (mailto: <gcalderone@ifc.inaf.it>)
00004 // 
00005 // This file is part of MCS.
00006 // 
00007 // MCS is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 // 
00012 // MCS is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with MCS; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 // 
00021 // ----------------------------------------------------------------------$
00022 
00023 #include "mcs.hh"
00024 using namespace mcs;
00025 
00026 
00027 //--------------------------------------------------------
00028 mcs::Conf::Conf(string filename) :
00029   lastval(STRING, 300)
00030 {
00031   if (! filename.empty())
00032     open(filename);
00033 }
00034 
00035 
00036 mcs::Conf::~Conf()
00037 {}
00038 
00039 
00040 void mcs::Conf::open(string filename)
00041 {
00042   ifstream in;
00043   string sect, s, key, val, comm;
00044 
00045   pcrecpp::RE re_brackets("\\[(\\w+[-+]?\\w+)\\]");
00046   pcrecpp::RE re_comments("(.*);(.*)");
00047   pcrecpp::RE re_equal_split("(.+)=(.+)");
00048 
00049 
00050   this->filename = filename;
00051 
00052   in.open(filename.c_str());
00053   if (!in)
00054     throw MCS_ERROR(MSG_CANT_OPEN_FILE, filename.csz);
00055 
00056 
00057   keys.clear();
00058   values.clear();
00059   sections.clear();
00060   comments.clear();
00061 
00062 
00063   while (getline(in, s)) {
00064     s = trim(s);
00065     s = remTabs(s);
00066 
00067     if (! s.empty()) {
00068       if (re_brackets.FullMatch(s, &val)) {  //This is a section
00069     sect = val;
00070       }
00071       else {
00072     if (re_equal_split.FullMatch(s)) {
00073       re_equal_split.FullMatch(s, &key, &val);
00074     
00075       comm = "";
00076       if (re_comments.FullMatch(val)) {
00077         s  = val;
00078         re_comments.FullMatch(s, &val, &comm);
00079       }
00080     
00081       keys.push_back(trim(key));
00082       values.push_back(trim(val));
00083       sections.push_back(sect);
00084       comments.push_back(trim(comm));
00085     }
00086       }
00087     }
00088   }
00089   in.close();
00090 
00091   //    unsigned int i;
00092   //    for (i=0; i<sections.size(); i++)
00093   //    cout << "-"  << sections[i] << "-\t"
00094   //         << "-"  << keys[i]     << "-\t"
00095   //         << "-"  << values[i]   << "-\t"
00096   //         << "-"  << comments[i] << "-"  << endl;    
00097 }
00098 
00099 
00100 
00101 
00102 
00103 
00104 int mcs::Conf::index(string section, string key)
00105 {
00106   unsigned int i;
00107   section = trim(section);
00108   key = trim(key);
00109   lastval.setNull();
00110 
00111   for (i=0; i<sections.size(); i++)
00112     if (sections[i] == section)
00113       if (keys[i] == key) {
00114     lastval = values[i];
00115     return (int) i;
00116       }
00117 
00118   return -1;
00119 }
00120 
00121 
00122 bool mcs::Conf::search(string section, string key, enum ThrowExceptions throwexc)
00123 {
00124   if (index(section, key) > -1)
00125     return true;
00126   else
00127     if (throwexc)
00128       throw MCS_ERROR(MSG_ENTRY_UNAVAILABLE, section.csz, key.csz);
00129     else
00130       return false;
00131 }
00132 
00133 
00134 
00135 string mcs::Conf::sval(string section, string key)
00136 {
00137   search(section, key, THROW);
00138   return lastval.sval();
00139 }
00140 
00141 
00142 int mcs::Conf::ival(string section, string key)
00143 {
00144   search(section, key, THROW);
00145   return lastval.ival();
00146 }
00147 
00148 
00149 long long int mcs::Conf::lval(string section, string key)
00150 {
00151   search(section, key, THROW);
00152   return lastval.lval();
00153 }
00154 
00155 
00156 Data& mcs::Conf::val(string section, string key)
00157 {
00158   if (section.length() + key.length() > 1)
00159     search(section, key, THROW);
00160   return lastval;
00161 }
00162 
00163 
00164 string mcs::Conf::sval(string section, string key, string defval)
00165 {
00166   if (search(section, key, DONT_THROW))
00167     return lastval.sval();
00168   else
00169     return defval;
00170 }
00171 
00172 
00173 int mcs::Conf::ival(string section, string key, int defval)
00174 {
00175   if (search(section, key, DONT_THROW))
00176     return lastval.ival();
00177   else
00178     return defval;
00179 }
00180 
00181 
00182 long long int mcs::Conf::lval(string section, string key, int defval)
00183 {
00184   if (search(section, key, DONT_THROW))
00185     return lastval.lval();
00186   else
00187     return defval;
00188 }
00189 
00190 
00191 void mcs::Conf::setval(string section, string key, string val, string comment)
00192 {
00193   val = trim(val);
00194   comment = trim(comment);
00195   int i = index(section, key);
00196   if (i > -1) {
00197     values[i] = val;
00198     comments[i] = comment;
00199   }
00200   else {
00201     sections.push_back(section);
00202     keys.push_back(key);
00203     values.push_back(val);
00204     comments.push_back(comment);
00205   }
00206 }
00207 
00208 
00209 void mcs::Conf::setval(string section, string key, Data* val, string comment)
00210 {
00211   setval(section, key, val->sval(), comment);
00212 }
00213 
00214 
00215 void mcs::Conf::setval(string section, string key, int val, string comment)
00216 {
00217   setval(section, key, itos(val), comment);
00218 }
00219 
00220 
00221 void mcs::Conf::setval(string section, string key, long long int val, string comment)
00222 {
00223   setval(section, key, itos(val), comment);
00224 }
00225 
00226 
00227 void mcs::Conf::save(string filename)
00228 {
00229   ofstream out;
00230   string sect;
00231   unsigned int i;
00232 
00233   if (filename.empty())
00234     filename = this->filename;
00235 
00236   out.open(filename.c_str());
00237   if (!out)
00238     throw MCS_ERROR(MSG_CANT_OPEN_FILE, filename.csz);
00239 
00240   for (i=0; i<sections.size(); i++) {
00241     if (sections[i] != sect) {
00242       sect = sections[i];
00243       out << "[" << sect << "]" << endl;
00244     }
00245 
00246     out << keys[i] << " = " << values[i];
00247 
00248     if (! comments[i].empty())
00249       out << " ;" << comments[i];
00250 
00251     out << endl;
00252   }
00253   out.close();
00254 }

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012