lua_filehelpers.cpp: Add lua script function 'exist()'

- Set Lua api version to 1.55


Origin commit data
------------------
Commit: 52c489f211
Author: Michael Liebmann <tuxcode.bbg@gmail.com>
Date: 2016-09-05 (Mon, 05 Sep 2016)
This commit is contained in:
Michael Liebmann
2016-09-05 15:25:07 +02:00
parent e217a03f2c
commit c8e4c6e0a5
3 changed files with 66 additions and 1 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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);
};