summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-10-11 12:56:26 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2014-10-11 12:56:26 +0200
commit4389ec60280c98af16ef8e38b91b09680427c76e (patch)
treee9ee4118105caaceb21f17df4cf5c0ef9b47b9a2 /src
parentdef669f31e10c8192a16f6f6ebdf1ccd420b5fe5 (diff)
downloadcrawler-4389ec60280c98af16ef8e38b91b09680427c76e.tar.gz
crawler-4389ec60280c98af16ef8e38b91b09680427c76e.tar.bz2
added getString function to read config values
Diffstat (limited to 'src')
-rwxr-xr-xsrc/libluaglue/GNUmakefile4
-rw-r--r--src/libluaglue/LuaVM.cpp45
-rwxr-xr-xsrc/libluaglue/Makefile.W321
3 files changed, 49 insertions, 1 deletions
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 <stdexcept>
#include <sstream>
@@ -87,3 +88,47 @@ void LuaVM::dumpState( )
lua_pop( m_lua, 1 );
}
}
+
+string LuaVM::getString( const string &key )
+{
+ vector<string> 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 = \