From c8e4c6e0a57a9e7fc127268543e019b5b87941b4 Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Mon, 5 Sep 2016 15:25:07 +0200 Subject: [PATCH] lua_filehelpers.cpp: Add lua script function 'exist()' - Set Lua api version to 1.55 Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/52c489f211e1dec9f8af21a3c58c83f67ba2e720 Author: Michael Liebmann Date: 2016-09-05 (Mon, 05 Sep 2016) --- src/gui/lua/lua_api_version.h | 2 +- src/gui/lua/lua_filehelpers.cpp | 64 +++++++++++++++++++++++++++++++++ src/gui/lua/lua_filehelpers.h | 1 + 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/gui/lua/lua_api_version.h b/src/gui/lua/lua_api_version.h index dd4987b95..5c47c47e2 100644 --- a/src/gui/lua/lua_api_version.h +++ b/src/gui/lua/lua_api_version.h @@ -4,4 +4,4 @@ * to luainstance.h changes */ #define LUA_API_VERSION_MAJOR 1 -#define LUA_API_VERSION_MINOR 54 +#define LUA_API_VERSION_MINOR 55 diff --git a/src/gui/lua/lua_filehelpers.cpp b/src/gui/lua/lua_filehelpers.cpp index 2335d32f0..c8d1cd252 100644 --- a/src/gui/lua/lua_filehelpers.cpp +++ b/src/gui/lua/lua_filehelpers.cpp @@ -59,6 +59,7 @@ void CLuaInstFileHelpers::LuaFileHelpersRegister(lua_State *L) { "mkdir", CLuaInstFileHelpers::FileHelpersMkdir }, { "readlink", CLuaInstFileHelpers::FileHelpersReadlink }, { "ln", CLuaInstFileHelpers::FileHelpersLn }, + { "exist", CLuaInstFileHelpers::FileHelpersExist }, { "__gc", CLuaInstFileHelpers::FileHelpersDelete }, { NULL, NULL } }; @@ -399,6 +400,69 @@ int CLuaInstFileHelpers::FileHelpersLn(lua_State *L) return 1; } +int CLuaInstFileHelpers::FileHelpersExist(lua_State *L) +{ + CLuaFileHelpers *D = FileHelpersCheckData(L, 1); + if (!D) return 0; + + int numargs = lua_gettop(L) - 1; + int min_numargs = 2; + if (numargs < min_numargs) { + printf("luascript exist: not enough arguments (%d, expected %d)\n", numargs, min_numargs); + lua_pushnil(L); + return 1; + } + + bool ret = false; + bool err = false; + int errLine = 0; + std::string errMsg = ""; + + const char *file = ""; + file = luaL_checkstring(L, 2); + const char *flag = ""; + flag = luaL_checkstring(L, 3); + + if (file_exists(file)) { + struct stat FileInfo; + if (lstat(file, &FileInfo) == -1) { + err = true; + errLine = __LINE__; + errMsg = (std::string)strerror(errno); + } + else if (strchr(flag, 'f') != NULL) { + if (S_ISREG(FileInfo.st_mode)) + ret = true; + } + else if (strchr(flag, 'l') != NULL) { + if (S_ISLNK(FileInfo.st_mode)) + ret = true; + } + else if (strchr(flag, 'd') != NULL) { + if (S_ISDIR(FileInfo.st_mode)) + ret = true; + } + else { + err = true; + errLine = __LINE__; + errMsg = (strlen(flag) == 0) ? "no" : "unknown"; + errMsg += " flag given."; + } + } + + if (err) { + lua_Debug ar; + lua_getstack(L, 1, &ar); + lua_getinfo(L, "Sl", &ar); + printf(">>> Lua script error [%s:%d] %s\n (error from neutrino: [%s:%d])\n", + ar.short_src, ar.currentline, errMsg.c_str(), __path_file__, errLine); + lua_pushnil(L); + return 1; + } + + lua_pushboolean(L, ret); + return 1; +} int CLuaInstFileHelpers::FileHelpersDelete(lua_State *L) diff --git a/src/gui/lua/lua_filehelpers.h b/src/gui/lua/lua_filehelpers.h index 23f8fbf08..853f9768e 100644 --- a/src/gui/lua/lua_filehelpers.h +++ b/src/gui/lua/lua_filehelpers.h @@ -46,6 +46,7 @@ class CLuaInstFileHelpers static int FileHelpersMkdir(lua_State *L); static int FileHelpersReadlink(lua_State *L); static int FileHelpersLn(lua_State *L); + static int FileHelpersExist(lua_State *L); static int FileHelpersDelete(lua_State *L); };