microsoft/onnxruntime-extensions

Public

mirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
zhanxi/debug_android_pack

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

base/file_sys.h

134lines · modecode

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