diff --git a/.vscode/settings.json b/.vscode/settings.json index 113c22c..ea5c5ef 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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", diff --git a/fs/fs.lua b/fs/fs.lua index 28eabf1..3d00497 100644 --- a/fs/fs.lua +++ b/fs/fs.lua @@ -1,3 +1,4 @@ -- 加载模块 local fs = require("lua_fs") -print(fs.get_current_directory()) \ No newline at end of file +print(fs.get_current_directory()) +print(fs.get_extension("test.lua")) \ No newline at end of file diff --git a/fs/lib.cxx b/fs/lib.cxx index c7be35e..88c31f4 100644 --- a/fs/lib.cxx +++ b/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 @@ -78,7 +90,7 @@ int get_parent(lua_State* L) } /// @brief 规范化路径 -/// @param path string 路径 +/// @param path string 路径 /// @return string int normalize(lua_State* L) { @@ -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; } \ No newline at end of file diff --git a/fs/lib.h b/fs/lib.h index 937a5b4..d4e5dae 100644 --- a/fs/lib.h +++ b/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 mkdir(lua_State* L); LIB_API int normalize(lua_State* L); +LIB_API int get_extension(lua_State* L); #ifdef __cplusplus }