forked from minetest/minetestmapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes minetest#98
- Loading branch information
Showing
8 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <cstdio> | ||
#include <fstream> | ||
#include <sstream> | ||
#include <stdexcept> | ||
#include <dirent.h> | ||
#include <unistd.h> // for usleep | ||
#include <sqlite3.h> | ||
|
||
#include "config.h" | ||
#include "POIAttributes.h" | ||
#include "util.h" | ||
|
||
POIAttributes::POIAttributes(const std::string &worldDir) | ||
{ | ||
std::ifstream ifs(worldDir + "world.mt"); | ||
if (!ifs.good()) | ||
throw std::runtime_error("Failed to read world.mt"); | ||
std::string backend = read_setting_default("mod_storage_backend", ifs, "sqlite3"); | ||
ifs.close(); | ||
|
||
if (backend == "sqlite3") | ||
readSqlite(worldDir + "mod_storage.sqlite"); | ||
else | ||
throw std::runtime_error(std::string("Unknown poi backend: ") + backend); | ||
} | ||
|
||
/**********/ | ||
|
||
#define SQLRES(f, good) \ | ||
result = (sqlite3_##f); \ | ||
if (result != good) { \ | ||
throw std::runtime_error(sqlite3_errmsg(db));\ | ||
} | ||
#define SQLOK(f) SQLRES(f, SQLITE_OK) | ||
|
||
void POIAttributes::readSqlite(const std::string &db_name) | ||
{ | ||
int result; | ||
sqlite3 *db; | ||
sqlite3_stmt *stmt_get_poi_pos; | ||
|
||
SQLOK(open_v2(db_name.c_str(), &db, SQLITE_OPEN_READONLY | | ||
SQLITE_OPEN_PRIVATECACHE, 0)) | ||
|
||
SQLOK(prepare_v2(db, | ||
"SELECT key, value FROM entries WHERE modname = 'poi'", | ||
-1, &stmt_get_poi_pos, NULL)) | ||
|
||
while ((result = sqlite3_step(stmt_get_poi_pos)) != SQLITE_DONE) { | ||
if (result == SQLITE_BUSY) { // Wait some time and try again | ||
usleep(10000); | ||
} else if (result != SQLITE_ROW) { | ||
throw std::runtime_error(sqlite3_errmsg(db)); | ||
} | ||
|
||
POI poi; | ||
const unsigned char *name_ = sqlite3_column_text(stmt_get_poi_pos, 0); | ||
poi.name = reinterpret_cast<const char*>(name_); | ||
const char *pos_ = reinterpret_cast<const char*>(sqlite3_column_text(stmt_get_poi_pos, 1)); | ||
float x, y, z; | ||
int items = sscanf(pos_, "(%f,%f,%f)", &x, &y, &z); | ||
if (items != 3) { | ||
std::cerr << "Failed to parse POI position '" << pos_ << "'" << std::endl; | ||
return; | ||
} | ||
poi.x = x; | ||
poi.y = y; | ||
poi.z = z; | ||
|
||
m_pois.push_back(poi); | ||
} | ||
|
||
sqlite3_finalize(stmt_get_poi_pos); | ||
sqlite3_close(db); | ||
} | ||
|
||
/**********/ | ||
|
||
POIAttributes::POIs::const_iterator POIAttributes::begin() const | ||
{ | ||
return m_pois.cbegin(); | ||
} | ||
|
||
POIAttributes::POIs::const_iterator POIAttributes::end() const | ||
{ | ||
return m_pois.cend(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include <list> | ||
#include <string> | ||
|
||
struct POI | ||
{ | ||
std::string name; | ||
float x, y, z; | ||
}; | ||
|
||
class POIAttributes | ||
{ | ||
public: | ||
typedef std::list<POI> POIs; | ||
|
||
POIAttributes(const std::string &worldDir); | ||
POIs::const_iterator begin() const; | ||
POIs::const_iterator end() const; | ||
|
||
private: | ||
void readFiles(const std::string &poisPath); | ||
void readSqlite(const std::string &db_name); | ||
|
||
POIs m_pois; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I had to make some assumptions about the fonts. Hopefully at least one of them are available.