microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
base/file_sys.h
141lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #ifdef _WIN32 |
| 7 | #ifndef WIN32_LEAN_AND_MEAN |
| 8 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers |
| 9 | #endif |
| 10 | |
| 11 | #ifndef NOMINMAX |
| 12 | #define NOMINMAX |
| 13 | #endif |
| 14 | #include <Windows.h> |
| 15 | #endif // _WIN32 |
| 16 | |
| 17 | #include <sys/stat.h> |
| 18 | |
| 19 | #include <string> |
| 20 | #include <fstream> |
| 21 | |
| 22 | |
| 23 | namespace ort_extensions { |
| 24 | |
| 25 | class path { |
| 26 | public: |
| 27 | path() = default; |
| 28 | explicit path(const std::string& path) : path_(path) { |
| 29 | #ifdef _WIN32 |
| 30 | w_path_ = to_wstring(); |
| 31 | #endif // _WIN32 |
| 32 | }; |
| 33 | |
| 34 | #ifdef _WIN32 |
| 35 | explicit path(const std::wstring& wpath) { |
| 36 | int size_needed = WideCharToMultiByte(CP_UTF8, 0, wpath.c_str(), -1, nullptr, 0, nullptr, nullptr); |
| 37 | std::string utf8_str(size_needed, 0); |
| 38 | WideCharToMultiByte(CP_UTF8, 0, wpath.c_str(), -1, &utf8_str[0], size_needed, nullptr, nullptr); |
| 39 | path_ = utf8_str; |
| 40 | } |
| 41 | #endif // _WIN32 |
| 42 | |
| 43 | static constexpr char separator = |
| 44 | #ifdef _WIN32 |
| 45 | '\\'; |
| 46 | #else |
| 47 | '/'; |
| 48 | #endif |
| 49 | |
| 50 | using ios_base = std::ios_base; |
| 51 | std::ifstream open(ios_base::openmode mode = ios_base::in) const { |
| 52 | // if Windows, need to convert the string to UTF-16 |
| 53 | #ifdef _WIN32 |
| 54 | return std::ifstream(w_path_, mode); |
| 55 | #else |
| 56 | return std::ifstream(path_, mode); |
| 57 | #endif // _WIN32 |
| 58 | } |
| 59 | |
| 60 | const std::string& string() const { |
| 61 | return path_; |
| 62 | } |
| 63 | |
| 64 | path join(const std::string& str) const { |
| 65 | return path(path_ + separator + str); |
| 66 | } |
| 67 | |
| 68 | path operator/(const std::string& str) const { |
| 69 | return join(str); |
| 70 | } |
| 71 | |
| 72 | path operator/(const path& path) { |
| 73 | return join(path.path_); |
| 74 | } |
| 75 | |
| 76 | bool is_regular_file() const { |
| 77 | auto info = get_stat(); |
| 78 | return (info.st_mode & S_IFREG) != 0; |
| 79 | } |
| 80 | |
| 81 | bool is_directory() const { |
| 82 | auto info = get_stat(); |
| 83 | return (info.st_mode & S_IFDIR) != 0; |
| 84 | } |
| 85 | |
| 86 | std::string extension() const { |
| 87 | return path_.substr(path_.find_last_of('.')); |
| 88 | } |
| 89 | |
| 90 | std::string parent_path() const { |
| 91 | std::string sep = {separator}; |
| 92 | #ifdef _WIN32 |
| 93 | sep += "/"; |
| 94 | #endif // _WIN32 |
| 95 | auto pos = path_.find_last_of(sep); |
| 96 | if (pos == std::string::npos) { |
| 97 | return ""; |
| 98 | } |
| 99 | return path_.substr(0, pos); |
| 100 | } |
| 101 | |
| 102 | #ifdef _WIN32 |
| 103 | struct _stat64 get_stat() const { |
| 104 | struct _stat64 info; |
| 105 | if (_wstat64(w_path_.c_str(), &info) != 0) { |
| 106 | return {}; |
| 107 | } |
| 108 | return info; |
| 109 | } |
| 110 | #else |
| 111 | struct stat get_stat() const { |
| 112 | struct stat info; |
| 113 | if (stat(path_.c_str(), &info) != 0) { |
| 114 | return {}; |
| 115 | } |
| 116 | return info; |
| 117 | } |
| 118 | #endif // _WIN32 |
| 119 | |
| 120 | bool exists() const { |
| 121 | auto info = get_stat(); |
| 122 | return (info.st_mode & S_IFMT) != 0; |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | std::string path_; |
| 127 | #ifdef _WIN32 |
| 128 | std::wstring w_path_; |
| 129 | |
| 130 | std::wstring to_wstring() const { |
| 131 | int size_needed = MultiByteToWideChar(CP_UTF8, 0, path_.c_str(), -1, nullptr, 0); |
| 132 | std::wstring utf16_str(size_needed, 0); |
| 133 | MultiByteToWideChar(CP_UTF8, 0, path_.c_str(), -1, &utf16_str[0], size_needed); |
| 134 | return utf16_str; |
| 135 | } |
| 136 | #endif // _WIN32 |
| 137 | }; |
| 138 | |
| 139 | } // namespace ort_extensions |
| 140 | |
| 141 | namespace ortx = ort_extensions; |
| 142 | |