diff --git a/src/gui/lua/Makefile.am b/src/gui/lua/Makefile.am index 7f7a96c17..f2684a0b0 100644 --- a/src/gui/lua/Makefile.am +++ b/src/gui/lua/Makefile.am @@ -31,4 +31,5 @@ noinst_LIBRARIES = libneutrino_gui_lua.a libneutrino_gui_lua_a_SOURCES = \ luainstance_helpers.cpp \ luainstance.cpp \ + lua_configfile.cpp \ lua_video.cpp diff --git a/src/gui/lua/lua_configfile.cpp b/src/gui/lua/lua_configfile.cpp new file mode 100644 index 000000000..44ebc789c --- /dev/null +++ b/src/gui/lua/lua_configfile.cpp @@ -0,0 +1,199 @@ +/* + * lua config file + * + * (C) 2014-2015 M. Liebmann (micha-bbg) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "luainstance.h" +#include "lua_configfile.h" + +CLuaInstConfigFile* CLuaInstConfigFile::getInstance() +{ + static CLuaInstConfigFile* LuaInstConfigFile = NULL; + + if(!LuaInstConfigFile) + LuaInstConfigFile = new CLuaInstConfigFile(); + return LuaInstConfigFile; +} + +CLuaConfigFile *CLuaInstConfigFile::LuaConfigFileCheck(lua_State *L, int n) +{ + return *(CLuaConfigFile **) luaL_checkudata(L, n, "configfile"); +} + +void CLuaInstConfigFile::LuaConfigFileRegister(lua_State *L) +{ + luaL_Reg meth[] = { + { "new", CLuaInstConfigFile::LuaConfigFileNew }, + { "loadConfig", CLuaInstConfigFile::LuaConfigFileLoadConfig }, + { "saveConfig", CLuaInstConfigFile::LuaConfigFileSaveConfig }, + { "clear", CLuaInstConfigFile::LuaConfigFileClear }, + { "getString", CLuaInstConfigFile::LuaConfigFileGetString }, + { "setString", CLuaInstConfigFile::LuaConfigFileSetString }, + { "getInt32", CLuaInstConfigFile::LuaConfigFileGetInt32 }, + { "setInt32", CLuaInstConfigFile::LuaConfigFileSetInt32 }, + { "getBool", CLuaInstConfigFile::LuaConfigFileGetBool }, + { "setBool", CLuaInstConfigFile::LuaConfigFileSetBool }, + { "__gc", CLuaInstConfigFile::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 CLuaInstConfigFile::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 CLuaInstConfigFile::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 CLuaInstConfigFile::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 CLuaInstConfigFile::LuaConfigFileClear(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + c->c->clear(); + return 0; +} + +int CLuaInstConfigFile::LuaConfigFileGetString(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + int numargs = lua_gettop(L); + + std::string ret; + const char *key = luaL_checkstring(L, 2); + const char *defaultVal = ""; + if (numargs > 2) + defaultVal = luaL_checkstring(L, 3); + ret = c->c->getString(key, defaultVal); + lua_pushstring(L, ret.c_str()); + return 1; +} + +int CLuaInstConfigFile::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 CLuaInstConfigFile::LuaConfigFileGetInt32(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + int numargs = lua_gettop(L); + + int ret; + const char *key = luaL_checkstring(L, 2); + int defaultVal = 0; + if (numargs > 2) + defaultVal = luaL_checkint(L, 3); + ret = c->c->getInt32(key, defaultVal); + lua_pushinteger(L, ret); + return 1; +} + +int CLuaInstConfigFile::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 CLuaInstConfigFile::LuaConfigFileGetBool(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + int numargs = lua_gettop(L); + + bool ret; + const char *key = luaL_checkstring(L, 2); + bool defaultVal = false; + if (numargs > 2) + defaultVal = _luaL_checkbool(L, 3); + ret = c->c->getBool(key, defaultVal); + lua_pushboolean(L, ret); + return 1; +} + +int CLuaInstConfigFile::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 CLuaInstConfigFile::LuaConfigFileDelete(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + delete c; + return 0; +} diff --git a/src/gui/lua/lua_configfile.h b/src/gui/lua/lua_configfile.h new file mode 100644 index 000000000..2ee1cb46e --- /dev/null +++ b/src/gui/lua/lua_configfile.h @@ -0,0 +1,56 @@ +/* + * lua config file + * + * (C) 2014-2015 M. Liebmann (micha-bbg) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _LUACONFIGFILE_H +#define _LUACONFIGFILE_H + +#include + +class CLuaConfigFile +{ + public: + CConfigFile *c; + CLuaConfigFile() { c = NULL; } + ~CLuaConfigFile() { delete c; } +}; + +class CLuaInstConfigFile +{ + public: + CLuaInstConfigFile() {}; + ~CLuaInstConfigFile() {}; + static CLuaInstConfigFile* getInstance(); + static void LuaConfigFileRegister(lua_State *L); + + private: + static CLuaConfigFile *LuaConfigFileCheck(lua_State *L, int n); + 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); +}; + +#endif //_LUACONFIGFILE_H diff --git a/src/gui/lua/luainstance.cpp b/src/gui/lua/luainstance.cpp index b700bd168..6cdd59655 100644 --- a/src/gui/lua/luainstance.cpp +++ b/src/gui/lua/luainstance.cpp @@ -40,6 +40,7 @@ #include #include "luainstance.h" +#include "lua_configfile.h" static void set_lua_variables(lua_State *L) { @@ -541,7 +542,7 @@ void CLuaInstance::registerFunctions() ComponentsTextRegister(lua); SignalBoxRegister(lua); CPictureRegister(lua); - LuaConfigFileRegister(lua); + CLuaInstConfigFile::getInstance()->LuaConfigFileRegister(lua); } CLuaData *CLuaInstance::CheckData(lua_State *L, int narg) @@ -2632,167 +2633,6 @@ 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; - int numargs = lua_gettop(L); - - std::string ret; - const char *key = luaL_checkstring(L, 2); - const char *defaultVal = ""; - if (numargs > 2) - 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 numargs = lua_gettop(L); - - int ret; - const char *key = luaL_checkstring(L, 2); - int defaultVal = 0; - if (numargs > 2) - 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; - int numargs = lua_gettop(L); - - bool ret; - const char *key = luaL_checkstring(L, 2); - bool defaultVal = false; - if (numargs > 2) - 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; -} - -// -------------------------------------------------------------------------------- - int CLuaInstance::checkVersion(lua_State *L) { int numargs = lua_gettop(L); diff --git a/src/gui/lua/luainstance.h b/src/gui/lua/luainstance.h index 5c4db9848..802ea873d 100644 --- a/src/gui/lua/luainstance.h +++ b/src/gui/lua/luainstance.h @@ -30,11 +30,11 @@ extern "C" { #include #include #include -#include #include #include "luainstance_helpers.h" + #define LUA_API_VERSION_MAJOR 1 #define LUA_API_VERSION_MINOR 22 @@ -173,15 +173,6 @@ 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 { @@ -298,20 +289,6 @@ private: static int CPictureSetCenterPos(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 int checkVersion(lua_State *L); static int createChannelIDfromUrl(lua_State *L); static int enableInfoClock(lua_State *L);