From 906c706a3de0d80940ece8220c122947b0c6ffa5 Mon Sep 17 00:00:00 2001 From: Fulup Ar Foll Date: Tue, 22 Aug 2017 18:03:10 +0200 Subject: Fix Lua->json Numeric table --- Controller-afb/ctl-lua.c | 44 ++++++++++--- nbproject/configurations.xml | 146 ++++++++++++++++++++++++------------------- 2 files changed, 115 insertions(+), 75 deletions(-) diff --git a/Controller-afb/ctl-lua.c b/Controller-afb/ctl-lua.c index 2aaa3a5..acadfb9 100644 --- a/Controller-afb/ctl-lua.c +++ b/Controller-afb/ctl-lua.c @@ -165,26 +165,50 @@ STATIC int LuaPushArgument (json_object *argsJ) { STATIC json_object *LuaPopOneArg (lua_State* luaState, int idx); +// Move a table from Internal Lua representation to Json one +// Numeric table are transformed in json array, string one in object +// Mix numeric/string key are not supported STATIC json_object *LuaTableToJson (lua_State* luaState, int index) { - int idx; #define LUA_KEY_INDEX -2 #define LUA_VALUE_INDEX -1 + + int idx; + int tableType; + json_object *tableJ= NULL; - json_object *tableJ= json_object_new_object(); - const char *key; - char number[3]; lua_pushnil(luaState); // 1st key if (index < 0) index--; for (idx=1; lua_next(luaState, index) != 0; idx++) { // uses 'key' (at index -2) and 'value' (at index -1) - if (lua_type(luaState,LUA_KEY_INDEX) == LUA_TSTRING) key= lua_tostring(luaState, LUA_KEY_INDEX); - else { - snprintf(number, sizeof(number),"%d", idx); - key=number; + if (lua_type(luaState,LUA_KEY_INDEX) == LUA_TSTRING) { + + if (!tableJ) { + tableJ= json_object_new_object(); + tableType=LUA_TSTRING; + } else if (tableType != LUA_TSTRING){ + AFB_ERROR("MIX Lua Table with key string/numeric not supported"); + return NULL; + } + + const char *key= lua_tostring(luaState, LUA_KEY_INDEX); + json_object *argJ= LuaPopOneArg(luaState, LUA_VALUE_INDEX); + json_object_object_add(tableJ, key, argJ); + + } else { + if (!tableJ) { + tableJ= json_object_new_array(); + tableType=LUA_TNUMBER; + } else if(tableType == LUA_TNUMBER) { + AFB_ERROR("MIX Lua Table with key string/numeric not supported"); + return NULL; + } + + json_object *argJ= LuaPopOneArg(luaState, LUA_VALUE_INDEX); + json_object_array_add(tableJ, argJ); } - json_object *argJ= LuaPopOneArg(luaState, LUA_VALUE_INDEX); - json_object_object_add(tableJ, key, argJ); + + lua_pop(luaState, 1); // removes 'value'; keeps 'key' for next iteration } diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 3501376..424cd2f 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -166,35 +166,83 @@ - + + + ../../../opt/include/alsa + /usr/include/json-c + Audio-Common + ../../../opt/include + build/Alsa-afb + - + + + build/Alsa-afb + - + + + ../../../opt/include/alsa + /usr/include/json-c + Audio-Common + ../../../opt/include + build/Alsa-afb + - + + + ../../../opt/include/alsa + /usr/include/json-c + Audio-Common + ../../../opt/include + build/Alsa-afb + - + + + ../../../opt/include/alsa + /usr/include/json-c + Audio-Common + ../../../opt/include + build/Alsa-afb + - + + + ../../../opt/include/afb + Audio-Common + /usr/include/json-c + build/Audio-Common + - + + + ../../../opt/include/afb + Audio-Common + /usr/include/json-c + build/Audio-Common + - + + + Audio-Common + /usr/include/json-c + build/Audio-Common + @@ -224,35 +272,13 @@ - - - ../../../opt/include/afb - Controller-afb - /usr/include/json-c - /usr/include/lua5.3 - Audio-Common - ../../../opt/include - build/Controller-afb - - - - - - - ../../../opt/include/afb - Controller-afb - /usr/include/json-c - build/Controller-afb - - - - ../../../opt/include ../../../opt/include/alsa /usr/include/p11-kit-1 /usr/include/json-c + /usr/include/lua5.3 Audio-Common build/Controller-afb @@ -272,10 +298,31 @@ MAX_SND_CARD=16 NATIVE_LINUX TLV_BYTE_SIZE=256 - audio_plugin_sample_EXPORTS + control_afb_EXPORTS + + + + ../../../opt/include/afb + Controller-afb + /usr/include/json-c + build/Controller-afb + + + + + + + ../../../opt/include/afb + Controller-afb + /usr/include/json-c + /usr/include/lua5.3 + build/Controller-afb + + + @@ -514,40 +561,9 @@ - ../../../opt/include - ../../../opt/include/alsa - /usr/include/p11-kit-1 - /usr/include/json-c - Audio-Common - build/Alsa-afb - - - CONTROL_MAXPATH_LEN=255 - MAX_LINEAR_DB_SCALE=24 - MAX_SND_CARD=16 - NATIVE_LINUX - TLV_BYTE_SIZE=256 - alsa_lowlevel_EXPORTS - - - - - - - ../../../opt/include - ../../../opt/include/alsa - /usr/include/p11-kit-1 - /usr/include/json-c - Audio-Common - build/Audio-Common + ../../../opt/include/afb + Alsa-afb - - CONTROL_MAXPATH_LEN=255 - MAX_LINEAR_DB_SCALE=24 - MAX_SND_CARD=16 - NATIVE_LINUX - TLV_BYTE_SIZE=256 - -- cgit 1.2.3-korg