From 96f0511b0f6f5a306c27c77f77cd277b2f25dd65 Mon Sep 17 00:00:00 2001 From: Syping Date: Thu, 24 Oct 2019 09:07:32 +0200 Subject: [PATCH] fix tables, getDirectoryContent replaces directoyListFiles --- src/luaenginecore/luaengine/LuaEngine.cpp | 13 ++-- src/luaengineio/luaengine/LuaEngineIO.cpp | 78 ++++++++++++++++++----- src/luaengineio/luaengine/LuaEngineIO.h | 4 +- src/luaengineos/luaengine/LuaEngineOS.cpp | 13 +++- 4 files changed, 82 insertions(+), 26 deletions(-) diff --git a/src/luaenginecore/luaengine/LuaEngine.cpp b/src/luaenginecore/luaengine/LuaEngine.cpp index 31207d9..83db65b 100644 --- a/src/luaenginecore/luaengine/LuaEngine.cpp +++ b/src/luaenginecore/luaengine/LuaEngine.cpp @@ -257,10 +257,9 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant) lua_createtable(L_p, 0, stringList.count()); int currentId = 1; for (const QString &string : stringList) { - char buffer[65]; - snprintf(buffer, 10, "%d", currentId); + lua_pushinteger(L_p, currentId); lua_pushstring(L_p, string.toUtf8().data()); - lua_setfield(L_p, -2, buffer); + lua_settable(L_p, -3); currentId++; } } @@ -269,10 +268,9 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant) lua_createtable(L_p, 0, variantList.count()); int currentId = 1; for (const QVariant &variant : variantList) { - char buffer[65]; - snprintf(buffer, 10, "%d", currentId); + lua_pushinteger(L_p, currentId); pushVariant(L_p, variant); - lua_setfield(L_p, -2, buffer); + lua_settable(L_p, -3); currentId++; } } @@ -282,8 +280,9 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant) QVariantMap::const_iterator it = variantMap.constBegin(); QVariantMap::const_iterator end = variantMap.constEnd(); while (it != end) { + lua_pushstring(L_p, it.key().toUtf8().data()); pushVariant(L_p, it.value()); - lua_setfield(L_p, -2, it.key().toUtf8().data()); + lua_settable(L_p, -3); it++; } } diff --git a/src/luaengineio/luaengine/LuaEngineIO.cpp b/src/luaengineio/luaengine/LuaEngineIO.cpp index 74ccf88..ba378c6 100644 --- a/src/luaengineio/luaengine/LuaEngineIO.cpp +++ b/src/luaengineio/luaengine/LuaEngineIO.cpp @@ -38,8 +38,11 @@ LuaEngineIO::LuaEngineIO(QObject *parent, bool loadBaseLibraries) : LuaEngine(pa void LuaEngineIO::pushClass(lua_State *L_p) { // Directory - pushFunction(L_p, "directoryListFiles", directoryListFiles); + pushFunction(L_p, "getDirectoryContent", getDirectoryContent); pushFunction(L_p, "getDirectoryPath", getDirectoryPath); + pushVariant(L_p, "Files", (int)1); + pushVariant(L_p, "Directories", (int)2); + pushVariant(L_p, "Subdirectories", (int)4); // File pushFunction(L_p, "linkFile", linkFile); @@ -52,16 +55,59 @@ void LuaEngineIO::pushClass(LuaEngine *luaEngine) pushClass(luaEngine->luaState()); } -int LuaEngineIO::directoryListFiles(lua_State *L_p) +int LuaEngineIO::getDirectoryContent(lua_State *L_p) { if (getArgumentCount(L_p) >= 1) { QStringList directories; QStringList nameFilters; QString directory = getVariant(L_p, 1).toString(); + QDir::Filters dirFilter = QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot; + QDirIterator::IteratorFlag dirIteratorFlag = QDirIterator::NoIteratorFlags; if (getArgumentCount(L_p) >= 2) { - nameFilters << getVariant(L_p, 2).toString(); + QVariant filterVariant = getVariant(L_p, 2); + if ((QMetaType::Type)filterVariant.type() == QMetaType::QVariantMap) { + QVariantMap filterMap = filterVariant.toMap(); + QVariantMap::const_iterator it = filterMap.constBegin(); + QVariantMap::const_iterator end = filterMap.constEnd(); + while (it != end) { + nameFilters << it.value().toString(); + it++; + } + } + else { + QString filterString = filterVariant.toString(); + if (!filterString.isEmpty()) { + nameFilters << filterVariant.toString(); + } + } + if (getArgumentCount(L_p) >= 3) { + bool ok; + int filterInt = getVariant(L_p, 3).toInt(&ok); + if (ok) { + switch (filterInt) { + case 1: + dirFilter = QDir::Files | QDir::NoDotAndDotDot; + break; + case 2: + dirFilter = QDir::Dirs | QDir::NoDotAndDotDot; + break; + case 4: + case 7: + dirIteratorFlag = QDirIterator::Subdirectories; + break; + case 5: + dirFilter = QDir::Files | QDir::NoDotAndDotDot; + dirIteratorFlag = QDirIterator::Subdirectories; + break; + case 6: + dirFilter = QDir::Dirs | QDir::NoDotAndDotDot; + dirIteratorFlag = QDirIterator::Subdirectories; + break; + } + } + } } - QDirIterator dirIterator(directory, nameFilters, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); + QDirIterator dirIterator(directory, nameFilters, dirFilter, dirIteratorFlag); while (dirIterator.hasNext()) { directories << dirIterator.next(); } @@ -71,6 +117,18 @@ int LuaEngineIO::directoryListFiles(lua_State *L_p) return 0; } +int LuaEngineIO::getDirectoryPath(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + QFileInfo fileInfo(getVariant(L_p, 1).toString()); + if (fileInfo.exists()) { + pushVariant(L_p, fileInfo.absoluteDir().absolutePath()); + return 1; + } + } + return 0; +} + int LuaEngineIO::linkFile(lua_State *L_p) { if (getArgumentCount(L_p) >= 2) { @@ -104,18 +162,6 @@ int LuaEngineIO::linkFile(lua_State *L_p) return 1; } -int LuaEngineIO::getDirectoryPath(lua_State *L_p) -{ - if (getArgumentCount(L_p) >= 1) { - QFileInfo fileInfo(getVariant(L_p, 1).toString()); - if (fileInfo.exists()) { - pushVariant(L_p, fileInfo.absoluteDir().absolutePath()); - return 1; - } - } - return 0; -} - QString LuaEngineIO::nameForPointer(void *pointer) { QString nameStorage; diff --git a/src/luaengineio/luaengine/LuaEngineIO.h b/src/luaengineio/luaengine/LuaEngineIO.h index fc96020..7048b21 100644 --- a/src/luaengineio/luaengine/LuaEngineIO.h +++ b/src/luaengineio/luaengine/LuaEngineIO.h @@ -30,9 +30,9 @@ public: LuaEngineIO(QObject *parent = nullptr, bool loadBaseLibraries = true); static void pushClass(lua_State *L_p); static void pushClass(LuaEngine *luaEngine); - static int directoryListFiles(lua_State *L_p); - static int linkFile(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); private: lua_State *L; diff --git a/src/luaengineos/luaengine/LuaEngineOS.cpp b/src/luaengineos/luaengine/LuaEngineOS.cpp index 6e3da8b..c42688b 100644 --- a/src/luaengineos/luaengine/LuaEngineOS.cpp +++ b/src/luaengineos/luaengine/LuaEngineOS.cpp @@ -56,7 +56,18 @@ int LuaEngineOS::executeProcess(lua_State *L_p) QString processPath = arguments.first().toString(); arguments.removeFirst(); for (const QVariant &argument : arguments) { - processArguments << argument.toString(); + if ((QMetaType::Type)argument.type() == QMetaType::QVariantMap) { + QVariantMap argumentMap = argument.toMap(); + QVariantMap::const_iterator it = argumentMap.constBegin(); + QVariantMap::const_iterator end = argumentMap.constEnd(); + while (it != end) { + processArguments << it.value().toString(); + it++; + } + } + else { + processArguments << argument.toString(); + } } if (runInBackground) { processSuccessed = QProcess::startDetached(processPath, processArguments);