From 20691186b54e0965101a06b648765f1b2d76fa94 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 8 Jul 2014 14:54:27 +0200 Subject: [PATCH] Read backend from world.mt, closes #6 --- CMakeLists.txt | 1 + TileGenerator.cpp | 21 +++++++++--------- TileGenerator.h | 2 -- db-redis.cpp | 56 +---------------------------------------------- mapper.cpp | 7 +----- util.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++ util.h | 10 +++++++++ 7 files changed, 80 insertions(+), 73 deletions(-) create mode 100644 util.cpp create mode 100644 util.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d23851b..ce358de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,6 +101,7 @@ set(mapper_SRCS TileGenerator.cpp ZlibDecompressor.cpp mapper.cpp + util.cpp db-sqlite3.cpp ) diff --git a/TileGenerator.cpp b/TileGenerator.cpp index 2406f7d..d8169e6 100644 --- a/TileGenerator.cpp +++ b/TileGenerator.cpp @@ -13,6 +13,7 @@ #include "PlayerAttributes.h" #include "TileGenerator.h" #include "ZlibDecompressor.h" +#include "util.h" #include "db-sqlite3.h" #if USE_LEVELDB #include "db-leveldb.h" @@ -96,7 +97,6 @@ TileGenerator::TileGenerator(): m_drawAlpha(false), m_shading(true), m_border(0), - m_backend("sqlite3"), m_image(0), m_xMin(INT_MAX), m_xMax(INT_MIN), @@ -233,11 +233,6 @@ void TileGenerator::parseColorsFile(const std::string &fileName) parseColorsStream(in); } -void TileGenerator::setBackend(std::string backend) -{ - m_backend = backend; -} - void TileGenerator::generate(const std::string &input, const std::string &output) { string input_path = input; @@ -289,18 +284,24 @@ void TileGenerator::parseColorsStream(std::istream &in) void TileGenerator::openDb(const std::string &input) { - if(m_backend == "sqlite3") + std::ifstream ifs((input + "/world.mt").c_str()); + if(!ifs.good()) + throw std::runtime_error("Failed to read world.mt"); + std::string backend = get_setting("backend", ifs); + ifs.close(); + + if(backend == "sqlite3") m_db = new DBSQLite3(input); #if USE_LEVELDB - else if(m_backend == "leveldb") + else if(backend == "leveldb") m_db = new DBLevelDB(input); #endif #if USE_REDIS - else if(m_backend == "redis") + else if(backend == "redis") m_db = new DBRedis(input); #endif else - throw std::runtime_error(((std::string) "Unknown map backend: ") + m_backend); + throw std::runtime_error(((std::string) "Unknown map backend: ") + backend); } void TileGenerator::loadBlocks() diff --git a/TileGenerator.h b/TileGenerator.h index 8a844c8..ce3525d 100644 --- a/TileGenerator.h +++ b/TileGenerator.h @@ -86,7 +86,6 @@ private: bool m_drawAlpha; bool m_shading; int m_border; - std::string m_backend; DB *m_db; gdImagePtr m_image; @@ -114,4 +113,3 @@ private: }; // class TileGenerator #endif // TILEGENERATOR_HEADER - diff --git a/db-redis.cpp b/db-redis.cpp index c09521d..c6f6564 100644 --- a/db-redis.cpp +++ b/db-redis.cpp @@ -3,6 +3,7 @@ #include #include "db-redis.h" #include "types.h" +#include "util.h" static inline int64_t stoi64(const std::string &s) { @@ -20,61 +21,6 @@ static inline std::string i64tos(int64_t i) return os.str(); } -inline std::string trim(const std::string &s) -{ - size_t front = 0; - while(s[front] == ' ' || - s[front] == '\t' || - s[front] == '\r' || - s[front] == '\n' - ) - ++front; - - size_t back = s.size(); - while(back > front && - (s[back-1] == ' ' || - s[back-1] == '\t' || - s[back-1] == '\r' || - s[back-1] == '\n' - ) - ) - --back; - - return s.substr(front, back - front); -} - -#define EOFCHECK() \ - if(is.eof()) \ - throw std::runtime_error("setting not found"); - -std::string get_setting(std::string name, std::istream &is) -{ - char c; - char s[256]; - std::string nm, value; - - next: - while((c = is.get()) == ' ' || c == '\t' || c == '\r' || c == '\n') - ; - EOFCHECK(); - if(c == '#') // Ignore comments - is.ignore(0xffff, '\n'); - EOFCHECK(); - s[0] = c; // The current char belongs to the name too - is.get(&s[1], 255, '='); - is.ignore(1); // Jump over the = - EOFCHECK(); - nm = trim(std::string(s)); - is.get(s, 256, '\n'); - value = trim(std::string(s)); - if(name == nm) - return value; - else - goto next; -} - -#undef EOFCHECK - std::string get_setting_default(std::string name, std::istream &is, const std::string def) { try { diff --git a/mapper.cpp b/mapper.cpp index a661511..80be4b6 100644 --- a/mapper.cpp +++ b/mapper.cpp @@ -34,7 +34,6 @@ void usage() " --noshading\n" " --min-y \n" " --max-y \n" - " --backend \n" " --geometry x:y+w+h\n" "Color format: '#000000'\n"; std::cout << usage_text; @@ -58,8 +57,7 @@ int main(int argc, char *argv[]) {"noshading", no_argument, 0, 'H'}, {"geometry", required_argument, 0, 'g'}, {"min-y", required_argument, 0, 'a'}, - {"max-y", required_argument, 0, 'c'}, - {"backend", required_argument, 0, 'd'}, + {"max-y", required_argument, 0, 'c'} }; string input; @@ -145,9 +143,6 @@ int main(int argc, char *argv[]) generator.setGeometry(x, y, w, h); } break; - case 'd': - generator.setBackend(optarg); - break; default: exit(1); } diff --git a/util.cpp b/util.cpp new file mode 100644 index 0000000..72541d1 --- /dev/null +++ b/util.cpp @@ -0,0 +1,56 @@ +#include "util.h" + +inline std::string trim(const std::string &s) +{ + size_t front = 0; + while(s[front] == ' ' || + s[front] == '\t' || + s[front] == '\r' || + s[front] == '\n' + ) + ++front; + + size_t back = s.size(); + while(back > front && + (s[back-1] == ' ' || + s[back-1] == '\t' || + s[back-1] == '\r' || + s[back-1] == '\n' + ) + ) + --back; + + return s.substr(front, back - front); +} + +#define EOFCHECK() \ + if(is.eof()) \ + throw std::runtime_error("setting not found"); + +std::string get_setting(std::string name, std::istream &is) +{ + char c; + char s[256]; + std::string nm, value; + + next: + while((c = is.get()) == ' ' || c == '\t' || c == '\r' || c == '\n') + ; + EOFCHECK(); + if(c == '#') // Ignore comments + is.ignore(0xffff, '\n'); + EOFCHECK(); + s[0] = c; // The current char belongs to the name too + is.get(&s[1], 255, '='); + is.ignore(1); // Jump over the = + EOFCHECK(); + nm = trim(std::string(s)); + is.get(s, 256, '\n'); + value = trim(std::string(s)); + if(name == nm) + return value; + else + goto next; +} + +#undef EOFCHECK diff --git a/util.h b/util.h new file mode 100644 index 0000000..4d4ce00 --- /dev/null +++ b/util.h @@ -0,0 +1,10 @@ +#ifndef _UTIL_H +#define _UTIL_H + +#include +#include +#include + +std::string get_setting(std::string name, std::istream &is); + +#endif // _UTIL_H