Skip to content

Commit

Permalink
Read backend from world.mt, closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Jul 8, 2014
1 parent fe534d9 commit 2069118
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 73 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -101,6 +101,7 @@ set(mapper_SRCS
TileGenerator.cpp
ZlibDecompressor.cpp
mapper.cpp
util.cpp
db-sqlite3.cpp
)

Expand Down
21 changes: 11 additions & 10 deletions TileGenerator.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions TileGenerator.h
Expand Up @@ -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;
Expand Down Expand Up @@ -114,4 +113,3 @@ class TileGenerator
}; // class TileGenerator

#endif // TILEGENERATOR_HEADER

56 changes: 1 addition & 55 deletions db-redis.cpp
Expand Up @@ -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)
{
Expand All @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions mapper.cpp
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
56 changes: 56 additions & 0 deletions 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
10 changes: 10 additions & 0 deletions 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

@sfan5 sfan5 commented on 2069118 Jul 11, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, 2cc1ffc

Please sign in to comment.