microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
snnn-patch-2

Branches

Tags

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

Clone

HTTPS

Download ZIP

base/file_sys.h

141lines · modeblame

c3c5f1cbWenbing Li2 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4#pragma once
5
6#ifdef _WIN32
4e10ee04Chester Liu1 years ago7#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
c3c5f1cbWenbing Li2 years ago14#include <Windows.h>
15#endif // _WIN32
16
17#include <sys/stat.h>
18
19#include <string>
20#include <fstream>
21
176c1d01Wenbing Li1 years ago22
c3c5f1cbWenbing Li2 years ago23namespace ort_extensions {
24
25class path {
26public:
27path() = default;
176c1d01Wenbing Li1 years ago28explicit path(const std::string& path) : path_(path) {
ca433cbeWenbing Li2 years ago29#ifdef _WIN32
30w_path_ = to_wstring();
31#endif // _WIN32
32};
33
34#ifdef _WIN32
176c1d01Wenbing Li1 years ago35explicit path(const std::wstring& wpath) {
ca433cbeWenbing Li2 years ago36int size_needed = WideCharToMultiByte(CP_UTF8, 0, wpath.c_str(), -1, nullptr, 0, nullptr, nullptr);
37std::string utf8_str(size_needed, 0);
38WideCharToMultiByte(CP_UTF8, 0, wpath.c_str(), -1, &utf8_str[0], size_needed, nullptr, nullptr);
39path_ = utf8_str;
40}
41#endif // _WIN32
c3c5f1cbWenbing Li2 years ago42
43static constexpr char separator =
44#ifdef _WIN32
45'\\';
46#else
47'/';
48#endif
49
50using ios_base = std::ios_base;
51std::ifstream open(ios_base::openmode mode = ios_base::in) const {
52// if Windows, need to convert the string to UTF-16
53#ifdef _WIN32
ca433cbeWenbing Li2 years ago54return std::ifstream(w_path_, mode);
c3c5f1cbWenbing Li2 years ago55#else
56return std::ifstream(path_, mode);
57#endif // _WIN32
58}
59
60const std::string& string() const {
61return path_;
62}
63
176c1d01Wenbing Li1 years ago64path join(const std::string& str) const {
65return path(path_ + separator + str);
c3c5f1cbWenbing Li2 years ago66}
67
176c1d01Wenbing Li1 years ago68path operator/(const std::string& str) const {
69return join(str);
c3c5f1cbWenbing Li2 years ago70}
71
72path operator/(const path& path) {
73return join(path.path_);
74}
75
176c1d01Wenbing Li1 years ago76bool is_regular_file() const {
77auto info = get_stat();
78return (info.st_mode & S_IFREG) != 0;
79}
80
c3c5f1cbWenbing Li2 years ago81bool is_directory() const {
176c1d01Wenbing Li1 years ago82auto info = get_stat();
83return (info.st_mode & S_IFDIR) != 0;
84}
85
86std::string extension() const {
87return path_.substr(path_.find_last_of('.'));
88}
89
90std::string parent_path() const {
91std::string sep = {separator};
92#ifdef _WIN32
93sep += "/";
94#endif // _WIN32
95auto pos = path_.find_last_of(sep);
96if (pos == std::string::npos) {
97return "";
98}
99return path_.substr(0, pos);
100}
101
c3c5f1cbWenbing Li2 years ago102#ifdef _WIN32
176c1d01Wenbing Li1 years ago103struct _stat64 get_stat() const {
c3c5f1cbWenbing Li2 years ago104struct _stat64 info;
ca433cbeWenbing Li2 years ago105if (_wstat64(w_path_.c_str(), &info) != 0) {
176c1d01Wenbing Li1 years ago106return {};
c3c5f1cbWenbing Li2 years ago107}
176c1d01Wenbing Li1 years ago108return info;
109}
c3c5f1cbWenbing Li2 years ago110#else
176c1d01Wenbing Li1 years ago111struct stat get_stat() const {
c3c5f1cbWenbing Li2 years ago112struct stat info;
113if (stat(path_.c_str(), &info) != 0) {
176c1d01Wenbing Li1 years ago114return {};
c3c5f1cbWenbing Li2 years ago115}
176c1d01Wenbing Li1 years ago116return info;
117}
c3c5f1cbWenbing Li2 years ago118#endif // _WIN32
176c1d01Wenbing Li1 years ago119
120bool exists() const {
121auto info = get_stat();
122return (info.st_mode & S_IFMT) != 0;
c3c5f1cbWenbing Li2 years ago123}
124
125private:
126std::string path_;
127#ifdef _WIN32
ca433cbeWenbing Li2 years ago128std::wstring w_path_;
129
c3c5f1cbWenbing Li2 years ago130std::wstring to_wstring() const {
131int size_needed = MultiByteToWideChar(CP_UTF8, 0, path_.c_str(), -1, nullptr, 0);
132std::wstring utf16_str(size_needed, 0);
133MultiByteToWideChar(CP_UTF8, 0, path_.c_str(), -1, &utf16_str[0], size_needed);
134return utf16_str;
135}
136#endif // _WIN32
137};
138
139} // namespace ort_extensions
176c1d01Wenbing Li1 years ago140
141namespace ortx = ort_extensions;