add json configuration parser to lua bindings.

This commit is contained in:
Enno Rehling 2014-06-12 17:34:57 -07:00
parent 8d2df36430
commit b84bf7ba56
6 changed files with 150 additions and 0 deletions

View file

@ -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

19
src/bind_config.c Normal file
View file

@ -0,0 +1,19 @@
#include "bind_config.h"
#include <platform.h>
#include <kernel/types.h>
#include <kernel/jsonconf.h>
#include <cJSON.h>
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)
{
}

13
src/bind_config.h Normal file
View file

@ -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

View file

@ -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");

8
src/config.pkg Normal file
View file

@ -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);
}
}

105
src/config.pkg.c Normal file
View file

@ -0,0 +1,105 @@
/*
** Lua binding: config
*/
#include "tolua.h"
#ifndef __cplusplus
#include <stdlib.h>
#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 <string.h>
/* 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;
}