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