Imported and updated ConfigUtility
This commit is contained in:
@@ -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 <cstdlib>
|
||||||
|
#include <fstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
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<std::string, std::string>::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<std::string, std::string>::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<std::string, std::string>::iterator it = table.find(s);
|
||||||
|
if (it == table.end()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return it->second == "true";
|
||||||
|
}
|
||||||
@@ -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 <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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<std::string, std::string>* GetMap() {
|
||||||
|
return &table;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::map<std::string, std::string> table;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+4
-4
@@ -1,8 +1,10 @@
|
|||||||
#configuration of the programs
|
#configuration of the programs
|
||||||
server.host = 127.0.0.1
|
server.host = 127.0.0.1
|
||||||
server.port = 1991
|
server.port = 21795
|
||||||
server.name = foobar
|
server.name = foobar
|
||||||
|
|
||||||
|
dbname = database.db
|
||||||
|
|
||||||
screen.w = 800
|
screen.w = 800
|
||||||
screen.h = 600
|
screen.h = 600
|
||||||
screen.f = false
|
screen.f = false
|
||||||
@@ -13,8 +15,6 @@ logos = rsc/graphics/logos
|
|||||||
sprites = rsc/graphics/sprites
|
sprites = rsc/graphics/sprites
|
||||||
tilesets = rsc/graphics/tilesets
|
tilesets = rsc/graphics/tilesets
|
||||||
interface = rsc/graphics/interface
|
interface = rsc/graphics/interface
|
||||||
|
scripts = rsc/scripts
|
||||||
|
|
||||||
#debugging
|
#debugging
|
||||||
debug = true
|
|
||||||
avatar = elliot
|
|
||||||
handle = UserName
|
|
||||||
|
|||||||
@@ -83,6 +83,9 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
running = true;
|
running = true;
|
||||||
|
|
||||||
|
//load config
|
||||||
|
config.Load("rsc\\config.cfg");
|
||||||
|
|
||||||
//Init SDL
|
//Init SDL
|
||||||
if (SDL_Init(0)) {
|
if (SDL_Init(0)) {
|
||||||
throw(runtime_error("Failed to initialize SDL"));
|
throw(runtime_error("Failed to initialize SDL"));
|
||||||
@@ -93,17 +96,17 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
if (SDLNet_Init()) {
|
if (SDLNet_Init()) {
|
||||||
throw(runtime_error("Failed to init SDL_net"));
|
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;
|
cout << "initialized SDL_net" << endl;
|
||||||
|
|
||||||
//Init SQL
|
//Init SQL
|
||||||
string dbname = (argc > 1) ? argv[1] : argv[0]; //fancy and unnecessary
|
string dbname = (config["dbname"].size()) ? config["dbname"] : std::string(argv[0]) + ".db"; //fancy and unnecessary
|
||||||
int ret = sqlite3_open_v2((dbname + ".db").c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_FULLMUTEX, nullptr);
|
int ret = sqlite3_open_v2(dbname.c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_FULLMUTEX, nullptr);
|
||||||
if (ret != SQLITE_OK || !database) {
|
if (ret != SQLITE_OK || !database) {
|
||||||
throw(runtime_error("Failed to open the server database"));
|
throw(runtime_error("Failed to open the server database"));
|
||||||
}
|
}
|
||||||
cout << "initialized SQL" << endl;
|
cout << "initialized SQL" << endl;
|
||||||
cout << "Database filename: \"" << dbname << ".db\"" << endl;
|
cout << "Database filename: " << dbname << endl;
|
||||||
|
|
||||||
//TODO: move this into a function?
|
//TODO: move this into a function?
|
||||||
//Run setup scripts
|
//Run setup scripts
|
||||||
@@ -127,7 +130,7 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
NetworkPacket packet;
|
NetworkPacket packet;
|
||||||
packet.meta.type = NetworkPacket::Type::PING;
|
packet.meta.type = NetworkPacket::Type::PING;
|
||||||
strcpy(packet.serverInfo.name, "foo");
|
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() {
|
void ServerApplication::Loop() {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
#include "udp_network_utility.hpp"
|
#include "udp_network_utility.hpp"
|
||||||
#include "network_queue.hpp"
|
#include "network_queue.hpp"
|
||||||
|
#include "config_utility.hpp"
|
||||||
|
|
||||||
#include "sqlite3/sqlite3.h"
|
#include "sqlite3/sqlite3.h"
|
||||||
#include "SDL/SDL.h"
|
#include "SDL/SDL.h"
|
||||||
@@ -48,6 +49,7 @@ private:
|
|||||||
|
|
||||||
//members
|
//members
|
||||||
bool running = false;
|
bool running = false;
|
||||||
|
ConfigUtility config;
|
||||||
|
|
||||||
//networking
|
//networking
|
||||||
UDPNetworkUtility networkUtil;
|
UDPNetworkUtility networkUtil;
|
||||||
|
|||||||
Reference in New Issue
Block a user