forked from Gnonthgol/sosi2osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.cpp
154 lines (136 loc) · 4.67 KB
/
tag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "sosi2osm.h"
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <iconv.h>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
iconv_t charDescriptor;
void setEncoding(char* encoding) {
charDescriptor = iconv_open("UTF-8", encoding);
}
char* toUTF8(char* in, char* outBuf, size_t outlen) {
if (in == NULL) {
return NULL;
}
size_t inlen = strlen(in)+1;
char* out = outBuf;
iconv(charDescriptor, NULL, NULL, NULL, NULL);
int r = iconv(charDescriptor, &in, &inlen, &out, &outlen);
if (r == -1) {
fprintf(stderr, "Unknown character '%c' (0x%hhx)\n", in[0], in[0]);
exit(1);
}
return outBuf;
}
lua_State *state;
void loadLua(char* filename) {
FILE *file_exist_check;
int r;
state = luaL_newstate();
luaL_openlibs(state);
char filename_string[256];
snprintf(filename_string, 256, "%s/lua/sosi2osm.lua", dirname(execname));
if(file_exist_check = fopen(filename_string, "r")){
fclose(file_exist_check);
r = luaL_loadfile(state, filename_string);
if (r) {
fprintf(stderr, "Failed to load lua library file %s\n", filename_string);
exit(1);
}
}
else {
snprintf(filename_string, 256, "/usr/local/share/sosi2osm/lua/sosi2osm.lua");
if(file_exist_check = fopen(filename_string, "r")){
fclose(file_exist_check);
r = luaL_loadfile(state, filename_string);
if (r) {
fprintf(stderr, "Failed to load lua library file %s\n", filename_string);
exit(1);
}
}
else {
snprintf(filename_string, 256, "/usr/share/sosi2osm/lua/sosi2osm.lua");
if(file_exist_check = fopen(filename_string, "r")){
fclose(file_exist_check);
r = luaL_loadfile(state, filename_string);
if (r) {
fprintf(stderr, "Failed to load lua library file %s\n", filename_string);
exit(1);
}
}
else {
fprintf(stderr, "Could not find sosi2osm.lua. It has to be installed in /usr/share/sosi2osm/lua/ or /usr/local/share/sosi2osm/lua/\n");
exit(1);
}
}
}
lua_call(state, 0, 0);
if(file_exist_check = fopen(filename, "r")) {
fclose(file_exist_check);
r = luaL_loadfile(state, filename);
if (r) {
fprintf(stderr, "Failed to load lua file %s\n", filename);
exit(1);
}
}
else {
snprintf(filename_string, 256, "/usr/local/share/sosi2osm/lua/%s",filename);
if(file_exist_check = fopen(filename_string, "r")) {
fclose(file_exist_check);
r = luaL_loadfile(state, filename_string);
if (r) {
fprintf(stderr, "Failed to load lua file %s\n", filename_string);
exit(1);
}
}
else {
snprintf(filename_string, 256, "/usr/share/sosi2osm/lua/%s",filename);
if(file_exist_check = fopen(filename_string, "r")) {
fclose(file_exist_check);
r = luaL_loadfile(state, filename_string);
if(r) {
fprintf(stderr, "Failed to load lua file %s\n", filename_string);
exit(1);
}
}
else {
fprintf(stderr, "Could not find %s. It has to be installed in /usr/share/sosi2osm/lua/ or /usr/local/share/sosi2osm/lua/\n",filename);
exit(1);
}
}
}
lua_setglobal(state, "getTagsFromInfoTable");
}
void outputTags(FILE* output) {
lua_getglobal(state, "getTagsFromInfoTable");
lua_newtable(state);
int size = 0;
long lines = getSOSILinesLength();
for (int i = 0; i < lines; i++) {
char buffer[256];
char* line = toUTF8(getSOSILine(i), buffer, 256);
if (line != NULL) {
lua_pushinteger(state, ++size);
lua_pushstring(state, line);
lua_settable(state, -3);
}
}
lua_setglobal(state, "info");
lua_call(state, 0, 1);
lua_pushnil(state);
while (lua_next(state, 1) != 0) {
if (lua_type(state, -2) != LUA_TSTRING) {
fprintf(stderr, "Error in lua script: key must be string, got '%s'\n", lua_tostring(state, -2));
exit(1);
}
const char* key = lua_tostring(state, -2);
const char* value = lua_tostring(state, -1);
fprintf(output, "<tag k=\"%s\" v=\"%s\"/>", key, value);
lua_pop(state, 1);
}
lua_pop(state, 1);
}