Skip to content

Commit

Permalink
Fix GetDirNameFromFilePath to support forward slash in windows (#11793)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianleiwu authored Jun 10, 2022
1 parent 5562b47 commit 768b9cf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
10 changes: 7 additions & 3 deletions onnxruntime/core/platform/path_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions onnxruntime/test/platform/path_lib_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 768b9cf

Please sign in to comment.