make I/O library

This commit is contained in:
Syping 2019-10-03 00:05:40 +02:00
parent 9321c1b9fb
commit 769d291f35
12 changed files with 247 additions and 72 deletions

View file

@ -21,15 +21,15 @@ CONFIG += c++11
static: DEFINES += LUAENGINE_STATIC
CONFIG(debug, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/debug -lluaengineos -L$$OUT_PWD/../luaenginegui/debug -lluaenginegui -L$$OUT_PWD/../luaengine/debug -lluaengine
CONFIG(release, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/release -lluaengineos -L$$OUT_PWD/../luaenginegui/release -lluaenginegui -L$$OUT_PWD/../luaengine/release -lluaengine
unix: LIBS += -L$$OUT_PWD/../luaengineos -lluaengineos -L$$OUT_PWD/../luaenginegui -lluaenginegui -L$$OUT_PWD/../luaengine -lluaengine
win32: LIBS += -luser32
CONFIG(debug, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/debug -lluaengineos -L$$OUT_PWD/../luaengineio/debug -lluaengineio -L$$OUT_PWD/../luaenginegui/debug -lluaenginegui -L$$OUT_PWD/../luaengine/debug -lluaengine
CONFIG(release, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/release -lluaengineos -L$$OUT_PWD/../luaengineio/release -lluaengineio -L$$OUT_PWD/../luaenginegui/release -lluaenginegui -L$$OUT_PWD/../luaengine/release -lluaengine
unix: LIBS += -L$$OUT_PWD/../luaengineos -lluaengineos -L$$OUT_PWD/../luaengineio -lluaengineio -L$$OUT_PWD/../luaenginegui -lluaenginegui -L$$OUT_PWD/../luaengine -lluaengine
INCLUDEPATH += \
../luaengine/lua \
../luaengine/luaengine \
../luaenginegui/luaengine \
../luaengineio/luaengine \
../luaengineos/luaengine
SOURCES += \

View file

@ -16,6 +16,7 @@
*****************************************************************************/
#include "LuaEngineGui.h"
#include "LuaEngineIO.h"
#include "LuaEngineOS.h"
#include <QApplication>
#include <QFont>
@ -52,6 +53,7 @@ int main(int argc, char *argv[])
#endif
LuaEngineGui luaEngineGui;
LuaEngineIO::pushClass(&luaEngineGui);
LuaEngineOS::pushClass(&luaEngineGui);
luaEngineGui.executeLuaScript(luaScript);

View file

@ -0,0 +1,110 @@
/*****************************************************************************
* 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 "LuaEngineIO.h"
#include <QDirIterator>
#include <QTextStream>
#include <QObject>
#include <QString>
#include <QFile>
#ifdef Q_OS_WIN
#include "windows.h"
#include <iostream>
#else
#include "unistd.h"
#endif
LuaEngineIO::LuaEngineIO(QObject *parent, bool loadBaseLibraries) : LuaEngine(parent, loadBaseLibraries)
{
L = luaState();
pushClass(L);
}
void LuaEngineIO::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);
}
void LuaEngineIO::pushClass(LuaEngine *luaEngine)
{
pushClass(luaEngine->luaState());
}
int LuaEngineIO::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 LuaEngineIO::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;
}
QString LuaEngineIO::nameForPointer(void *pointer)
{
QString nameStorage;
QTextStream(&nameStorage) << "LuaEngineIO" << pointer;
return nameStorage;
}

View file

@ -0,0 +1,41 @@
/*****************************************************************************
* 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.
*****************************************************************************/
#ifndef LUAENGINEIO_H
#define LUAENGINEIO_H
#include "LuaEngineIO_global.h"
#include "LuaEngine.h"
#include <QString>
#include <QObject>
class LUAENGINEIOSHARED_EXPORT LuaEngineIO : public LuaEngine
{
Q_OBJECT
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);
private:
lua_State *L;
static QString nameForPointer(void *pointer);
};
#endif // LUAENGINEIO_H

View file

@ -0,0 +1,33 @@
/*****************************************************************************
* 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.
*****************************************************************************/
#ifndef LUAENGINEIO_GLOBAL_H
#define LUAENGINEIO_GLOBAL_H
#include <QtCore/qglobal.h>
#ifndef LUAENGINE_STATIC
#ifdef LUAENGINEIO_LIBRARY
#define LUAENGINEIOSHARED_EXPORT Q_DECL_EXPORT
#else
#define LUAENGINEIOSHARED_EXPORT Q_DECL_IMPORT
#endif
#else
#define LUAENGINEIOSHARED_EXPORT
#endif
#endif // LUAENGINEIO_GLOBAL_H

View file

@ -0,0 +1,49 @@
#/*****************************************************************************
#* 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.
#*****************************************************************************/
QT += core
TARGET = luaengineio
TEMPLATE = lib
CONFIG += c++11 \
skip_target_version_ext
VERSION = 0.1
DEFINES += LUAENGINEIO_LIBRARY
static: DEFINES += LUAENGINE_STATIC
CONFIG(debug, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengine/debug -lluaengine
CONFIG(release, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengine/release -lluaengine
unix: LIBS += -L$$OUT_PWD/../luaengine -lluaengine
INCLUDEPATH += \
../luaengine/lua \
../luaengine/luaengine
SOURCES += \
luaengine/LuaEngineIO.cpp
HEADERS += \
luaengine/LuaEngineIO.h \
luaengine/LuaEngineIO_global.h
win32 {
RC_LANG = 0x0
QMAKE_TARGET_COMPANY = "Syping"
QMAKE_TARGET_DESCRIPTION = "LuaEngine I/O Library"
QMAKE_TARGET_COPYRIGHT = "Copyright (c) 2019 Syping"
QMAKE_TARGET_PRODUCT = "luaengineio"
}

View file

@ -21,7 +21,6 @@
#include <QProcess>
#include <QObject>
#include <QString>
#include <QDebug>
#include <QFile>
#ifdef Q_OS_WIN
#include "windows.h"
@ -38,14 +37,6 @@ LuaEngineOS::LuaEngineOS(QObject *parent, bool loadBaseLibraries) : LuaEngine(pa
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);
}
@ -55,58 +46,6 @@ 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) {

View file

@ -30,8 +30,6 @@ public:
LuaEngineOS(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 executeProcess(lua_State *L_p);
private:

View file

@ -1,6 +1,6 @@
/*****************************************************************************
* luaEngine Lua Engine for Qt
* Copyright (C) 2018-2019 Syping
* 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.

View file

@ -21,15 +21,15 @@ CONFIG += c++11
static: DEFINES += LUAENGINE_STATIC
CONFIG(debug, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/debug -lluaengineos -L$$OUT_PWD/../luaenginegui/debug -lluaenginegui -L$$OUT_PWD/../luaengine/debug -lluaengine
CONFIG(release, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/release -lluaengineos -L$$OUT_PWD/../luaenginegui/release -lluaenginegui -L$$OUT_PWD/../luaengine/release -lluaengine
unix: LIBS += -L$$OUT_PWD/../luaengineos -lluaengineos -L$$OUT_PWD/../luaenginegui -lluaenginegui -L$$OUT_PWD/../luaengine -lluaengine
win32: LIBS += -luser32
CONFIG(debug, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/debug -lluaengineos -L$$OUT_PWD/../luaengineio/debug -lluaengineio -L$$OUT_PWD/../luaenginegui/debug -lluaenginegui -L$$OUT_PWD/../luaengine/debug -lluaengine
CONFIG(release, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengineos/release -lluaengineos -L$$OUT_PWD/../luaengineio/release -lluaengineio -L$$OUT_PWD/../luaenginegui/release -lluaenginegui -L$$OUT_PWD/../luaengine/release -lluaengine
unix: LIBS += -L$$OUT_PWD/../luaengineos -lluaengineos -L$$OUT_PWD/../luaengineio -lluaengineio -L$$OUT_PWD/../luaenginegui -lluaenginegui -L$$OUT_PWD/../luaengine -lluaengine
INCLUDEPATH += \
../luaengine/lua \
../luaengine/luaengine \
../luaenginegui/luaengine \
../luaengineio/luaengine \
../luaengineos/luaengine
SOURCES += \

View file

@ -16,6 +16,7 @@
*****************************************************************************/
#include "LuaEngineGui.h"
#include "LuaEngineIO.h"
#include "LuaEngineOS.h"
#include <QApplication>
#include <QFont>
@ -46,6 +47,7 @@ int main(int argc, char *argv[])
QFile luaScript(arguments.first().toString());
LuaEngineGui luaEngineGui;
LuaEngineIO::pushClass(&luaEngineGui);
LuaEngineOS::pushClass(&luaEngineGui);
luaEngineGui.executeLuaScript(&luaScript);

View file

@ -20,6 +20,7 @@ CONFIG += ordered
SUBDIRS += luaengine \
luaenginegui \
luaengineio \
luaengineos
CONFIG(WITH_LUAENGINEAPP): SUBDIRS += luaengineapp