add:添加文件路径操作接口。
This commit is contained in:
parent
e338d9f95a
commit
b2ca6efec2
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
build
|
build
|
||||||
.vs
|
.vs
|
||||||
.cache
|
.cache
|
||||||
cmake-*
|
cmake-*
|
||||||
|
auto_build
|
@ -1,5 +1,3 @@
|
|||||||
-- 加载模块
|
-- 加载模块
|
||||||
local fs = require("lua_fs")
|
local fs = require("lua_fs")
|
||||||
print(fs.file_exists("D:\\download"))
|
print(fs.get_current_directory())
|
||||||
local d = fs.is_regular_file("Java")
|
|
||||||
print(d)
|
|
75
fs/lib.cxx
75
fs/lib.cxx
@ -4,9 +4,20 @@
|
|||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
static const luaL_Reg lua_fs[] = {
|
/*
|
||||||
{"file_exists", file_exists}, {"is_regular_file", is_regular_file}, {"copy_file", copy_file},
|
return 语句的值代表着你从 C++ 函数返回的 Lua 堆栈上的元素个数。
|
||||||
{"delete_file", delete_file}, {"get_dir_files", get_dir_files}, {NULL, NULL}};
|
*/
|
||||||
|
|
||||||
|
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)
|
int luaopen_lua_fs(lua_State* L)
|
||||||
{
|
{
|
||||||
@ -14,10 +25,55 @@ int luaopen_lua_fs(lua_State* L)
|
|||||||
return 1;
|
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 文件是否存在
|
/// @brief 文件是否存在
|
||||||
/// @param path string 文件路径
|
/// @param path string 文件路径
|
||||||
/// @return boolean
|
/// @return boolean
|
||||||
int file_exists(lua_State* L)
|
int exists(lua_State* L)
|
||||||
{
|
{
|
||||||
const char* path = luaL_checkstring(L, 1);
|
const char* path = luaL_checkstring(L, 1);
|
||||||
bool result = fs::exists(path);
|
bool result = fs::exists(path);
|
||||||
@ -60,8 +116,17 @@ int delete_file(lua_State* L)
|
|||||||
return 1;
|
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 获取一个目录下所有文件(不递归)
|
/// @brief 获取一个目录下所有文件(不递归)
|
||||||
/// @param dir_path string 目录
|
/// @param dir_path string 目录
|
||||||
/// @return table
|
/// @return table
|
||||||
int get_dir_files(lua_State* L)
|
int get_dir_files(lua_State* L)
|
||||||
{
|
{
|
||||||
|
6
fs/lib.h
6
fs/lib.h
@ -20,11 +20,15 @@ extern "C" {
|
|||||||
#include <lualib.h>
|
#include <lualib.h>
|
||||||
|
|
||||||
LIB_API int luaopen_lua_fs(lua_State* L);
|
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 is_regular_file(lua_State* L);
|
||||||
LIB_API int copy_file(lua_State* L);
|
LIB_API int copy_file(lua_State* L);
|
||||||
LIB_API int delete_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_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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user