🛰️航天仿真算法库 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
177 bool empty() const
178 {
179 return path_.empty();
180 }
181
182 // 操作符重载
183 path operator/(const path& other) const
184 {
185 if (path_.empty()) return other;
186 if (other.path_.empty()) return *this;
187
188 // 确保路径分隔符正确
189 bool needs_separator = !is_separator(path_.back()) && !is_separator(other.path_.front());
190 if (needs_separator) {
191 return path_ + preferred_separator() + other.path_;
192 }
193 return path_ + other.path_;
194 }
195
196 path& operator/=(const path& other)
197 {
198 *this = *this / other;
199 return *this;
200 }
201
202 operator string_type() const
203 {
204 return (path_);
205 }
206 private:
207 string_type path_;
208 };
209
210 // 文件类型枚举
211 enum class file_type
212 {
213 none,
214 not_found,
215 regular,
216 directory,
217 symlink,
218 unknown
219 };
220
221 // 文件状态
223 {
224 public:
225 file_status() : type_(file_type::none)
226 {}
227 explicit file_status(file_type type) : type_(type)
228 {}
229
230 file_type type() const
231 {
232 return type_;
233 }
234
235 private:
236 file_type type_;
237 };
238
239 // 目录条目
241 {
242
243 public:
244 directory_entry() = default;
245 explicit directory_entry(const _AST_FS path& p) : path_(p)
246 {}
247
248 const _AST_FS path& path() const
249 {
250 return path_;
251 }
252 AST_UTIL_API
253 file_status status() const;
254 private:
255 _AST_FS path path_;
256 };
257
258 // 目录迭代器
259 class AST_UTIL_API directory_iterator
260 {
261 public:
262 directory_iterator() : impl_(nullptr)
263 {}
264 explicit directory_iterator(const path& p);
265 ~directory_iterator() = default;
266
268 directory_iterator& operator=(const directory_iterator&);
269 directory_iterator(directory_iterator&& other) noexcept;
270 directory_iterator& operator=(directory_iterator&& other) noexcept;
271
272 const directory_entry& operator*() const
273 {
274 return entry_;
275 }
276 const directory_entry* operator->() const
277 {
278 return &entry_;
279 }
280
281 directory_iterator& operator++();
282
283 bool operator==(const directory_iterator& other) const
284 {
285 return impl_ == other.impl_;
286 }
287 bool operator!=(const directory_iterator& other) const
288 {
289 return !(*this == other);
290 }
291
292 private:
293 struct impl;
294 std::shared_ptr<impl> impl_;
295 directory_entry entry_;
296
297 void read_next_entry();
298 };
299
300 // 基础文件操作
301 AST_UTIL_API bool exists(const path& p);
302 AST_UTIL_API uintmax_t file_size(const path& p);
303 AST_UTIL_API file_status status(const path& p) noexcept;
304
305 A_ALWAYS_INLINE bool is_regular_file(file_status s) noexcept{ return s.type() == file_type::regular; }
306 A_ALWAYS_INLINE bool is_directory(file_status s) noexcept{ return s.type() == file_type::directory; }
307 A_ALWAYS_INLINE bool is_directory(const path& p) noexcept{ return is_directory(status(p)); }
308 A_ALWAYS_INLINE bool is_regular_file(const path& p) noexcept{ return is_regular_file(status(p)); }
309
310 // 目录操作
311 AST_UTIL_API bool create_directory(const path& p) noexcept;
312 AST_UTIL_API bool create_directories(const path& p) noexcept;
313 AST_UTIL_API bool remove(const path& p) noexcept;
314 AST_UTIL_API uintmax_t remove_all(const path& p) noexcept;
315
316 // 文件操作
317 AST_UTIL_API bool copy_file(const path& from, const path& to) noexcept;
318 AST_UTIL_API bool rename(const path& old_p, const path& new_p) noexcept;
319
320 // 目录迭代器相关
321 inline directory_iterator begin(directory_iterator iter) noexcept
322 {
323 return iter;
324 }
325 inline directory_iterator end(const directory_iterator&) noexcept
326 {
327 return directory_iterator();
328 }
329
330 // 空间信息
332 {
333 uintmax_t capacity;
334 uintmax_t free;
335 uintmax_t available;
336 };
337 inline bool operator==(const path& left, const path& right)
338 {
339 return (left.native() == right.native());
340 }
341 inline bool operator!=(const path& left, const path& right)
342 {
343 return (left.native() != right.native());
344 }
345 AST_UTIL_API space_info space(const path& p);
346
347 // 最后修改时间(简化版,返回time_t)
348 AST_UTIL_API std::time_t last_write_time(const path& p);
349
350 // 当前路径相关函数
351 AST_UTIL_API path current_path() noexcept(false);
352 AST_UTIL_API path current_path(std::error_code& ec) noexcept;
353 AST_UTIL_API void current_path(const path& new_path) noexcept(false);
354 AST_UTIL_API void current_path(const path& new_path, std::error_code& ec) noexcept;
355
356} // namespace simple_fs
357
358
359
360AST_NAMESPACE_END
Unit s
定义 Unit.cpp:435
Unit none
无单位
定义 Unit.cpp:421
定义 FileSystemSimple.hpp:241
定义 FileSystemSimple.hpp:260
定义 FileSystemSimple.hpp:223
定义 FileSystemSimple.hpp:55
定义 FileSystemSimple.hpp:67
path stem() const
获取文件名的无扩展名部分
定义 FileSystemSimple.hpp:130
constexpr EDimension operator/(EDimension dim1, EDimension dim2) noexcept
量纲除法运算符
定义 Dimension.hpp:232
定义 FileSystemSimple.cpp:133
定义 FileSystemSimple.hpp:332