From 3eae385f3f1848ce3b07e21e35a3b0bc887c2dda Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Wed, 3 May 2017 00:43:23 -0400 Subject: [PATCH] Allow wesnoth.read_file to list a directory contents --- src/scripting/lua_fileops.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/scripting/lua_fileops.cpp b/src/scripting/lua_fileops.cpp index 8adf2305fb86..2f991775ca1e 100644 --- a/src/scripting/lua_fileops.cpp +++ b/src/scripting/lua_fileops.cpp @@ -20,6 +20,7 @@ #include "game_errors.hpp" #include "log.hpp" #include "scripting/lua_common.hpp" // for chat_message, luaW_pcall +#include "scripting/push_check.hpp" #include #include @@ -106,7 +107,7 @@ int intf_have_file(lua_State *L) } /** - * Reads a file into a string. + * Reads a file into a string, or a directory into a list of files therein. * - Arg 1: string containing the file name. * - Ret 1: string */ @@ -129,6 +130,16 @@ int intf_read_file(lua_State *L) if(p.empty()) { return luaL_argerror(L, -1, "file not found"); } + if(filesystem::is_directory(p)) { + std::vector files, dirs; + filesystem::get_files_in_dir(p, &files, &dirs); + size_t ndirs = dirs.size(); + std::copy(files.begin(), files.end(), std::back_inserter(dirs)); + lua_push(L, dirs); + lua_pushnumber(L, ndirs); + lua_setfield(L, -2, "ndirs"); + return 1; + } const std::unique_ptr fs(filesystem::istream_file(p)); fs->exceptions(std::ios_base::goodbit); size_t size = 0;