fix tables, getDirectoryContent replaces directoyListFiles

This commit is contained in:
Syping 2019-10-24 09:07:32 +02:00
parent 1f0737a981
commit 96f0511b0f
4 changed files with 82 additions and 26 deletions

View file

@ -257,10 +257,9 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
lua_createtable(L_p, 0, stringList.count()); lua_createtable(L_p, 0, stringList.count());
int currentId = 1; int currentId = 1;
for (const QString &string : stringList) { for (const QString &string : stringList) {
char buffer[65]; lua_pushinteger(L_p, currentId);
snprintf(buffer, 10, "%d", currentId);
lua_pushstring(L_p, string.toUtf8().data()); lua_pushstring(L_p, string.toUtf8().data());
lua_setfield(L_p, -2, buffer); lua_settable(L_p, -3);
currentId++; currentId++;
} }
} }
@ -269,10 +268,9 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
lua_createtable(L_p, 0, variantList.count()); lua_createtable(L_p, 0, variantList.count());
int currentId = 1; int currentId = 1;
for (const QVariant &variant : variantList) { for (const QVariant &variant : variantList) {
char buffer[65]; lua_pushinteger(L_p, currentId);
snprintf(buffer, 10, "%d", currentId);
pushVariant(L_p, variant); pushVariant(L_p, variant);
lua_setfield(L_p, -2, buffer); lua_settable(L_p, -3);
currentId++; currentId++;
} }
} }
@ -282,8 +280,9 @@ void LuaEngine::pushVariant(lua_State *L_p, const QVariant &variant)
QVariantMap::const_iterator it = variantMap.constBegin(); QVariantMap::const_iterator it = variantMap.constBegin();
QVariantMap::const_iterator end = variantMap.constEnd(); QVariantMap::const_iterator end = variantMap.constEnd();
while (it != end) { while (it != end) {
lua_pushstring(L_p, it.key().toUtf8().data());
pushVariant(L_p, it.value()); pushVariant(L_p, it.value());
lua_setfield(L_p, -2, it.key().toUtf8().data()); lua_settable(L_p, -3);
it++; it++;
} }
} }

View file

@ -38,8 +38,11 @@ LuaEngineIO::LuaEngineIO(QObject *parent, bool loadBaseLibraries) : LuaEngine(pa
void LuaEngineIO::pushClass(lua_State *L_p) void LuaEngineIO::pushClass(lua_State *L_p)
{ {
// Directory // Directory
pushFunction(L_p, "directoryListFiles", directoryListFiles); pushFunction(L_p, "getDirectoryContent", getDirectoryContent);
pushFunction(L_p, "getDirectoryPath", getDirectoryPath); pushFunction(L_p, "getDirectoryPath", getDirectoryPath);
pushVariant(L_p, "Files", (int)1);
pushVariant(L_p, "Directories", (int)2);
pushVariant(L_p, "Subdirectories", (int)4);
// File // File
pushFunction(L_p, "linkFile", linkFile); pushFunction(L_p, "linkFile", linkFile);
@ -52,16 +55,59 @@ void LuaEngineIO::pushClass(LuaEngine *luaEngine)
pushClass(luaEngine->luaState()); pushClass(luaEngine->luaState());
} }
int LuaEngineIO::directoryListFiles(lua_State *L_p) int LuaEngineIO::getDirectoryContent(lua_State *L_p)
{ {
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
QStringList directories; QStringList directories;
QStringList nameFilters; QStringList nameFilters;
QString directory = getVariant(L_p, 1).toString(); 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) { 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()) { while (dirIterator.hasNext()) {
directories << dirIterator.next(); directories << dirIterator.next();
} }
@ -71,6 +117,18 @@ int LuaEngineIO::directoryListFiles(lua_State *L_p)
return 0; 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) int LuaEngineIO::linkFile(lua_State *L_p)
{ {
if (getArgumentCount(L_p) >= 2) { if (getArgumentCount(L_p) >= 2) {
@ -104,18 +162,6 @@ int LuaEngineIO::linkFile(lua_State *L_p)
return 1; 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 LuaEngineIO::nameForPointer(void *pointer)
{ {
QString nameStorage; QString nameStorage;

View file

@ -30,9 +30,9 @@ public:
LuaEngineIO(QObject *parent = nullptr, bool loadBaseLibraries = true); LuaEngineIO(QObject *parent = nullptr, bool loadBaseLibraries = true);
static void pushClass(lua_State *L_p); static void pushClass(lua_State *L_p);
static void pushClass(LuaEngine *luaEngine); static void pushClass(LuaEngine *luaEngine);
static int directoryListFiles(lua_State *L_p); static int getDirectoryContent(lua_State *L_p);
static int linkFile(lua_State *L_p);
static int getDirectoryPath(lua_State *L_p); static int getDirectoryPath(lua_State *L_p);
static int linkFile(lua_State *L_p);
private: private:
lua_State *L; lua_State *L;

View file

@ -56,7 +56,18 @@ int LuaEngineOS::executeProcess(lua_State *L_p)
QString processPath = arguments.first().toString(); QString processPath = arguments.first().toString();
arguments.removeFirst(); arguments.removeFirst();
for (const QVariant &argument : arguments) { 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) { if (runInBackground) {
processSuccessed = QProcess::startDetached(processPath, processArguments); processSuccessed = QProcess::startDetached(processPath, processArguments);