Skip to content

Commit

Permalink
Allow wesnoth.read_file to list a directory contents
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed May 3, 2017
1 parent e2a2790 commit 3eae385
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/scripting/lua_fileops.cpp
Expand Up @@ -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 <exception>
#include <string>
Expand Down Expand Up @@ -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
*/
Expand All @@ -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<std::string> 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<std::istream> fs(filesystem::istream_file(p));
fs->exceptions(std::ios_base::goodbit);
size_t size = 0;
Expand Down

0 comments on commit 3eae385

Please sign in to comment.