[sosi2osm] 10/30: Add support for parsing the sosi metadata to osm tags in lua

Ruben Undheim rubund-guest at moszumanska.debian.org
Sat Oct 4 13:07:55 UTC 2014


This is an automated email from the git hooks/post-receive script.

rubund-guest pushed a commit to branch upstream
in repository sosi2osm.

commit 6db33571f68636b59e6478dc880bccb87c5154a1
Author: Knut Karevoll <gnonthgol at gmail.com>
Date:   Wed Oct 9 00:09:43 2013 +0200

    Add support for parsing the sosi metadata to osm tags in lua
---
 Makefile        |  4 ++--
 lua/default.lua | 37 +++++++++++++++++++++++++++++++++++
 sosi2osm.cpp    |  6 ++++--
 sosi2osm.h      |  1 +
 tag.cpp         | 60 ++++++++++++++++++++++++++++++++++++++++++---------------
 5 files changed, 88 insertions(+), 20 deletions(-)

diff --git a/Makefile b/Makefile
index 1f0a8e3..d85e779 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
 PROGNAME=sosi2osm
 OBJFILES=sosi2osm.o sosi.o tag.o node.o
 
-CPPFLAGS = -I /usr/local/include/fyba/ -DLINUX -DUNIX -g
-LDFLAGS = -lfyba -lfygm -lfyut -lproj
+CPPFLAGS = -I /usr/local/include/fyba/ `pkg-config --cflags lua5.1-c++` -DLINUX -DUNIX -g
+LDFLAGS = -lfyba -lfygm -lfyut -lproj `pkg-config --libs lua5.1-c++`
 
 all: $(PROGNAME)
 
diff --git a/lua/default.lua b/lua/default.lua
new file mode 100755
index 0000000..9c22d17
--- /dev/null
+++ b/lua/default.lua
@@ -0,0 +1,37 @@
+#! /usr/bin/env lua
+
+out = {}
+
+for i, line in pairs(info) do
+    indent = 0
+    while line:byte() == 46 do
+        line = line:sub(2)
+        indent = indent +1
+    end
+    
+    if indent == 1 then line = line:sub(1, -2) end
+    
+    tokens = {}
+    for x in string.gmatch(line, "(%S+)") do
+        if #tokens > 0 and string.byte(tokens[#tokens]) == 34 then
+            tokens[#tokens] = table.concat({tokens[#tokens], x}, " ")
+        else
+            tokens[#tokens+1] = x
+        end
+        
+        if #tokens[#tokens] > 1
+            and string.byte(tokens[#tokens], 1) == 34
+            and string.byte(tokens[#tokens], -1) == 34 then
+            tokens[#tokens] = tokens[#tokens]:sub(2, -2)
+        end
+    end
+    
+    if #tokens == 2 then
+        out[tokens[1]] = tokens[2]
+    elseif #tokens > 2 then
+        out[tokens[1]] = table.concat(tokens, " ", 2)
+    end
+end
+
+return out
+
diff --git a/sosi2osm.cpp b/sosi2osm.cpp
index 6b6c0e1..6cc99ba 100644
--- a/sosi2osm.cpp
+++ b/sosi2osm.cpp
@@ -4,7 +4,7 @@
 #include "sosi2osm.h"
 
 void usage() {
-    printf("Usage: sosi2osm [sosi file]\n");
+    printf("Usage: sosi2osm [sosi file] [lua file]\n");
 }
 
 void handleHead() {
@@ -58,13 +58,15 @@ void outputRelation() {
 }
 
 int main(int argc, char** args) {
-    if (argc != 2) {
+    if (argc != 3) {
         usage();
         return 1;
     }
     
     char* input_filename = args[1];
     
+    loadLua(args[2]);
+    
     if (!openSOSI(input_filename)) {
         closeSOSI();
         return 1;
diff --git a/sosi2osm.h b/sosi2osm.h
index 13e4e79..2a09619 100644
--- a/sosi2osm.h
+++ b/sosi2osm.h
@@ -18,6 +18,7 @@ long getSOSIRefsSize();
 // Tag
 
 void setEncoding(char* encoding);
+void loadLua(char* filename);
 void outputTags();
 
 // Node
diff --git a/tag.cpp b/tag.cpp
index 4edc975..3f62fda 100644
--- a/tag.cpp
+++ b/tag.cpp
@@ -1,7 +1,11 @@
 #include "sosi2osm.h"
 
 #include <string.h>
+#include <stdlib.h>
 #include <iconv.h>
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
 
 iconv_t charDescriptor;
 void setEncoding(char* encoding) {
@@ -26,25 +30,49 @@ char* toUTF8(char* in, char* outBuf, size_t outlen) {
     return outBuf;
 }
 
+lua_State *state;
+void loadLua(char* filename) {
+    state = luaL_newstate();
+    luaL_openlibs(state);
+    int r = luaL_loadfile (state, filename);
+    if (r) {
+        fprintf(stderr, "Failed to load lua file %s\n", filename);
+        exit(1);
+    }
+    
+    lua_setglobal(state, "getTagsFromInfoTable");
+}
+
 void outputTags() {
+    lua_getglobal(state, "getTagsFromInfoTable");
+    lua_newtable(state);
+    int size = 0;
+
     long lines = getSOSILinesLength();
-    for (int i = 1; i < lines; i++) {
+    for (int i = 0; i < lines; i++) {
         char buffer[256];
-        char* key = toUTF8(getSOSILine(i), buffer, 256);
-        if (key != NULL) {
-            while (key[0] == '.') key++;
-            
-            char* value = strchr(key, ' ');
-            if (value != NULL) {
-                value[0] = '\0';
-                value++;
-                while (value[0] == '"') value++;
-                char* last = value + strlen(value);
-                while (last[-1] == '"') last--;
-                *last = '\0';
-                
-                printf("<tag k=\"%s\" v=\"%s\"/>", key, value);
-            }
+        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);
+        printf("<tag k=\"%s\" v=\"%s\"/>", key, value);
+        lua_pop(state, 1);
+    }
+    
+    lua_pop(state, 1);
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/sosi2osm.git



More information about the Pkg-grass-devel mailing list