From 2ebc3afc7cf86b77779b477074eab23862e5cb7b Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 25 Mar 2018 14:32:11 +0200 Subject: [PATCH] Refactoring (1) --- PlayerAttributes.cpp | 47 ++++++++++++++++++++++---------------------- TileGenerator.cpp | 2 +- db-redis.cpp | 29 +++++++++------------------ include/util.h | 18 ++++++++++++----- util.cpp | 15 ++++++++++---- 5 files changed, 57 insertions(+), 54 deletions(-) diff --git a/PlayerAttributes.cpp b/PlayerAttributes.cpp index 4bbedea..865e115 100644 --- a/PlayerAttributes.cpp +++ b/PlayerAttributes.cpp @@ -10,8 +10,10 @@ #include #include #include + #include "config.h" #include "PlayerAttributes.h" +#include "util.h" using namespace std; @@ -21,39 +23,36 @@ PlayerAttributes::PlayerAttributes(const std::string &sourceDirectory) string playersPath = sourceDirectory + "players"; DIR *dir; dir = opendir (playersPath.c_str()); - if (dir == NULL) { + if (dir == NULL) return; - } struct dirent *ent; while ((ent = readdir (dir)) != NULL) { - if (ent->d_name[0] == '.') { + if (ent->d_name[0] == '.') continue; - } 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; - istringstream positionStream(position, istringstream::in); - positionStream >> player.x; - positionStream >> comma; - positionStream >> player.y; - positionStream >> comma; - positionStream >> player.z; + istringstream iss(position); + char tmp; + iss >> tmp; // '(' + iss >> player.x; + iss >> tmp; // ',' + iss >> player.y; + iss >> tmp; // ',' + iss >> player.z; + iss >> tmp; // ')' + if(tmp != ')') + continue; player.name = name; player.x /= 10.0; diff --git a/TileGenerator.cpp b/TileGenerator.cpp index 9135bfc..4369af3 100644 --- a/TileGenerator.cpp +++ b/TileGenerator.cpp @@ -290,7 +290,7 @@ void TileGenerator::openDb(const std::string &input) std::ifstream ifs((input + "/world.mt").c_str()); if(!ifs.good()) throw std::runtime_error("Failed to read world.mt"); - backend = get_setting("backend", ifs); + backend = read_setting("backend", ifs); ifs.close(); } diff --git a/db-redis.cpp b/db-redis.cpp index d6e2561..53a5660 100644 --- a/db-redis.cpp +++ b/db-redis.cpp @@ -28,35 +28,24 @@ static inline std::string i64tos(int64_t i) 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) { std::ifstream ifs((mapdir + "/world.mt").c_str()); if(!ifs.good()) throw std::runtime_error("Failed to read world.mt"); std::string tmp; - try { - tmp = get_setting("redis_address", ifs); - ifs.seekg(0); - hash = get_setting("redis_hash", ifs); - 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"); - } + + tmp = read_setting("redis_address", ifs); + ifs.seekg(0); + hash = read_setting("redis_hash", ifs); + ifs.seekg(0); + 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); - if(!ctx) + if(!ctx) { 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; redisFree(ctx); throw std::runtime_error(err); diff --git a/include/util.h b/include/util.h index 4d4ce00..1c7d397 100644 --- a/include/util.h +++ b/include/util.h @@ -1,10 +1,18 @@ -#ifndef _UTIL_H -#define _UTIL_H +#ifndef UTIL_H +#define UTIL_H #include -#include #include -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 diff --git a/util.cpp b/util.cpp index f515ca2..12f1b1c 100644 --- a/util.cpp +++ b/util.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "util.h" 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); } -#define EOFCHECK() \ - if(is.eof()) \ - throw std::runtime_error(((std::string) "Setting '") + name + "' not found"); +#define EOFCHECK() do { \ + if (is.eof()) { \ + 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 s[256];