add:添加文件路径操作接口。

This commit is contained in:
taynpg 2025-02-13 12:33:54 +08:00
parent e338d9f95a
commit b2ca6efec2
4 changed files with 78 additions and 10 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
build
.vs
.cache
cmake-*
cmake-*
auto_build

View File

@ -1,5 +1,3 @@
-- 加载模块
local fs = require("lua_fs")
print(fs.file_exists("D:\\download"))
local d = fs.is_regular_file("Java")
print(d)
print(fs.get_current_directory())

View File

@ -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)
{

View File

@ -20,11 +20,15 @@ extern "C" {
#include <lualib.h>
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
}