/* * ConfigManager.cpp * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "ConfigManager.h" #include "ConfigEntry.h" #include "Configurable.h" #include "Logger.h" #include "Util.h" #include #include namespace Mad { namespace Common { ConfigManager ConfigManager::configManager; bool ConfigManager::Compare::operator() (const Configurable *c1, const Configurable *c2) { if(c1->getPriority() != c2->getPriority()) return c1->getPriority() > c2->getPriority(); else return c1 < c2; } void ConfigManager::handleConfigEntry(const ConfigEntry &entry) { bool handled = false; for(std::set::iterator c = configurables.begin(); c != configurables.end(); ++c) { if((*c)->handleConfigEntry(entry, handled)) handled = true; } if(!handled) Logger::logf(Logger::WARNING, "Invalid config option '%s'.", entry[0].getKey().c_str()); } bool ConfigManager::loadFile(const std::string &filename, bool finish) { std::ifstream file(filename.c_str()); ConfigEntry entry; std::vector subEntry; std::string line; if(!file.good()) return false; while(!(file.eof() && line.empty())) { std::string nextLine; char bracket = 0; if(line.empty()) { std::getline(file, line); size_t pos = line.find_first_of('#'); if(pos != std::string::npos) line = line.substr(0, pos); line = Util::trim(line); if(line.empty()) continue; } size_t pos = line.find_first_of("{}"); if(pos != std::string::npos) { bracket = line[pos]; line = Util::trim(line.substr(0, pos)); try { nextLine = Util::trim(line.substr(pos+1)); } catch(std::out_of_range& e) {} } if(!line.empty()) { pos = line.find_first_of(" \t"); subEntry.clear(); if(pos == std::string::npos) { subEntry.push_back(line); } else { subEntry.push_back(line.substr(0, pos)); subEntry.push_back(Util::trim(line.substr(pos))); } entry.push(subEntry); handleConfigEntry(entry); entry.pop(); } switch(bracket) { case '{': entry.push(subEntry); break; case '}': entry.pop(); } line = nextLine; } // TODO Depth check if(finish) { for(std::set::iterator c = configurables.begin(); c != configurables.end(); ++c) (*c)->configFinished(); } return true; } } }