mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-08-31 01:11:12 +02:00
CLuaInstance: Add CConfigFile functions
Functions: loadConfig(), saveConfig(), clear(), getString(), setString(), getInt32(), setInt32(), getBool(), setBool()
This commit is contained in:
@@ -531,6 +531,7 @@ void CLuaInstance::registerFunctions()
|
|||||||
ComponentsTextRegister(lua);
|
ComponentsTextRegister(lua);
|
||||||
SignalBoxRegister(lua);
|
SignalBoxRegister(lua);
|
||||||
CPictureRegister(lua);
|
CPictureRegister(lua);
|
||||||
|
LuaConfigFileRegister(lua);
|
||||||
}
|
}
|
||||||
|
|
||||||
CLuaData *CLuaInstance::CheckData(lua_State *L, int narg)
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------
|
||||||
|
@@ -30,6 +30,7 @@ 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>
|
||||||
|
|
||||||
/* this is stored as userdata in the lua_State */
|
/* this is stored as userdata in the lua_State */
|
||||||
@@ -155,6 +156,14 @@ 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
|
||||||
@@ -259,6 +268,19 @@ private:
|
|||||||
static int CPictureSetPicture(lua_State *L);
|
static int CPictureSetPicture(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 bool tableLookup(lua_State*, const char*, std::string&);
|
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_Integer&);
|
||||||
static bool tableLookup(lua_State*, const char*, lua_Unsigned&);
|
static bool tableLookup(lua_State*, const char*, lua_Unsigned&);
|
||||||
|
Reference in New Issue
Block a user