| 3372 | } |
| 3373 | |
| 3374 | static int filesystem_listdir_recursive(lua_State *L) |
| 3375 | { |
| 3376 | luaL_checktype(L,1,LUA_TSTRING); |
| 3377 | string dir=lua_tostring(L,1); |
| 3378 | int depth = 10; |
| 3379 | if (lua_gettop(L) >= 2 && !lua_isnil(L, 2)) |
| 3380 | depth = luaL_checkint(L, 2); |
| 3381 | bool include_prefix = true; |
| 3382 | if (lua_gettop(L) >= 3 && !lua_isnil(L, 3)) |
| 3383 | include_prefix = lua_toboolean(L, 3); |
| 3384 | std::map<std::filesystem::path, bool> files; |
| 3385 | int err = DFHack::Filesystem::listdir_recursive(dir, files, depth, include_prefix); |
| 3386 | if (err != 0 && err != -1) { |
| 3387 | lua_pushnil(L); |
| 3388 | lua_pushstring(L, strerror(err)); |
| 3389 | lua_pushinteger(L, err); |
| 3390 | return 3; |
| 3391 | } |
| 3392 | lua_newtable(L); |
| 3393 | int i = 1; |
| 3394 | for (auto it = files.begin(); it != files.end(); ++it) |
| 3395 | { |
| 3396 | lua_pushinteger(L, i++); |
| 3397 | lua_newtable(L); |
| 3398 | lua_pushstring(L, "path"); |
| 3399 | lua_pushstring(L, Filesystem::as_string(it->first).c_str()); |
| 3400 | lua_settable(L, -3); |
| 3401 | lua_pushstring(L, "isdir"); |
| 3402 | lua_pushboolean(L, it->second); |
| 3403 | lua_settable(L, -3); |
| 3404 | lua_settable(L, -3); |
| 3405 | } |
| 3406 | return 1; |
| 3407 | } |
| 3408 | |
| 3409 | static const luaL_Reg dfhack_filesystem_funcs[] = { |
| 3410 | {"listdir", filesystem_listdir}, |
nothing calls this directly
no test coverage detected