From 768b9cfb60cc981a823144d548597d0df47afd46 Mon Sep 17 00:00:00 2001 From: Tianlei Wu Date: Fri, 10 Jun 2022 14:37:30 -0700 Subject: [PATCH] Fix GetDirNameFromFilePath to support forward slash in windows (#11793) --- onnxruntime/core/platform/path_lib.cc | 10 +++++++--- onnxruntime/test/platform/path_lib_test.cc | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/platform/path_lib.cc b/onnxruntime/core/platform/path_lib.cc index ecb66a55f4f16..aea20d1703a1f 100644 --- a/onnxruntime/core/platform/path_lib.cc +++ b/onnxruntime/core/platform/path_lib.cc @@ -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& s, std::basic_string& 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(ret.data()), ret.length() + 1); if (!st.IsOK()) { std::ostringstream oss; diff --git a/onnxruntime/test/platform/path_lib_test.cc b/onnxruntime/test/platform/path_lib_test.cc index 798774d447449..f4a41347b9b39 100644 --- a/onnxruntime/test/platform/path_lib_test.cc +++ b/onnxruntime/test/platform/path_lib_test.cc @@ -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");