#pragma once /// 平台抽象层 /// 跨平台兼容宏 + M_PI 补丁 (MSVC 未定义) /// /// Windows / POSIX 差异约定(P3-7 跨平台收口): /// - 动态库:Windows 走 LoadLibraryA(失败时 UTF-8→UTF-16 用 LoadLibraryW 重试), /// POSIX 走 dlopen/dlsym/dlclose; /// - 文件路径:Windows 优先 ANSI 窄 API,失败时用 UTF-8→UTF-16 宽 API 重试 /// (load_library/delete_file/make_temp_file/read_file_bytes 均如此), /// 因此调用方统一传 UTF-8 字符串即可; /// - 错误信息:Windows 用 FormatMessageW 取系统消息并转 UTF-8;POSIX 用 dlerror; /// - 进程:run_command 在 Windows 用 cmd.exe /C(带超时版本 CreateProcessA + /// TerminateProcess/taskkill),POSIX 用 fork/exec + SIGKILL; /// 命令字符串中的非 ASCII 字符在 Windows 上受 ANSI 代码页限制(已知限制); /// - 临时目录:Windows 读 TEMP,POSIX 读 TMPDIR(缺省 /tmp); /// - MSVC 下 M_PI 需要本头或 _USE_MATH_DEFINES(本头在包含处兜底定义)。 #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #include namespace vde::platform { /// 动态库句柄 using LibHandle = void*; /// 加载动态库 (.so / .dll / .dylib / .so.hmos) /// @return 非空 → 成功,nullptr → 失败 LibHandle load_library(const std::string& path); /// 卸载 void unload_library(LibHandle h); /// 查找符号 void* find_symbol(LibHandle h, const std::string& name); /// 获取最后一次加载错误信息 std::string last_error(); /// 创建临时文件(跨平台安全) /// /// 文件名包含 PID + 原子计数器 + 单调纳秒 + 随机分量,并以「独占创建」 /// (Windows CREATE_NEW / POSIX O_CREAT|O_EXCL)保证并发进程、并发线程之间 /// 路径唯一且不会复用已存在文件(K-11:修复旧实现「秒级时间戳 + 进程内 /// 计数器」在并行测试/服务下跨进程撞名、互相删除的缺陷)。 /// 清理语义:本函数只创建空文件,删除由调用方 `delete_file` 负责。 /// @param prefix 文件名前缀 /// @param suffix 文件扩展名(如 ".dxf") /// @param[out] path 返回临时文件完整路径 /// @return true 成功 bool make_temp_file(const std::string& prefix, const std::string& suffix, std::string& path); /// 删除文件 bool delete_file(const std::string& path); /// 读取整个文件(二进制,不做换行转换) /// @param path 文件路径。Windows 下先按当前 ANSI 代码页解释;若文件打不开, /// 再按 UTF-8 → UTF-16 宽字符重试,以支持 ANSI 无法表示的字符(如 Ø) /// @param[out] out 文件内容 /// @return true 成功 bool read_file_bytes(const std::string& path, std::string& out); /// 读取文件前 max_bytes 字节(二进制) /// @return true 成功(文件短于 max_bytes 时返回实际长度) bool read_file_head(const std::string& path, std::size_t max_bytes, std::string& out); /// 执行系统命令 /// @return 0 成功;超时返回 124(Windows 实现会终止进程) int run_command(const std::string& cmd); /// 执行系统命令,带超时(Windows:CreateProcess + WaitForSingleObject) /// @param timeout_ms 0 表示不超时;超时返回 124 int run_command(const std::string& cmd, unsigned timeout_ms); } // namespace vde::platform