mirror of
https://github.com/minetest/minetestmapper.git
synced 2024-11-14 14:40:30 +01:00
Refactoring (1)
This commit is contained in:
parent
e543517eb8
commit
2ebc3afc7c
|
@ -10,8 +10,10 @@
|
|||
#include <dirent.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
29
db-redis.cpp
29
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);
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
#ifndef _UTIL_H
|
||||
#define _UTIL_H
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#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
|
||||
|
|
15
util.cpp
15
util.cpp
|
@ -1,3 +1,6 @@
|
|||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
#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];
|
||||
|
|
Loading…
Reference in New Issue
Block a user