🛰️航天仿真算法库 SpaceAST 0.3.1
载入中...
搜索中...
未找到
FileStream.hpp
浏览该文件的文档.
1
21
22#pragma once
23
24#include "AstGlobal.h"
25#include "AstUtil/StringView.hpp"
26#include "AstUtil/Encode.hpp" // for aUtf8ToWide
27#include <ios> // for std::ios_base, std::ios::in, ...
28#include <fstream> // for std::filebuf, std::ifstream, std::ofstream, ...
29#include <string> // for std::string
30
31AST_NAMESPACE_BEGIN
32
42namespace cxx
43{
44
45#if defined(_WIN32) || defined(AST_PARSED_BY_DOXYGEN)
46namespace detail
47{
48
55inline std::filebuf* openfilebuf(std::filebuf& buf, const char* filepath, std::ios_base::openmode mode)
56{
57#if defined(_WIN32)
58 std::wstring wpath;
59 errc_t rc = aUtf8ToWide(filepath, wpath);
60 if(rc == eNoError)
61 return buf.open(wpath.c_str(), mode);
62 else
63 return nullptr;
64#else
65 return buf.open(filepath, mode);
66#endif
67}
68
69} // namespace detail
70
71
79class ifstream : public std::ifstream
80{
81public:
82 ifstream() = default;
83
84 explicit ifstream(const char* filepath, std::ios_base::openmode mode = std::ios::in)
85 {
86 open(filepath, mode);
87 }
88
89 explicit ifstream(const std::string& filepath, std::ios_base::openmode mode = std::ios::in)
90 {
91 open(filepath.c_str(), mode);
92 }
93
97 void open(const char* filepath, std::ios_base::openmode mode = std::ios::in)
98 {
99 // 与 std::ifstream::open 一致:始终补上 in
100 if (detail::openfilebuf(*rdbuf(), filepath, mode | std::ios::in))
101 clear();
102 else
103 setstate(std::ios::failbit);
104 }
105};
106
107
110class ofstream : public std::ofstream
111{
112public:
113 ofstream() = default;
114
115 explicit ofstream(const char* filepath, std::ios_base::openmode mode = std::ios::out)
116 {
117 open(filepath, mode);
118 }
119
120 explicit ofstream(const std::string& filepath, std::ios_base::openmode mode = std::ios::out)
121 {
122 open(filepath.c_str(), mode);
123 }
124
128 void open(const char* filepath, std::ios_base::openmode mode = std::ios::out)
129 {
130 // 与 std::ofstream::open 一致:始终补上 out
131 if (detail::openfilebuf(*rdbuf(), filepath, mode | std::ios::out))
132 clear();
133 else
134 setstate(std::ios::failbit);
135 }
136};
137
138
141class fstream : public std::fstream
142{
143public:
144 fstream() = default;
145
146 explicit fstream(const char* filepath, std::ios_base::openmode mode = std::ios::in | std::ios::out)
147 {
148 open(filepath, mode);
149 }
150
151 explicit fstream(const std::string& filepath, std::ios_base::openmode mode = std::ios::in | std::ios::out)
152 {
153 open(filepath.c_str(), mode);
154 }
155
159 void open(const char* filepath, std::ios_base::openmode mode = std::ios::in | std::ios::out)
160 {
161 if (detail::openfilebuf(*rdbuf(), filepath, mode))
162 clear();
163 else
164 setstate(std::ios::failbit);
165 }
166};
167
168#else
169
170using std::ifstream;
171using std::ofstream;
172using std::fstream;
173
174#endif
175
176} // namespace cxx
177
178
181AST_NAMESPACE_END
std::filebuf * openfilebuf(std::filebuf &buf, const char *filepath, std::ios_base::openmode mode)
以 UTF-8 编码的路径打开文件缓冲
定义 FileStream.hpp:55
以 UTF-8 编码的路径打开的文件输入输出流,语义见 ifstream
定义 FileStream.hpp:142
void open(const char *filepath, std::ios_base::openmode mode=std::ios::in|std::ios::out)
打开文件
定义 FileStream.hpp:159
以 UTF-8 编码的路径打开的文件输入流
定义 FileStream.hpp:80
void open(const char *filepath, std::ios_base::openmode mode=std::ios::in)
打开文件
定义 FileStream.hpp:97
以 UTF-8 编码的路径打开的文件输出流,语义见 ifstream
定义 FileStream.hpp:111
void open(const char *filepath, std::ios_base::openmode mode=std::ios::out)
打开文件
定义 FileStream.hpp:128
errc_t aUtf8ToWide(const char *utf8, std::wstring &wide)
将UTF-8编码的字符串转换为宽字符编码
定义 Encode.cpp:258