From 9b17fe144dadc590e1c273902a83b44c405e39e3 Mon Sep 17 00:00:00 2001 From: Syping Date: Fri, 3 Jul 2020 19:18:44 +0200 Subject: [PATCH] add read and write eIO functions --- src/luaengineio/luaengine/LuaEngineIO.cpp | 64 +++++++++++++++++++++++ src/luaengineio/luaengine/LuaEngineIO.h | 4 ++ 2 files changed, 68 insertions(+) diff --git a/src/luaengineio/luaengine/LuaEngineIO.cpp b/src/luaengineio/luaengine/LuaEngineIO.cpp index ce55b31..5910006 100644 --- a/src/luaengineio/luaengine/LuaEngineIO.cpp +++ b/src/luaengineio/luaengine/LuaEngineIO.cpp @@ -43,6 +43,12 @@ void LuaEngineIO::pushClass(lua_State *L_p) pushVariant(L_p, "Directories", (int)2); pushVariant(L_p, "Subdirectories", (int)4); + // Engine IO + pushFunction(L_p, "eIORead", eIORead); + pushFunction(L_p, "eIOReadAll", eIOReadAll); + pushFunction(L_p, "eIOReadLine", eIOReadLine); + pushFunction(L_p, "eIOWrite", eIOWrite); + // File pushFunction(L_p, "checkFileExists", checkFileExists); pushFunction(L_p, "linkFile", linkFile); @@ -87,6 +93,64 @@ int LuaEngineIO::checkFileExists(lua_State *L_p) return 1; } +int LuaEngineIO::eIORead(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 2) { + void *pointer = getPointer(L_p, 1); + if (pointer != NULL) { + if (((QObject*)pointer)->inherits("QIODevice")) { + const QByteArray readData = ((QIODevice*)pointer)->read(getVariant(L_p, 2).toLongLong()); + pushVariant(L_p, readData); + return 1; + } + } + } + return 0; +} + +int LuaEngineIO::eIOReadAll(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + void *pointer = getPointer(L_p, 1); + if (pointer != NULL) { + if (((QObject*)pointer)->inherits("QIODevice")) { + const QByteArray readData = ((QIODevice*)pointer)->readAll(); + pushVariant(L_p, readData); + return 1; + } + } + } + return 0; +} + +int LuaEngineIO::eIOReadLine(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + void *pointer = getPointer(L_p, 1); + if (pointer != NULL) { + if (((QObject*)pointer)->inherits("QIODevice")) { + const QByteArray readData = ((QIODevice*)pointer)->readLine(); + pushVariant(L_p, readData); + return 1; + } + } + } + return 0; +} + +int LuaEngineIO::eIOWrite(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 2) { + void *pointer = getPointer(L_p, 1); + if (pointer != NULL) { + if (((QObject*)pointer)->inherits("QIODevice")) { + ((QIODevice*)pointer)->write(getVariant(L_p, 2).toByteArray()); + } + } + } + return 0; +} + 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 463ce5a..8a28322 100644 --- a/src/luaengineio/luaengine/LuaEngineIO.h +++ b/src/luaengineio/luaengine/LuaEngineIO.h @@ -32,6 +32,10 @@ public: static void pushClass(LuaEngine *luaEngine); static int checkDirectoryExists(lua_State *L_p); static int checkFileExists(lua_State *L_p); + static int eIORead(lua_State *L_p); + static int eIOReadAll(lua_State *L_p); + static int eIOReadLine(lua_State *L_p); + static int eIOWrite(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);