Read backend from world.mt, closes #6

This commit is contained in:
sfan5 2014-07-08 14:54:27 +02:00
parent fe534d9026
commit 20691186b5
7 changed files with 80 additions and 73 deletions

@ -101,6 +101,7 @@ set(mapper_SRCS
TileGenerator.cpp TileGenerator.cpp
ZlibDecompressor.cpp ZlibDecompressor.cpp
mapper.cpp mapper.cpp
util.cpp
db-sqlite3.cpp db-sqlite3.cpp
) )

@ -13,6 +13,7 @@
#include "PlayerAttributes.h" #include "PlayerAttributes.h"
#include "TileGenerator.h" #include "TileGenerator.h"
#include "ZlibDecompressor.h" #include "ZlibDecompressor.h"
#include "util.h"
#include "db-sqlite3.h" #include "db-sqlite3.h"
#if USE_LEVELDB #if USE_LEVELDB
#include "db-leveldb.h" #include "db-leveldb.h"
@ -96,7 +97,6 @@ TileGenerator::TileGenerator():
m_drawAlpha(false), m_drawAlpha(false),
m_shading(true), m_shading(true),
m_border(0), m_border(0),
m_backend("sqlite3"),
m_image(0), m_image(0),
m_xMin(INT_MAX), m_xMin(INT_MAX),
m_xMax(INT_MIN), m_xMax(INT_MIN),
@ -233,11 +233,6 @@ void TileGenerator::parseColorsFile(const std::string &fileName)
parseColorsStream(in); parseColorsStream(in);
} }
void TileGenerator::setBackend(std::string backend)
{
m_backend = backend;
}
void TileGenerator::generate(const std::string &input, const std::string &output) void TileGenerator::generate(const std::string &input, const std::string &output)
{ {
string input_path = input; string input_path = input;
@ -289,18 +284,24 @@ void TileGenerator::parseColorsStream(std::istream &in)
void TileGenerator::openDb(const std::string &input) 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); m_db = new DBSQLite3(input);
#if USE_LEVELDB #if USE_LEVELDB
else if(m_backend == "leveldb") else if(backend == "leveldb")
m_db = new DBLevelDB(input); m_db = new DBLevelDB(input);
#endif #endif
#if USE_REDIS #if USE_REDIS
else if(m_backend == "redis") else if(backend == "redis")
m_db = new DBRedis(input); m_db = new DBRedis(input);
#endif #endif
else 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() void TileGenerator::loadBlocks()

@ -86,7 +86,6 @@ private:
bool m_drawAlpha; bool m_drawAlpha;
bool m_shading; bool m_shading;
int m_border; int m_border;
std::string m_backend;
DB *m_db; DB *m_db;
gdImagePtr m_image; gdImagePtr m_image;
@ -114,4 +113,3 @@ private:
}; // class TileGenerator }; // class TileGenerator
#endif // TILEGENERATOR_HEADER #endif // TILEGENERATOR_HEADER

@ -3,6 +3,7 @@
#include <fstream> #include <fstream>
#include "db-redis.h" #include "db-redis.h"
#include "types.h" #include "types.h"
#include "util.h"
static inline int64_t stoi64(const std::string &s) static inline int64_t stoi64(const std::string &s)
{ {
@ -20,61 +21,6 @@ static inline std::string i64tos(int64_t i)
return os.str(); 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) std::string get_setting_default(std::string name, std::istream &is, const std::string def)
{ {
try { try {

@ -34,7 +34,6 @@ void usage()
" --noshading\n" " --noshading\n"
" --min-y <y>\n" " --min-y <y>\n"
" --max-y <y>\n" " --max-y <y>\n"
" --backend <sqlite3/leveldb/redis>\n"
" --geometry x:y+w+h\n" " --geometry x:y+w+h\n"
"Color format: '#000000'\n"; "Color format: '#000000'\n";
std::cout << usage_text; std::cout << usage_text;
@ -58,8 +57,7 @@ int main(int argc, char *argv[])
{"noshading", no_argument, 0, 'H'}, {"noshading", no_argument, 0, 'H'},
{"geometry", required_argument, 0, 'g'}, {"geometry", required_argument, 0, 'g'},
{"min-y", required_argument, 0, 'a'}, {"min-y", required_argument, 0, 'a'},
{"max-y", required_argument, 0, 'c'}, {"max-y", required_argument, 0, 'c'}
{"backend", required_argument, 0, 'd'},
}; };
string input; string input;
@ -145,9 +143,6 @@ int main(int argc, char *argv[])
generator.setGeometry(x, y, w, h); generator.setGeometry(x, y, w, h);
} }
break; break;
case 'd':
generator.setBackend(optarg);
break;
default: default:
exit(1); exit(1);
} }

56
util.cpp Normal file

@ -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

10
util.h Normal file

@ -0,0 +1,10 @@
#ifndef _UTIL_H
#define _UTIL_H
#include <string>
#include <stdexcept>
#include <fstream>
std::string get_setting(std::string name, std::istream &is);
#endif // _UTIL_H