Imported ConfigUtility, updating it for this project

This commit is contained in:
Kayne Ruse
2013-05-03 02:22:34 +10:00
parent f349e59fd3
commit 96f0821d9b
6 changed files with 195 additions and 3 deletions
+55
View File
@@ -0,0 +1,55 @@
#include "config_utility.hpp"
#include <stdexcept>
#include <fstream>
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();
}
+41
View File
@@ -0,0 +1,41 @@
#ifndef CONFIGUTILITY_HPP_
#define CONFIGUTILITY_HPP_
#include <map>
#include <string>
#include <cstdlib>
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<std::string, std::string>* GetMap() {
return &table;
}
private:
std::map<std::string, std::string> table;
};
#endif
+2 -2
View File
@@ -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)
+55
View File
@@ -0,0 +1,55 @@
#include "config_utility.hpp"
#include <stdexcept>
#include <fstream>
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();
}
+41
View File
@@ -0,0 +1,41 @@
#ifndef CONFIGUTILITY_HPP_
#define CONFIGUTILITY_HPP_
#include <map>
#include <string>
#include <cstdlib>
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<std::string, std::string>* GetMap() {
return &table;
}
private:
std::map<std::string, std::string> table;
};
#endif
+1 -1
View File
@@ -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