added file.list function
This commit is contained in:
@@ -140,6 +140,37 @@ static int l_file_write_bytes(lua_State* L) {
|
||||
}
|
||||
}
|
||||
|
||||
static int l_file_list_all_res(lua_State* L, const std::string& path) {
|
||||
auto files = scripting::engine->getResPaths()->listdirRaw(path);
|
||||
lua_createtable(L, files.size(), 0);
|
||||
for (size_t i = 0; i < files.size(); i++) {
|
||||
lua_pushstring(L, files[i].c_str());
|
||||
lua_rawseti(L, -2, i+1);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_file_list(lua_State* L) {
|
||||
std::string dirname = lua_tostring(L, 1);
|
||||
if (dirname.find(':') == std::string::npos) {
|
||||
return l_file_list_all_res(L, dirname);
|
||||
}
|
||||
fs::path path = resolve_path(L, dirname);
|
||||
if (!fs::is_directory(path)) {
|
||||
throw std::runtime_error(util::quote(path.u8string())+" is not a directory");
|
||||
}
|
||||
lua_createtable(L, 0, 0);
|
||||
size_t index = 1;
|
||||
for (auto& entry : fs::directory_iterator(path)) {
|
||||
auto name = entry.path().filename().u8string();
|
||||
auto file = dirname + "/" + name;
|
||||
lua_pushstring(L, file.c_str());
|
||||
lua_rawseti(L, -2, index);
|
||||
index++;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg filelib [] = {
|
||||
{"resolve", lua_wrap_errors<l_file_resolve>},
|
||||
{"find", lua_wrap_errors<l_file_find>},
|
||||
@@ -153,5 +184,6 @@ const luaL_Reg filelib [] = {
|
||||
{"mkdirs", lua_wrap_errors<l_file_mkdirs>},
|
||||
{"read_bytes", lua_wrap_errors<l_file_read_bytes>},
|
||||
{"write_bytes", lua_wrap_errors<l_file_write_bytes>},
|
||||
{"list", lua_wrap_errors<l_file_list>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user