From 708d73700029539af30436770227418dfb939e45 Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Sun, 13 Dec 2015 23:32:16 +0100 Subject: [PATCH 1/4] CLuaInstance: Add class for using libcurl - Add simple download function - Set Lua api version to 1.24 parameter typ default ---------------------------------------- url string required o, outputfile string when empty then save to string as secund return value A, userAgent string empty v, verbose bool false s, silent bool false connectTimeout number 20 ipv4 bool false ipv6 bool false useProxy bool true (default) followRedir bool true maxRedirs number 20 Example: -- simplest program call: -- ---------------------- local curl = curl.new() local ret, data = curl:download{url="http://example.com", o="/tmp/test.txt"} if ret ~= CURL.OK then print("Error: " .. data) end -- or -- local curl = curl.new() local ret, data = curl:download{url="http://example.com"} if ret == CURL.OK then -- downloaded data print(data) .. else print("Error: " .. data) end Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/4f9158c2a92c00a3c28dc3dba2a0b263eed7cb85 Author: Michael Liebmann Date: 2015-12-13 (Sun, 13 Dec 2015) --- src/gui/lua/Makefile.am | 1 + src/gui/lua/lua_curl.cpp | 352 ++++++++++++++++++++++++++++++ src/gui/lua/lua_curl.h | 60 +++++ src/gui/lua/luainstance.cpp | 14 ++ src/gui/lua/luainstance.h | 2 +- src/gui/lua/luainstance_helpers.h | 1 + 6 files changed, 429 insertions(+), 1 deletion(-) create mode 100644 src/gui/lua/lua_curl.cpp create mode 100644 src/gui/lua/lua_curl.h diff --git a/src/gui/lua/Makefile.am b/src/gui/lua/Makefile.am index 90358b08a..c5471750f 100644 --- a/src/gui/lua/Makefile.am +++ b/src/gui/lua/Makefile.am @@ -36,6 +36,7 @@ libneutrino_gui_lua_a_SOURCES = \ lua_cc_text.cpp \ lua_cc_window.cpp \ lua_configfile.cpp \ + lua_curl.cpp \ lua_hintbox.cpp \ lua_menue.cpp \ lua_messagebox.cpp \ diff --git a/src/gui/lua/lua_curl.cpp b/src/gui/lua/lua_curl.cpp new file mode 100644 index 000000000..f25549af5 --- /dev/null +++ b/src/gui/lua/lua_curl.cpp @@ -0,0 +1,352 @@ +/* + * simple lua curl functions + * + * (C) 2015 M. Liebmann (micha-bbg) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include "luainstance.h" +#include "lua_curl.h" + +CLuaInstCurl* CLuaInstCurl::getInstance() +{ + static CLuaInstCurl* LuaInstCurl = NULL; + + if (!LuaInstCurl) + LuaInstCurl = new CLuaInstCurl(); + return LuaInstCurl; +} + +CLuaCurl *CLuaInstCurl::CurlCheckData(lua_State *L, int n) +{ + return *(CLuaCurl **) luaL_checkudata(L, n, LUA_CURL_CLASSNAME); +} + +void CLuaInstCurl::LuaCurlRegister(lua_State *L) +{ + luaL_Reg meth[] = { + { "new", CLuaInstCurl::CurlNew }, + { "download", CLuaInstCurl::CurlDownload }, + { "__gc", CLuaInstCurl::CurlDelete }, + { NULL, NULL } + }; + + luaL_newmetatable(L, LUA_CURL_CLASSNAME); + luaL_setfuncs(L, meth, 0); + lua_pushvalue(L, -1); + lua_setfield(L, -1, "__index"); + lua_setglobal(L, LUA_CURL_CLASSNAME); +} + +int CLuaInstCurl::CurlNew(lua_State *L) +{ + CLuaCurl **udata = (CLuaCurl **) lua_newuserdata(L, sizeof(CLuaCurl *)); + *udata = new CLuaCurl(); + luaL_getmetatable(L, LUA_CURL_CLASSNAME); + lua_setmetatable(L, -2); + return 1; +} + +size_t CLuaInstCurl::CurlWriteToString(void *ptr, size_t size, size_t nmemb, void *data) +{ + if (size * nmemb > 0) { + std::string* pStr = (std::string*) data; + pStr->append((char*) ptr, nmemb); + } + return size*nmemb; +} + +int CLuaInstCurl::CurlProgressFunc(void *p, double dltotal, double dlnow, double /*ultotal*/, double /*ulnow*/) +{ + if (dltotal <= 0.0) + return 0; + + struct progressData *_pgd = static_cast(p); + if (_pgd->last_dlnow == dlnow) + return 0; + _pgd->last_dlnow = dlnow; + + double dlSpeed; + long responseCode = 0; + curl_easy_getinfo(_pgd->curl, CURLINFO_SPEED_DOWNLOAD, &dlSpeed); + curl_easy_getinfo(_pgd->curl, CURLINFO_RESPONSE_CODE, &responseCode); + + double dlFragment = dlnow / dltotal; + if (responseCode != 200) { + dlFragment = 0; + dlSpeed = 0; + } + + int showDots = 50; + int dots = round(dlFragment * showDots); + printf(" %3.0f%% [", dlFragment * 100); + int i = 0; + for (; i < dots-1; i++) + printf("="); + if (i < showDots) { + printf(">"); + i++; + } + for (; i < showDots; i++) + printf(" "); + printf("] speed: %.03f KB/sec \r", dlSpeed/1024); fflush(stdout); + + return 0; +} + +int CLuaInstCurl::CurlDownload(lua_State *L) +{ +/* + parameter typ default + ---------------------------------------- + url string required + o, outputfile string when empty then save to string + as secund return value + A, userAgent string empty + v, verbose bool false + s, silent bool false + connectTimeout number 20 + ipv4 bool false + ipv6 bool false + useProxy bool true (default) + followRedir bool true + maxRedirs number 20 +*/ + +/* +Example: + -- simplest program call: + -- ---------------------- + local curl = curl.new() + local ret, data = curl:download{url="http://example.com", o="/tmp/test.txt"} + if ret ~= CURL.OK then + print("Error: " .. data) + end + + -- or -- + + local curl = curl.new() + local ret, data = curl:download{url="http://example.com"} + if ret == CURL.OK then + -- downloaded data + print(data) + .. + else + print("Error: " .. data) + end +*/ + +#define CURL_MSG_ERROR "[curl:download \33[1;31mERROR!\33[0m]" + + lua_assert(lua_istable(L,1)); + + char errMsg[1024]; + CURL *curl_handle = curl_easy_init(); + if (!curl_handle) { + memset(errMsg, '\0', sizeof(errMsg)); + snprintf(errMsg, sizeof(errMsg)-1, "error creating cUrl handle."); + printf("%s %s\n", CURL_MSG_ERROR, errMsg); + lua_pushinteger(L, LUA_CURL_ERR_HANDLE); + lua_pushstring(L, errMsg); + return 2; + } + + std::string url = ""; + tableLookup(L, "url", url); + if (url.empty()) { + curl_easy_cleanup(curl_handle); + memset(errMsg, '\0', sizeof(errMsg)); + snprintf(errMsg, sizeof(errMsg)-1, "no url given."); + printf("%s %s\n", CURL_MSG_ERROR, errMsg); + lua_pushinteger(L, LUA_CURL_ERR_NO_URL); + lua_pushstring(L, errMsg); + return 2; + } + + std::string outputfile = ""; + bool toFile = tableLookup(L, "o", outputfile) || tableLookup(L, "outputfile", outputfile); + std::string retString = ""; + FILE *fp = NULL; + if (toFile) { + fp = fopen(outputfile.c_str(), "wb"); + if (fp == NULL) { + memset(errMsg, '\0', sizeof(errMsg)); + snprintf(errMsg, sizeof(errMsg)-1, "Can't create %s", outputfile.c_str()); + printf("%s %s\n", CURL_MSG_ERROR, errMsg); + curl_easy_cleanup(curl_handle); + lua_pushinteger(L, LUA_CURL_ERR_CREATE_FILE); + lua_pushstring(L, errMsg); + return 2; + } + } + + std::string userAgent = ""; + tableLookup(L, "A", userAgent) || tableLookup(L, "userAgent", userAgent); + + bool verbose = false; + tableLookup(L, "v", verbose) || tableLookup(L, "verbose", verbose); + + bool silent = false; + if (!verbose) + tableLookup(L, "s", silent) || tableLookup(L, "silent", silent); + + lua_Integer connectTimeout = 20; + tableLookup(L, "connectTimeout", connectTimeout); + + bool ipv4 = false; + tableLookup(L, "ipv4", ipv4); + bool ipv6 = false; + tableLookup(L, "ipv6", ipv6); + + bool useProxy = true; + tableLookup(L, "useProxy", useProxy); + + bool followRedir = true; + tableLookup(L, "followRedir", followRedir); + + lua_Integer maxRedirs = 20; + tableLookup(L, "maxRedirs", maxRedirs); + + curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); + if (toFile) { + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL); + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, fp); + } + else { + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &CLuaInstCurl::CurlWriteToString); + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)&retString); + } + curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1L); + curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, (long)(4*connectTimeout)); + curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, (long)connectTimeout); + curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1L); + curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L); + + if (!userAgent.empty()) + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, userAgent.c_str()); + + if (ipv4 && ipv6) + curl_easy_setopt(curl_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER); + else if (ipv4) + curl_easy_setopt(curl_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + else if (ipv6) + curl_easy_setopt(curl_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6); + + if (!g_settings.softupdate_proxyserver.empty() && useProxy) { + curl_easy_setopt(curl_handle, CURLOPT_PROXY, g_settings.softupdate_proxyserver.c_str()); + if (!g_settings.softupdate_proxyusername.empty()) { + std::string tmp = g_settings.softupdate_proxyusername + ":" + g_settings.softupdate_proxypassword; + curl_easy_setopt(curl_handle, CURLOPT_PROXYUSERPWD, tmp.c_str()); + } + } + + curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, (followRedir)?1L:0L); + curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, (long)maxRedirs); + curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, (silent)?1L:0L); + curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, (verbose)?1L:0L); + + progressData pgd; + + if (!silent) { + curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, CLuaInstCurl::CurlProgressFunc); + pgd.curl = curl_handle; + pgd.last_dlnow = -1; + curl_easy_setopt(curl_handle, CURLOPT_PROGRESSDATA, &pgd); + } + + char cerror[CURL_ERROR_SIZE]; + curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, cerror); + + printf("\n[curl:download] download %s => %s\n", url.c_str(), (toFile)?outputfile.c_str():"return string"); + if (!silent) + printf("\e[?25l"); /* cursor off */ + printf("\n"); + CURLcode ret = curl_easy_perform(curl_handle); + if (!silent) + printf("\e[?25h"); /* cursor on */ + printf("\n"); + + std::string msg; + if (!silent) { + double dsize, dtime; + char *dredirect=NULL, *deffektive=NULL; + curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &dsize); + curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME, &dtime); + CURLcode res1 = curl_easy_getinfo(curl_handle, CURLINFO_EFFECTIVE_URL, &deffektive); + CURLcode res2 = curl_easy_getinfo(curl_handle, CURLINFO_REDIRECT_URL, &dredirect); + + char msg1[1024]; + memset(msg1, '\0', sizeof(msg1)); + snprintf(msg1, sizeof(msg1)-1, "\n[curl:download] O.K. size: %.0f bytes, time: %.02f sec.", dsize, dtime); + msg = msg1; + if (toFile) + msg += std::string("\n file: ") + outputfile; + else + msg += std::string("\n output: return string"); + msg += std::string("\n url: ") + url; + if ((res1 == CURLE_OK) && deffektive) { + std::string tmp1 = std::string(deffektive); + std::string tmp2 = url; + if (trim(tmp1, " /") != trim(tmp2, " /")) + msg += std::string("\n effektive url: ") + deffektive; + } + if ((res2 == CURLE_OK) && dredirect) + msg += std::string("\n redirect to: ") + dredirect; + } + + curl_easy_cleanup(curl_handle); + if (toFile) + fclose(fp); + + if (ret != 0) { + memset(errMsg, '\0', sizeof(errMsg)); + snprintf(errMsg, sizeof(errMsg)-1, "%s", cerror); + printf("%s curl error: %s\n", CURL_MSG_ERROR, errMsg); + if (toFile) + unlink(outputfile.c_str()); + lua_pushinteger(L, LUA_CURL_ERR_CURL); + lua_pushstring(L, errMsg); + return 2; + } + + if (silent == false) + printf("%s\n \n", msg.c_str()); + + lua_pushinteger(L, LUA_CURL_OK); + lua_pushstring(L, retString.c_str()); + return 2; +} + +int CLuaInstCurl::CurlDelete(lua_State *L) +{ + CLuaCurl *D = CurlCheckData(L, 1); + delete D; + return 0; +} diff --git a/src/gui/lua/lua_curl.h b/src/gui/lua/lua_curl.h new file mode 100644 index 000000000..67b00eec4 --- /dev/null +++ b/src/gui/lua/lua_curl.h @@ -0,0 +1,60 @@ +/* + * simple lua curl functions + * + * (C) 2015 M. Liebmann (micha-bbg) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _LUACURL_H +#define _LUACURL_H + +class CLuaCurl +{ + public: + CLuaCurl() {}; + ~CLuaCurl() {}; +}; + +struct progressData { + CURL *curl; + double last_dlnow; +}; + +class CLuaInstCurl +{ + public: + enum { + LUA_CURL_OK = 0, + LUA_CURL_ERR_HANDLE = 1, + LUA_CURL_ERR_NO_URL = 2, + LUA_CURL_ERR_CREATE_FILE = 3, + LUA_CURL_ERR_CURL = 4 + }; + + CLuaInstCurl() {}; + ~CLuaInstCurl() {}; + static CLuaInstCurl* getInstance(); + static void LuaCurlRegister(lua_State *L); + + private: + static CLuaCurl *CurlCheckData(lua_State *L, int n); + static int CurlNew(lua_State *L); + static size_t CurlWriteToString(void *ptr, size_t size, size_t nmemb, void *data); + static int CurlProgressFunc(void *p, double dltotal, double dlnow, double ultotal, double ulnow); + static int CurlDownload(lua_State *L); + static int CurlDelete(lua_State *L); +}; + +#endif //_LUACURL_H diff --git a/src/gui/lua/luainstance.cpp b/src/gui/lua/luainstance.cpp index 7e34b3510..bfba9584f 100644 --- a/src/gui/lua/luainstance.cpp +++ b/src/gui/lua/luainstance.cpp @@ -41,6 +41,7 @@ #include "lua_cc_text.h" #include "lua_cc_window.h" #include "lua_configfile.h" +#include "lua_curl.h" #include "lua_hintbox.h" #include "lua_menue.h" #include "lua_messagebox.h" @@ -306,6 +307,17 @@ static void set_lua_variables(lua_State *L) { NULL, 0 } }; + + table_key curl_status[] = + { + { "OK", (lua_Integer)CLuaInstCurl::LUA_CURL_OK }, + { "ERR_HANDLE", (lua_Integer)CLuaInstCurl::LUA_CURL_ERR_HANDLE }, + { "ERR_NO_URL", (lua_Integer)CLuaInstCurl::LUA_CURL_ERR_NO_URL }, + { "ERR_CREATE_FILE", (lua_Integer)CLuaInstCurl::LUA_CURL_ERR_CREATE_FILE }, + { "ERR_CURL", (lua_Integer)CLuaInstCurl::LUA_CURL_ERR_CURL }, + { NULL, 0 } + }; + /* list of environment variable arrays to be exported */ lua_envexport e[] = { @@ -318,6 +330,7 @@ static void set_lua_variables(lua_State *L) { "PLAYSTATE", playstate }, { "CC", ccomponents }, { "DYNFONT", dynfont }, + { "CURL", curl_status }, { NULL, NULL } }; @@ -556,6 +569,7 @@ void CLuaInstance::registerFunctions() CLuaInstCCText::getInstance()->CCTextRegister(lua); CLuaInstCCWindow::getInstance()->CCWindowRegister(lua); CLuaInstConfigFile::getInstance()->LuaConfigFileRegister(lua); + CLuaInstCurl::getInstance()->LuaCurlRegister(lua); CLuaInstHintbox::getInstance()->HintboxRegister(lua); CLuaInstMenu::getInstance()->MenuRegister(lua); CLuaInstMessagebox::getInstance()->MessageboxRegister(lua); diff --git a/src/gui/lua/luainstance.h b/src/gui/lua/luainstance.h index 5913edb32..a4ff1e561 100644 --- a/src/gui/lua/luainstance.h +++ b/src/gui/lua/luainstance.h @@ -31,7 +31,7 @@ extern "C" { #include "luainstance_helpers.h" #define LUA_API_VERSION_MAJOR 1 -#define LUA_API_VERSION_MINOR 23 +#define LUA_API_VERSION_MINOR 24 /* inspired by Steve Kemp http://www.steve.org.uk/ */ class CLuaInstance diff --git a/src/gui/lua/luainstance_helpers.h b/src/gui/lua/luainstance_helpers.h index c3ac68039..99eaa8cce 100644 --- a/src/gui/lua/luainstance_helpers.h +++ b/src/gui/lua/luainstance_helpers.h @@ -28,6 +28,7 @@ #define LUA_CLASSNAME "neutrino" #define LUA_VIDEO_CLASSNAME "video" #define LUA_MISC_CLASSNAME "misc" +#define LUA_CURL_CLASSNAME "curl" //#define LUA_WIKI "http://wiki.tuxbox.org/....." #define LUA_WIKI "https://slknet.de/wiki/w" From dcdf1a6b27b913a50410c050438086a58e98615d Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Sun, 13 Dec 2015 23:32:24 +0100 Subject: [PATCH 2/4] CLuaMenuFilebrowser::exec: Fix value handling when dirMode = false - Add luaId to action function as parameter - Set Lua api version to 1.25 Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/5259e80b1c760032fd6b5f6a573a4d961df3bbad Author: Michael Liebmann Date: 2015-12-13 (Sun, 13 Dec 2015) --- src/gui/lua/lua_menue.cpp | 7 +++++-- src/gui/lua/luainstance.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/gui/lua/lua_menue.cpp b/src/gui/lua/lua_menue.cpp index e8cf0337c..85be4460b 100644 --- a/src/gui/lua/lua_menue.cpp +++ b/src/gui/lua/lua_menue.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "luainstance.h" @@ -165,15 +166,17 @@ int CLuaMenuFilebrowser::exec(CMenuTarget* /*parent*/, const std::string& /*acti if (!filter.empty()) fileBrowser.Filter = &fileFilter; - if (fileBrowser.exec(value->c_str()) == true) + std::string tmpValue = (dirMode) ? *value : getPathName(*value); + if (fileBrowser.exec(tmpValue.c_str()) == true) *value = fileBrowser.getSelectedFile()->Name; if (!luaAction.empty()) { lua_pushglobaltable(L); lua_getfield(L, -1, luaAction.c_str()); lua_remove(L, -2); + lua_pushstring(L, luaId.c_str()); lua_pushstring(L, value->c_str()); - int status = lua_pcall(L, 1 /* one arg */, 1 /* one result */, 0); + int status = lua_pcall(L, 2 /* two arg */, 1 /* one result */, 0); if (status) { fprintf(stderr, "[CLuaMenuFilebrowser::%s:%d] error in script: %s\n", __func__, __LINE__, lua_tostring(L, -1)); luaL_error(L, " => %s", lua_tostring(L, -1)); diff --git a/src/gui/lua/luainstance.h b/src/gui/lua/luainstance.h index a4ff1e561..96cce8fee 100644 --- a/src/gui/lua/luainstance.h +++ b/src/gui/lua/luainstance.h @@ -31,7 +31,7 @@ extern "C" { #include "luainstance_helpers.h" #define LUA_API_VERSION_MAJOR 1 -#define LUA_API_VERSION_MINOR 24 +#define LUA_API_VERSION_MINOR 25 /* inspired by Steve Kemp http://www.steve.org.uk/ */ class CLuaInstance From a094a41f0056812e18ebaad6c517f8bfeab5e72c Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Sun, 13 Dec 2015 23:32:27 +0100 Subject: [PATCH 3/4] lua_misc.cpp: Fix compiler error 'overflow in implicit constant conversion' - Set Lua api version to 1.26 Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/5a7de075a2e13eacf3bcd200bc18e5f3c0eba037 Author: Michael Liebmann Date: 2015-12-13 (Sun, 13 Dec 2015) --- src/gui/lua/lua_misc.cpp | 4 ++-- src/gui/lua/luainstance.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gui/lua/lua_misc.cpp b/src/gui/lua/lua_misc.cpp index 03b396975..072b44802 100644 --- a/src/gui/lua/lua_misc.cpp +++ b/src/gui/lua/lua_misc.cpp @@ -118,12 +118,12 @@ int CLuaInstMisc::strSub(lua_State *L) return 1; } const char *s1; - int pos=0, len=std::string::npos; + size_t pos=0, len=std::string::npos; std::string ret=""; s1 = luaL_checkstring(L, 2); pos = luaL_checkint(L, 3); if (numargs > 3) - len = luaL_checkint(L, 4); + len = (size_t)luaL_checkint(L, 4); std::string str(s1); ret = str.substr(pos, len); diff --git a/src/gui/lua/luainstance.h b/src/gui/lua/luainstance.h index 96cce8fee..c77fc2b5b 100644 --- a/src/gui/lua/luainstance.h +++ b/src/gui/lua/luainstance.h @@ -31,7 +31,7 @@ extern "C" { #include "luainstance_helpers.h" #define LUA_API_VERSION_MAJOR 1 -#define LUA_API_VERSION_MINOR 25 +#define LUA_API_VERSION_MINOR 26 /* inspired by Steve Kemp http://www.steve.org.uk/ */ class CLuaInstance From 6715abcdc8b5d1685b2a937a02a8653279358710 Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Mon, 14 Dec 2015 05:11:17 +0100 Subject: [PATCH 4/4] CLuaInstConfigFile: Add parameters & functions - LuaConfigFileNew: Add parameter delimiter & saveDefaults - Add 'deleteKey' function - Set Lua api version to 1.27 Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/30ebe003f43c7ce845d0ca00c2937657009c2ac2 Author: Michael Liebmann Date: 2015-12-14 (Mon, 14 Dec 2015) --- src/gui/lua/lua_configfile.cpp | 22 +++++++++++++++++++++- src/gui/lua/lua_configfile.h | 1 + src/gui/lua/luainstance.h | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/gui/lua/lua_configfile.cpp b/src/gui/lua/lua_configfile.cpp index 44ebc789c..010b9131a 100644 --- a/src/gui/lua/lua_configfile.cpp +++ b/src/gui/lua/lua_configfile.cpp @@ -57,6 +57,7 @@ void CLuaInstConfigFile::LuaConfigFileRegister(lua_State *L) { "setInt32", CLuaInstConfigFile::LuaConfigFileSetInt32 }, { "getBool", CLuaInstConfigFile::LuaConfigFileGetBool }, { "setBool", CLuaInstConfigFile::LuaConfigFileSetBool }, + { "deleteKey", CLuaInstConfigFile::LuaConfigFileDeleteKey }, { "__gc", CLuaInstConfigFile::LuaConfigFileDelete }, { NULL, NULL } }; @@ -70,9 +71,17 @@ void CLuaInstConfigFile::LuaConfigFileRegister(lua_State *L) int CLuaInstConfigFile::LuaConfigFileNew(lua_State *L) { + int numargs = lua_gettop(L); + const char *delimiter = "\t"; + if (numargs > 0) + delimiter = luaL_checkstring(L, 1); + bool saveDefaults = true; + if (numargs > 1) + saveDefaults = _luaL_checkbool(L, 2); + CLuaConfigFile **udata = (CLuaConfigFile **) lua_newuserdata(L, sizeof(CLuaConfigFile *)); *udata = new CLuaConfigFile(); - (*udata)->c = new CConfigFile('\t'); + (*udata)->c = new CConfigFile(delimiter[0], saveDefaults); luaL_getmetatable(L, "configfile"); lua_setmetatable(L, -2); return 1; @@ -190,6 +199,17 @@ int CLuaInstConfigFile::LuaConfigFileSetBool(lua_State *L) return 0; } +int CLuaInstConfigFile::LuaConfigFileDeleteKey(lua_State *L) +{ + CLuaConfigFile *c = LuaConfigFileCheck(L, 1); + if (!c) return 0; + + const char *s1 = luaL_checkstring(L, 2); + std::string key(s1); + c->c->deleteKey(key); + return 0; +} + int CLuaInstConfigFile::LuaConfigFileDelete(lua_State *L) { CLuaConfigFile *c = LuaConfigFileCheck(L, 1); diff --git a/src/gui/lua/lua_configfile.h b/src/gui/lua/lua_configfile.h index 2ee1cb46e..b945b506e 100644 --- a/src/gui/lua/lua_configfile.h +++ b/src/gui/lua/lua_configfile.h @@ -50,6 +50,7 @@ class CLuaInstConfigFile static int LuaConfigFileSetInt32(lua_State *L); static int LuaConfigFileGetBool(lua_State *L); static int LuaConfigFileSetBool(lua_State *L); + static int LuaConfigFileDeleteKey(lua_State *L); static int LuaConfigFileDelete(lua_State *L); }; diff --git a/src/gui/lua/luainstance.h b/src/gui/lua/luainstance.h index c77fc2b5b..e2bf16a8d 100644 --- a/src/gui/lua/luainstance.h +++ b/src/gui/lua/luainstance.h @@ -31,7 +31,7 @@ extern "C" { #include "luainstance_helpers.h" #define LUA_API_VERSION_MAJOR 1 -#define LUA_API_VERSION_MINOR 26 +#define LUA_API_VERSION_MINOR 27 /* inspired by Steve Kemp http://www.steve.org.uk/ */ class CLuaInstance