1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/*-----------------------------------------------------------------------------
Copyright 2007 Milan Babuskov
This file is part of Vodovod
Vodovod 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 2 of the License, or
(at your option) any later version.
Vodovod 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 Vodovod in file COPYING; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-----------------------------------------------------------------------------*/
//------------------------------------------------------------------------------
#include <string>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include "config.h"
#include <climits>
//------------------------------------------------------------------------------
using namespace std;
//-----------------------------------------------------------------------------
SDLKey getKey(taAction action)
{
std::string keys[6] = { "LEFT ", "RIGHT ", "DOWN ", "UP ", "DROP ", "FLOOD " };
int key;
if (!config().getValue(keys[(int)action], key))
return SDLK_LAST;
else
return (SDLKey)key;
}
//-----------------------------------------------------------------------------
Config& config()
{
static Config c;
return c;
}
//-----------------------------------------------------------------------------
//! return true if value exists, false if not
bool Config::getValue(string key, string& value)
{
if (dataM.find(key) == dataM.end())
return false;
value = dataM[key];
return true;
}
//-----------------------------------------------------------------------------
bool Config::getValue(std::string key)
{
bool b;
if (getValue(key, b))
return b;
else
return true; // all options are ON by default
}
//-----------------------------------------------------------------------------
bool Config::getValue(string key, int& value)
{
string s;
if (!getValue(key, s))
return false;
stringstream ss;
ss << s;
ss >> value;
return true;
}
//-----------------------------------------------------------------------------
bool Config::getValue(string key, double& value)
{
string s;
if (!getValue(key, s))
return false;
stringstream ss;
ss << s;
ss >> value;
return true;
}
//-----------------------------------------------------------------------------
bool Config::getValue(string key, bool& value)
{
string s;
if (!getValue(key, s))
return false;
value = (s == "1");
return true;
}
//-----------------------------------------------------------------------------
//! return true if value existed, false if not
bool Config::setValue(string key, string value, bool saveIt)
{
bool ret = (dataM.end() != dataM.find(key));
if (ret)
dataM.erase(key);
dataM[key] = value;
if (saveIt)
save();
return ret;
}
//-----------------------------------------------------------------------------
bool Config::setValue(string key, int value, bool saveIt)
{
stringstream ss;
ss << value;
return setValue(key, ss.str(), saveIt);
}
//-----------------------------------------------------------------------------
bool Config::setValue(string key, double value, bool saveIt)
{
stringstream ss;
ss << value;
return setValue(key, ss.str(), saveIt);
}
//-----------------------------------------------------------------------------
bool Config::setValue(string key, bool value, bool saveIt)
{
if (value)
return setValue(key, string("1"), saveIt);
else
return setValue(key, string("0"), saveIt);
}
//-----------------------------------------------------------------------------
Config::Config()
{
load();
// setup default keys if not available
std::string names[] = {
"DOWN ", "DROP ", "LEFT ", "RIGHT ", "UP ", "FLOOD "
};
int keys[] = { 274, 32, 276, 275, 273, 13 };
for (int i=0; i<sizeof(names)/sizeof(std::string); ++i)
if (dataM.find(names[i]) == dataM.end())
setValue(names[i], keys[i]);
if (dataM.find("show_grid") == dataM.end())
setValue("show_grid", false);
if (dataM.find("fullscreen") == dataM.end())
setValue("fullscreen", false);
}
//-----------------------------------------------------------------------------
Config::~Config()
{
save();
}
//-----------------------------------------------------------------------------
bool Config::save()
{
char vodovod_conf[1024];
#ifndef _WIN32
char *home = getenv("HOME");
if (home != NULL)
{
snprintf(vodovod_conf, sizeof(vodovod_conf), "%s/.vodovod", home);
mkdir(vodovod_conf, 0755);
}
strncat(vodovod_conf, "/vodovod.conf", sizeof(vodovod_conf));
#endif
std::ofstream file(vodovod_conf);
if (!file)
return false;
file << "Abandoned Bricks configuration file." << endl << endl << "[Settings]" << endl;
for (map<string, string>::const_iterator it = dataM.begin(); it != dataM.end(); ++it)
{
file << (*it).first << "=" << (*it).second << endl;
}
file.close();
return true;
}
//-----------------------------------------------------------------------------
// this gets called from main() so we're sure config.ini is in the right place
bool Config::load()
{
char vodovod_conf[1024];
#ifndef _WIN32
char *home = getenv("HOME");
if (home != NULL)
snprintf(vodovod_conf, sizeof(vodovod_conf), "%s/.vodovod/vodovod.conf", home);
#endif
std::ifstream file(vodovod_conf);
if (!file)
return false;
// I had to do it this way, since standard line << file, doesn't work good if data has spaces in it.
std::stringstream ss; // read entire file into string buffer
ss << file.rdbuf();
std::string s(ss.str());
dataM.clear();
while (true)
{
string::size_type t = s.find('\n');
if (t == string::npos)
break;
string line = s.substr(0, t);
s.erase(0, t+1);
string::size_type p = line.find('=');
if (p == string::npos)
continue;
string key = line.substr(0, p);
line.erase(0, p + 1);
line.erase(line.find_last_not_of(" \t\n\r")+1); // right trim
setValue(key, line, false);
}
file.close();
return true;
}
//-----------------------------------------------------------------------------
|