From b74e80ee6f780b255c5aa8f3175c7a6656131c9b Mon Sep 17 00:00:00 2001 From: "M. Liebmann" Date: Thu, 11 Sep 2014 10:58:34 +0200 Subject: [PATCH] CLuaInstance: Add CConfigFile functions Functions: loadConfig(), saveConfig(), clear(), getString(), setString(), getInt32(), setInt32(), getBool(), setBool() --- src/gui/luainstance.cpp | 153 ++++++++++++++++++++++++++++++++++++++++ src/gui/luainstance.h | 22 ++++++ 2 files changed, 175 insertions(+) diff --git a/src/gui/luainstance.cpp b/src/gui/luainstance.cpp index 69cc70a5d..38d07250b 100644 --- a/src/gui/luainstance.cpp +++ b/src/gui/luainstance.cpp @@ -531,6 +531,7 @@ void CLuaInstance::registerFunctions() ComponentsTextRegister(lua); SignalBoxRegister(lua); CPictureRegister(lua); + LuaConfigFileRegister(lua); } CLuaData *CLuaInstance::CheckData(lua_State *L, int narg) @@ -2201,3 +2202,155 @@ int CLuaInstance::CPictureDelete(lua_State *L) } // -------------------------------------------------------------------------------- + +CLuaConfigFile *CLuaInstance::LuaConfigFileCheck(lua_State *L, int n) +{ + return *(CLuaConfigFile **) luaL_checkudata(L, n, "configfile"); +} + +void CLuaInstance::LuaConfigFileRegister(lua_State *L) +{ + luaL_Reg meth[] = { + { "new", CLuaInstance::LuaConfigFileNew }, + { "loadConfig", CLuaInstance::LuaConfigFileLoadConfig }, + { "saveConfig", CLuaInstance::LuaConfigFileSaveConfig }, + { "clear", CLuaInstance::LuaConfigFileClear }, + { "getString", CLuaInstance::LuaConfigFileGetString }, + { "setString", CLuaInstance::LuaConfigFileSetString }, + { "getInt32", CLuaInstance::LuaConfigFileGetInt32 }, + { "setInt32", CLuaInstance::LuaConfigFileSetInt32 }, + { "getBool", CLuaInstance::LuaConfigFileGetBool }, + { "setBool", CLuaInstance::LuaConfigFileSetBool }, + { "__gc", CLuaInstance::LuaConfigFileDelete }, + { NULL, NULL } + }; + + luaL_newmetatable(L, "configfile"); + luaL_setfuncs(L, meth, 0); + lua_pushvalue(L, -1); + lua_setfield(L, -1, "__index"); + lua_setglobal(L, "configfile"); +} + +int CLuaInstance::LuaConfigFileNew(lua_State *L) +{ + CLuaConfigFile **udata = (CLuaConfigFile **) lua_newuserdata(L, sizeof(CLuaConfigFile *)); + *udata = new CLuaConfigFile(); + (*udata)->c = new CConfigFile('\t'); + luaL_getmetatable(L, "configfile"); + lua_setmetatable(L, -2); + return 1; +} + +int CLuaInstance::LuaConfigFileLoadConfig(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + const char *fname = luaL_checkstring(L, 2); + bool ret = c->c->loadConfig(fname); + lua_pushboolean(L, ret); + return 1; +} + +int CLuaInstance::LuaConfigFileSaveConfig(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + const char *fname = luaL_checkstring(L, 2); + bool ret = c->c->saveConfig(fname); + lua_pushboolean(L, ret); + return 1; +} + +int CLuaInstance::LuaConfigFileClear(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + c->c->clear(); + return 0; +} + +int CLuaInstance::LuaConfigFileGetString(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + std::string ret; + const char *key = luaL_checkstring(L, 2); + const char *defaultVal = luaL_checkstring(L, 3); + ret = c->c->getString(key, defaultVal); + lua_pushstring(L, ret.c_str()); + return 1; +} + +int CLuaInstance::LuaConfigFileSetString(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + const char *key = luaL_checkstring(L, 2); + const char *val = luaL_checkstring(L, 3); + c->c->setString(key, val); + return 0; +} + +int CLuaInstance::LuaConfigFileGetInt32(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + int ret; + const char *key = luaL_checkstring(L, 2); + int defaultVal = luaL_checkint(L, 3); + ret = c->c->getInt32(key, defaultVal); + lua_pushinteger(L, ret); + return 1; +} + +int CLuaInstance::LuaConfigFileSetInt32(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + const char *key = luaL_checkstring(L, 2); + int val = luaL_checkint(L, 3); + c->c->setInt32(key, val); + return 0; +} + +int CLuaInstance::LuaConfigFileGetBool(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + bool ret; + const char *key = luaL_checkstring(L, 2); + bool defaultVal = _luaL_checkbool(L, 3); + ret = c->c->getBool(key, defaultVal); + lua_pushboolean(L, ret); + return 1; +} + +int CLuaInstance::LuaConfigFileSetBool(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + const char *key = luaL_checkstring(L, 2); + bool val = _luaL_checkbool(L, 3); + c->c->setBool(key, val); + return 0; +} + +int CLuaInstance::LuaConfigFileDelete(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + delete c; + return 0; +} + +// -------------------------------------------------------------------------------- diff --git a/src/gui/luainstance.h b/src/gui/luainstance.h index f73007da5..daf778ebf 100644 --- a/src/gui/luainstance.h +++ b/src/gui/luainstance.h @@ -30,6 +30,7 @@ extern "C" { #include #include #include +#include #include /* this is stored as userdata in the lua_State */ @@ -155,6 +156,14 @@ class CLuaPicture ~CLuaPicture() { if (parent == NULL) delete cp; } }; +class CLuaConfigFile +{ + public: + CConfigFile *c; + CLuaConfigFile() { c = NULL; } + ~CLuaConfigFile() { delete c; } +}; + /* inspired by Steve Kemp http://www.steve.org.uk/ */ class CLuaInstance @@ -259,6 +268,19 @@ private: static int CPictureSetPicture(lua_State *L); static int CPictureDelete(lua_State *L); + static CLuaConfigFile *LuaConfigFileCheck(lua_State *L, int n); + static void LuaConfigFileRegister(lua_State *L); + static int LuaConfigFileNew(lua_State *L); + static int LuaConfigFileLoadConfig(lua_State *L); + static int LuaConfigFileSaveConfig(lua_State *L); + static int LuaConfigFileClear(lua_State *L); + static int LuaConfigFileGetString(lua_State *L); + static int LuaConfigFileSetString(lua_State *L); + static int LuaConfigFileGetInt32(lua_State *L); + static int LuaConfigFileSetInt32(lua_State *L); + static int LuaConfigFileGetBool(lua_State *L); + static int LuaConfigFileSetBool(lua_State *L); + static int LuaConfigFileDelete(lua_State *L); static bool tableLookup(lua_State*, const char*, std::string&); static bool tableLookup(lua_State*, const char*, lua_Integer&); static bool tableLookup(lua_State*, const char*, lua_Unsigned&);