diff --git a/.clang-format b/.clang-format index 402f22e..414ec4c 100644 --- a/.clang-format +++ b/.clang-format @@ -11,7 +11,7 @@ ReflowComments: true SpacesBeforeTrailingComments: 3 TabWidth: 4 ConstructorInitializerAllOnOneLineOrOnePerLine: true -ColumnLimit: 80 +ColumnLimit: 100 AllowShortBlocksOnASingleLine: Never AllowShortFunctionsOnASingleLine: None AllowShortEnumsOnASingleLine: false diff --git a/CMakeLists.txt b/CMakeLists.txt index fec82c2..4668a02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,5 +16,5 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/) add_subdirectory(lua) -add_subdirectory(demo) +add_subdirectory(fs) add_executable(elua main.cpp) \ No newline at end of file diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt deleted file mode 100644 index c14842a..0000000 --- a/demo/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -project(demo LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 17) - -if (MSVC) - add_compile_options(/source-charset:utf-8) -endif() - -add_library(demo SHARED lib.h lib.cxx) -target_link_libraries(demo PRIVATE lua) \ No newline at end of file diff --git a/demo/lib.cxx b/demo/lib.cxx deleted file mode 100644 index eff5d9e..0000000 --- a/demo/lib.cxx +++ /dev/null @@ -1,16 +0,0 @@ -#include "lib.h" - -int c_multiply(lua_State* L) -{ - double a = luaL_checknumber(L, 1); - double b = luaL_checknumber(L, 2); - lua_pushnumber(L, a * b); - return 1; -} - -// 注册 C 函数 -int luaopen_mylib(lua_State* L) -{ - lua_register(L, "c_multiply", c_multiply); // 注册为全局函数 - return 0; -} \ No newline at end of file diff --git a/demo/lib.h b/demo/lib.h deleted file mode 100644 index de3a4c5..0000000 --- a/demo/lib.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef LIB_HEADER -#define LIB_HEADER - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include - -int luaopen_mylib(lua_State* L); -int c_multiply(lua_State* L); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/fs/CMakeLists.txt b/fs/CMakeLists.txt new file mode 100644 index 0000000..dc40de4 --- /dev/null +++ b/fs/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.16) + +project(lua_fs LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 17) + +if (MSVC) + add_compile_options(/source-charset:utf-8) +endif() + +add_library(lua_fs SHARED lib.h lib.cxx) +target_link_libraries(lua_fs PRIVATE lua) +target_compile_definitions(lua_fs PRIVATE FS_LIB_EXPORT) \ No newline at end of file diff --git a/fs/debug.bat b/fs/debug.bat new file mode 100644 index 0000000..f09bab8 --- /dev/null +++ b/fs/debug.bat @@ -0,0 +1,2 @@ +@echo off +cmd /k "set PATH=%PATH%;%~dp0..\build\bin\Debug" \ No newline at end of file diff --git a/fs/fs.lua b/fs/fs.lua new file mode 100644 index 0000000..1b64b6a --- /dev/null +++ b/fs/fs.lua @@ -0,0 +1,12 @@ +-- 加载模块 +local fs = require "lua_fs" + +print(fs.file_exists("D:\\download")) +local files = fs.get_dir_files("D:\\download") +if files then + for i, filename in ipairs(files) do + print(i, filename) -- 输出索引和文件名 + end +else + print("Error:") +end \ No newline at end of file diff --git a/fs/lib.cxx b/fs/lib.cxx new file mode 100644 index 0000000..ba0ac38 --- /dev/null +++ b/fs/lib.cxx @@ -0,0 +1,75 @@ +#include "lib.h" +#include +#include + +namespace fs = std::filesystem; + +static const luaL_Reg lua_fs[] = { + {"file_exists", file_exists}, {"is_regular_file", is_regular_file}, {"copy_file", copy_file}, + {"delete_file", delete_file}, {"get_dir_files", get_dir_files}, {NULL, NULL}}; + +int file_exists(lua_State* L) +{ + const char* path = luaL_checkstring(L, 1); + bool result = fs::exists(path); + lua_pushboolean(L, result); + return 1; +} + +int is_regular_file(lua_State* L) +{ + const char* path = luaL_checkstring(L, 1); + bool result = fs::is_regular_file(path); + lua_pushboolean(L, result); + return 1; +} + +int copy_file(lua_State* L) +{ + const char* source = luaL_checkstring(L, 1); + const char* destination = luaL_checkstring(L, 2); + bool result = fs::copy_file(source, destination); + lua_pushboolean(L, result); + return 1; +} + +int delete_file(lua_State* L) +{ + const char* path = luaL_checkstring(L, 1); + bool result = fs::remove(path); + lua_pushboolean(L, result); + return 1; +} + +int luaopen_lua_fs(lua_State* L) +{ + luaL_newlib(L, lua_fs); + return 1; +} + +int get_dir_files(lua_State* L) +{ + // 获取第一个参数:目录路径 + const char* dir_path = luaL_checkstring(L, 1); + + // 检查目录是否存在 + if (!fs::exists(dir_path) || !fs::is_directory(dir_path)) { + lua_pushnil(L); + lua_pushstring(L, "Invalid directory path"); + return 2; // 返回 nil 和错误信息 + } + + // 创建一个新的 Lua 表用于存放文件列表 + lua_newtable(L); + int table_index = lua_gettop(L); // 获取当前 Lua 表的位置 + + int index = 1; + for (const auto& entry : fs::directory_iterator(dir_path)) { + if (fs::is_regular_file(entry.status())) { + lua_pushstring(L, entry.path().filename().string().c_str()); + lua_rawseti(L, table_index, index); + index++; + } + } + return 1; +} \ No newline at end of file diff --git a/fs/lib.h b/fs/lib.h new file mode 100644 index 0000000..e80d8e4 --- /dev/null +++ b/fs/lib.h @@ -0,0 +1,33 @@ +#ifndef LIB_HEADER +#define LIB_HEADER + +#ifdef _WIN32 +#ifdef FS_LIB_EXPORT +#define LIB_API __declspec(dllexport) +#else +#define LIB_API __declspec(dllimport) +#endif +#else +#define LIB_API +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +LIB_API int luaopen_lua_fs(lua_State* L); +LIB_API int file_exists(lua_State* L); +LIB_API int is_regular_file(lua_State* L); +LIB_API int copy_file(lua_State* L); +LIB_API int delete_file(lua_State* L); +LIB_API int get_dir_files(lua_State* L); + +#ifdef __cplusplus +} +#endif + +#endif // LIB_HEADER