microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c3145b8f52cb80bda7a3a073f73e818987196f77

Branches

Tags

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

Clone

HTTPS

Download ZIP

base/file_sys.h

97lines · 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
15namespace ort_extensions {
16
17class path {
18 public:
19 path() = default;
20 path(const std::string& path) : path_(path) {
21#ifdef _WIN32
22 w_path_ = to_wstring();
23#endif // _WIN32
24 };
25
26#ifdef _WIN32
27 path(const std::wstring& wpath) {
28 int size_needed = WideCharToMultiByte(CP_UTF8, 0, wpath.c_str(), -1, nullptr, 0, nullptr, nullptr);
29 std::string utf8_str(size_needed, 0);
30 WideCharToMultiByte(CP_UTF8, 0, wpath.c_str(), -1, &utf8_str[0], size_needed, nullptr, nullptr);
31 path_ = utf8_str;
32 }
33#endif // _WIN32
34
35 static constexpr char separator =
36#ifdef _WIN32
37 '\\';
38#else
39 '/';
40#endif
41
42 using ios_base = std::ios_base;
43 std::ifstream open(ios_base::openmode mode = ios_base::in) const {
44 // if Windows, need to convert the string to UTF-16
45#ifdef _WIN32
46 return std::ifstream(w_path_, mode);
47#else
48 return std::ifstream(path_, mode);
49#endif // _WIN32
50 }
51
52 const std::string& string() const {
53 return path_;
54 }
55
56 path join(const std::string& path) const {
57 return path_ + separator + path;
58 }
59
60 path operator/(const std::string& path) const {
61 return join(path);
62 }
63
64 path operator/(const path& path) {
65 return join(path.path_);
66 }
67
68 bool is_directory() const {
69#ifdef _WIN32
70 struct _stat64 info;
71 if (_wstat64(w_path_.c_str(), &info) != 0) {
72 return false;
73 }
74#else
75 struct stat info;
76 if (stat(path_.c_str(), &info) != 0) {
77 return false;
78 }
79#endif // _WIN32
80 return (info.st_mode & S_IFDIR) != 0;
81 }
82
83 private:
84 std::string path_;
85#ifdef _WIN32
86 std::wstring w_path_;
87
88 std::wstring to_wstring() const {
89 int size_needed = MultiByteToWideChar(CP_UTF8, 0, path_.c_str(), -1, nullptr, 0);
90 std::wstring utf16_str(size_needed, 0);
91 MultiByteToWideChar(CP_UTF8, 0, path_.c_str(), -1, &utf16_str[0], size_needed);
92 return utf16_str;
93 }
94#endif // _WIN32
95};
96
97} // namespace ort_extensions
98