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",
|
||||
"editor.fontSize": 14,
|
||||
"editor.fontFamily": "'Source Code Pro', 'Source Code Pro', 'Source Code Pro'",
|
||||
"terminal.integrated.fontFamily": "Source Code Pro",
|
||||
"editor.fontFamily": "'Monaspace Krypton Light', 'Monaspace Krypton Light', 'Monaspace Krypton Light'",
|
||||
"terminal.integrated.fontFamily": "Monaspace Krypton Light",
|
||||
"cmake.configureOnOpen": true,
|
||||
"cmake.debugConfig": {
|
||||
"console": "integratedTerminal",
|
||||
|
@ -1,3 +1,4 @@
|
||||
-- 加载模块
|
||||
local fs = require("lua_fs")
|
||||
print(fs.get_current_directory())
|
||||
print(fs.get_extension("test.lua"))
|
22
fs/lib.cxx
22
fs/lib.cxx
@ -23,6 +23,7 @@ static const luaL_Reg lua_fs[] = {{"exists", exists},
|
||||
{"append_path", append_path},
|
||||
{"mkdir", mkdir},
|
||||
{"normalize", normalize},
|
||||
{"get_extension", get_extension},
|
||||
{NULL, NULL}};
|
||||
|
||||
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 获取父目录
|
||||
/// @param path string 路径
|
||||
/// @return string
|
||||
@ -166,7 +178,7 @@ int get_current_directory(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// @brief 获取一个目录下所有文件(不递归)
|
||||
/// @brief 获取一个目录下所有文件(包括文件夹,不递归)
|
||||
/// @param dir_path string 目录
|
||||
/// @return table
|
||||
int get_dir_files(lua_State* L)
|
||||
@ -187,11 +199,9 @@ int get_dir_files(lua_State* L)
|
||||
|
||||
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++;
|
||||
}
|
||||
lua_pushstring(L, entry.path().filename().string().c_str());
|
||||
lua_rawseti(L, table_index, index);
|
||||
index++;
|
||||
}
|
||||
return 1;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user