summaryrefslogtreecommitdiff
path: root/lib/liblua_wrapper.cc
diff options
context:
space:
mode:
authordeva <deva>2006-08-16 23:48:22 +0000
committerdeva <deva>2006-08-16 23:48:22 +0000
commit6c07f9219bed6ccddc9b65ad40414cf0a9f7d633 (patch)
tree95f5fe5db16808d7b13511a6344bc269692e6e17 /lib/liblua_wrapper.cc
parentcca64725502c1035ca50675dda6c42dad18f0cbf (diff)
Finished the Configuration class (Still missing vector<string> though!)
Replaced the old MiavConfig class with the new Configuration class in all the the appropriate places.
Diffstat (limited to 'lib/liblua_wrapper.cc')
-rw-r--r--lib/liblua_wrapper.cc63
1 files changed, 57 insertions, 6 deletions
diff --git a/lib/liblua_wrapper.cc b/lib/liblua_wrapper.cc
index 7f493c5..6535f4e 100644
--- a/lib/liblua_wrapper.cc
+++ b/lib/liblua_wrapper.cc
@@ -76,6 +76,42 @@ int LibLUAWrapper::loadFile(char *fname)
return 0;
}
+int LibLUAWrapper::loadBuffer(char *buffer)
+{
+ int s;
+
+ s = luaL_loadstring(L, buffer);
+
+ switch(s) {
+ case 0: //no errors;
+ break;
+ case LUA_ERRSYNTAX: //syntax error during pre-compilation;
+ case LUA_ERRMEM: //memory allocation error.
+ error = std::string(lua_tostring(L, lua_gettop(L)));
+ return 1;
+ default:
+ error = std::string("Unknown return value of luaL_loadstring.");
+ return 1;
+ }
+
+ s = lua_pcall(L, 0, LUA_MULTRET, 0);
+
+ switch(s) {
+ case 0: // Success
+ break;
+ case LUA_ERRRUN:// a runtime error.
+ case LUA_ERRMEM:// memory allocation error. For such errors, Lua does not call the error handler function.
+ case LUA_ERRERR:// error while running the error handler function.
+ error = std::string(lua_tostring(L, lua_gettop(L)));
+ return 1;
+ default:
+ error = std::string("Unknown return value of lua_pcall.");
+ return 1;
+ }
+
+ return 0;
+}
+
int LibLUAWrapper::getInteger(char *name)
{
lua_getfield(L, LUA_GLOBALSINDEX, name);
@@ -134,19 +170,34 @@ std::string LibLUAWrapper::getError()
}
#ifdef LUA_TEST
+// Compile with:
+// g++ liblua_wrapper.cc -DLUA_TEST -I /home/deva/lib/lua-5.1.1/include/ -L /home/deva/lib/lua-5.1.1/lib -llua
+
+const char preload[] =
+"a = 42.0\n"
+"b = 42\n"
+"c = 42\n"
+"d = \"42\"\n";
int main()
{
LibLUAWrapper lua;
- if(!lua.loadFile("test.lua")) {
- printf("a: %f\n", lua.getReal("a"));
- printf("b: %d\n", lua.getInteger("b"));
- printf("c: %d\n", lua.getBoolean("c"));
- printf("fisk: %s\n", lua.getString("fisk").c_str());
- } else {
+ if(lua.loadBuffer((char*)preload)) {
+ fprintf(stderr, "LUA buffer error: %s\n", lua.getError().c_str());
+ return 1;
+ }
+
+ if(lua.loadFile("test.lua")) {
fprintf(stderr, "LUA load error: %s\n", lua.getError().c_str());
+ return 1;
}
+
+ printf("a: %f\n", lua.getReal("a"));
+ printf("b: %d\n", lua.getInteger("b"));
+ printf("c: %d\n", lua.getBoolean("c"));
+ printf("d: %s\n", lua.getString("d").c_str());
+
return 0;
}