luaengineapp/src/luaengineos/luaengine/LuaEngineOS.cpp
2019-10-01 06:07:06 +02:00

169 lines
5.5 KiB
C++

/*****************************************************************************
* luaEngine Lua Engine for Qt
* Copyright (C) 2019 Syping
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "LuaEngineOS.h"
#include <QDirIterator>
#include <QTextStream>
#include <QProcess>
#include <QObject>
#include <QString>
#include <QDebug>
#include <QFile>
#ifdef Q_OS_WIN
#include "windows.h"
#include <iostream>
#else
#include "unistd.h"
#endif
LuaEngineOS::LuaEngineOS(QObject *parent, bool loadBaseLibraries) : LuaEngine(parent, loadBaseLibraries)
{
L = luaState();
pushClass(L);
}
void LuaEngineOS::pushClass(lua_State *L_p)
{
// Directory
pushFunction(L_p, "directoryListFiles", directoryListFiles);
// File
pushFunction(L_p, "linkFile", linkFile);
pushVariant(L_p, "Symlink", (int)0);
pushVariant(L_p, "Hardlink", (int)1);
// Process
pushFunction(L_p, "executeProcess", executeProcess);
}
void LuaEngineOS::pushClass(LuaEngine *luaEngine)
{
pushClass(luaEngine->luaState());
}
int LuaEngineOS::directoryListFiles(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 1) {
QStringList directories;
QStringList nameFilters;
QString directory = getVariant(L_p, 1).toString();
if (getArgumentCount(L_p) >= 2) {
nameFilters << getVariant(L_p, 2).toString();
}
QDirIterator dirIterator(directory, nameFilters, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (dirIterator.hasNext()) {
directories << dirIterator.next();
}
pushVariant(L_p, directories);
return 1;
}
return 0;
}
int LuaEngineOS::linkFile(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 2) {
bool symlinkMode = true;
if (getArgumentCount(L_p) >= 3 && getVariant(L_p, 3).toInt() == 1) {
symlinkMode = false;
}
bool linkSucceeded = false;
if (symlinkMode) {
#ifdef Q_OS_WIN
QString targetFile = getVariant(L_p, 2).toString();
if (targetFile.right(4) != ".lnk") {
targetFile += ".lnk";
}
linkSucceeded = QFile::link(getVariant(L_p, 1).toString(), targetFile);
#else
linkSucceeded = QFile::link(getVariant(L_p, 1).toString(), getVariant(L_p, 2).toString());
#endif
}
else {
#ifdef Q_OS_WIN
linkSucceeded = CreateHardLinkW(getVariant(L_p, 2).toString().toStdWString().c_str(), getVariant(L_p, 1).toString().toStdWString().c_str(), NULL);
#else
linkSucceeded = link(getVariant(L_p, 1).toString().toUtf8().data(), getVariant(L_p, 2).toString().toUtf8().data());
#endif
}
pushVariant(L_p, linkSucceeded);
return 1;
}
getVariant(L_p, false);
return 1;
}
int LuaEngineOS::executeProcess(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 1) {
int processReturn = 0;
bool runInBackground = false;
bool processSuccessed = false;
if (getArgumentCount(L_p) >= 2) {
QVariantList arguments = getArguments(L_p);
QVariant lastArgument = arguments.last();
if (lastArgument.type() == QVariant::Bool) {
runInBackground = lastArgument.toBool();
arguments.removeLast();
}
if (arguments.length() >= 2) {
QStringList processArguments;
QString processPath = arguments.first().toString();
arguments.removeFirst();
for (const QVariant &argument : arguments) {
processArguments << argument.toString();
}
if (runInBackground) {
processSuccessed = QProcess::startDetached(processPath, processArguments);
}
else {
processReturn = QProcess::execute(processPath, processArguments);
}
}
else {
if (runInBackground) {
processSuccessed = QProcess::startDetached(arguments.first().toString());
}
else {
processReturn = QProcess::execute(arguments.first().toString());
}
}
}
else {
processReturn = QProcess::execute(getVariant(L_p, 1).toString());
}
if (runInBackground && !processSuccessed) {
processReturn = -2;
}
else if (!runInBackground && processReturn == 0) {
processSuccessed = true;
}
pushVariant(L_p, processSuccessed);
pushVariant(L_p, processReturn);
return 2;
}
pushVariant(L_p, false);
pushVariant(L_p, -2);
return 2;
}
QString LuaEngineOS::nameForPointer(void *pointer)
{
QString nameStorage;
QTextStream(&nameStorage) << "LuaEngineOS" << pointer;
return nameStorage;
}