1 | /* |
2 | * SPDX-License-Identifier: Apache-2.0 |
3 | */ |
4 | |
5 | // Copyright (c) ONNX Project Contributors. |
6 | // Licensed under the MIT license. |
7 | |
8 | #pragma once |
9 | |
10 | #include <string> |
11 | #ifdef _WIN32 |
12 | #include <windows.h> |
13 | #include <filesystem> |
14 | #include "onnx/checker.h" |
15 | #endif |
16 | |
17 | namespace ONNX_NAMESPACE { |
18 | |
19 | #ifdef _WIN32 |
20 | constexpr const char k_preferred_path_separator = '\\'; |
21 | #else // POSIX |
22 | constexpr const char k_preferred_path_separator = '/'; |
23 | #endif |
24 | |
25 | #ifdef _WIN32 |
26 | inline std::wstring path_join(const std::wstring& origin, const std::wstring& append) { |
27 | return (std::filesystem::path(origin) / std::filesystem::path(append)).wstring(); |
28 | } |
29 | inline std::wstring utf8str_to_wstring(const std::string& utf8str) { |
30 | if (utf8str.size() > INT_MAX) { |
31 | fail_check("utf8str_to_wstring: string is too long for converting to wstring." ); |
32 | } |
33 | int size_required = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), (int)utf8str.size(), NULL, 0); |
34 | std::wstring ws_str(size_required, 0); |
35 | MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), (int)utf8str.size(), &ws_str[0], size_required); |
36 | return ws_str; |
37 | } |
38 | |
39 | #else |
40 | std::string path_join(const std::string& origin, const std::string& append); |
41 | // TODO: also use std::filesystem::path for clean_relative_path after ONNX has supported C++17 for POSIX |
42 | // Clean up relative path when there is ".." in the path, e.g.: a/b/../c -> a/c |
43 | // It cannot work with absolute path |
44 | std::string clean_relative_path(const std::string& path); |
45 | #endif |
46 | |
47 | } // namespace ONNX_NAMESPACE |
48 | |