| 1 | /* |
| 2 | RailControl - Model Railway Control Software |
| 3 | |
| 4 | Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 | |
| 6 | RailControl is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 3, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | RailControl is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | See the GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with RailControl; see the file LICENCE. If not see |
| 18 | <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <fstream> |
| 22 | #include <istream> // std::ws |
| 23 | #include <sstream> |
| 24 | |
| 25 | #include "Config.h" |
| 26 | #include "Logger/Logger.h" |
| 27 | #include "Utils/Integer.h" |
| 28 | |
| 29 | using std::map; |
| 30 | using std::string; |
| 31 | |
| 32 | Config::Config(const std::string& fileName) |
| 33 | { |
| 34 | Logger::Logger* logger = Logger::LoggerServer::Instance().GetLogger("Config"); |
| 35 | // read config values |
| 36 | logger->Info(Languages::TextReadingConfigFile, fileName); |
| 37 | |
| 38 | std::ifstream configFile; |
| 39 | configFile.open(fileName); |
| 40 | if (!configFile.is_open()) |
| 41 | { |
| 42 | logger->Warning(Languages::TextUnableToOpenFile, fileName); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | for (string line; std::getline(configFile, line); ) |
| 47 | { |
| 48 | std::istringstream iss(line); |
| 49 | string configKey; |
| 50 | string eq; |
| 51 | string configValue; |
| 52 | |
| 53 | iss >> configKey >> eq >> configValue >> std::ws; |
| 54 | |
| 55 | if ((configKey.size() == 0) || (configKey[0] == '#')) |
| 56 | { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | if (eq.compare("=") != 0) |
| 61 | { |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | config[configKey] = configValue; |
| 66 | logger->Info(Languages::TextParameterFoundInConfigFile, configKey, configValue); |
| 67 | } |
| 68 | configFile.close(); |
| 69 | } |
| 70 | |
| 71 | const string& Config::getStringValue(const string& key, const string& defaultValue) |
| 72 | { |
| 73 | if (config.count(key) != 1) |
| 74 | { |
| 75 | return defaultValue; |
| 76 | } |
| 77 | return config[key]; |
| 78 | } |
| 79 | |
| 80 | int Config::getIntValue(const string& key, const int defaultValue) |
| 81 | { |
| 82 | if (config.count(key) != 1) |
| 83 | { |
| 84 | return defaultValue; |
| 85 | } |
| 86 | return Utils::Integer::StringToInteger(config[key], defaultValue); |
| 87 | } |
| 88 | |
| 89 | bool Config::getBoolValue(const string& key, const bool defaultValue) |
| 90 | { |
| 91 | const string value = Utils::Utils::StringToLower(getStringValue(key, string(defaultValue ? "1" : "0"))); |
| 92 | return ((value.compare("true") == 0) || (value.compare("on") == 0) || (value.compare("1") == 0)); |
| 93 | } |