add:添加获取文件后缀。
This commit is contained in:
parent
73d165ec93
commit
73ab3f4ba0
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"files.autoSave": "onFocusChange",
|
"files.autoSave": "onFocusChange",
|
||||||
"editor.fontSize": 14,
|
"editor.fontSize": 14,
|
||||||
"editor.fontFamily": "'Source Code Pro', 'Source Code Pro', 'Source Code Pro'",
|
"editor.fontFamily": "'Monaspace Krypton Light', 'Monaspace Krypton Light', 'Monaspace Krypton Light'",
|
||||||
"terminal.integrated.fontFamily": "Source Code Pro",
|
"terminal.integrated.fontFamily": "Monaspace Krypton Light",
|
||||||
"cmake.configureOnOpen": true,
|
"cmake.configureOnOpen": true,
|
||||||
"cmake.debugConfig": {
|
"cmake.debugConfig": {
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
-- 加载模块
|
-- 加载模块
|
||||||
local fs = require("lua_fs")
|
local fs = require("lua_fs")
|
||||||
print(fs.get_current_directory())
|
print(fs.get_current_directory())
|
||||||
|
print(fs.get_extension("test.lua"))
|
24
fs/lib.cxx
24
fs/lib.cxx
@ -23,6 +23,7 @@ static const luaL_Reg lua_fs[] = {{"exists", exists},
|
|||||||
{"append_path", append_path},
|
{"append_path", append_path},
|
||||||
{"mkdir", mkdir},
|
{"mkdir", mkdir},
|
||||||
{"normalize", normalize},
|
{"normalize", normalize},
|
||||||
|
{"get_extension", get_extension},
|
||||||
{NULL, NULL}};
|
{NULL, NULL}};
|
||||||
|
|
||||||
int luaopen_lua_fs(lua_State* L)
|
int luaopen_lua_fs(lua_State* L)
|
||||||
@ -66,6 +67,17 @@ int mkdir(lua_State* L)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief 获取文件扩展名
|
||||||
|
/// @param path string 文件路径
|
||||||
|
/// @return string
|
||||||
|
int get_extension(lua_State* L)
|
||||||
|
{
|
||||||
|
const char* path = luaL_checkstring(L, 1);
|
||||||
|
auto r = fs::path(path).extension().string();
|
||||||
|
lua_pushstring(L, r.c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief 获取父目录
|
/// @brief 获取父目录
|
||||||
/// @param path string 路径
|
/// @param path string 路径
|
||||||
/// @return string
|
/// @return string
|
||||||
@ -78,7 +90,7 @@ int get_parent(lua_State* L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// @brief 规范化路径
|
/// @brief 规范化路径
|
||||||
/// @param path string 路径
|
/// @param path string 路径
|
||||||
/// @return string
|
/// @return string
|
||||||
int normalize(lua_State* L)
|
int normalize(lua_State* L)
|
||||||
{
|
{
|
||||||
@ -166,7 +178,7 @@ int get_current_directory(lua_State* L)
|
|||||||
return 1;
|
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)
|
||||||
@ -187,11 +199,9 @@ int get_dir_files(lua_State* L)
|
|||||||
|
|
||||||
int index = 1;
|
int index = 1;
|
||||||
for (const auto& entry : fs::directory_iterator(dir_path)) {
|
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_pushstring(L, entry.path().filename().string().c_str());
|
lua_rawseti(L, table_index, index);
|
||||||
lua_rawseti(L, table_index, index);
|
index++;
|
||||||
index++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
1
fs/lib.h
1
fs/lib.h
@ -30,6 +30,7 @@ LIB_API int get_parent(lua_State* L);
|
|||||||
LIB_API int append_path(lua_State* L);
|
LIB_API int append_path(lua_State* L);
|
||||||
LIB_API int mkdir(lua_State* L);
|
LIB_API int mkdir(lua_State* L);
|
||||||
LIB_API int normalize(lua_State* L);
|
LIB_API int normalize(lua_State* L);
|
||||||
|
LIB_API int get_extension(lua_State* L);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user