🛰️航天仿真算法库 SpaceAST 0.4.0
载入中...
搜索中...
未找到
FileSystemSimple.hpp
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/StringView.hpp"
25#include "AstUtil/Path.hpp"
26
27#include <string>
28#include <vector>
29#include <system_error>
30#include <chrono>
31#include <cstdint>
32#include <ctime>
33#include <algorithm>
34#include <cstring>
35#include <memory>
36
37AST_NAMESPACE_BEGIN
38
39
40
41#define _AST_FS _AST fs_simple::
42
43
48namespace fs_simple
49{
50 // 定义平台相关的字符串类型
51 using string_type = std::string;
52 using char_type = char;
53
54 // 错误码
55 class filesystem_error : public std::system_error
56 {
57 public:
58 using std::system_error::system_error;
59 explicit filesystem_error(
60 const std::string& what,
61 std::error_code ec=std::make_error_code(std::errc::operation_not_supported)
62 ): std::system_error(ec, what)
63 {}
64 };
65
66 // 路径类
67 class path
68 {
69 public:
70 path() = default;
71
72 path(const char_type* source) : path_(source)
73 {}
74 path(const string_type& source) : path_(source)
75 {}
76 path(StringView source) : path_(source.data(), source.size())
77 {}
78
79 const std::string& string() const{return path_;}
80
81 // 返回平台相关的字符串类型
82 const string_type& native() const{return path_;}
83
84 // 返回平台相关的字符指针
85 const char_type* c_str() const{return path_.c_str();}
86
87
88
89 static const char_type* separators()
90 {
91 #ifdef _WIN32
92 return "/\\";
93 #else
94 return "/";
95 #endif
96 }
97
98 static char_type preferred_separator()
99 {
100 #ifdef _WIN32
101 return '\\';
102 #else
103 return '/';
104 #endif
105 }
106
107 // 路径操作
108 path filename() const
109 {
110 auto pos = path_.find_last_of(separators());
111 if (pos == string_type::npos) return path_;
112 return path_.substr(pos + 1);
113 }
114
115 path extension() const
116 {
117 auto name = filename().native();
118 auto pos = name.find_last_of(char_type('.'));
119 if (pos == string_type::npos) return string_type();
120 return name.substr(pos);
121 }
122
125 path stem() const
126 {
127 auto name = filename().native();
128 auto pos = name.find_last_of(char_type('.'));
129 if (pos == string_type::npos) return name;
130 return name.substr(0, pos);
131 }
132
133 path parent_path() const
134 {
135 auto pos = path_.find_last_of(separators());
136 if (pos == string_type::npos) return string_type();
137 return path_.substr(0, pos);
138 }
139
140 bool is_absolute() const
141 {
142 return aPathIsAbsolute(path_);
143 }
144
145 bool is_relative() const
146 {
147 return aPathIsRelative(path_);
148 }
149
152 AST_UTIL_API
153 path lexically_relative(const path& base) const;
154
155 bool empty() const
156 {
157 return path_.empty();
158 }
159
160 // 操作符重载
161 path operator/(const path& other) const
162 {
163 if (path_.empty()) return other;
164 if (other.path_.empty()) return *this;
165
166 // 确保路径分隔符正确
167 bool needs_separator = !aIsPathSeparator(path_.back()) && !aIsPathSeparator(other.path_.front());
168 if (needs_separator) {
169 return path_ + preferred_separator() + other.path_;
170 }
171 return path_ + other.path_;
172 }
173
174 path& operator/=(const path& other)
175 {
176 *this = *this / other;
177 return *this;
178 }
179
180 operator string_type() const
181 {
182 return (path_);
183 }
184 private:
185 string_type path_{};
186 };
187
188 // 文件类型枚举
189 enum class file_type
190 {
191 none,
192 not_found,
193 regular,
194 directory,
195 symlink,
196 unknown
197 };
198
199 // 文件状态
201 {
202 public:
203 file_status() : type_(file_type::none)
204 {}
205 explicit file_status(file_type type) : type_(type)
206 {}
207
208 file_type type() const
209 {
210 return type_;
211 }
212
213 private:
214 file_type type_;
215 };
216
217 // 目录条目
219 {
220
221 public:
222 directory_entry() = default;
223 explicit directory_entry(const _AST_FS path& p) : path_(p)
224 {}
225
226 const _AST_FS path& path() const
227 {
228 return path_;
229 }
230 AST_UTIL_API
231 file_status status() const;
232 private:
233 _AST_FS path path_{};
234 };
235
236 // 目录迭代器
237 class AST_UTIL_API directory_iterator
238 {
239 public:
240 directory_iterator() : impl_(nullptr), entry_()
241 {}
242 explicit directory_iterator(const path& p);
243 ~directory_iterator() = default;
244
246 directory_iterator& operator=(const directory_iterator&);
247 directory_iterator(directory_iterator&& other) noexcept;
248 directory_iterator& operator=(directory_iterator&& other) noexcept;
249
250 const directory_entry& operator*() const
251 {
252 return entry_;
253 }
254 const directory_entry* operator->() const
255 {
256 return &entry_;
257 }
258
259 directory_iterator& operator++();
260
261 bool operator==(const directory_iterator& other) const
262 {
263 return impl_ == other.impl_;
264 }
265 bool operator!=(const directory_iterator& other) const
266 {
267 return !(*this == other);
268 }
269
270 private:
271 struct impl;
272 std::shared_ptr<impl> impl_;
273 directory_entry entry_;
274
275 void read_next_entry();
276 };
277
278 // 基础文件操作
279 AST_UTIL_API bool exists(const path& p);
280 AST_UTIL_API bool exists(const path& p, std::error_code& ec) noexcept;
281 AST_UTIL_API uintmax_t file_size(const path& p);
282 AST_UTIL_API uintmax_t file_size(const path& p, std::error_code& ec) noexcept;
283 AST_UTIL_API bool is_empty(const path& p);
284 AST_UTIL_API bool is_empty(const path& p, std::error_code& ec) noexcept;
285 AST_UTIL_API file_status status(const path& p) noexcept;
286 AST_UTIL_API file_status status(const path& p, std::error_code& ec) noexcept;
287
289 AST_UTIL_API file_status symlink_status(const path& p) noexcept;
290 AST_UTIL_API file_status symlink_status(const path& p, std::error_code& ec) noexcept;
291
292 A_ALWAYS_INLINE bool is_regular_file(file_status s) noexcept{ return s.type() == file_type::regular; }
293 A_ALWAYS_INLINE bool is_directory(file_status s) noexcept{ return s.type() == file_type::directory; }
294 A_ALWAYS_INLINE bool is_directory(const path& p) noexcept{ return is_directory(status(p)); }
295 AST_UTIL_API bool is_directory(const path& p, std::error_code& ec) noexcept;
296 A_ALWAYS_INLINE bool is_regular_file(const path& p) noexcept{ return is_regular_file(status(p)); }
297 A_ALWAYS_INLINE bool is_symlink(file_status s) noexcept{ return s.type() == file_type::symlink; }
298 AST_UTIL_API bool is_symlink(const path& p) noexcept;
299 AST_UTIL_API bool is_symlink(const path& p, std::error_code& ec) noexcept;
300
301 // 目录操作
302 AST_UTIL_API bool create_directory(const path& p) noexcept;
303 AST_UTIL_API bool create_directory(const path& p, std::error_code& ec) noexcept;
304 AST_UTIL_API bool create_directories(const path& p) noexcept;
305 AST_UTIL_API bool create_directories(const path& p, std::error_code& ec) noexcept;
306 AST_UTIL_API bool remove(const path& p) noexcept;
307 AST_UTIL_API bool remove(const path& p, std::error_code& ec) noexcept;
308 AST_UTIL_API uintmax_t remove_all(const path& p) noexcept;
309
310 // 文件操作
311 AST_UTIL_API bool copy_file(const path& from, const path& to) noexcept;
312 AST_UTIL_API bool rename(const path& old_p, const path& new_p) noexcept;
313
314 // 目录迭代器相关
315 inline directory_iterator begin(const directory_iterator& iter) noexcept
316 {
317 return iter;
318 }
319 inline directory_iterator end(const directory_iterator&) noexcept
320 {
321 return directory_iterator();
322 }
323
324 // 空间信息
326 {
327 uintmax_t capacity;
328 uintmax_t free;
329 uintmax_t available;
330 };
331 inline bool operator==(const path& left, const path& right)
332 {
333 return (left.native() == right.native());
334 }
335 inline bool operator!=(const path& left, const path& right)
336 {
337 return (left.native() != right.native());
338 }
339 AST_UTIL_API space_info space(const path& p);
340
341 // 最后修改时间(简化版,返回time_t)
342 AST_UTIL_API std::time_t last_write_time(const path& p);
343
344 // 当前路径相关函数
345 AST_UTIL_API path current_path() noexcept(false);
346 AST_UTIL_API path current_path(std::error_code& ec) noexcept;
347 AST_UTIL_API void current_path(const path& new_path) noexcept(false);
348 AST_UTIL_API void current_path(const path& new_path, std::error_code& ec) noexcept;
349
352 AST_UTIL_API path relative(const path& p, const path& base, std::error_code& ec) noexcept;
353
358 AST_UTIL_API path absolute(const path& p);
359 AST_UTIL_API path absolute(const path& p, std::error_code& ec) noexcept;
360
363 AST_UTIL_API path temp_directory_path();
364 AST_UTIL_API path temp_directory_path(std::error_code& ec) noexcept;
365
366} // namespace simple_fs
367
368
369
370AST_NAMESPACE_END
定义 FileSystemSimple.hpp:219
定义 FileSystemSimple.hpp:238
定义 FileSystemSimple.hpp:201
定义 FileSystemSimple.hpp:56
定义 FileSystemSimple.hpp:68
path stem() const
获取文件名的无扩展名部分
定义 FileSystemSimple.hpp:125
MatrixMN< _Scalar, I, K > operator*(const MatrixMN< _Scalar, I, J > &left, const MatrixMN< _Scalar, J, K > &right)
矩阵乘法
定义 MathOperator.hpp:580
Unit s
秒
定义 Unit.cpp:465
Unit none
无单位
定义 Unit.cpp:451
bool aPathIsAbsolute(StringView path)
路径是否是绝对路径,语义与 std::filesystem::path::is_absolute 一致
定义 Path.cpp:56
constexpr EDimension operator/(EDimension dim1, EDimension dim2) noexcept
量纲除法运算符
定义 Dimension.hpp:232
定义 FileSystemSimple.cpp:135
定义 FileSystemSimple.hpp:326