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