🛰️航天仿真算法库 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
128 path stem() const
129 {
130 auto name = filename().native();
131 auto pos = name.find_last_of(char_type('.'));
132 if (pos == string_type::npos) return name;
133 return name.substr(0, pos);
134 }
135
136 path parent_path() const
137 {
138 auto pos = path_.find_last_of(separators());
139 if (pos == string_type::npos) return string_type();
140 return path_.substr(0, pos);
141 }
142
143 bool empty() const
144 {
145 return path_.empty();
146 }
147
148 // 操作符重载
149 path operator/(const path& other) const
150 {
151 if (path_.empty()) return other;
152 if (other.path_.empty()) return *this;
153
154 // 确保路径分隔符正确
155 bool needs_separator = !is_separator(path_.back()) && !is_separator(other.path_.front());
156 if (needs_separator) {
157 return path_ + preferred_separator() + other.path_;
158 }
159 return path_ + other.path_;
160 }
161
162 path& operator/=(const path& other)
163 {
164 *this = *this / other;
165 return *this;
166 }
167
168 operator string_type() const
169 {
170 return (path_);
171 }
172 private:
173 string_type path_;
174 };
175
176 // 文件类型枚举
177 enum class file_type
178 {
179 none,
180 not_found,
181 regular,
182 directory,
183 symlink,
184 unknown
185 };
186
187 // 文件状态
189 {
190 public:
191 file_status() : type_(file_type::none)
192 {}
193 explicit file_status(file_type type) : type_(type)
194 {}
195
196 file_type type() const
197 {
198 return type_;
199 }
200
201 private:
202 file_type type_;
203 };
204
205 // 目录条目
207 {
208
209 public:
210 directory_entry() = default;
211 explicit directory_entry(const _AST_FS path& p) : path_(p)
212 {}
213
214 const _AST_FS path& path() const
215 {
216 return path_;
217 }
218 AST_UTIL_API
219 file_status status() const;
220 private:
221 _AST_FS path path_;
222 };
223
224 // 目录迭代器
225 class AST_UTIL_API directory_iterator
226 {
227 public:
228 directory_iterator() : impl_(nullptr)
229 {}
230 explicit directory_iterator(const path& p);
231 ~directory_iterator() = default;
232
234 directory_iterator& operator=(const directory_iterator&);
235 directory_iterator(directory_iterator&& other) noexcept;
236 directory_iterator& operator=(directory_iterator&& other) noexcept;
237
238 const directory_entry& operator*() const
239 {
240 return entry_;
241 }
242 const directory_entry* operator->() const
243 {
244 return &entry_;
245 }
246
247 directory_iterator& operator++();
248
249 bool operator==(const directory_iterator& other) const
250 {
251 return impl_ == other.impl_;
252 }
253 bool operator!=(const directory_iterator& other) const
254 {
255 return !(*this == other);
256 }
257
258 private:
259 struct impl;
260 std::shared_ptr<impl> impl_;
261 directory_entry entry_;
262
263 void read_next_entry();
264 };
265
266 // 基础文件操作
267 AST_UTIL_API bool exists(const path& p);
268 AST_UTIL_API uintmax_t file_size(const path& p);
269 AST_UTIL_API file_status status(const path& p) noexcept;
270
271 A_ALWAYS_INLINE bool is_regular_file(file_status s) noexcept{ return s.type() == file_type::regular; }
272 A_ALWAYS_INLINE bool is_directory(file_status s) noexcept{ return s.type() == file_type::directory; }
273 A_ALWAYS_INLINE bool is_directory(const path& p) noexcept{ return is_directory(status(p)); }
274 A_ALWAYS_INLINE bool is_regular_file(const path& p) noexcept{ return is_regular_file(status(p)); }
275
276 // 目录操作
277 AST_UTIL_API bool create_directory(const path& p) noexcept;
278 AST_UTIL_API bool create_directories(const path& p) noexcept;
279 AST_UTIL_API bool remove(const path& p) noexcept;
280 AST_UTIL_API uintmax_t remove_all(const path& p) noexcept;
281
282 // 文件操作
283 AST_UTIL_API bool copy_file(const path& from, const path& to) noexcept;
284 AST_UTIL_API bool rename(const path& old_p, const path& new_p) noexcept;
285
286 // 目录迭代器相关
287 inline directory_iterator begin(directory_iterator iter) noexcept
288 {
289 return iter;
290 }
291 inline directory_iterator end(const directory_iterator&) noexcept
292 {
293 return directory_iterator();
294 }
295
296 // 空间信息
298 {
299 uintmax_t capacity;
300 uintmax_t free;
301 uintmax_t available;
302 };
303 inline bool operator==(const path& left, const path& right)
304 {
305 return (left.native() == right.native());
306 }
307 inline bool operator!=(const path& left, const path& right)
308 {
309 return (left.native() != right.native());
310 }
311 AST_UTIL_API space_info space(const path& p);
312
313 // 最后修改时间(简化版,返回time_t)
314 AST_UTIL_API std::time_t last_write_time(const path& p);
315
316 // 当前路径相关函数
317 AST_UTIL_API path current_path() noexcept(false);
318 AST_UTIL_API path current_path(std::error_code& ec) noexcept;
319 AST_UTIL_API void current_path(const path& new_path) noexcept(false);
320 AST_UTIL_API void current_path(const path& new_path, std::error_code& ec) noexcept;
321
322} // namespace simple_fs
323
324
325
326AST_NAMESPACE_END
Unit s
定义 Unit.cpp:425
Unit none
无单位
定义 Unit.cpp:411
定义 FileSystemSimple.hpp:207
定义 FileSystemSimple.hpp:226
定义 FileSystemSimple.hpp:189
定义 FileSystemSimple.hpp:55
定义 FileSystemSimple.hpp:67
constexpr EDimension operator/(EDimension dim1, EDimension dim2) noexcept
量纲除法运算符
定义 Dimension.hpp:231
定义 FileSystemSimple.cpp:133
定义 FileSystemSimple.hpp:298