| 1 | /* |
| 2 | =========================================================================== |
| 3 | blockattack - Block Attack - Rise of the Blocks |
| 4 | Copyright (C) 2005-2012 Poul Sander |
| 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see http://www.gnu.org/licenses/ |
| 18 | |
| 19 | Source information and contacts persons can be found at |
| 20 | https://blockattack.net |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | #include "common.h" |
| 25 | #include <sstream> |
| 26 | #include <cstring> |
| 27 | #include "os.hpp" |
| 28 | #include "sago/SagoMiscSdl2.hpp" |
| 29 | #include "sago/SagoMisc.hpp" |
| 30 | #include <stdarg.h> |
| 31 | |
| 32 | |
| 33 | bool strequals(const char* a, const char* b) { |
| 34 | return strcmp(a,b) == 0; |
| 35 | } |
| 36 | |
| 37 | void dieOnNullptr(bool ptr, const char* msg) { |
| 38 | if (!ptr) { |
| 39 | sago::SagoFatalError(msg); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | double str2double(const std::string& str2parse) { |
| 44 | try { |
| 45 | return std::stod(str2parse); |
| 46 | } |
| 47 | catch (...) { |
| 48 | return 0.0; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | int str2int(const std::string& str2parse) { |
| 54 | try { |
| 55 | return std::stoi(str2parse); |
| 56 | } |
| 57 | catch (...) { |
| 58 | return 0; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Takes a number of milliseconds and returns the value in commonTime format. |
| 64 | */ |
| 65 | commonTime TimeHandler::ms2ct(unsigned int milliseconds) { |
| 66 | commonTime ct; |
| 67 | ct.days = 0; |
| 68 | unsigned int time = milliseconds; |
| 69 | ct.hours = time/(1000*60*60); |
| 70 | time = time % (1000*60*60); |
| 71 | ct.minutes = time/(1000*60); |
| 72 | time = time % (1000*60); |
| 73 | ct.seconds = time/1000; |
| 74 | return ct; |
| 75 | } |
| 76 | |
| 77 | commonTime TimeHandler::getTime(const std::string& name) { |
| 78 | commonTime ct; |
| 79 | ct.days = Config::getInstance()->getInt(name+"Days"); |
| 80 | ct.hours = Config::getInstance()->getInt(name+"Hours"); |
| 81 | ct.minutes = Config::getInstance()->getInt(name+"Minutes"); |
| 82 | ct.seconds = Config::getInstance()->getInt(name+"Seconds"); |
| 83 | return ct; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the total runtime with toAdd added but without writing it to config file. |
| 88 | * Used for stats |
| 89 | */ |
| 90 | commonTime TimeHandler::peekTime(const std::string& name, const commonTime& toAdd) { |
| 91 | commonTime ct = getTime(name); |
| 92 | |
| 93 | ct.seconds +=toAdd.seconds; |
| 94 | ct.minutes +=ct.seconds/60; |
| 95 | ct.seconds = ct.seconds%60; |
| 96 | |
| 97 | ct.minutes += toAdd.minutes; |
| 98 | ct.hours += ct.minutes/60; |
| 99 | ct.minutes = ct.minutes%60; |
| 100 | |
| 101 | ct.hours += toAdd.hours; |
| 102 | ct.days += ct.hours/24; |
| 103 | ct.hours = ct.hours%24; |
| 104 | |
| 105 | ct.days += toAdd.days; |
| 106 | return ct; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Same as peekTotalTime but writes the time to the config file. |
| 111 | * Should only be called only once! when the program shuts down |
| 112 | */ |
| 113 | commonTime TimeHandler::addTime(const std::string& name, const commonTime& toAdd) { |
| 114 | commonTime ct = peekTime(name,toAdd); |
| 115 | |
| 116 | Config::getInstance()->setInt(name+"Days",ct.days); |
| 117 | Config::getInstance()->setInt(name+"Hours",ct.hours); |
| 118 | Config::getInstance()->setInt(name+"Minutes",ct.minutes); |
| 119 | Config::getInstance()->setInt(name+"Seconds",ct.seconds); |
| 120 | return ct; |
| 121 | } |
| 122 | |
| 123 | Config* Config::instance = 0; |
| 124 | |
| 125 | Config::Config() { |
| 126 | configMap.clear(); |
| 127 | load(); |
| 128 | shuttingDown = 0; // Not shutting down |
| 129 | } |
| 130 | |
| 131 | void Config::load() { |
| 132 | std::string filecontent = sago::GetFileContent("configFile"); |
| 133 | std::stringstream inFile(filecontent); |
| 134 | std::string key; |
| 135 | std::string previuskey; |
| 136 | if (inFile) { |
| 137 | while (!inFile.eof()) { |
| 138 | inFile >> key; |
| 139 | if (key==previuskey) { //the last entry will be read 2 times if a linebreak is missing in the end |
| 140 | continue; |
| 141 | } |
| 142 | previuskey = key; |
| 143 | inFile.get(); //Read the space between the key and the content |
| 144 | std::string value; |
| 145 | std::getline(inFile, value); |
| 146 | #if DEBUG |
| 147 | std::cerr << "Config read: " << key << " with:\"" << value << "\"" << "\n"; |
| 148 | #endif |
| 149 | configMap[key] = value; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | Config* Config::getInstance() { |
| 155 | if (Config::instance==0) { |
| 156 | Config::instance = new Config(); |
| 157 | |
| 158 | } |
| 159 | return Config::instance; |
| 160 | } |
| 161 | |
| 162 | void Config::save() { |
| 163 | std::stringstream outFile; |
| 164 | std::map<std::string,std::string>::iterator iter; |
| 165 | for (iter = configMap.begin(); iter != configMap.end(); ++iter) { |
| 166 | outFile << iter->first << " " << iter->second << "\n"; |
| 167 | } |
| 168 | outFile << "\n"; //The last entry in the file will be read double if a linebreak is missing |
| 169 | //This is checked on load too in case a user changes it himself. |
| 170 | sago::WriteFileContent("configFile", outFile.str()); |
| 171 | } |
| 172 | |
| 173 | bool Config::exists(const std::string& varName) const { |
| 174 | //Using that find returns an iterator to the end of the map if not found |
| 175 | return configMap.find(varName) != configMap.end(); |
| 176 | } |
| 177 | |
| 178 | void Config::setDefault(const std::string& varName,const std::string& content) { |
| 179 | if (exists(varName)) { |
| 180 | return; //Already exists do not change |
| 181 | } |
| 182 | setString(varName,content); |
| 183 | } |
| 184 | |
| 185 | void Config::setShuttingDown(long shuttingDown) { |
| 186 | this->shuttingDown = shuttingDown; |
| 187 | } |
| 188 | |
| 189 | long Config::isShuttingDown() const { |
| 190 | return shuttingDown; |
| 191 | } |
| 192 | |
| 193 | void Config::setString(const std::string& varName, const std::string& content) { |
| 194 | configMap[varName] = content; |
| 195 | } |
| 196 | |
| 197 | void Config::setInt(const std::string& varName, int content) { |
| 198 | configMap[varName] = std::to_string(content); |
| 199 | } |
| 200 | |
| 201 | void Config::setValue(const std::string& varName,double content) { |
| 202 | configMap[varName] = std::to_string(content); |
| 203 | } |
| 204 | |
| 205 | std::string Config::getString(const std::string& varName) { |
| 206 | if (exists(varName)) { |
| 207 | return configMap[varName]; |
| 208 | } |
| 209 | else { |
| 210 | return ""; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | int Config::getInt(const std::string& varName) { |
| 215 | if (exists(varName)) { |
| 216 | return str2int(configMap[varName]); |
| 217 | } |
| 218 | else { |
| 219 | return 0; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | int Config::getInt(const std::string& varName, int defaultValue) { |
| 224 | if (exists(varName)) { |
| 225 | return str2int(configMap[varName]); |
| 226 | } |
| 227 | else { |
| 228 | setInt(varName, defaultValue); |
| 229 | return defaultValue; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | double Config::getValue(const std::string& varName) { |
| 234 | if (exists(varName)) { |
| 235 | return str2double(configMap[varName]); |
| 236 | } |
| 237 | else { |
| 238 | return 0.0; |
| 239 | } |
| 240 | } |