Skip to content

Commit

Permalink
切割fs.pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed May 10, 2024
1 parent a475ff4 commit ac3aa68
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
24 changes: 14 additions & 10 deletions binding/lua_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,16 +865,19 @@ namespace bee::lua_filesystem {

static lua::cxx::status pairs(lua_State* L) {
path_ref p(L, 1);
const char* flags = luaL_optstring(L, 2, "");
luaL_argcheck(L, (flags[0] == '\0' || (flags[0] == 'r' && flags[1] == '\0')), 2, "invalid flags");
if (flags[0] == 'r') {
if (auto s = pairs_directory<fs::recursive_directory_iterator>::constructor(L, p); !s) {
return s;
}
} else {
if (auto s = pairs_directory<fs::directory_iterator>::constructor(L, p); !s) {
return s;
}
if (auto s = pairs_directory<fs::directory_iterator>::constructor(L, p); !s) {
return s;
}
lua_pushnil(L);
lua_pushnil(L);
lua_rotate(L, -4, -1);
return 4;
}

static lua::cxx::status pairs_r(lua_State* L) {
path_ref p(L, 1);
if (auto s = pairs_directory<fs::recursive_directory_iterator>::constructor(L, p); !s) {
return s;
}
lua_pushnil(L);
lua_pushnil(L);
Expand Down Expand Up @@ -958,6 +961,7 @@ namespace bee::lua_filesystem {
{ "create_hard_link", lua::cxx::cfunc<create_hard_link> },
{ "temp_directory_path", lua::cxx::cfunc<temp_directory_path> },
{ "pairs", lua::cxx::cfunc<pairs> },
{ "pairs_r", lua::cxx::cfunc<pairs_r> },
{ "exe_path", exe_path },
{ "dll_path", dll_path },
{ "filelock", filelock },
Expand Down
10 changes: 5 additions & 5 deletions test/test_filesystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,10 @@ function test_fs:test_copy_file_2()
end

function test_fs:test_pairs()
local function pairs_ok(dir, flags, expected)
local function pairs_ok(dir, pairs, expected)
local fsdir = fs.path(dir)
local result = {}
for path, status in fs.pairs(fsdir, flags) do
for path, status in pairs(fsdir) do
result[path:string()] = status:type()
end
lt.assertEquals(result, expected)
Expand All @@ -639,7 +639,7 @@ function test_fs:test_pairs()
fs.create_directories "temp"
create_file("temp/temp1.txt")
create_file("temp/temp2.txt")
pairs_ok("temp", nil, {
pairs_ok("temp", fs.pairs, {
["temp/temp1.txt"] = "regular",
["temp/temp2.txt"] = "regular",
})
Expand All @@ -650,12 +650,12 @@ function test_fs:test_pairs()
create_file("temp/temp2.txt")
create_file("temp/temp/temp1.txt")
create_file("temp/temp/temp2.txt")
pairs_ok("temp", nil, {
pairs_ok("temp", fs.pairs, {
["temp/temp1.txt"] = "regular",
["temp/temp2.txt"] = "regular",
["temp/temp"] = "directory",
})
pairs_ok("temp", "r", {
pairs_ok("temp", fs.pairs_r, {
["temp/temp1.txt"] = "regular",
["temp/temp2.txt"] = "regular",
["temp/temp"] = "directory",
Expand Down

0 comments on commit ac3aa68

Please sign in to comment.