Skip to content

Commit 13e1c68

Browse files
authored
Merge pull request #5361 from ab9rf/std-filesystem
more path/string normalization
2 parents ba84c1c + 1902333 commit 13e1c68

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

library/LuaApi.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,12 +3207,7 @@ static int filesystem_listdir_recursive(lua_State *L)
32073207
lua_pushinteger(L, i++);
32083208
lua_newtable(L);
32093209
lua_pushstring(L, "path");
3210-
auto p = (it->first).string();
3211-
if constexpr (std::filesystem::path::preferred_separator != '/')
3212-
{
3213-
std::ranges::replace(p, std::filesystem::path::preferred_separator, '/');
3214-
}
3215-
lua_pushstring(L, p.c_str());
3210+
lua_pushstring(L, Filesystem::as_string(it->first).c_str());
32163211
lua_settable(L, -3);
32173212
lua_pushstring(L, "isdir");
32183213
lua_pushboolean(L, it->second);

library/include/modules/Filesystem.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,34 @@ SOFTWARE.
5555

5656
namespace DFHack {
5757
namespace Filesystem {
58-
DFHACK_EXPORT void init ();
59-
DFHACK_EXPORT bool chdir (std::filesystem::path path) noexcept;
60-
DFHACK_EXPORT std::filesystem::path getcwd ();
61-
DFHACK_EXPORT bool restore_cwd ();
62-
DFHACK_EXPORT std::filesystem::path get_initial_cwd ();
63-
DFHACK_EXPORT bool mkdir (std::filesystem::path path) noexcept;
58+
DFHACK_EXPORT void init();
59+
DFHACK_EXPORT bool chdir(std::filesystem::path path) noexcept;
60+
DFHACK_EXPORT std::filesystem::path getcwd();
61+
DFHACK_EXPORT bool restore_cwd();
62+
DFHACK_EXPORT std::filesystem::path get_initial_cwd();
63+
DFHACK_EXPORT bool mkdir(std::filesystem::path path) noexcept;
6464
// returns true on success or if directory already exists
65-
DFHACK_EXPORT bool mkdir_recursive (std::filesystem::path path) noexcept;
66-
DFHACK_EXPORT bool rmdir (std::filesystem::path path) noexcept;
67-
DFHACK_EXPORT bool stat (std::filesystem::path path, std::filesystem::file_status &info) noexcept;
68-
DFHACK_EXPORT bool exists (std::filesystem::path path) noexcept;
69-
DFHACK_EXPORT bool isfile (std::filesystem::path path) noexcept;
70-
DFHACK_EXPORT bool isdir (std::filesystem::path path) noexcept;
71-
DFHACK_EXPORT std::time_t mtime (std::filesystem::path path) noexcept;
72-
DFHACK_EXPORT int listdir (std::filesystem::path dir, std::vector<std::filesystem::path> &files) noexcept;
65+
DFHACK_EXPORT bool mkdir_recursive(std::filesystem::path path) noexcept;
66+
DFHACK_EXPORT bool rmdir(std::filesystem::path path) noexcept;
67+
DFHACK_EXPORT bool stat(std::filesystem::path path, std::filesystem::file_status& info) noexcept;
68+
DFHACK_EXPORT bool exists(std::filesystem::path path) noexcept;
69+
DFHACK_EXPORT bool isfile(std::filesystem::path path) noexcept;
70+
DFHACK_EXPORT bool isdir(std::filesystem::path path) noexcept;
71+
DFHACK_EXPORT std::time_t mtime(std::filesystem::path path) noexcept;
72+
DFHACK_EXPORT int listdir(std::filesystem::path dir, std::vector<std::filesystem::path>& files) noexcept;
7373
// set include_prefix to false to prevent dir from being prepended to
7474
// paths returned in files
75-
DFHACK_EXPORT int listdir_recursive (std::filesystem::path dir, std::map<std::filesystem::path, bool> &files,
75+
DFHACK_EXPORT int listdir_recursive(std::filesystem::path dir, std::map<std::filesystem::path, bool>& files,
7676
int depth = 10, bool include_prefix = true) noexcept;
7777
DFHACK_EXPORT std::filesystem::path canonicalize(std::filesystem::path p) noexcept;
78+
inline std::string as_string(std::filesystem::path path) noexcept
79+
{
80+
auto pStr = path.string();
81+
if constexpr (std::filesystem::path::preferred_separator != '/')
82+
{
83+
std::ranges::replace(pStr, std::filesystem::path::preferred_separator, '/');
84+
}
85+
return pStr;
86+
}
7887
}
7988
}

plugins/orders.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static void list_library(color_ostream &out) {
153153
if (name.extension() != ".json")
154154
continue; // skip non-.json files
155155
auto sname = name.stem();
156-
out << "library/" << sname << std::endl;
156+
out << Filesystem::as_string("library" / sname) << std::endl;
157157
}
158158
}
159159

@@ -172,7 +172,7 @@ static command_result orders_list_command(color_ostream & out)
172172
if (name.extension() != ".json")
173173
continue; // skip non-.json files
174174
auto sname = name.stem();
175-
out << sname << std::endl;
175+
out << sname.string() << std::endl;
176176
}
177177

178178
list_library(out);

0 commit comments

Comments
 (0)