![]() |
Kukatz 3D
0.1
Török Attila szakdolgozata
|
00001 /* 00002 * configmanager.cpp - Kukatz 3D 00003 * Copyright (c) 2011 - TÖRÖK Attila (torokati44@gmail.com) 00004 * 00005 * This software is provided 'as-is', without any express or implied 00006 * warranty. In no event will the authors be held liable for any damages 00007 * arising from the use of this software. 00008 * 00009 * Permission is granted to anyone to use this software for any purpose, 00010 * including commercial applications, and to alter it and redistribute it 00011 * freely, subject to the following restrictions: 00012 * 00013 * 1. The origin of this software must not be misrepresented; you must not 00014 * claim that you wrote the original software. If you use this software 00015 * in a product, an acknowledgment in the product documentation would be 00016 * appreciated but is not required. 00017 * 00018 * 2. Altered source versions must be plainly marked as such, and must not be 00019 * misrepresented as being the original software. 00020 * 00021 * 3. This notice may not be removed or altered from any source 00022 * distribution. 00023 */ 00024 00025 #include "configmanager.hpp" 00026 00027 #include <iostream> 00028 #include <fstream> 00029 #include <cstring> 00030 #include <sstream> 00031 00032 ConfigManager::ConfigManager() 00033 { 00034 // -------- DEFAULTS -------- 00035 00036 values["fullscreen"] = std::string("1"); 00037 values["3D"] = std::string("off"); 00038 values["num_edibles"] = std::string("1"); 00039 values["supply_rate"] = std::string("0"); 00040 values["show_ai_cameras"] = std::string("0"); 00041 values["show_player_names"] = std::string("1"); 00042 00043 // -------- CONFIGFILE -------- 00044 00045 std::ifstream file("config"); 00046 00047 if (file.is_open()) 00048 { 00049 std::string line; 00050 while (!file.eof()) 00051 { 00052 line = ""; 00053 file >> line; 00054 00055 if ((line.length()) > 0 && (line[0] != '#')) 00056 { 00057 std::string name(strtok((char*)line.c_str(), " =")); 00058 std::string value(strtok(0, " =")); 00059 00060 if ((name.length() > 0) && (value.length() > 0)) 00061 { 00062 values[name] = value; 00063 } 00064 } 00065 } 00066 } 00067 } 00068 00069 void ConfigManager::save() 00070 { 00071 std::ofstream file("config", std::ios_base::trunc); 00072 if (file.is_open()) 00073 { 00074 std::map<std::string, std::string>::const_iterator end = values.end(); 00075 for (std::map<std::string, std::string>::const_iterator i = values.begin(); i != end; ++i) 00076 { 00077 file << i->first << "=" << i->second << "\n"; 00078 } 00079 } 00080 else 00081 { 00082 std::cerr << "ERROR! Couldn't open config file to save configuration.\n"; 00083 } 00084 } 00085 00086 bool ConfigManager::is_set(std::string k) 00087 { 00088 return (values.count(k) != 0); 00089 } 00090 00091 bool ConfigManager::get_bool(std::string k) 00092 { 00093 std::string& v = values[k]; 00094 return ((v == "1") || (v == "y") || (v == "Y") || (v == "yes") || 00095 (v == "YES") || (v == "Yes") || (v == "on") || (v == "ON") || (v == "On")); 00096 } 00097 00098 int ConfigManager::get_int(std::string k) 00099 { 00100 ss.clear(); 00101 ss.str(""); 00102 00103 ss.str(values[k]); 00104 00105 int ret; 00106 00107 ss >> ret; 00108 00109 return ret; 00110 } 00111 00112 unsigned int ConfigManager::get_unsigned_int(std::string k) 00113 { 00114 ss.clear(); 00115 ss.str(""); 00116 00117 ss.str(values[k]); 00118 00119 unsigned int ret; 00120 00121 ss >> ret; 00122 00123 return ret; 00124 } 00125 00126 float ConfigManager::get_float(std::string k) 00127 { 00128 ss.clear(); 00129 ss.str(""); 00130 00131 ss.str(values[k]); 00132 00133 float ret; 00134 00135 ss >> ret; 00136 00137 return ret; 00138 } 00139 00140 double ConfigManager::get_double(std::string k) 00141 { 00142 ss.clear(); 00143 ss.str(""); 00144 00145 ss.str(values[k]); 00146 00147 double ret; 00148 00149 ss >> ret; 00150 00151 return ret; 00152 } 00153 00154 std::string ConfigManager::get_string(std::string k) 00155 { 00156 return values[k]; 00157 } 00158 00159 void ConfigManager::set(std::string k, bool v) 00160 { 00161 values[k] = std::string(v?"1":"0"); 00162 } 00163 00164 void ConfigManager::set(std::string k, int v) 00165 { 00166 ss.clear(); 00167 ss.str(""); 00168 00169 ss << v; 00170 00171 values[k] = ss.str(); 00172 } 00173 00174 void ConfigManager::set(std::string k, unsigned int v) 00175 { 00176 ss.clear(); 00177 ss.str(""); 00178 00179 ss << v; 00180 00181 values[k] = ss.str(); 00182 } 00183 00184 void ConfigManager::set(std::string k, float v) 00185 { 00186 ss.clear(); 00187 ss.str(""); 00188 00189 ss << v; 00190 00191 values[k] = ss.str(); 00192 } 00193 00194 void ConfigManager::set(std::string k, double v) 00195 { 00196 ss.clear(); 00197 ss.str(""); 00198 00199 ss << v; 00200 00201 values[k] = ss.str(); 00202 } 00203 00204 void ConfigManager::set(std::string k, std::string v) 00205 { 00206 values[k] = v; 00207 } 00208 00209 ConfigManager::~ConfigManager() 00210 { 00211 save(); 00212 }