Skip to content

Commit

Permalink
File.cpp: revert get_parent_dir change
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 committed Jun 24, 2023
1 parent 0a0ef50 commit b0f444b
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions Utilities/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,24 +387,53 @@ shared_ptr<fs::device_base> fs::set_virtual_device(const std::string& name, shar

std::string fs::get_parent_dir(std::string_view path, u32 parent_level)
{
std::string normalized_path = std::filesystem::path(path).lexically_normal().string();
std::string_view result = path;

#ifdef _WIN32
std::replace(normalized_path.begin(), normalized_path.end(), '\\', '/');
#endif

if (normalized_path.ends_with('/'))
normalized_path.pop_back();
// Number of path components to remove
usz to_remove = levels;

while (parent_level--)
while (to_remove--)
{
if (const auto pos = normalized_path.find_last_of('/'); pos != umax)
normalized_path = normalized_path.substr(0, pos);
// Trim contiguous delimiters at the end
if (usz sz = result.find_last_not_of(delim) + 1)
{
result = result.substr(0, sz);
}
else
{
return "/";
}

const auto elem = result.substr(result.find_last_of(delim) + 1);

if (elem.empty() || elem.size() == result.size())
{
break;
}

if (elem == ".")
{
to_remove += 1;
}

if (elem == "..")
{
to_remove += 2;
}

result.remove_suffix(elem.size());
}

if (usz sz = result.find_last_not_of(delim) + 1)
{
result = result.substr(0, sz);
}
else
{
return "/";
}

return normalized_path.empty() ? "/" : normalized_path;
return std::string{result};
}

bool fs::stat(const std::string& path, stat_t& info)
Expand Down

0 comments on commit b0f444b

Please sign in to comment.