From 4389ec60280c98af16ef8e38b91b09680427c76e Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 11 Oct 2014 12:56:26 +0200 Subject: added getString function to read config values --- src/libluaglue/GNUmakefile | 4 +++- src/libluaglue/LuaVM.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/libluaglue/Makefile.W32 | 1 + 3 files changed, 49 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/libluaglue/GNUmakefile b/src/libluaglue/GNUmakefile index 203df13..c39d741 100755 --- a/src/libluaglue/GNUmakefile +++ b/src/libluaglue/GNUmakefile @@ -10,7 +10,9 @@ INCLUDE_LDFLAGS = \ INCLUDE_DIRS = \ -I. \ - -I$(TOPDIR)/include/luaglue + -I$(TOPDIR)/include \ + -I$(TOPDIR)/include/luaglue \ + -I$(TOPDIR)/include/util INCLUDE_LIBS = \ diff --git a/src/libluaglue/LuaVM.cpp b/src/libluaglue/LuaVM.cpp index 7b165f1..d3e2122 100644 --- a/src/libluaglue/LuaVM.cpp +++ b/src/libluaglue/LuaVM.cpp @@ -1,4 +1,5 @@ #include "LuaVM.hpp" +#include "StringUtils.hpp" #include #include @@ -87,3 +88,47 @@ void LuaVM::dumpState( ) lua_pop( m_lua, 1 ); } } + +string LuaVM::getString( const string &key ) +{ + vector parts = split( key, "." ); + + string res; + + if( parts.size( ) == 1 ) { + lua_getglobal( m_lua, parts[0].c_str( ) ); + + if( lua_isnil( m_lua, -1 ) ) { + lua_pop( m_lua, 1 ); + return ""; + } + + res = lua_tostring( m_lua, -1 ); + lua_pop( m_lua, 1 ); + + return res; + } + + lua_getglobal( m_lua, parts[0].c_str( ) ); + for( size_t i = 1; i <= parts.size( ) - 1; i++ ) { + if( !lua_istable( m_lua, -1 ) ) { + lua_pop( m_lua, i ); + ostringstream ss; + ss << "table expected in '" << key << "' when derefencing field '" << parts[i] << "'"; + throw runtime_error( ss.str( ) ); + } + lua_getfield( m_lua, -1, parts[i].c_str( ) ); + } + + if( lua_isnil( m_lua, -1 ) ) { + lua_pop( m_lua, parts.size( ) ); + ostringstream ss; + ss << "key '" << key << "' refers to an empty element"; + throw runtime_error( ss.str( ) ); + } + + res = lua_tostring( m_lua, -1 ); + lua_pop( m_lua, parts.size( ) ); + + return res; +} diff --git a/src/libluaglue/Makefile.W32 b/src/libluaglue/Makefile.W32 index 8b34ea8..ec0cd48 100755 --- a/src/libluaglue/Makefile.W32 +++ b/src/libluaglue/Makefile.W32 @@ -11,6 +11,7 @@ INCLUDE_CXXFLAGS = \ INCLUDE_DIRS = \ /I. \ /I$(TOPDIR)\include\luaglue \ + /I$(TOPDIR)\include\util \ /I$(TOPDIR)\lua\src INCLUDE_LDFLAGS = \ -- cgit v1.2.3-54-g00ecf