From 066adfdf95f44ad23590c28e45e1831b819a2051 Mon Sep 17 00:00:00 2001 From: lethosor Date: Fri, 30 Jan 2015 17:29:17 -0500 Subject: [PATCH] Implement a function to list directories recursively --- library/LuaApi.cpp | 33 ++++++++++++++++++++++++++++ library/include/modules/Filesystem.h | 3 +++ library/modules/Filesystem.cpp | 30 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 56264ebbf..7832ce513 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2007,8 +2007,41 @@ static int filesystem_listdir(lua_State *L) return 1; } +static int filesystem_listdir_recursive(lua_State *L) +{ + luaL_checktype(L,1,LUA_TSTRING); + std::string dir=lua_tostring(L,1); + int depth = 10; + if (lua_type(L, 2) == LUA_TNUMBER) + depth = lua_tounsigned(L, 2); + std::map files; + int err = DFHack::Filesystem::listdir_recursive(dir, files, depth); + if (err) + { + lua_pushnil(L); + lua_pushinteger(L, err); + return 2; + } + lua_newtable(L); + int i = 1; + for (auto it = files.begin(); it != files.end(); ++it) + { + lua_pushinteger(L, i++); + lua_newtable(L); + lua_pushstring(L, "path"); + lua_pushstring(L, (it->first).c_str()); + lua_settable(L, -3); + lua_pushstring(L, "isdir"); + lua_pushboolean(L, it->second); + lua_settable(L, -3); + lua_settable(L, -3); + } + return 1; +} + static const luaL_Reg dfhack_filesystem_funcs[] = { {"listdir", filesystem_listdir}, + {"listdir_recursive", filesystem_listdir_recursive}, {NULL, NULL} }; diff --git a/library/include/modules/Filesystem.h b/library/include/modules/Filesystem.h index a88af5cf3..bc7a02337 100644 --- a/library/include/modules/Filesystem.h +++ b/library/include/modules/Filesystem.h @@ -47,6 +47,7 @@ SOFTWARE. #pragma once #include "Export.h" +#include #include #ifndef _WIN32 @@ -158,5 +159,7 @@ namespace DFHack { DFHACK_EXPORT int64_t ctime (std::string path); DFHACK_EXPORT int64_t mtime (std::string path); DFHACK_EXPORT int listdir (std::string dir, std::vector &files); + DFHACK_EXPORT int listdir_recursive (std::string dir, std::map &files, + int depth = 10, std::string prefix = ""); } } diff --git a/library/modules/Filesystem.cpp b/library/modules/Filesystem.cpp index 3dff1d2b4..67b5cc3b4 100644 --- a/library/modules/Filesystem.cpp +++ b/library/modules/Filesystem.cpp @@ -172,3 +172,33 @@ int Filesystem::listdir (std::string dir, std::vector &files) return 0; } +int Filesystem::listdir_recursive (std::string dir, std::map &files, + int depth /* = 10 */, std::string prefix /* = "" */) +{ + int err; + if (depth < 0) + return -1; + if (prefix == "") + prefix = dir; + std::vector tmp; + err = listdir(dir, tmp); + if (err) + return err; + for (auto file = tmp.begin(); file != tmp.end(); ++file) + { + if (*file == "." || *file == "..") + continue; + std::string rel_path = prefix + "/" + *file; + if (isdir(rel_path)) + { + files.insert(std::pair(rel_path, true)); + err = listdir_recursive(dir + "/" + *file, files, depth - 1, rel_path); + if (err) + return err; + } + else + { + files.insert(std::pair(rel_path, false)); + } + } +}