CLuaInstance: Move configfile functions in separate classes / files

- No api changes, code only


Origin commit data
------------------
Branch: ni/coolstream
Commit: f52ba5ba4a
Author: Michael Liebmann <tuxcode.bbg@gmail.com>
Date: 2015-12-12 (Sat, 12 Dec 2015)



------------------
This commit was generated by Migit
This commit is contained in:
Michael Liebmann
2015-12-12 10:21:50 +01:00
parent 256500c3e4
commit 0904d4097c
5 changed files with 259 additions and 186 deletions

View File

@@ -31,4 +31,5 @@ noinst_LIBRARIES = libneutrino_gui_lua.a
libneutrino_gui_lua_a_SOURCES = \ libneutrino_gui_lua_a_SOURCES = \
luainstance_helpers.cpp \ luainstance_helpers.cpp \
luainstance.cpp \ luainstance.cpp \
lua_configfile.cpp \
lua_video.cpp lua_video.cpp

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <global.h>
#include <system/debug.h>
#include <neutrino.h>
#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;
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef _LUACONFIGFILE_H
#define _LUACONFIGFILE_H
#include <configfile.h>
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

View File

@@ -40,6 +40,7 @@
#include <neutrino.h> #include <neutrino.h>
#include "luainstance.h" #include "luainstance.h"
#include "lua_configfile.h"
static void set_lua_variables(lua_State *L) static void set_lua_variables(lua_State *L)
{ {
@@ -541,7 +542,7 @@ void CLuaInstance::registerFunctions()
ComponentsTextRegister(lua); ComponentsTextRegister(lua);
SignalBoxRegister(lua); SignalBoxRegister(lua);
CPictureRegister(lua); CPictureRegister(lua);
LuaConfigFileRegister(lua); CLuaInstConfigFile::getInstance()->LuaConfigFileRegister(lua);
} }
CLuaData *CLuaInstance::CheckData(lua_State *L, int narg) 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 CLuaInstance::checkVersion(lua_State *L)
{ {
int numargs = lua_gettop(L); int numargs = lua_gettop(L);

View File

@@ -30,11 +30,11 @@ extern "C" {
#include <gui/widget/hintbox.h> #include <gui/widget/hintbox.h>
#include <gui/widget/messagebox.h> #include <gui/widget/messagebox.h>
#include <gui/components/cc.h> #include <gui/components/cc.h>
#include <configfile.h>
#include <vector> #include <vector>
#include "luainstance_helpers.h" #include "luainstance_helpers.h"
#define LUA_API_VERSION_MAJOR 1 #define LUA_API_VERSION_MAJOR 1
#define LUA_API_VERSION_MINOR 22 #define LUA_API_VERSION_MINOR 22
@@ -173,15 +173,6 @@ class CLuaPicture
~CLuaPicture() { if (parent == NULL) delete cp; } ~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/ */ /* inspired by Steve Kemp http://www.steve.org.uk/ */
class CLuaInstance class CLuaInstance
{ {
@@ -298,20 +289,6 @@ private:
static int CPictureSetCenterPos(lua_State *L); static int CPictureSetCenterPos(lua_State *L);
static int CPictureDelete(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 checkVersion(lua_State *L);
static int createChannelIDfromUrl(lua_State *L); static int createChannelIDfromUrl(lua_State *L);
static int enableInfoClock(lua_State *L); static int enableInfoClock(lua_State *L);