Skip to content

Commit 2069118

Browse files
committedJul 8, 2014
Read backend from world.mt, closes #6
1 parent fe534d9 commit 2069118

File tree

7 files changed

+80
-73
lines changed

7 files changed

+80
-73
lines changed
 

Diff for: ‎CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ set(mapper_SRCS
101101
TileGenerator.cpp
102102
ZlibDecompressor.cpp
103103
mapper.cpp
104+
util.cpp
104105
db-sqlite3.cpp
105106
)
106107

Diff for: ‎TileGenerator.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "PlayerAttributes.h"
1414
#include "TileGenerator.h"
1515
#include "ZlibDecompressor.h"
16+
#include "util.h"
1617
#include "db-sqlite3.h"
1718
#if USE_LEVELDB
1819
#include "db-leveldb.h"
@@ -96,7 +97,6 @@ TileGenerator::TileGenerator():
9697
m_drawAlpha(false),
9798
m_shading(true),
9899
m_border(0),
99-
m_backend("sqlite3"),
100100
m_image(0),
101101
m_xMin(INT_MAX),
102102
m_xMax(INT_MIN),
@@ -233,11 +233,6 @@ void TileGenerator::parseColorsFile(const std::string &fileName)
233233
parseColorsStream(in);
234234
}
235235

236-
void TileGenerator::setBackend(std::string backend)
237-
{
238-
m_backend = backend;
239-
}
240-
241236
void TileGenerator::generate(const std::string &input, const std::string &output)
242237
{
243238
string input_path = input;
@@ -289,18 +284,24 @@ void TileGenerator::parseColorsStream(std::istream &in)
289284

290285
void TileGenerator::openDb(const std::string &input)
291286
{
292-
if(m_backend == "sqlite3")
287+
std::ifstream ifs((input + "/world.mt").c_str());
288+
if(!ifs.good())
289+
throw std::runtime_error("Failed to read world.mt");
290+
std::string backend = get_setting("backend", ifs);
291+
ifs.close();
292+
293+
if(backend == "sqlite3")
293294
m_db = new DBSQLite3(input);
294295
#if USE_LEVELDB
295-
else if(m_backend == "leveldb")
296+
else if(backend == "leveldb")
296297
m_db = new DBLevelDB(input);
297298
#endif
298299
#if USE_REDIS
299-
else if(m_backend == "redis")
300+
else if(backend == "redis")
300301
m_db = new DBRedis(input);
301302
#endif
302303
else
303-
throw std::runtime_error(((std::string) "Unknown map backend: ") + m_backend);
304+
throw std::runtime_error(((std::string) "Unknown map backend: ") + backend);
304305
}
305306

306307
void TileGenerator::loadBlocks()

Diff for: ‎TileGenerator.h

-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class TileGenerator
8686
bool m_drawAlpha;
8787
bool m_shading;
8888
int m_border;
89-
std::string m_backend;
9089

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

116115
#endif // TILEGENERATOR_HEADER
117-

Diff for: ‎db-redis.cpp

+1-55
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <fstream>
44
#include "db-redis.h"
55
#include "types.h"
6+
#include "util.h"
67

78
static inline int64_t stoi64(const std::string &s)
89
{
@@ -20,61 +21,6 @@ static inline std::string i64tos(int64_t i)
2021
return os.str();
2122
}
2223

23-
inline std::string trim(const std::string &s)
24-
{
25-
size_t front = 0;
26-
while(s[front] == ' ' ||
27-
s[front] == '\t' ||
28-
s[front] == '\r' ||
29-
s[front] == '\n'
30-
)
31-
++front;
32-
33-
size_t back = s.size();
34-
while(back > front &&
35-
(s[back-1] == ' ' ||
36-
s[back-1] == '\t' ||
37-
s[back-1] == '\r' ||
38-
s[back-1] == '\n'
39-
)
40-
)
41-
--back;
42-
43-
return s.substr(front, back - front);
44-
}
45-
46-
#define EOFCHECK() \
47-
if(is.eof()) \
48-
throw std::runtime_error("setting not found");
49-
50-
std::string get_setting(std::string name, std::istream &is)
51-
{
52-
char c;
53-
char s[256];
54-
std::string nm, value;
55-
56-
next:
57-
while((c = is.get()) == ' ' || c == '\t' || c == '\r' || c == '\n')
58-
;
59-
EOFCHECK();
60-
if(c == '#') // Ignore comments
61-
is.ignore(0xffff, '\n');
62-
EOFCHECK();
63-
s[0] = c; // The current char belongs to the name too
64-
is.get(&s[1], 255, '=');
65-
is.ignore(1); // Jump over the =
66-
EOFCHECK();
67-
nm = trim(std::string(s));
68-
is.get(s, 256, '\n');
69-
value = trim(std::string(s));
70-
if(name == nm)
71-
return value;
72-
else
73-
goto next;
74-
}
75-
76-
#undef EOFCHECK
77-
7824
std::string get_setting_default(std::string name, std::istream &is, const std::string def)
7925
{
8026
try {

Diff for: ‎mapper.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ void usage()
3434
" --noshading\n"
3535
" --min-y <y>\n"
3636
" --max-y <y>\n"
37-
" --backend <sqlite3/leveldb/redis>\n"
3837
" --geometry x:y+w+h\n"
3938
"Color format: '#000000'\n";
4039
std::cout << usage_text;
@@ -58,8 +57,7 @@ int main(int argc, char *argv[])
5857
{"noshading", no_argument, 0, 'H'},
5958
{"geometry", required_argument, 0, 'g'},
6059
{"min-y", required_argument, 0, 'a'},
61-
{"max-y", required_argument, 0, 'c'},
62-
{"backend", required_argument, 0, 'd'},
60+
{"max-y", required_argument, 0, 'c'}
6361
};
6462

6563
string input;
@@ -145,9 +143,6 @@ int main(int argc, char *argv[])
145143
generator.setGeometry(x, y, w, h);
146144
}
147145
break;
148-
case 'd':
149-
generator.setBackend(optarg);
150-
break;
151146
default:
152147
exit(1);
153148
}

