Fix GetDirNameFromFilePath to support forward slash in windows (#11793)

This commit is contained in:
Tianlei Wu 2022-06-10 14:37:30 -07:00 committed by GitHub
parent 5562b47f06
commit 768b9cfb60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View file

@ -14,7 +14,7 @@
#ifdef _WIN32
#if _GAMING_XBOX
// Hacky, but the PathCch* APIs work on Xbox. Presumably PathCch.h needs to be updated to include the
// Hacky, but the PathCch* APIs work on Xbox. Presumably PathCch.h needs to be updated to include the
// GAMES partition. It would be worthwhile to investigate this a bit more (or just use std::filesystem).
#pragma push_macro("WINAPI_FAMILY")
#undef WINAPI_FAMILY
@ -95,12 +95,16 @@ Status RemoveFileSpec(PWSTR pszPath, size_t cchPath) {
} // namespace
common::Status GetDirNameFromFilePath(const std::basic_string<ORTCHAR_T>& s, std::basic_string<ORTCHAR_T>& ret) {
std::wstring input = s;
if (input.empty()) {
if (s.empty()) {
ret = ORT_TSTR(".");
return Status::OK();
}
ret = s;
// Replace slash to backslash since we use PathCchRemoveBackslash
std::replace(ret.begin(), ret.end(), ORTCHAR_T('/'), ORTCHAR_T('\\'));
auto st = onnxruntime::RemoveFileSpec(const_cast<wchar_t*>(ret.data()), ret.length() + 1);
if (!st.IsOK()) {
std::ostringstream oss;

View file

@ -40,6 +40,26 @@ TEST(PathTest, windows_root) {
TEST(PathTest, root) {
PATH_EXPECT("\\", "\\");
}
TEST(PathTest, windows_slash_path_1) {
PATH_EXPECT("C:\\Windows", "C:/Windows/a.txt");
}
TEST(PathTest, windows_slash_path_2) {
PATH_EXPECT("C:\\Windows", "C:\\Windows/a.txt");
}
TEST(PathTest, windows_slash_path_3) {
PATH_EXPECT("C:\\Windows", "C:\\Windows//a.txt");
}
TEST(PathTest, windows_slash_path_4) {
PATH_EXPECT("C:\\Windows", "C:\\Windows\\\\system32/");
}
TEST(PathTest, windows_slash_path_5) {
PATH_EXPECT("C:\\Windows", "C:/Windows//system32/");
}
#else
TEST(PathTest, simple) {
PATH_EXPECT("/Windows", "/Windows/a.txt");