From eb7ad502994083affdd501526a20a90d27a64a28 Mon Sep 17 00:00:00 2001 From: Syping Date: Mon, 4 Nov 2019 13:15:51 +0100 Subject: [PATCH] add checkFileExists and checkDirectoryExists --- src/luaengineio/luaengine/LuaEngineIO.cpp | 28 +++++++++++++++++++++++ src/luaengineio/luaengine/LuaEngineIO.h | 2 ++ 2 files changed, 30 insertions(+) diff --git a/src/luaengineio/luaengine/LuaEngineIO.cpp b/src/luaengineio/luaengine/LuaEngineIO.cpp index 9e9187a..e7f4216 100644 --- a/src/luaengineio/luaengine/LuaEngineIO.cpp +++ b/src/luaengineio/luaengine/LuaEngineIO.cpp @@ -41,6 +41,7 @@ LuaEngineIO::LuaEngineIO(QObject *parent, bool loadBaseLibraries) : LuaEngine(pa void LuaEngineIO::pushClass(lua_State *L_p) { // Directory + pushFunction(L_p, "checkDirectoryExists", checkDirectoryExists); pushFunction(L_p, "getDirectoryContent", getDirectoryContent); pushFunction(L_p, "getDirectoryPath", getDirectoryPath); pushVariant(L_p, "Files", (int)1); @@ -48,6 +49,7 @@ void LuaEngineIO::pushClass(lua_State *L_p) pushVariant(L_p, "Subdirectories", (int)4); // File + pushFunction(L_p, "checkFileExists", checkFileExists); pushFunction(L_p, "linkFile", linkFile); pushVariant(L_p, "Symlink", (int)0); pushVariant(L_p, "Hardlink", (int)1); @@ -64,6 +66,32 @@ void LuaEngineIO::pushClass(LuaEngine *luaEngine) pushClass(luaEngine->luaState()); } +int LuaEngineIO::checkDirectoryExists(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + QFileInfo fileInfo(getVariant(L_p, 1).toString()); + if (fileInfo.exists() && fileInfo.isDir()) { + pushVariant(L_p, true); + return 1; + } + } + pushVariant(L_p, false); + return 1; +} + +int LuaEngineIO::checkFileExists(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + QFileInfo fileInfo(getVariant(L_p, 1).toString()); + if (fileInfo.exists() && fileInfo.isFile()) { + pushVariant(L_p, true); + return 1; + } + } + pushVariant(L_p, false); + return 1; +} + int LuaEngineIO::getDirectoryContent(lua_State *L_p) { if (getArgumentCount(L_p) >= 1) { diff --git a/src/luaengineio/luaengine/LuaEngineIO.h b/src/luaengineio/luaengine/LuaEngineIO.h index 4e0bf91..de44602 100644 --- a/src/luaengineio/luaengine/LuaEngineIO.h +++ b/src/luaengineio/luaengine/LuaEngineIO.h @@ -32,6 +32,8 @@ public: LuaEngineIO(QObject *parent = nullptr, bool loadBaseLibraries = true); static void pushClass(lua_State *L_p); static void pushClass(LuaEngine *luaEngine); + static int checkDirectoryExists(lua_State *L_p); + static int checkFileExists(lua_State *L_p); static int getDirectoryContent(lua_State *L_p); static int getDirectoryPath(lua_State *L_p); static int linkFile(lua_State *L_p);