From fa3fc18ddfe4b1cd7c48bfc236abee6cfd629bee Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 9 Nov 2013 21:13:00 +1100 Subject: [PATCH] Imported and updated ConfigUtility --- common/config_utility.cpp | 105 ++++++++++++++++++++++++++++++++++ common/config_utility.hpp | 60 +++++++++++++++++++ rsc/config.cfg | 8 +-- server/server_application.cpp | 13 +++-- server/server_application.hpp | 2 + 5 files changed, 179 insertions(+), 9 deletions(-) create mode 100644 common/config_utility.cpp create mode 100644 common/config_utility.hpp diff --git a/common/config_utility.cpp b/common/config_utility.cpp new file mode 100644 index 0000000..a66c730 --- /dev/null +++ b/common/config_utility.cpp @@ -0,0 +1,105 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "config_utility.hpp" + +#include +#include +#include + +using namespace std; + +void ConfigUtility::Load(string fname) { + ifstream is(fname); + + if (!is.is_open()) { + throw(runtime_error("Failed to open config file")); + } + + string key, val; + + for (;;) { //forever + //eat whitespace + while(isspace(is.peek())) + is.ignore(); + + //end of file + if (is.eof()) + break; + + //skip comment lines + if (is.peek() == '#') { + while(is.peek() != '\n' && !is.eof()) { + is.ignore(); + } + continue; + } + + //read in the pair + getline(is, key,'='); + getline(is, val); + + //trim the strings at the start & end + while(key.size() && isspace(*key.begin())) key.erase(0, 1); + while(val.size() && isspace(*val.begin())) val.erase(0, 1); + + while(key.size() && isspace(*(key.end()-1))) key.erase(key.end() - 1); + while(val.size() && isspace(*(val.end()-1))) val.erase(val.end() - 1); + + //allow empty/wiped values + if (key.size() == 0) { + continue; + } + + //save the pair + table[key] = val; + } + + is.close(); +} + +std::string& ConfigUtility::String(std::string s) { + return table[s]; +} + +int ConfigUtility::Integer(std::string s) { + std::map::iterator it = table.find(s); + if (it == table.end()) { + return 0; + } + return atoi(it->second.c_str()); +} + +double ConfigUtility::Double(std::string s) { + std::map::iterator it = table.find(s); + if (it == table.end()) { + return 0.0; + } + return atof(it->second.c_str()); +} + +bool ConfigUtility::Boolean(std::string s) { + std::map::iterator it = table.find(s); + if (it == table.end()) { + return false; + } + return it->second == "true"; +} diff --git a/common/config_utility.hpp b/common/config_utility.hpp new file mode 100644 index 0000000..3de540f --- /dev/null +++ b/common/config_utility.hpp @@ -0,0 +1,60 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef CONFIGUTILITY_HPP_ +#define CONFIGUTILITY_HPP_ + +#include +#include + +class ConfigUtility { +public: + ConfigUtility() = default; + ConfigUtility(std::string s) { Load(s); } + + void Load(std::string fname); + + //convert to a type + std::string& String(std::string); + int Integer(std::string); + double Double(std::string); + bool Boolean(std::string); + + //shorthand + std::string& operator[](std::string s) { + return String(s); + } + int Int(std::string s) { + return Integer(s); + } + bool Bool(std::string s) { + return Boolean(s); + } + + //OO breaker + std::map* GetMap() { + return &table; + } +private: + std::map table; +}; + +#endif diff --git a/rsc/config.cfg b/rsc/config.cfg index 0a83e92..aaef46c 100644 --- a/rsc/config.cfg +++ b/rsc/config.cfg @@ -1,8 +1,10 @@ #configuration of the programs server.host = 127.0.0.1 -server.port = 1991 +server.port = 21795 server.name = foobar +dbname = database.db + screen.w = 800 screen.h = 600 screen.f = false @@ -13,8 +15,6 @@ logos = rsc/graphics/logos sprites = rsc/graphics/sprites tilesets = rsc/graphics/tilesets interface = rsc/graphics/interface +scripts = rsc/scripts #debugging -debug = true -avatar = elliot -handle = UserName diff --git a/server/server_application.cpp b/server/server_application.cpp index adac213..cfc8c06 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -83,6 +83,9 @@ void ServerApplication::Init(int argc, char** argv) { } running = true; + //load config + config.Load("rsc\\config.cfg"); + //Init SDL if (SDL_Init(0)) { throw(runtime_error("Failed to initialize SDL")); @@ -93,17 +96,17 @@ void ServerApplication::Init(int argc, char** argv) { if (SDLNet_Init()) { throw(runtime_error("Failed to init SDL_net")); } - networkUtil.Open(21795, sizeof(NetworkPacket)); + networkUtil.Open(config.Int("server.port"), sizeof(NetworkPacket)); cout << "initialized SDL_net" << endl; //Init SQL - string dbname = (argc > 1) ? argv[1] : argv[0]; //fancy and unnecessary - int ret = sqlite3_open_v2((dbname + ".db").c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_FULLMUTEX, nullptr); + string dbname = (config["dbname"].size()) ? config["dbname"] : std::string(argv[0]) + ".db"; //fancy and unnecessary + int ret = sqlite3_open_v2(dbname.c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_FULLMUTEX, nullptr); if (ret != SQLITE_OK || !database) { throw(runtime_error("Failed to open the server database")); } cout << "initialized SQL" << endl; - cout << "Database filename: \"" << dbname << ".db\"" << endl; + cout << "Database filename: " << dbname << endl; //TODO: move this into a function? //Run setup scripts @@ -127,7 +130,7 @@ void ServerApplication::Init(int argc, char** argv) { NetworkPacket packet; packet.meta.type = NetworkPacket::Type::PING; strcpy(packet.serverInfo.name, "foo"); - networkUtil.Send("127.0.0.1", 21795, &packet, sizeof(NetworkPacket)); + networkUtil.Send(config["server.host"].c_str(), config.Int("server.port"), &packet, sizeof(NetworkPacket)); } void ServerApplication::Loop() { diff --git a/server/server_application.hpp b/server/server_application.hpp index d7ee509..2dd5290 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -24,6 +24,7 @@ #include "udp_network_utility.hpp" #include "network_queue.hpp" +#include "config_utility.hpp" #include "sqlite3/sqlite3.h" #include "SDL/SDL.h" @@ -48,6 +49,7 @@ private: //members bool running = false; + ConfigUtility config; //networking UDPNetworkUtility networkUtil;