microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
leca/genai

Branches

Tags

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

Clone

HTTPS

Download ZIP

base/file_sys.h

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