Diff for: ‎util.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "util.h"
2+
3+
inline std::string trim(const std::string &s)
4+
{
5+
size_t front = 0;
6+
while(s[front] == ' ' ||
7+
s[front] == '\t' ||
8+
s[front] == '\r' ||
9+
s[front] == '\n'
10+
)
11+
++front;
12+
13+
size_t back = s.size();
14+
while(back > front &&
15+
(s[back-1] == ' ' ||
16+
s[back-1] == '\t' ||
17+
s[back-1] == '\r' ||
18+
s[back-1] == '\n'
19+
)
20+
)
21+
--back;
22+
23+
return s.substr(front, back - front);
24+
}
25+
26+
#define EOFCHECK() \
27+
if(is.eof()) \
28+
throw std::runtime_error("setting not found");
29+
30+
std::string get_setting(std::string name, std::istream &is)
31+
{
32+
char c;
33+
char s[256];
34+
std::string nm, value;
35+
36+
next:
37+
while((c = is.get()) == ' ' || c == '\t' || c == '\r' || c == '\n')
38+
;
39+
EOFCHECK();
40+
if(c == '#') // Ignore comments
41+
is.ignore(0xffff, '\n');
42+
EOFCHECK();
43+
s[0] = c; // The current char belongs to the name too
44+
is.get(&s[1], 255, '=');
45+
is.ignore(1); // Jump over the =
46+
EOFCHECK();
47+
nm = trim(std::string(s));
48+
is.get(s, 256, '\n');
49+
value = trim(std::string(s));
50+
if(name == nm)
51+
return value;
52+
else
53+
goto next;
54+
}
55+
56+
#undef EOFCHECK

Diff for: ‎util.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _UTIL_H
2+
#define _UTIL_H
3+
4+
#include <string>
5+
#include <stdexcept>
6+
#include <fstream>
7+
8+
std::string get_setting(std::string name, std::istream &is);
9+
10+
#endif // _UTIL_H

2 commit comments

Comments
 (2)

ShadowNinja commented on Jul 10, 2014

@ShadowNinja

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 commented on Jul 11, 2014

@sfan5
Author

done, 2cc1ffc

Please sign in to comment.