From b84bf7ba56d9abb26b50a20d1594b2bbce5e3a8b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 12 Jun 2014 17:34:57 -0700 Subject: [PATCH] add json configuration parser to lua bindings. --- src/CMakeLists.txt | 3 ++ src/bind_config.c | 19 ++++++++ src/bind_config.h | 13 ++++++ src/bindings.c | 2 + src/config.pkg | 8 ++++ src/config.pkg.c | 105 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 150 insertions(+) create mode 100644 src/bind_config.c create mode 100644 src/bind_config.h create mode 100644 src/config.pkg create mode 100644 src/config.pkg.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d52c220ef..64ad949e6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,6 +48,7 @@ MACRO(TOLUA_BINDING PKGFILE FILES) ENDMACRO(TOLUA_BINDING) IF(NOT MSVC) +TOLUA_BINDING(config.pkg bind_config.h) TOLUA_BINDING(process.pkg bind_process.h) TOLUA_BINDING(eressea.pkg bind_eressea.h) TOLUA_BINDING(settings.pkg bind_settings.h) @@ -84,10 +85,12 @@ set(SERVER_SRC main.c console.c helpers.c + config.pkg.c process.pkg.c eressea.pkg.c settings.pkg.c bind_building.c + bind_config.c bind_eressea.c bind_faction.c bind_hashtable.c diff --git a/src/bind_config.c b/src/bind_config.c new file mode 100644 index 000000000..1dc34b82c --- /dev/null +++ b/src/bind_config.c @@ -0,0 +1,19 @@ +#include "bind_config.h" + +#include +#include +#include +#include + +void config_parse(const char *json) +{ + cJSON * conf = cJSON_Parse(json); + if (conf) { + json_config(conf); + cJSON_Delete(conf); + } +} + +void config_read(const char *filename) +{ +} diff --git a/src/bind_config.h b/src/bind_config.h new file mode 100644 index 000000000..7ddc91c92 --- /dev/null +++ b/src/bind_config.h @@ -0,0 +1,13 @@ +#ifndef BIND_ERESSEA_CONFIG_H +#define BIND_ERESSEA_CONFIG_H +#ifdef __cplusplus +extern "C" { +#endif + +void config_parse(const char *json); +void config_read(const char *filename); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/bindings.c b/src/bindings.c index e63499068..6159c665e 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -85,6 +85,7 @@ without prior permission by the authors of Eressea. TOLUA_PKG(eressea); TOLUA_PKG(process); TOLUA_PKG(settings); +TOLUA_PKG(config); int log_lua_error(lua_State * L) { @@ -1058,6 +1059,7 @@ int tolua_bindings_open(lua_State * L) tolua_eressea_open(L); tolua_process_open(L); tolua_settings_open(L); + tolua_config_open(L); /* register user types */ tolua_usertype(L, TOLUA_CAST "spell"); diff --git a/src/config.pkg b/src/config.pkg new file mode 100644 index 000000000..d892a2f1b --- /dev/null +++ b/src/config.pkg @@ -0,0 +1,8 @@ +$#include "bind_config.h" + +module eressea { + module config { + void config_read @ read(const char *filename); + void config_parse @ parse(const char *json); + } +} diff --git a/src/config.pkg.c b/src/config.pkg.c new file mode 100644 index 000000000..44beb9577 --- /dev/null +++ b/src/config.pkg.c @@ -0,0 +1,105 @@ +/* +** Lua binding: config +*/ + +#include "tolua.h" + +#ifndef __cplusplus +#include +#endif +#ifdef __cplusplus + extern "C" int tolua_bnd_takeownership (lua_State* L); // from tolua_map.c +#else + int tolua_bnd_takeownership (lua_State* L); /* from tolua_map.c */ +#endif +#include + +/* Exported function */ +TOLUA_API int tolua_config_open (lua_State* tolua_S); +LUALIB_API int luaopen_config (lua_State* tolua_S); + +#include "bind_config.h" + +/* function to register type */ +static void tolua_reg_types (lua_State* tolua_S) +{ +} + +/* function: config_read */ +static int tolua_config_eressea_config_read00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isstring(tolua_S,1,0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char* filename = ((const char*) tolua_tostring(tolua_S,1,0)); + { + config_read(filename); + } + } + return 0; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'read'.",&tolua_err); + return 0; +#endif +} + +/* function: config_parse */ +static int tolua_config_eressea_config_parse00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isstring(tolua_S,1,0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char* json = ((const char*) tolua_tostring(tolua_S,1,0)); + { + config_parse(json); + } + } + return 0; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'parse'.",&tolua_err); + return 0; +#endif +} + +/* Open lib function */ +LUALIB_API int luaopen_config (lua_State* tolua_S) +{ + tolua_open(tolua_S); + tolua_reg_types(tolua_S); + tolua_module(tolua_S,NULL,0); + tolua_beginmodule(tolua_S,NULL); + tolua_module(tolua_S,"eressea",0); + tolua_beginmodule(tolua_S,"eressea"); + tolua_module(tolua_S,"config",0); + tolua_beginmodule(tolua_S,"config"); + tolua_function(tolua_S,"read",tolua_config_eressea_config_read00); + tolua_function(tolua_S,"parse",tolua_config_eressea_config_parse00); + tolua_endmodule(tolua_S); + tolua_endmodule(tolua_S); + tolua_endmodule(tolua_S); + return 1; +} +/* Open tolua function */ +TOLUA_API int tolua_config_open (lua_State* tolua_S) +{ + lua_pushcfunction(tolua_S, luaopen_config); + lua_pushstring(tolua_S, "config"); + lua_call(tolua_S, 1, 0); + return 1; +}