Skip to content
Permalink
Browse files

Read backend from world.mt, closes #6

  • Loading branch information
sfan5 committed Jul 8, 2014
1 parent fe534d9 commit 20691186b54e0965101a06b648765f1b2d76fa94
Showing with 80 additions and 73 deletions.
  1. +1 −0 CMakeLists.txt
  2. +11 −10 TileGenerator.cpp
  3. +0 −2 TileGenerator.h
  4. +1 −55 db-redis.cpp
  5. +1 −6 mapper.cpp
  6. +56 −0 util.cpp
  7. +10 −0 util.h
@@ -101,6 +101,7 @@ set(mapper_SRCS
TileGenerator.cpp
ZlibDecompressor.cpp
mapper.cpp
util.cpp
db-sqlite3.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()
@@ -86,7 +86,6 @@ class TileGenerator
bool m_drawAlpha;
bool m_shading;
int m_border;
std::string m_backend;

DB *m_db;
gdImagePtr m_image;
@@ -114,4 +113,3 @@ class TileGenerator
}; // class TileGenerator

#endif // TILEGENERATOR_HEADER

@@ -3,6 +3,7 @@
#include <fstream>
#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 {
@@ -34,7 +34,6 @@ void usage()
" --noshading\n"
" --min-y <y>\n"
" --max-y <y>\n"
" --backend <sqlite3/leveldb/redis>\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);
}
@@ -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
@@ -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

2 comments on commit 2069118

@ShadowNinja

This comment has been minimized.

Copy link
Member

@ShadowNinja ShadowNinja replied Jul 10, 2014

This doesn't let you override the backend, which was very useful when I was improving the SQLite3 and LevelDB backends as I was switching between them and comparing them a lot.

@sfan5

This comment has been minimized.

Copy link
Member Author

@sfan5 sfan5 replied Jul 11, 2014

done, 2cc1ffc

Please sign in to comment.
You can’t perform that action at this time.