From a69bbfe23cb1d99baa7b44dbd88be205be285c9c Mon Sep 17 00:00:00 2001 From: Brian Caine Date: Thu, 24 Jul 2008 22:08:42 -0400 Subject: [PATCH] Fixed Error So yeah. The SettingsDB::sanitizeString() function was buggy. See the comment if you care. Which you don't. --- include/core/Game.h | 1 + include/core/SettingsDB.h | 5 ++++- include/core/parsegame.h | 1 + src/core/Game.cpp | 8 ++++++++ src/core/SettingsDB.cpp | 29 +++++++++++++++++++---------- src/core/parsegame.cpp | 31 +++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 11 deletions(-) diff --git a/include/core/Game.h b/include/core/Game.h index 88a59d0..e333733 100644 --- a/include/core/Game.h +++ b/include/core/Game.h @@ -66,6 +66,7 @@ namespace fragrant std::string base; std::vector presentations; + std::map controls; std::string graphics_plugin; std::string scripting_plugin; diff --git a/include/core/SettingsDB.h b/include/core/SettingsDB.h index b242929..603cd6d 100644 --- a/include/core/SettingsDB.h +++ b/include/core/SettingsDB.h @@ -37,8 +37,11 @@ namespace fragrant std::string name; std::string url; std::string last_presentation; + std::string controls; }; - GameInfo makeGameInfo(std::string, std::string, std::string); + + GameInfo makeGameInfo(std::string, std::string, std::string, + std::string controls = ""); class SettingsDB { diff --git a/include/core/parsegame.h b/include/core/parsegame.h index 0a205d9..9cba2a7 100644 --- a/include/core/parsegame.h +++ b/include/core/parsegame.h @@ -104,6 +104,7 @@ namespace XMLParser }; game parsegameFromBuffer(std::string); + controls parsecontrolsFromBuffer(std::string); game parsegame(xmlpp::Node*); std::string parsename(xmlpp::Node*); diff --git a/src/core/Game.cpp b/src/core/Game.cpp index 7cd1954..8fbe17c 100644 --- a/src/core/Game.cpp +++ b/src/core/Game.cpp @@ -96,6 +96,8 @@ Game::Game(GameData gdata, std::string dpresentation, data = gdata; level = 0; + // do the control thing here; debug + for (cur = 0; cur < gdata.levels.size(); cur++) { LevelData tdata; @@ -502,6 +504,12 @@ GameData Game::parseXML(std::string path, MediaLoader& media_loader) results.presentations.push_back(temp_presentation); } + for (cur = 0; cur < xml_game.m_controls.m_event.size(); cur++) + { + XMLParser::parsegame::event event = xml_game.m_controls.m_event.at(cur); + results.controls[event.m_name] = event.m_default; + } + results.graphics_plugin = xml_game.m_plugins.m_graphics; results.input_plugin = xml_game.m_plugins.m_input; results.audio_plugin = xml_game.m_plugins.m_audio; diff --git a/src/core/SettingsDB.cpp b/src/core/SettingsDB.cpp index f135f6b..301f5d8 100644 --- a/src/core/SettingsDB.cpp +++ b/src/core/SettingsDB.cpp @@ -22,18 +22,20 @@ #include #include +#include #include "../../include/core/SettingsDB.h" using namespace fragrant; GameInfo fragrant::makeGameInfo(std::string name, - std::string url, std::string last) + std::string url, std::string last, std::string controls) { GameInfo results; results.name = name; results.url = url; results.last_presentation = last; + results.controls = controls; return results; } @@ -70,7 +72,8 @@ SettingsDB::SettingsDB(std::string dblocation) if (!table_exists) { std::string query = "create table game_info(gamename varchar[10]," - " lastmode varchar[20], url varchar[40]);"; + " lastmode varchar[20], url varchar[40], cont" + "rols varchar[100]);"; sqlite3_exec(db, query.c_str(), mainquery, this, 0); } @@ -174,12 +177,14 @@ void SettingsDB::setGamePresentations( query = "insert into game_info values('" + sanitizeString(iter->first) + "', '" + sanitizeString(iter->second.last_presentation) + "', '" + - sanitizeString(iter->second.url) + "');"; + sanitizeString(iter->second.url) + "', '" + + sanitizeString(iter->second.controls) + "');"; else query = "update game_info set lastmode='" + sanitizeString(iter->second.last_presentation) +"', url='" + - sanitizeString(iter->second.url) + "' where gamename='" + + sanitizeString(iter->second.url) + "', controls='" + + sanitizeString(iter->second.controls) + "' where gamename='" + sanitizeString(iter->first) + "';"; sqlite3_exec(db, query.c_str(), generalcallback, this, 0); @@ -190,13 +195,17 @@ void SettingsDB::setGamePresentations( std::string SettingsDB::sanitizeString(std::string input) { - int curpos = input.find(std::string("'")); + pcrecpp::RE replace_stuff("'"); + replace_stuff.GlobalReplace("''", &input); - while (curpos < input.size()) - { - input.replace(curpos, curpos + 1, std::string("''")); - curpos = input.find(std::string("'"), curpos + 2); - } + // hot damn + + // my original code was buggy and obnoxious and longer than the + // above solution + + // note to self: don't try and reinvent the wheel + + // unless the existing wheel is shitty return input; } diff --git a/src/core/parsegame.cpp b/src/core/parsegame.cpp index 61303a0..633938d 100644 --- a/src/core/parsegame.cpp +++ b/src/core/parsegame.cpp @@ -515,6 +515,37 @@ game XMLParser::parsegame::parsegameFromBuffer(std::string buffer) return results; } +controls XMLParser::parsegame::parsecontrolsFromBuffer(std::string buffer) +{ + controls results; + + xmlpp::Node* base = 0; + + try + { + xmlpp::DomParser parser; + parser.set_substitute_entities(); + + parser.parse_memory(buffer); + + if (parser) + base = parser.get_document()->get_root_node(); + else + throw std::runtime_error("some weird error"); + + results = XMLParser::parsegame::parsecontrols(base); + } + + catch (const std::exception& e) + { + throw std::runtime_error( + std::string("XMLParser::parsegame::parsecontrolsFromBuffer(): " + "xmlpp error: ") + e.what()); + } + + return results; +} + game XMLParser::parsegame::parsegame(xmlpp::Node* node) { game results; -- 2.11.4.GIT