the same way that I got the return value from lua for the random table param is how i got the size of the table. So this is the update LuaConfig.hpp
@note there may be bugs although I haven't had any and if so then fix them and post the fix please :D
cat LuaConfig.hpp/* * LuaConfig.hpp * * Created on: Oct 24, 2009 * Author: coolkehon */#ifndef LUACONFIG_HPP_#define LUACONFIG_HPP_#include <lua.hpp>#include <iostream>#include <string>#include <vector>#include <boost/algorithm/string.hpp>class LuaConfig { private: /** pointer to a lua state */ lua_State * m_state; /** true if this owns the lua_State */ bool m_stown; /** table * @note wasn't sure what to call this * @brief this is appended to any lookups so as to stay in a table */ std::string m_table; /** loads the path of a variable and adds it to the top of the stack * @return true if loaded */ virtual bool loadPath(std::string path) { if(!m_state) return false; // lua_pushstring(m_state,path.c_str()); std::string str = "return " + path; if(m_table.size() > 0) str = "return " + m_table + "." + path; luaL_dostring(m_state,str.c_str()); if(lua_isnoneornil(m_state,-1)) return false; /** links to the path * @example player.pos.start.x */// std::vector<std::string> links;// boost::split(links, path, boost::is_any_of(".[]"));// if(links.size() <= 0) // return false;//// std::cout << "links = { \"";// lua_getglobal(m_state,links[0].c_str());// for(uint i = 1; i < links.size(); i++)// {//// std::cout << links << "\", \"";// // /** checks if all are digits and if so then it // * means an index aka table[index] */// boost::all(links,boost::is_digit()) ?// lua_rawgeti(m_state,-1,atoi(links.c_str())) :// lua_getfield(m_state,-1,links.c_str());// // // if(lua_isnil(m_state,-1))// {// std::cout << "links[" << i << "] = nil\n";// return false;// }// else// std::cout << "found " << links << std::endl;// } // std::cout << " }" << std::endl; return true; } public: virtual ~LuaConfig() { if(m_state && m_stown) lua_close(m_state); } LuaConfig(lua_State * state = NULL, bool own = false) { m_stown = own; m_state = state; m_table = ""; } /** checks if a given path exist */ virtual bool exist(std::string path) { if(!m_state) return false; return loadPath(path); } /** simply because libconfig spells it this way * @brief the same as exist */ virtual bool exists(std::string path) { return exist(path); } /** looks up value at path and stores it in value * @param path path to the lua variable * @param value value to store the variable in * @return true if found, false otherwise */ virtual bool lookupValue(std::string path, bool &value) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_isboolean(m_state,-1)) { value = lua_toboolean(m_state, -1); return true; } return false; } virtual bool lookupValue(std::string path, int &value) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_isnumber(m_state,-1)) { value = lua_tonumber(m_state, -1); return true; } return false; } virtual bool lookupValue(std::string path, uint32_t &value) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_isnumber(m_state,-1)) { value = lua_tonumber(m_state, -1); return true; } return false; } virtual bool lookupValue(std::string path, std::string &value) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_isstring(m_state,-1)) { value = lua_tostring(m_state, -1) != NULL ? lua_tostring(m_state,-1) : ""; return true; } return false; } virtual bool lookupValue(std::string path, float &value) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_isnumber(m_state,-1)) { value = (float) lua_tonumber(m_state, -1); return true; } return false; } virtual bool lookupValue(std::string path, double &value) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_isnumber(m_state,-1)) { value = (double) lua_tonumber(m_state, -1); return true; } return false; } /** lookup the type of a path * @return one of the lua type constants aka LUA_* */ virtual int getType(std::string path) { if(!m_state) return LUA_TNONE; if(!loadPath(path)) return LUA_TNIL; return lua_type(m_state,-1); } /** checks if a given path is a boolean */ virtual bool isBool(std::string path) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_isboolean(m_state,-1)) return true; return false; } /** checks if a given path is a number */ virtual bool isNumber(std::string path) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_isnumber(m_state,-1)) return true; return false; } /** checks if a given path is a string */ virtual bool isString(std::string path) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_isstring(m_state,-1)) return true; return false; } /** checks if a given path is a table */ virtual bool isTable(std::string path) { if(!m_state) return false; if(!loadPath(path)) return false; if(lua_istable(m_state,-1)) return true; return false; } /** gets the size of a table */ virtual int getTableSize(std::string path) { if(!m_state) return 0; if(!loadPath(path)) return 0; if(!lua_istable(m_state,-1)) return 0; std::string str; m_table.size() > 0 ? str = "return table.getn(" + m_table + "." + path + ")" : str = "return table.getn(" + m_table + path + ")"; luaL_dostring(m_state,str.c_str()); return lua_tonumber(m_state,-1); } /** sets the current table for lookups * @param path path to the table * @return true on success */ virtual bool setCurTable(std::string path) { std::string oldpath = m_table; m_table = ""; if(!isTable(path)) { m_table = oldpath; return false; } m_table = path; return true; } /** return the current table path */ virtual std::string getCurTable() { return m_table; }};#endif /* LUACONFIG_HPP_ */