3 Copyright (C) 2013 PilzAdam <pilzadam@minetest.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_settings.h"
21 #include "lua_api/l_internal.h"
26 int LuaSettings::gc_object(lua_State* L)
28 LuaSettings* o = *(LuaSettings **)(lua_touserdata(L, 1));
33 // get(self, key) -> value
34 int LuaSettings::l_get(lua_State* L)
37 LuaSettings* o = checkobject(L, 1);
39 std::string key = std::string(luaL_checkstring(L, 2));
40 if (o->m_settings->exists(key)) {
41 std::string value = o->m_settings->get(key);
42 lua_pushstring(L, value.c_str());
50 // get_bool(self, key) -> boolean
51 int LuaSettings::l_get_bool(lua_State* L)
54 LuaSettings* o = checkobject(L, 1);
56 std::string key = std::string(luaL_checkstring(L, 2));
57 if (o->m_settings->exists(key)) {
58 bool value = o->m_settings->getBool(key);
59 lua_pushboolean(L, value);
67 // set(self, key, value)
68 int LuaSettings::l_set(lua_State* L)
71 LuaSettings* o = checkobject(L, 1);
73 std::string key = std::string(luaL_checkstring(L, 2));
74 const char* value = luaL_checkstring(L, 3);
76 if (!o->m_settings->set(key, value))
77 throw LuaError("Invalid sequence found in setting parameters");
82 // remove(self, key) -> success
83 int LuaSettings::l_remove(lua_State* L)
86 LuaSettings* o = checkobject(L, 1);
88 std::string key = std::string(luaL_checkstring(L, 2));
90 bool success = o->m_settings->remove(key);
91 lua_pushboolean(L, success);
96 // get_names(self) -> {key1, ...}
97 int LuaSettings::l_get_names(lua_State* L)
100 LuaSettings* o = checkobject(L, 1);
102 std::vector<std::string> keys = o->m_settings->getNames();
105 for (unsigned int i=0; i < keys.size(); i++)
107 lua_pushstring(L, keys[i].c_str());
108 lua_rawseti(L, -2, i + 1);
114 // write(self) -> success
115 int LuaSettings::l_write(lua_State* L)
117 NO_MAP_LOCK_REQUIRED;
118 LuaSettings* o = checkobject(L, 1);
120 bool success = o->m_settings->updateConfigFile(o->m_filename.c_str());
121 lua_pushboolean(L, success);
126 // to_table(self) -> {[key1]=value1,...}
127 int LuaSettings::l_to_table(lua_State* L)
129 NO_MAP_LOCK_REQUIRED;
130 LuaSettings* o = checkobject(L, 1);
132 std::vector<std::string> keys = o->m_settings->getNames();
135 for (unsigned int i=0; i < keys.size(); i++)
137 lua_pushstring(L, o->m_settings->get(keys[i]).c_str());
138 lua_setfield(L, -2, keys[i].c_str());
144 LuaSettings::LuaSettings(const char* filename)
146 m_filename = std::string(filename);
148 m_settings = new Settings();
149 m_settings->readConfigFile(m_filename.c_str());
152 LuaSettings::~LuaSettings()
157 void LuaSettings::Register(lua_State* L)
160 int methodtable = lua_gettop(L);
161 luaL_newmetatable(L, className);
162 int metatable = lua_gettop(L);
164 lua_pushliteral(L, "__metatable");
165 lua_pushvalue(L, methodtable);
166 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
168 lua_pushliteral(L, "__index");
169 lua_pushvalue(L, methodtable);
170 lua_settable(L, metatable);
172 lua_pushliteral(L, "__gc");
173 lua_pushcfunction(L, gc_object);
174 lua_settable(L, metatable);
176 lua_pop(L, 1); // drop metatable
178 luaL_openlib(L, 0, methods, 0); // fill methodtable
179 lua_pop(L, 1); // drop methodtable
181 // Can be created from Lua (Settings(filename))
182 lua_register(L, className, create_object);
185 // LuaSettings(filename)
186 // Creates an LuaSettings and leaves it on top of stack
187 int LuaSettings::create_object(lua_State* L)
189 NO_MAP_LOCK_REQUIRED;
190 const char* filename = luaL_checkstring(L, 1);
191 LuaSettings* o = new LuaSettings(filename);
192 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
193 luaL_getmetatable(L, className);
194 lua_setmetatable(L, -2);
198 LuaSettings* LuaSettings::checkobject(lua_State* L, int narg)
200 NO_MAP_LOCK_REQUIRED;
201 luaL_checktype(L, narg, LUA_TUSERDATA);
202 void *ud = luaL_checkudata(L, narg, className);
203 if(!ud) luaL_typerror(L, narg, className);
204 return *(LuaSettings**)ud; // unbox pointer
207 const char LuaSettings::className[] = "Settings";
208 const luaL_reg LuaSettings::methods[] = {
209 luamethod(LuaSettings, get),
210 luamethod(LuaSettings, get_bool),
211 luamethod(LuaSettings, set),
212 luamethod(LuaSettings, remove),
213 luamethod(LuaSettings, get_names),
214 luamethod(LuaSettings, write),
215 luamethod(LuaSettings, to_table),