475 lines
16 KiB
C++
475 lines
16 KiB
C++
#pragma once
|
||
// ─────────────────────────────────────────────────────────────
|
||
// std::filesystem 的 MinGW/GCC 8.1 兼容垫片。
|
||
//
|
||
// 本工具链的 <filesystem> 存在库级缺陷:fs_path.h 内 operator/=(Windows
|
||
// 分支)会解析到不可见的 operator!= 重载,一旦实例化 path 拼接即编译失败
|
||
// (单头最小复现即触发,与仓库代码无关)。仓库既有口径是「MinGW 上不使用
|
||
// std::filesystem」(nlohmann/json 亦通过 JSON_HAS_FILESYSTEM=0 对 MinGW
|
||
// GCC 8 整体关闭,gcode_import/test_dwg_object 等均手写替代)。
|
||
//
|
||
// 用法:#include "vde/fs_compat.h" 后用 vde::fs(或别名 fs)替代
|
||
// std::filesystem。仅覆盖仓库内实际用到的子集:path 拼接 / parent_path /
|
||
// string、temp_directory_path、create_directories、exists、file_size、
|
||
// remove。非 MinGW 工具链上直接别名到 std::filesystem,行为一致。
|
||
// ─────────────────────────────────────────────────────────────
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <fstream>
|
||
#include <string>
|
||
#include <system_error>
|
||
|
||
// Wave21(1.0.491):原始 stub 只在 GCC 8.1 时代的 MinGW 上需要(<filesystem> 库级
|
||
// 缺陷)。当前工具链 MinGW-w64 GCC 16.1 的 std::filesystem 已可用(仓库大量代码与
|
||
// UTF-8 路径测试直接使用它并通过),因此默认对所有平台使用 std::filesystem 别名;
|
||
// 需要在旧 MinGW(GCC < 13 等)上构建时定义 VDE_FS_LEGACY_STUB=1 恢复上面的轻量
|
||
// 自定义 vde::fs 实现(保留原行为,仅供旧工具链回退)。
|
||
#if (defined(__MINGW32__) || defined(__MINGW64__)) && defined(VDE_FS_LEGACY_STUB)
|
||
|
||
#include <direct.h>
|
||
#include <io.h>
|
||
#include <sys/stat.h>
|
||
#include <filesystem>
|
||
#include <vector>
|
||
|
||
namespace vde {
|
||
namespace fs {
|
||
|
||
class path {
|
||
public:
|
||
path() = default;
|
||
path(const char* p) : s_(p ? p : "") {}
|
||
path(const std::string& p) : s_(p) {}
|
||
|
||
// Wave21/A55 origin 的 UTF-8 路径修复以 std::filesystem::path 为形参/返回值调用
|
||
// vde::foundation::utf8_path.h helper;MinGW 下 vde::fs::path 是上面的轻量自定义
|
||
// 路径,补一对显式互转避免编译失败(std::filesystem 一侧按其窄字符串解释,ASCII
|
||
// 逐字节一致;非 ASCII 走 fs_compat 自身 UTF-8 语义,与 Wave21 之前的 helper 一致)。
|
||
path(const std::filesystem::path& p) : s_(p.string()) {}
|
||
operator std::filesystem::path() const { return std::filesystem::path(s_); }
|
||
|
||
path& operator/=(const path& rhs) {
|
||
if (!rhs.s_.empty()) {
|
||
if (is_abs_(rhs.s_)) {
|
||
s_ = rhs.s_;
|
||
} else if (s_.empty() || s_.back() == '/' || s_.back() == '\\') {
|
||
s_ += rhs.s_;
|
||
} else {
|
||
s_ += '/';
|
||
s_ += rhs.s_;
|
||
}
|
||
}
|
||
return *this;
|
||
}
|
||
|
||
const std::string& string() const { return s_; }
|
||
std::string generic_string() const {
|
||
std::string out = s_;
|
||
for (char& ch : out) if (ch == '\\') ch = '/';
|
||
return out;
|
||
}
|
||
const char* c_str() const { return s_.c_str(); }
|
||
bool empty() const { return s_.empty(); }
|
||
void clear() { s_.clear(); }
|
||
bool has_parent_path() const { return !parent_path().empty(); }
|
||
bool is_relative() const { return !is_abs_(s_); }
|
||
bool is_absolute() const { return is_abs_(s_); }
|
||
path filename() const {
|
||
const std::size_t pos = s_.find_last_of("/\\");
|
||
return path(pos == std::string::npos ? s_ : s_.substr(pos + 1));
|
||
}
|
||
path extension() const {
|
||
const std::string name = filename().string();
|
||
const std::size_t dot = name.find_last_of('.');
|
||
if (dot == std::string::npos || dot == 0) return path();
|
||
return path(name.substr(dot));
|
||
}
|
||
|
||
path parent_path() const {
|
||
const std::size_t pos = s_.find_last_of("/\\");
|
||
if (pos == std::string::npos) return path();
|
||
return path(s_.substr(0, pos == 0 ? 1 : pos));
|
||
}
|
||
|
||
friend path operator/(path lhs, const path& rhs) { lhs /= rhs; return lhs; }
|
||
friend bool operator==(const path& a, const path& b) { return a.s_ == b.s_; }
|
||
friend bool operator!=(const path& a, const path& b) { return a.s_ != b.s_; }
|
||
friend bool operator<(const path& a, const path& b) { return a.s_ < b.s_; }
|
||
|
||
private:
|
||
static bool is_abs_(const std::string& p) {
|
||
if (p.empty()) return false;
|
||
if (p[0] == '/' || p[0] == '\\') return true;
|
||
return p.size() >= 2 && p[1] == ':';
|
||
}
|
||
|
||
std::string s_;
|
||
};
|
||
|
||
inline std::ostream& operator<<(std::ostream& os, const path& p) {
|
||
return os << p.string();
|
||
}
|
||
|
||
namespace detail {
|
||
inline bool stat_path(const path& p, struct _stat64* st) {
|
||
return ::_stat64(p.string().c_str(), st) == 0;
|
||
}
|
||
} // namespace detail
|
||
|
||
inline path temp_directory_path() {
|
||
const char* t = std::getenv("TEMP");
|
||
if (t == nullptr || *t == '\0') t = std::getenv("TMP");
|
||
return path((t != nullptr && *t != '\0') ? t : ".");
|
||
}
|
||
|
||
inline path temp_directory_path(std::error_code& ec) {
|
||
ec.clear();
|
||
return temp_directory_path();
|
||
}
|
||
|
||
inline bool exists(const path& p, std::error_code& ec) {
|
||
ec.clear();
|
||
struct _stat64 st;
|
||
return detail::stat_path(p, &st);
|
||
}
|
||
|
||
inline bool exists(const path& p) {
|
||
std::error_code ec;
|
||
return exists(p, ec);
|
||
}
|
||
|
||
inline unsigned long long file_size(const path& p, std::error_code& ec) {
|
||
struct _stat64 st;
|
||
if (!detail::stat_path(p, &st)) {
|
||
ec = std::make_error_code(std::errc::no_such_file_or_directory);
|
||
return static_cast<unsigned long long>(-1);
|
||
}
|
||
ec.clear();
|
||
return static_cast<unsigned long long>(st.st_size);
|
||
}
|
||
|
||
inline unsigned long long file_size(const path& p) {
|
||
std::error_code ec;
|
||
const unsigned long long sz = file_size(p, ec);
|
||
return ec ? 0 : sz;
|
||
}
|
||
|
||
inline bool remove(const path& p, std::error_code& ec) {
|
||
ec.clear();
|
||
struct _stat64 st;
|
||
if (!detail::stat_path(p, &st)) return false; // 不存在:std 语义返回 false 且不报错
|
||
if (std::remove(p.string().c_str()) == 0) return true;
|
||
ec = std::make_error_code(std::errc::operation_not_permitted);
|
||
return false;
|
||
}
|
||
|
||
inline bool remove(const path& p) {
|
||
std::error_code ec;
|
||
return remove(p, ec);
|
||
}
|
||
|
||
inline bool create_directories(const path& p, std::error_code& ec) {
|
||
ec.clear();
|
||
if (p.empty()) return false;
|
||
struct _stat64 st;
|
||
if (detail::stat_path(p, &st)) return false; // 已存在:std 语义返回 false(非新建)
|
||
const std::string& s = p.string();
|
||
std::size_t i = 0;
|
||
while (i <= s.size()) {
|
||
const std::size_t nxt = s.find_first_of("/\\", i);
|
||
const std::string seg = s.substr(0, nxt == std::string::npos ? s.size() : nxt);
|
||
if (!seg.empty() && seg.back() != ':') {
|
||
if (::_mkdir(seg.c_str()) != 0) {
|
||
struct _stat64 st2;
|
||
if (!detail::stat_path(path(seg), &st2)) {
|
||
ec = std::make_error_code(std::errc::no_such_file_or_directory);
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
if (nxt == std::string::npos) break;
|
||
i = nxt + 1;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
inline bool create_directories(const path& p) {
|
||
std::error_code ec;
|
||
const bool created = create_directories(p, ec);
|
||
return !ec && created;
|
||
}
|
||
|
||
|
||
enum class directory_options { none = 0, skip_permission_denied = 1 };
|
||
|
||
class directory_entry {
|
||
public:
|
||
directory_entry() = default;
|
||
explicit directory_entry(vde::fs::path p) : p_(std::move(p)) {}
|
||
const vde::fs::path& path() const { return p_; }
|
||
bool is_directory(std::error_code& ec) const {
|
||
struct _stat64 st;
|
||
ec.clear();
|
||
if (!detail::stat_path(p_, &st)) {
|
||
ec = std::make_error_code(std::errc::no_such_file_or_directory);
|
||
return false;
|
||
}
|
||
return (st.st_mode & _S_IFDIR) != 0;
|
||
}
|
||
bool is_regular_file(std::error_code& ec) const {
|
||
struct _stat64 st;
|
||
ec.clear();
|
||
if (!detail::stat_path(p_, &st)) {
|
||
ec = std::make_error_code(std::errc::no_such_file_or_directory);
|
||
return false;
|
||
}
|
||
return (st.st_mode & _S_IFREG) != 0;
|
||
}
|
||
bool is_directory() const {
|
||
std::error_code ec;
|
||
return is_directory(ec);
|
||
}
|
||
bool is_regular_file() const {
|
||
std::error_code ec;
|
||
return is_regular_file(ec);
|
||
}
|
||
bool is_symlink(std::error_code& ec) const { ec.clear(); return false; }
|
||
private:
|
||
vde::fs::path p_;
|
||
};
|
||
|
||
class directory_iterator {
|
||
public:
|
||
directory_iterator() = default;
|
||
directory_iterator(const path& dir, std::error_code& ec) {
|
||
init(dir, ec);
|
||
}
|
||
directory_iterator(const path& dir, directory_options, std::error_code& ec) {
|
||
init(dir, ec);
|
||
}
|
||
private:
|
||
void init(const path& dir, std::error_code& ec) {
|
||
ec.clear();
|
||
std::string pattern = dir.string();
|
||
if (!pattern.empty() && pattern.back() != '/' && pattern.back() != '\\')
|
||
pattern += '/';
|
||
pattern += '*';
|
||
struct _finddata_t fd;
|
||
const intptr_t handle = _findfirst(pattern.c_str(), &fd);
|
||
if (handle == -1) return;
|
||
do {
|
||
const std::string name = fd.name;
|
||
if (name == "." || name == "..") continue;
|
||
entries_.emplace_back(path(dir.string() + "/" + name));
|
||
} while (_findnext(handle, &fd) == 0);
|
||
_findclose(handle);
|
||
if (!entries_.empty()) current_ = entries_[0];
|
||
}
|
||
public:
|
||
const directory_entry* operator->() const { return ¤t_; }
|
||
const directory_entry& operator*() const { return current_; }
|
||
directory_iterator& operator++() {
|
||
std::error_code ec;
|
||
increment(ec);
|
||
return *this;
|
||
}
|
||
void increment(std::error_code& ec) {
|
||
ec.clear();
|
||
++index_;
|
||
if (index_ < entries_.size()) {
|
||
current_ = entries_[index_];
|
||
} else {
|
||
entries_.clear();
|
||
index_ = 0;
|
||
current_ = directory_entry{};
|
||
}
|
||
}
|
||
friend bool operator==(const directory_iterator& a,
|
||
const directory_iterator& b) {
|
||
const bool ae = a.entries_.empty() || a.index_ >= a.entries_.size();
|
||
const bool be = b.entries_.empty() || b.index_ >= b.entries_.size();
|
||
if (ae || be) return ae && be;
|
||
return a.index_ == b.index_;
|
||
}
|
||
friend bool operator!=(const directory_iterator& a,
|
||
const directory_iterator& b) {
|
||
return !(a == b);
|
||
}
|
||
private:
|
||
std::vector<directory_entry> entries_;
|
||
std::size_t index_ = 0;
|
||
directory_entry current_;
|
||
};
|
||
|
||
inline directory_iterator begin(directory_iterator it) { return it; }
|
||
inline directory_iterator end(const directory_iterator&) { return directory_iterator{}; }
|
||
|
||
inline bool is_directory(const path& p, std::error_code& ec) {
|
||
directory_entry e(p);
|
||
return e.is_directory(ec);
|
||
}
|
||
inline bool is_regular_file(const path& p, std::error_code& ec) {
|
||
struct _stat64 st;
|
||
ec.clear();
|
||
if (!detail::stat_path(p, &st)) {
|
||
ec = std::make_error_code(std::errc::no_such_file_or_directory);
|
||
return false;
|
||
}
|
||
return (st.st_mode & _S_IFREG) != 0;
|
||
}
|
||
inline bool is_regular_file(const path& p) {
|
||
std::error_code ec;
|
||
return is_regular_file(p, ec);
|
||
}
|
||
inline bool is_directory(const path& p) {
|
||
std::error_code ec;
|
||
return is_directory(p, ec);
|
||
}
|
||
inline path weakly_canonical(const path& p, std::error_code& ec) {
|
||
ec.clear();
|
||
char buf[_MAX_PATH];
|
||
if (_fullpath(buf, p.string().c_str(), _MAX_PATH) == nullptr) return p;
|
||
return path(buf);
|
||
}
|
||
inline path current_path(std::error_code& ec) {
|
||
ec.clear();
|
||
char buf[_MAX_PATH];
|
||
if (_getcwd(buf, _MAX_PATH) == nullptr) {
|
||
ec = std::make_error_code(std::errc::io_error);
|
||
return path(".");
|
||
}
|
||
return path(buf);
|
||
}
|
||
|
||
class recursive_directory_iterator {
|
||
public:
|
||
recursive_directory_iterator() = default;
|
||
recursive_directory_iterator(const path& dir, directory_options, std::error_code& ec) {
|
||
collect(dir, ec);
|
||
if (!entries_.empty()) current_ = entries_[0];
|
||
}
|
||
const directory_entry* operator->() const { return ¤t_; }
|
||
const directory_entry& operator*() const { return current_; }
|
||
void increment(std::error_code& ec) {
|
||
ec.clear();
|
||
++index_;
|
||
if (index_ < entries_.size()) {
|
||
current_ = entries_[index_];
|
||
} else {
|
||
entries_.clear();
|
||
index_ = 0;
|
||
current_ = directory_entry{};
|
||
}
|
||
}
|
||
friend bool operator==(const recursive_directory_iterator& a,
|
||
const recursive_directory_iterator& b) {
|
||
const bool ae = a.entries_.empty() || a.index_ >= a.entries_.size();
|
||
const bool be = b.entries_.empty() || b.index_ >= b.entries_.size();
|
||
if (ae || be) return ae && be;
|
||
return a.index_ == b.index_;
|
||
}
|
||
friend bool operator!=(const recursive_directory_iterator& a,
|
||
const recursive_directory_iterator& b) {
|
||
return !(a == b);
|
||
}
|
||
private:
|
||
void collect(const path& dir, std::error_code& ec) {
|
||
for (directory_iterator it(dir, directory_options::skip_permission_denied, ec), end;
|
||
!ec && it != end; it.increment(ec)) {
|
||
entries_.push_back(*it);
|
||
std::error_code sub_ec;
|
||
if (it->is_directory(sub_ec) && !sub_ec) collect(it->path(), ec);
|
||
if (ec) return;
|
||
}
|
||
}
|
||
std::vector<directory_entry> entries_;
|
||
std::size_t index_ = 0;
|
||
directory_entry current_;
|
||
};
|
||
|
||
inline recursive_directory_iterator begin(recursive_directory_iterator it) { return it; }
|
||
inline recursive_directory_iterator end(const recursive_directory_iterator&) {
|
||
return recursive_directory_iterator{};
|
||
}
|
||
|
||
inline path relative(const path& p, const path& base, std::error_code& ec) {
|
||
ec.clear();
|
||
std::string a = p.generic_string();
|
||
std::string b = base.generic_string();
|
||
while (b.size() > 1 && b.back() == '/') b.pop_back();
|
||
if (!b.empty() && a.compare(0, b.size(), b) == 0) {
|
||
if (a.size() == b.size()) return path(".");
|
||
if (a[b.size()] == '/') return path(a.substr(b.size() + 1));
|
||
}
|
||
ec = std::make_error_code(std::errc::invalid_argument);
|
||
return path();
|
||
}
|
||
|
||
inline bool rename(const path& from, const path& to, std::error_code& ec) {
|
||
ec.clear();
|
||
std::remove(to.string().c_str());
|
||
if (std::rename(from.string().c_str(), to.string().c_str()) == 0) return true;
|
||
ec = std::make_error_code(std::errc::io_error);
|
||
return false;
|
||
}
|
||
|
||
enum class copy_options { none = 0, overwrite_existing = 1 };
|
||
|
||
inline bool copy_file(const path& from, const path& to, copy_options opts,
|
||
std::error_code& ec) {
|
||
ec.clear();
|
||
if (opts != copy_options::overwrite_existing && exists(to)) {
|
||
ec = std::make_error_code(std::errc::file_exists);
|
||
return false;
|
||
}
|
||
std::ifstream in(from.string(), std::ios::binary);
|
||
if (!in) { ec = std::make_error_code(std::errc::no_such_file_or_directory); return false; }
|
||
std::ofstream out(to.string(), std::ios::binary | std::ios::trunc);
|
||
if (!out) { ec = std::make_error_code(std::errc::io_error); return false; }
|
||
out << in.rdbuf();
|
||
if (!out.good()) { ec = std::make_error_code(std::errc::io_error); return false; }
|
||
return true;
|
||
}
|
||
|
||
inline bool is_empty(const path& p, std::error_code& ec) {
|
||
ec.clear();
|
||
struct _stat64 st;
|
||
if (!detail::stat_path(p, &st)) {
|
||
ec = std::make_error_code(std::errc::no_such_file_or_directory);
|
||
return false;
|
||
}
|
||
if ((st.st_mode & _S_IFDIR) == 0) return st.st_size == 0;
|
||
directory_iterator it(p, directory_options::skip_permission_denied, ec), end;
|
||
return !ec && it == end;
|
||
}
|
||
|
||
inline bool remove_all(const path& p, std::error_code& ec) {
|
||
ec.clear();
|
||
struct _stat64 st;
|
||
if (!detail::stat_path(p, &st)) return false;
|
||
if ((st.st_mode & _S_IFDIR) == 0) return remove(p, ec);
|
||
for (directory_iterator it(p, directory_options::skip_permission_denied, ec), end;
|
||
!ec && it != end; it.increment(ec)) {
|
||
remove_all(it->path(), ec);
|
||
if (ec) return false;
|
||
}
|
||
return ::_rmdir(p.string().c_str()) == 0;
|
||
}
|
||
inline bool remove_all(const path& p) {
|
||
std::error_code ec;
|
||
return remove_all(p, ec);
|
||
}
|
||
|
||
} // namespace fs
|
||
} // namespace vde
|
||
|
||
#else // 非 MinGW:直接别名 std::filesystem
|
||
|
||
#include <filesystem>
|
||
|
||
namespace vde {
|
||
namespace fs = std::filesystem;
|
||
} // namespace vde
|
||
|
||
#endif
|