CLuaInstance: Add CConfigFile functions

Functions: loadConfig(), saveConfig(), clear(), getString(),
           setString(), getInt32(), setInt32(), getBool(), setBool()
This commit is contained in:
M. Liebmann
2014-09-11 10:58:34 +02:00
parent 457f974eed
commit b74e80ee6f
2 changed files with 175 additions and 0 deletions

View File

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

View File

@@ -30,6 +30,7 @@ extern "C" {
#include <gui/widget/hintbox.h>
#include <gui/widget/messagebox.h>
#include <gui/components/cc.h>
#include <configfile.h>
#include <vector>
/* 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&);