ConfigFileManager.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2004 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 
00021 #include "ConfigFileManager.h"
00022 #include "INIFile.h"
00023 
00024 #include <set>
00025 
00026 using Pentagram::istring;
00027 using std::string;
00028 
00029 ConfigFileManager* ConfigFileManager::configfilemanager = 0;
00030 
00031 ConfigFileManager::ConfigFileManager()
00032 {
00033         con.Print(MM_INFO, "Creating ConfigFileManager...\n");
00034 
00035         assert(configfilemanager == 0);
00036         configfilemanager = this;
00037 }
00038 
00039 ConfigFileManager::~ConfigFileManager()
00040 {
00041         con.Print(MM_INFO, "Destroying ConfigFileManager...\n");
00042 
00043         clear();
00044         configfilemanager = 0;
00045 }
00046 
00047 bool ConfigFileManager::readConfigFile(string fname, istring root,
00048                                                                            bool readonly)
00049 {
00050         INIFile* inifile = new INIFile();
00051         inifile->clear(root);
00052         if (!inifile->readConfigFile(fname)) {
00053                 delete inifile;
00054                 return false;
00055         }
00056         if (readonly)
00057                 inifile->setReadonly();
00058 
00059         inifiles.push_back(inifile);
00060         return true;
00061 }
00062 
00063 bool ConfigFileManager::readConfigString(string config, istring root,
00064                                                                                  bool readonly)
00065 {
00066         INIFile* inifile = new INIFile();
00067         inifile->clear(root);
00068         if (!inifile->readConfigString(config)) {
00069                 delete inifile;
00070                 return false;
00071         }
00072         if (readonly)
00073                 inifile->setReadonly();
00074 
00075         inifiles.push_back(inifile);
00076         return true;
00077 }
00078 
00079 void ConfigFileManager::write(istring root)
00080 {
00081         for (std::vector<INIFile*>::iterator i = inifiles.begin();
00082                  i != inifiles.end(); ++i)
00083         {
00084                 if (!(*i)->isReadonly() && (root == "" || (*i)->checkRoot(root)))
00085                         (*i)->write();
00086         }
00087 }
00088 
00089 void ConfigFileManager::clear()
00090 {
00091         for (std::vector<INIFile*>::iterator i = inifiles.begin();
00092                  i != inifiles.end(); ++i)
00093         {
00094                 delete (*i);
00095         }
00096         inifiles.clear();
00097 }
00098 
00099 void ConfigFileManager::clearRoot(Pentagram::istring root)
00100 {
00101         std::vector<INIFile*>::iterator i = inifiles.begin();
00102 
00103         while (i != inifiles.end()) {
00104                 if ((*i)->checkRoot(root)) {
00105                         delete (*i);
00106                         i = inifiles.erase(i);
00107                 }
00108                 else {
00109                         ++i;
00110                 }
00111         }
00112 }
00113 
00114 bool ConfigFileManager::exists(istring key)
00115 {
00116         return (findKeyINI(key) != 0);
00117 }
00118 
00119 bool ConfigFileManager::get(istring key, string& ret)
00120 {
00121         INIFile* ini = findKeyINI(key);
00122         if (!ini) return false;
00123 
00124         ini->value(key, ret);
00125         return true;
00126 }
00127 
00128 
00129 bool ConfigFileManager::get(istring key, int& ret)
00130 {
00131         INIFile* ini = findKeyINI(key);
00132         if (!ini) return false;
00133 
00134         ini->value(key, ret);
00135         return true;
00136 }
00137 
00138 bool ConfigFileManager::get(istring key, bool& ret)
00139 {
00140         INIFile* ini = findKeyINI(key);
00141         if (!ini) return false;
00142 
00143         ini->value(key, ret);
00144         return true;
00145 }
00146 
00147 void ConfigFileManager::set(istring key, string val)
00148 {
00149         INIFile* ini = findWriteINI(key);
00150         if (!ini) return;
00151 
00152         ini->set(key, val);
00153 }
00154 
00155 void ConfigFileManager::set(istring key, const char* val)
00156 {
00157         INIFile* ini = findWriteINI(key);
00158         if (!ini) return;
00159 
00160         ini->set(key, val);
00161 }
00162 
00163 void ConfigFileManager::set(istring key, int val)
00164 {
00165         INIFile* ini = findWriteINI(key);
00166         if (!ini) return;
00167 
00168         ini->set(key, val);
00169 }
00170 
00171 void ConfigFileManager::set(istring key, bool val)
00172 {
00173         INIFile* ini = findWriteINI(key);
00174         if (!ini) return;
00175 
00176         ini->set(key, val);
00177 }
00178 
00179 void ConfigFileManager::unset(istring key)
00180 {
00181         INIFile* ini = findWriteINI(key);
00182         if (!ini) return;
00183 
00184         ini->unset(key);
00185 }
00186 
00187 
00188 
00189 std::vector<istring> ConfigFileManager::listKeys(istring section,
00190                                                                                                  bool longformat)
00191 {
00192         std::vector<istring> keys;
00193 
00194         std::set<istring> keyset;
00195         std::set<istring>::iterator iter;
00196 
00197         for (std::vector<INIFile*>::iterator i = inifiles.begin();
00198                  i != inifiles.end(); ++i)
00199         {
00200                 if ((*i)->checkRoot(section)) {
00201                         (*i)->listKeys(keyset, section, longformat);
00202                 }
00203         }
00204 
00205         for (iter = keyset.begin(); iter != keyset.end(); ++iter)
00206         {
00207                 keys.push_back(*iter);
00208         }
00209 
00210         return keys;
00211 }
00212 
00213 std::vector<istring> ConfigFileManager::listSections(istring root,
00214                                                                                                          bool longformat)
00215 {
00216         std::vector<istring> sections;
00217 
00218         std::set<istring> sectionset;
00219         std::set<istring>::iterator iter;
00220 
00221         for (std::vector<INIFile*>::iterator i = inifiles.begin();
00222                  i != inifiles.end(); ++i)
00223         {
00224                 if ((*i)->checkRoot(root)) {
00225                         (*i)->listSections(sectionset, longformat);
00226                 }
00227         }
00228 
00229         for (iter = sectionset.begin(); iter != sectionset.end(); ++iter)
00230         {
00231                 sections.push_back(*iter);
00232         }
00233 
00234         return sections;
00235 }
00236 
00237 std::map<istring,std::string> ConfigFileManager::listKeyValues(istring section,
00238                                                                                                                            bool longformat)
00239 {
00240         std::map<istring, std::string> values;
00241 
00242         for (std::vector<INIFile*>::iterator i = inifiles.begin();
00243                  i != inifiles.end(); ++i)
00244         {
00245                 if ((*i)->checkRoot(section)) {
00246                         (*i)->listKeyValues(values, section, longformat);
00247                 }
00248         }
00249 
00250         return values;
00251 }
00252 
00253 
00254 INIFile* ConfigFileManager::findKeyINI(istring key)
00255 {
00256         for (std::vector<INIFile*>::reverse_iterator i = inifiles.rbegin();
00257                  i != inifiles.rend(); ++i)
00258         {
00259                 if ((*i)->hasKey(key))
00260                         return (*i);
00261         }
00262 
00263         return 0;
00264 }
00265 
00266 INIFile* ConfigFileManager::findWriteINI(istring key)
00267 {
00268         for (std::vector<INIFile*>::reverse_iterator i = inifiles.rbegin();
00269                  i != inifiles.rend(); ++i)
00270         {
00271                 if (!(*i)->isReadonly() && (*i)->checkRoot(key))
00272                         return (*i);
00273         }
00274 
00275         return 0;
00276 }

Generated on Fri Jul 27 22:27:11 2007 for pentagram by  doxygen 1.4.7