From 96f0821d9b9d4ed3b6c85d67e6f5b9d82536ee9e Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 3 May 2013 02:22:34 +1000 Subject: [PATCH] Imported ConfigUtility, updating it for this project --- client/config_utility.cpp | 55 +++++++++++++++++++++++++++++++++++++++ client/config_utility.hpp | 41 +++++++++++++++++++++++++++++ client/makefile | 4 +-- server/config_utility.cpp | 55 +++++++++++++++++++++++++++++++++++++++ server/config_utility.hpp | 41 +++++++++++++++++++++++++++++ server/makefile | 2 +- 6 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 client/config_utility.cpp create mode 100644 client/config_utility.hpp create mode 100644 server/config_utility.cpp create mode 100644 server/config_utility.hpp diff --git a/client/config_utility.cpp b/client/config_utility.cpp new file mode 100644 index 0000000..05e9601 --- /dev/null +++ b/client/config_utility.cpp @@ -0,0 +1,55 @@ +#include "config_utility.hpp" + +#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(); +} diff --git a/client/config_utility.hpp b/client/config_utility.hpp new file mode 100644 index 0000000..83ef689 --- /dev/null +++ b/client/config_utility.hpp @@ -0,0 +1,41 @@ +#ifndef CONFIGUTILITY_HPP_ +#define CONFIGUTILITY_HPP_ + +#include +#include +#include + +class ConfigUtility { +public: + ConfigUtility() {} + ConfigUtility(std::string s) { Load(s); } + + void Load(std::string fname); + + std::string String(std::string s) { + return table[s]; + } + const char* CString(std::string s) { + return table[s].c_str(); + } + int Integer(std::string s) { + return atoi(table[s].c_str()); + } + double Double(std::string s) { + return atof(table[s].c_str()); + } + bool Boolean(std::string s) { + return table[s] == "true"; + } + + std::string& operator[](std::string s) { + return table[s]; + } + std::map* GetMap() { + return &table; + } +private: + std::map table; +}; + +#endif diff --git a/client/makefile b/client/makefile index 100cfeb..11f72a1 100644 --- a/client/makefile +++ b/client/makefile @@ -4,14 +4,14 @@ LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi #objects OBJDIR=obj -OBJ=$(addprefix $(OBJDIR)/,base_scene.o scene_manager.o main.o surface_manager.o image.o sprite_sheet.o player.o) +OBJ=$(addprefix $(OBJDIR)/,base_scene.o scene_manager.o main.o surface_manager.o image.o sprite_sheet.o player.o player_manager.o config_utility.o) #output OUTDIR=out OUT=$(addprefix $(OUTDIR)/,a) #source -SRC=test_systems.cpp in_game.cpp player_manager.cpp +SRC=test_systems.cpp in_game.cpp #targets all: $(OBJ) $(OUT) diff --git a/server/config_utility.cpp b/server/config_utility.cpp new file mode 100644 index 0000000..05e9601 --- /dev/null +++ b/server/config_utility.cpp @@ -0,0 +1,55 @@ +#include "config_utility.hpp" + +#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(); +} diff --git a/server/config_utility.hpp b/server/config_utility.hpp new file mode 100644 index 0000000..83ef689 --- /dev/null +++ b/server/config_utility.hpp @@ -0,0 +1,41 @@ +#ifndef CONFIGUTILITY_HPP_ +#define CONFIGUTILITY_HPP_ + +#include +#include +#include + +class ConfigUtility { +public: + ConfigUtility() {} + ConfigUtility(std::string s) { Load(s); } + + void Load(std::string fname); + + std::string String(std::string s) { + return table[s]; + } + const char* CString(std::string s) { + return table[s].c_str(); + } + int Integer(std::string s) { + return atoi(table[s].c_str()); + } + double Double(std::string s) { + return atof(table[s].c_str()); + } + bool Boolean(std::string s) { + return table[s] == "true"; + } + + std::string& operator[](std::string s) { + return table[s]; + } + std::map* GetMap() { + return &table; + } +private: + std::map table; +}; + +#endif diff --git a/server/makefile b/server/makefile index 60a4629..44466c8 100644 --- a/server/makefile +++ b/server/makefile @@ -4,7 +4,7 @@ LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi #objects OBJDIR=obj -OBJ=$(addprefix $(OBJDIR)/,main.o) +OBJ=$(addprefix $(OBJDIR)/,main.o config_utility.o) #output OUTDIR=out