Resolve relative file paths

Implemented path normalization so relative paths can be correctly
resolved
This commit is contained in:
x1nixmzeng
2015-02-11 01:11:52 +00:00
parent dbfd0b0f7b
commit 4351f48c7b
4 changed files with 94 additions and 8 deletions

View File

@@ -104,4 +104,26 @@ std::wstring fix_path_separators(const std::wstring& source, wchar_t new_sep) {
return dest;
}
std::string fix_path_separators(const std::string& source, char new_sep) {
// Swap all separators to new_sep.
char old_sep = new_sep == '\\' ? '/' : '\\';
std::string::size_type pos = 0;
std::string dest = source;
while ((pos = source.find_first_of(old_sep, pos)) != std::string::npos) {
dest[pos] = new_sep;
++pos;
}
// Replace redundant separators.
pos = 0;
while ((pos = dest.find_first_of(new_sep, pos)) != std::string::npos) {
if (pos < dest.size() - 1) {
if (dest[pos + 1] == new_sep) {
dest.erase(pos + 1, 1);
}
}
++pos;
}
return dest;
}
} // namespace poly

View File

@@ -45,6 +45,8 @@ std::wstring join_paths(const std::wstring& left, const std::wstring& right,
// separators.
std::wstring fix_path_separators(const std::wstring& source,
wchar_t new_sep = poly::path_separator);
std::string fix_path_separators(const std::string& source,
char new_sep = poly::path_separator);
} // namespace poly