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