From b2ca6efec2952dc9102c5c975c04b3eccfee9080 Mon Sep 17 00:00:00 2001 From: taynpg Date: Thu, 13 Feb 2025 12:33:54 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B7=AF=E5=BE=84=E6=93=8D=E4=BD=9C=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- fs/fs.lua | 4 +-- fs/lib.cxx | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++---- fs/lib.h | 6 ++++- 4 files changed, 78 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 5b861d3..10b05df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build .vs .cache -cmake-* \ No newline at end of file +cmake-* +auto_build \ No newline at end of file diff --git a/fs/fs.lua b/fs/fs.lua index 25d1330..28eabf1 100644 --- a/fs/fs.lua +++ b/fs/fs.lua @@ -1,5 +1,3 @@ -- 加载模块 local fs = require("lua_fs") -print(fs.file_exists("D:\\download")) -local d = fs.is_regular_file("Java") -print(d) \ No newline at end of file +print(fs.get_current_directory()) \ No newline at end of file diff --git a/fs/lib.cxx b/fs/lib.cxx index bf58272..937a9b4 100644 --- a/fs/lib.cxx +++ b/fs/lib.cxx @@ -4,9 +4,20 @@ 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}}; +/* + return 语句的值代表着你从 C++ 函数返回的 Lua 堆栈上的元素个数。 +*/ + +static const luaL_Reg lua_fs[] = {{"exists", exists}, + {"is_regular_file", is_regular_file}, + {"copy_file", copy_file}, + {"delete_file", delete_file}, + {"get_dir_files", get_dir_files}, + {"get_current_directory", get_current_directory}, + {"get_parent", get_parent}, + {"append_path", append_path}, + {"mkdir", mkdir}, + {NULL, NULL}}; int luaopen_lua_fs(lua_State* L) { @@ -14,10 +25,55 @@ int luaopen_lua_fs(lua_State* L) return 1; } +/// @brief 创建文件夹 +/// @param path string 路径或者文件夹名称 +/// @return boolean +int mkdir(lua_State* L) +{ + const char* path = luaL_checkstring(L, 1); + try { + fs::path p(path); + if (p.is_relative()) { + p = fs::current_path().append(path); + } + fs::create_directories(p); + lua_pushboolean(L, true); + return 1; + } catch (const std::exception& e) { + lua_pushnil(L); + lua_pushstring(L, e.what()); + return 2; + } +} + +/// @brief 获取父目录 +/// @param path string 路径 +/// @return string +int get_parent(lua_State* L) +{ + const char* path = luaL_checkstring(L, 1); + auto r = fs::path(path).parent_path().string(); + lua_pushstring(L, r.c_str()); + return 1; +} + +/// @brief 拼接路径 +/// @param path string 基本路径 +/// @param append string 添加路径 +/// @return string +int append_path(lua_State* L) +{ + const char* path = luaL_checkstring(L, 1); + const char* append = luaL_checkstring(L, 2); + auto r = fs::path(path).append(append).string(); + lua_pushstring(L, r.c_str()); + return 1; +} + /// @brief 文件是否存在 /// @param path string 文件路径 /// @return boolean -int file_exists(lua_State* L) +int exists(lua_State* L) { const char* path = luaL_checkstring(L, 1); bool result = fs::exists(path); @@ -60,8 +116,17 @@ int delete_file(lua_State* L) return 1; } +/// @brief 获取当前工作目录 +/// @return string +int get_current_directory(lua_State* L) +{ + std::string current_dir = fs::current_path().string(); + lua_pushstring(L, current_dir.c_str()); + return 1; +} + /// @brief 获取一个目录下所有文件(不递归) -/// @param dir_path string 目录 +/// @param dir_path string 目录 /// @return table int get_dir_files(lua_State* L) { diff --git a/fs/lib.h b/fs/lib.h index e80d8e4..d64b114 100644 --- a/fs/lib.h +++ b/fs/lib.h @@ -20,11 +20,15 @@ extern "C" { #include LIB_API int luaopen_lua_fs(lua_State* L); -LIB_API int file_exists(lua_State* L); +LIB_API int 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); +LIB_API int get_current_directory(lua_State* L); +LIB_API int get_parent(lua_State* L); +LIB_API int append_path(lua_State* L); +LIB_API int mkdir(lua_State* L); #ifdef __cplusplus }