From f1376883a6cb68bb692b6031c9560e0409649573 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 5 Jun 2011 14:07:46 +0200 Subject: luamm: add a version of loadstring which also takes the string length as a parameter --- src/FbTk/Luamm.cc | 21 +++++++++++++++++++-- src/FbTk/Luamm.hh | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/FbTk/Luamm.cc b/src/FbTk/Luamm.cc index 3de86db..c4ea2e0 100644 --- a/src/FbTk/Luamm.cc +++ b/src/FbTk/Luamm.cc @@ -145,6 +145,19 @@ namespace lua { return r ? 3 : 1; } + struct reader_data { + const void *s; + size_t len; + }; + + const char *string_reader(lua_State *, void *data, size_t *size) + { + reader_data *d = static_cast(data); + *size = d->len; + d->len = 0; + return static_cast(d->s); + } + } std::string exception::get_error_msg(state *L) @@ -336,9 +349,13 @@ namespace lua { } } - void state::loadstring(const char *s) throw(lua::syntax_error, std::bad_alloc) + void + state::loadstring(const char *s, size_t len, const char *chunkname) + throw(lua::syntax_error, std::bad_alloc) { - switch(luaL_loadstring(cobj, s)) { + reader_data data = { s, len }; + + switch(lua_load(cobj, string_reader, &data, chunkname)) { case 0: return; case LUA_ERRSYNTAX: diff --git a/src/FbTk/Luamm.hh b/src/FbTk/Luamm.hh index 218eea9..0ebe6ef 100644 --- a/src/FbTk/Luamm.hh +++ b/src/FbTk/Luamm.hh @@ -23,6 +23,7 @@ #define FBTK_LUAMM_HH #include +#include #include #include @@ -255,7 +256,8 @@ namespace lua { void getglobal(const char *name) { getfield(GLOBALSINDEX, name); } bool lessthan(int index1, int index2); void loadfile(const char *filename) throw(lua::syntax_error, lua::file_error, std::bad_alloc); - void loadstring(const char *s) throw(lua::syntax_error, std::bad_alloc); + void loadstring(const char *s, const char *chunkname = NULL) throw(lua::syntax_error, std::bad_alloc) { loadstring(s, strlen(s), chunkname); } + void loadstring(const char *s, size_t len, const char *chunkname = NULL) throw(lua::syntax_error, std::bad_alloc); bool next(int index); // register is a reserved word :/ template -- cgit v0.11.2