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 o->m_settings->set(key, value);
81 // remove(self, key) -> success
82 int LuaSettings::l_remove(lua_State* L)
85 LuaSettings* o = checkobject(L, 1);
87 std::string key = std::string(luaL_checkstring(L, 2));
89 bool success = o->m_settings->remove(key);
90 lua_pushboolean(L, success);
95 // get_names(self) -> {key1, ...}
96 int LuaSettings::l_get_names(lua_State* L)
99 LuaSettings* o = checkobject(L, 1);
101 std::vector<std::string> keys = o->m_settings->getNames();
104 for (unsigned int i=0; i < keys.size(); i++)
106 lua_pushstring(L, keys[i].c_str());
107 lua_rawseti(L, -2, i + 1);
113 // write(self) -> success
114 int LuaSettings::l_write(lua_State* L)
116 NO_MAP_LOCK_REQUIRED;
117 LuaSettings* o = checkobject(L, 1);
119 bool success = o->m_settings->updateConfigFile(o->m_filename.c_str());
120 lua_pushboolean(L, success);
125 // to_table(self) -> {[key1]=value1,...}
126 int LuaSettings::l_to_table(lua_State* L)
128 NO_MAP_LOCK_REQUIRED;
129 LuaSettings* o = checkobject(L, 1);
131 std::vector<std::string> keys = o->m_settings->getNames();
134 for (unsigned int i=0; i < keys.size(); i++)
136 lua_pushstring(L, o->m_settings->get(keys[i]).c_str());
137 lua_setfield(L, -2, keys[i].c_str());
143 LuaSettings::LuaSettings(const char* filename)
145 m_filename = std::string(filename);
147 m_settings = new Settings();
148 m_settings->readConfigFile(m_filename.c_str());
151 LuaSettings::~LuaSettings()
156 void LuaSettings::Register(lua_State* L)
159 int methodtable = lua_gettop(L);
160 luaL_newmetatable(L, className);
161 int metatable = lua_gettop(L);
163 lua_pushliteral(L, "__metatable");
164 lua_pushvalue(L, methodtable);
165 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
167 lua_pushliteral(L, "__index");
168 lua_pushvalue(L, methodtable);
169 lua_settable(L, metatable);
171 lua_pushliteral(L, "__gc");
172 lua_pushcfunction(L, gc_object);
173 lua_settable(L, metatable);
175 lua_pop(L, 1); // drop metatable
177 luaL_openlib(L, 0, methods, 0); // fill methodtable
178 lua_pop(L, 1); // drop methodtable
180 // Can be created from Lua (Settings(filename))
181 lua_register(L, className, create_object);
184 // LuaSettings(filename)
185 // Creates an LuaSettings and leaves it on top of stack
186 int LuaSettings::create_object(lua_State* L)
188 NO_MAP_LOCK_REQUIRED;
189 const char* filename = luaL_checkstring(L, 1);
190 LuaSettings* o = new LuaSettings(filename);
191 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
192 luaL_getmetatable(L, className);
193 lua_setmetatable(L, -2);
197 LuaSettings* LuaSettings::checkobject(lua_State* L, int narg)
199 NO_MAP_LOCK_REQUIRED;
200 luaL_checktype(L, narg, LUA_TUSERDATA);
201 void *ud = luaL_checkudata(L, narg, className);
202 if(!ud) luaL_typerror(L, narg, className);
203 return *(LuaSettings**)ud; // unbox pointer
206 const char LuaSettings::className[] = "Settings";
207 const luaL_reg LuaSettings::methods[] = {
208 luamethod(LuaSettings, get),
209 luamethod(LuaSettings, get_bool),
210 luamethod(LuaSettings, set),
211 luamethod(LuaSettings, remove),
212 luamethod(LuaSettings, get_names),
213 luamethod(LuaSettings, write),
214 luamethod(LuaSettings, to_table),