Added angle modification for static graphics in levels.
[potpourri.git] / src / core / SettingsDB.cpp
blob301f5d858a450d4135be101f8440cc67f9327de7
1 // Copyright 2008 Brian Caine
3 // This file is part of Potpourri.
5 // Potpourri is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Potpourri is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTIBILITY of FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Potpourri. If not, see <http://www.gnu.org/licenses/>.
19 // NOTES:
21 // The source for the event pipeline
23 #include <iostream>
24 #include <stdexcept>
25 #include <pcrecpp.h>
27 #include "../../include/core/SettingsDB.h"
29 using namespace fragrant;
31 GameInfo fragrant::makeGameInfo(std::string name,
32 std::string url, std::string last, std::string controls)
34 GameInfo results;
35 results.name = name;
36 results.url = url;
37 results.last_presentation = last;
38 results.controls = controls;
40 return results;
43 SettingsDB::SettingsDB(std::string dblocation)
45 int result = sqlite3_open(dblocation.c_str(), &db);
47 if (result)
49 std::cerr << "SettingsDB::SettingsDB(): Error opening db, "
50 << sqlite3_errmsg(db) << std::endl;
51 sqlite3_close(db);
52 throw std::runtime_error("");
55 std::string query = "select * from sqlite_master;";
56 table_exists = false;
57 to_search = "general";
58 sqlite3_exec(db, query.c_str(), mainquery, this, 0);
60 if (!table_exists)
62 std::string query = "create table general(key varchar[10], "
63 "value varchar[20]);";
64 sqlite3_exec(db, query.c_str(), mainquery, this, 0);
67 table_exists = false;
68 to_search = "game_info";
70 sqlite3_exec(db, query.c_str(), mainquery, this, 0);
72 if (!table_exists)
74 std::string query = "create table game_info(gamename varchar[10],"
75 " lastmode varchar[20], url varchar[40], cont"
76 "rols varchar[100]);";
77 sqlite3_exec(db, query.c_str(), mainquery, this, 0);
80 plugin_folder = getValueFromGeneral("plugin_folder");
81 game_presentations = getGamePresentations();
84 SettingsDB::~SettingsDB()
86 setValueFromGeneral("plugin_folder", plugin_folder);
87 setGamePresentations(game_presentations);
89 sqlite3_close(db);
92 std::string& SettingsDB::pluginFolder()
94 return plugin_folder;
97 std::vector<GameInfo> SettingsDB::getGameList()
99 std::vector<GameInfo> results;
101 std::map<std::string, GameInfo>::iterator iter;
102 for (iter = game_presentations.begin();
103 iter != game_presentations.end();
104 iter++)
105 results.push_back(iter->second);
107 return results;
110 GameInfo& SettingsDB::getPresentationName(std::string game)
112 if (game_presentations.find(game) == game_presentations.end())
113 game_presentations[game] = makeGameInfo("", "", "");
115 return game_presentations[game];
118 std::string SettingsDB::getValueFromGeneral(std::string value)
120 std::string query = "select * from general where key='" +
121 sanitizeString(value) + "';";
122 general_callback_called = false;
123 sqlite3_exec(db, query.c_str(), generalcallback, this, 0);
124 if (general_callback_called)
125 return general_callback_result;
126 else
127 setValueFromGeneral(value, "");
129 return "";
132 void SettingsDB::setValueFromGeneral(std::string key, std::string newvalue)
134 std::string query = "select * from general where key='" +
135 sanitizeString(key) + "';";
136 general_callback_called = false;
137 sqlite3_exec(db, query.c_str(), generalcallback, this, 0);
139 if (general_callback_called)
141 std::string query = "update general set value='" +
142 sanitizeString(newvalue) + "' where key='" + sanitizeString(key) + "';";
143 general_callback_called = false;
144 sqlite3_exec(db, query.c_str(), generalcallback, this, 0);
146 else
148 std::string query = "insert into general values('" +
149 sanitizeString(key) + "', '" + sanitizeString(newvalue) +"');";
150 general_callback_called = false;
151 sqlite3_exec(db, query.c_str(), generalcallback, this, 0);
154 return;
157 std::map<std::string, GameInfo> SettingsDB::getGamePresentations()
159 contents.clear();
160 std::string query = "select * from game_info;";
162 sqlite3_exec(db, query.c_str(), content_callback, this, 0);
164 return contents;
167 void SettingsDB::setGamePresentations(
168 std::map<std::string, GameInfo> presentations)
170 std::map<std::string, GameInfo> current_contents = getGamePresentations();
172 std::map<std::string, GameInfo>::iterator iter;
173 for (iter = presentations.begin(); iter != presentations.end(); iter++)
175 std::string query;
176 if (current_contents.find(iter->first) == current_contents.end())
177 query = "insert into game_info values('" +
178 sanitizeString(iter->first) + "', '" +
179 sanitizeString(iter->second.last_presentation) + "', '" +
180 sanitizeString(iter->second.url) + "', '" +
181 sanitizeString(iter->second.controls) + "');";
183 else
184 query = "update game_info set lastmode='" +
185 sanitizeString(iter->second.last_presentation) +"', url='" +
186 sanitizeString(iter->second.url) + "', controls='" +
187 sanitizeString(iter->second.controls) + "' where gamename='" +
188 sanitizeString(iter->first) + "';";
190 sqlite3_exec(db, query.c_str(), generalcallback, this, 0);
193 return;
196 std::string SettingsDB::sanitizeString(std::string input)
198 pcrecpp::RE replace_stuff("'");
199 replace_stuff.GlobalReplace("''", &input);
201 // hot damn
203 // my original code was buggy and obnoxious and longer than the
204 // above solution
206 // note to self: don't try and reinvent the wheel
208 // unless the existing wheel is shitty
210 return input;
213 int SettingsDB::mainquery(void* sdb, int argc, char** argv,
214 char** colname)
216 SettingsDB* db = static_cast<SettingsDB*>(sdb);
218 if (std::string(argv[1]) == db->to_search)
219 db->table_exists = true;
221 return 0;
224 int SettingsDB::generalcallback(void* sdb, int argc, char** argv,
225 char** colname)
227 SettingsDB* db = static_cast<SettingsDB*>(sdb);
229 db->general_callback_called = true;
230 db->general_callback_result = argv[1];
232 return 0;
235 int SettingsDB::content_callback(void* sdb, int argc, char** argv,
236 char** colname)
238 SettingsDB* db = static_cast<SettingsDB*>(sdb);
240 db->contents[argv[0]] = makeGameInfo(argv[0], argv[2], argv[1]);
242 return 0;