00001 #include "ConfigManager.h"
00002 #include "Error.h"
00003 #include <fstream>
00004 #include <iostream>
00005 #include "utils.h"
00006
00007 ConfigManager::ConfigManager(std::string filename) :
00008 m_filename(filename)
00009 {
00010 }
00011
00012 ConfigSection &ConfigManager::getSection(std::string name)
00013 {
00014 SectionMap::iterator it = m_sections.find(name);
00015 if (it == m_sections.end()) {
00016 std::string errorMsg = "getSection failed: Config section '";
00017 errorMsg += name;
00018 errorMsg += "' does not exists!";
00019 throw Error(errorMsg.c_str());
00020 }
00021 return it->second;
00022 }
00023
00024 ConfigSection &ConfigManager::getSectionSilent(std::string name)
00025 {
00026 SectionMap::iterator it = m_sections.find(name);
00027 if (it == m_sections.end()) {
00028 addEmptySection(name);
00029 return getSection(name);
00030 }
00031 return it->second;
00032 }
00033
00034 void ConfigManager::setSection(std::string name, ConfigSection §ion)
00035 {
00036 SectionMap::iterator it = m_sections.find(name);
00037 if (it != m_sections.end()) {
00038 it->second = section;
00039 } else {
00040 m_sections.insert(std::pair<std::string, ConfigSection>(name, section));
00041 }
00042 }
00043
00044 void ConfigManager::addEmptySection(std::string name)
00045 {
00046 SectionMap::iterator it = m_sections.find(name);
00047 if (it != m_sections.end()) {
00048 std::string errorMsg = "addEmptySection failed: Config section '";
00049 errorMsg += name;
00050 errorMsg += "' already exists!";
00051 throw Error(errorMsg.c_str());
00052 } else {
00053 ConfigSection dummy;
00054 m_sections.insert(std::pair<std::string, ConfigSection>(name, dummy));
00055 }
00056 }
00057
00058 void ConfigManager::save()
00059 {
00060 std::ofstream outFile(m_filename.c_str());
00061 SectionMap::iterator it;
00062 for (it = m_sections.begin(); it != m_sections.end(); ++it) {
00063 outFile << "[" << it->first << "]" << std::endl;
00064 it->second.save(outFile);
00065 outFile << "[/" << it->first << "]" << std::endl << std::endl;
00066 }
00067 }
00068
00069 void ConfigManager::load()
00070 {
00071 std::ifstream inFile(m_filename.c_str());
00072 if (!inFile.good()) {
00073 std::string msg("ConfigManager::load(): cannot open file ");
00074 msg += m_filename;
00075 throw Error(msg.c_str());
00076 }
00077 std::string sectionString, sectionName, endSection, line;
00078 bool inSection = false;
00079 while (std::getline(inFile, line)) {
00080 if (!inSection && (line.find('[') != std::string::npos) &&
00081 (line.find(']') != std::string::npos)) {
00082
00083 inSection = true;
00084 sectionName = line;
00085 sectionName = sectionName.substr(1, sectionName.find(']')-1);
00086 endSection = "[/" + sectionName + "]";
00087 } else if (inSection) {
00088 if (line.find(endSection) != std::string::npos) {
00089
00090 ConfigSection tmpSection;
00091 tmpSection.load(sectionString);
00092 setSection(sectionName, tmpSection);
00093 inSection = false;
00094 } else {
00095
00096 sectionString += line + "\n";
00097 }
00098 }
00099 }
00100 if (inSection) {
00101 std::string errMsg("Config file ");
00102 errMsg += m_filename;
00103 errMsg += " is malformated:\nSection ";
00104 errMsg += sectionName + " is not closed!";
00105 throw Error(errMsg.c_str());
00106 }
00107 }