Refactoring (1)

This commit is contained in:
sfan5 2018-03-25 14:32:11 +02:00
parent e543517eb8
commit 2ebc3afc7c
5 changed files with 57 additions and 54 deletions

@ -10,8 +10,10 @@
#include <dirent.h> #include <dirent.h>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include "config.h" #include "config.h"
#include "PlayerAttributes.h" #include "PlayerAttributes.h"
#include "util.h"
using namespace std; using namespace std;
@ -21,39 +23,36 @@ PlayerAttributes::PlayerAttributes(const std::string &sourceDirectory)
string playersPath = sourceDirectory + "players"; string playersPath = sourceDirectory + "players";
DIR *dir; DIR *dir;
dir = opendir (playersPath.c_str()); dir = opendir (playersPath.c_str());
if (dir == NULL) { if (dir == NULL)
return; return;
}
struct dirent *ent; struct dirent *ent;
while ((ent = readdir (dir)) != NULL) { while ((ent = readdir (dir)) != NULL) {
if (ent->d_name[0] == '.') { if (ent->d_name[0] == '.')
continue; continue;
}
string path = playersPath + PATH_SEPARATOR + ent->d_name; string path = playersPath + PATH_SEPARATOR + ent->d_name;
ifstream in(path.c_str());
if(!in.good())
continue;
string name, position;
name = read_setting("name", in);
in.seekg(0);
position = read_setting("position", in);
ifstream in;
in.open(path.c_str(), ifstream::in);
string buffer;
string name;
string position;
while (getline(in, buffer)) {
if (buffer.find("name = ") == 0) {
name = buffer.substr(7);
}
else if (buffer.find("position = ") == 0) {
position = buffer.substr(12, buffer.length() - 13);
}
}
char comma;
Player player; Player player;
istringstream positionStream(position, istringstream::in); istringstream iss(position);
positionStream >> player.x; char tmp;
positionStream >> comma; iss >> tmp; // '('
positionStream >> player.y; iss >> player.x;
positionStream >> comma; iss >> tmp; // ','
positionStream >> player.z; iss >> player.y;
iss >> tmp; // ','
iss >> player.z;
iss >> tmp; // ')'
if(tmp != ')')
continue;
player.name = name; player.name = name;
player.x /= 10.0; player.x /= 10.0;

@ -290,7 +290,7 @@ void TileGenerator::openDb(const std::string &input)
std::ifstream ifs((input + "/world.mt").c_str()); std::ifstream ifs((input + "/world.mt").c_str());
if(!ifs.good()) if(!ifs.good())
throw std::runtime_error("Failed to read world.mt"); throw std::runtime_error("Failed to read world.mt");
backend = get_setting("backend", ifs); backend = read_setting("backend", ifs);
ifs.close(); ifs.close();
} }

@ -28,35 +28,24 @@ static inline std::string i64tos(int64_t i)
return os.str(); return os.str();
} }
std::string get_setting_default(std::string name, std::istream &is, const std::string def)
{
try {
return get_setting(name, is);
} catch(std::runtime_error e) {
return def;
}
}
DBRedis::DBRedis(const std::string &mapdir) DBRedis::DBRedis(const std::string &mapdir)
{ {
std::ifstream ifs((mapdir + "/world.mt").c_str()); std::ifstream ifs((mapdir + "/world.mt").c_str());
if(!ifs.good()) if(!ifs.good())
throw std::runtime_error("Failed to read world.mt"); throw std::runtime_error("Failed to read world.mt");
std::string tmp; std::string tmp;
try {
tmp = get_setting("redis_address", ifs); tmp = read_setting("redis_address", ifs);
ifs.seekg(0); ifs.seekg(0);
hash = get_setting("redis_hash", ifs); hash = read_setting("redis_hash", ifs);
ifs.seekg(0); ifs.seekg(0);
} catch(std::runtime_error e) {
throw std::runtime_error("Set redis_address and redis_hash in world.mt to use the redis backend");
}
const char *addr = tmp.c_str(); const char *addr = tmp.c_str();
int port = stoi64(get_setting_default("redis_port", ifs, "6379")); int port = stoi64(read_setting_default("redis_port", ifs, "6379"));
ctx = tmp.find('/') != std::string::npos ? redisConnectUnix(addr) : redisConnect(addr, port); ctx = tmp.find('/') != std::string::npos ? redisConnectUnix(addr) : redisConnect(addr, port);
if(!ctx) if(!ctx) {
throw std::runtime_error("Cannot allocate redis context"); throw std::runtime_error("Cannot allocate redis context");
else if(ctx->err) { } else if(ctx->err) {
std::string err = std::string("Connection error: ") + ctx->errstr; std::string err = std::string("Connection error: ") + ctx->errstr;
redisFree(ctx); redisFree(ctx);
throw std::runtime_error(err); throw std::runtime_error(err);

@ -1,10 +1,18 @@
#ifndef _UTIL_H #ifndef UTIL_H
#define _UTIL_H #define UTIL_H
#include <string> #include <string>
#include <stdexcept>
#include <fstream> #include <fstream>
std::string get_setting(std::string name, std::istream &is); std::string read_setting(const std::string &name, std::istream &is);
#endif // _UTIL_H inline std::string read_setting_default(const std::string &name, std::istream &is, const std::string &def)
{
try {
return read_setting(name, is);
} catch(std::runtime_error e) {
return def;
}
}
#endif // UTIL_H

@ -1,3 +1,6 @@
#include <stdexcept>
#include <sstream>
#include "util.h" #include "util.h"
inline std::string trim(const std::string &s) inline std::string trim(const std::string &s)
@ -23,11 +26,15 @@ inline std::string trim(const std::string &s)
return s.substr(front, back - front); return s.substr(front, back - front);
} }
#define EOFCHECK() \ #define EOFCHECK() do { \
if(is.eof()) \ if (is.eof()) { \
throw std::runtime_error(((std::string) "Setting '") + name + "' not found"); std::ostringstream oss; \
oss << "Setting '" << name << "' not found."; \
throw std::runtime_error(oss.str()); \
} \
} while(0)
std::string get_setting(std::string name, std::istream &is) std::string read_setting(const std::string &name, std::istream &is)
{ {
char c; char c;
char s[256]; char s[256];