Files
hm 58d7971c47 feat(brep/cae/foundation): Wave22 UTF-8 final paths, IGES orientation residuals and measure parallelism
- A56: add header-only open_file_utf8 (Windows _wfopen, mode preserved) and
  convert the last 8 narrow user-path opens (fea_mesh abaqus/ansys/nastran
  exports, hdf5_writer/result_export .part atomic write chains, sldprt
  temporary .x_t write and import_parasolid_xt); src/ std::fopen and
  std::rename are now 0.
- A57: consume IGES 510 OUTERLOOPFLAG=0 (geometric outer loop by Newell
  magnitude), 508 per-edge ORIENTATION (rotate loop start to first forward
  edge), and fallback shell 514 ORIENTATION for open shells; new trim stats.
- A58: adaptive OpenMP chunking, body_measure_parallel_worth() and parallel
  nested mesh volume; bug28366 fuselage volume 45.4 s -> 9.8-11.4 s with
  bit-identical volumes.
- Tests: 4 UTF-8 cases, 10 IGES orientation cases, measure perf gate.
2026-11-23 08:46:58 +08:00

137 lines
5.7 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file utf8_path.h
* @brief UTF-8 路径 → std::filesystem::path / std::ifstream 的轻量封装
*
* 背景(Wave18 / VDE-1396):Windows 上 `std::ifstream(std::string)` 内部经 CRT
* `fopen` 按当前 ANSI 代码页(简体中文系统为 CP936)解释窄字符路径。调用方传入的
* UTF-8 中文/非 ANSI 文件名会被转成错误的宽字符序列,于是“文件存在但打不开”。
* 真实语料 \`corpus/real/iges\` 下两个 60 MB 中文名 IGES 因此 import 失败,
* 根因不是文件损坏。
*
* 解法:先把 UTF-8 字节串构造为 `std::filesystem::path`(C++20 下由
* `char8_t` `u8string` 重载转换,Windows 内部得到宽字符路径),再交给
* `std::ifstream(const std::filesystem::path&)`。这样 CRT 走宽字符
* `CreateFileW`,不再经过 ANSI 代码页,UTF-8 文件名可以正常打开。
*
* 行为约定:
* - 纯 ASCII 路径:UTF-8 解码结果与直接窄字符构造逐字节一致,行为不变;
* - 非法 UTF-8(历史调用方仍传本地编码路径):回退窄字符 `path(std::string)`,
* 保持旧有按 ANSI 代码页解释的行为,不因本工具抛异常;
* - 空串返回空 path(构造出的流打开失败,与原行为一致);
* - 本 header 全部 inline,不新增编译单元、不改 CMake。
*
* 注意:只封装“如何打开/构造路径”,不改变任何解析逻辑。
*/
#pragma once
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <ios>
#include <string>
#include <string_view>
#include <system_error>
namespace vde::foundation {
/**
* @brief 把 UTF-8 字节路径转换为 `std::filesystem::path`
*
* Windows 下经由 `std::filesystem::path` 的 `char8_t` 重载得到宽字符原生路径;
* 非 Windows 平台原生窄字符路径本身就是 UTF-8 字节,语义一致。
*
* @param value UTF-8 编码路径(允许纯 ASCII;空串返回空 path)
* @return 可交给 `std::ifstream` / `std::filesystem` 的 path
*/
inline std::filesystem::path path_from_utf8(std::string_view value) {
if (value.empty()) return {};
#if defined(__cpp_char8_t)
try {
return std::filesystem::path(
reinterpret_cast<const char8_t*>(value.data()));
} catch (const std::system_error&) {
// 非法 UTF-8(历史调用方可能传本地代码页路径):回退窄字符构造,
// 保持旧有“按 ANSI 代码页解释”的行为,避免把异常抛给调用方。
return std::filesystem::path(std::string(value));
}
#else
return std::filesystem::u8path(value.data(), value.data() + value.size());
#endif
}
/**
* @brief 以 UTF-8 路径打开输入文件流
*
* 只替换路径构造方式,返回的流与原 `std::ifstream(path, mode)` 等价;调用方照旧
* 用 `is_open()` / `good()` 判断(路径为 UTF-8 但文件不存在时同样只是打开失败,
* 不抛异常)。
*
* @param value UTF-8 编码路径
* @param mode 打开模式,默认只读文本(与原 `std::ifstream(path)` 一致)
*/
inline std::ifstream open_ifstream_utf8(std::string_view value,
std::ios_base::openmode mode = std::ios::in) {
return std::ifstream(path_from_utf8(value), mode);
}
/**
* @brief 以 UTF-8 路径打开输出文件流
*
* 与 open_ifstream_utf8 对称:只替换路径构造方式,返回的流与原
* `std::ofstream(path, mode)` 等价,调用方照旧用 is_open()/good() 判断。
*
* @param value UTF-8 编码路径
* @param mode 打开模式,默认只写出(与原 `std::ofstream(path)` 一致)
*/
inline std::ofstream open_ofstream_utf8(std::string_view value,
std::ios_base::openmode mode = std::ios::out) {
return std::ofstream(path_from_utf8(value), mode);
}
/**
* @brief 以 UTF-8 路径打开 C 标准库 `FILE*` 流(保留 `mode` 语义)
*
* 部分底层实现只提供 `FILE*` 入口(HDF5 落盘、网格/结果导出、临时文本文件等),
* 原 `std::fopen(narrow.c_str(), mode)` 同样会按 ANSI 代码页解释 UTF-8 字节。
* 这里先把路径转成宽字符原生路径,再把 `mode` 逐字节扩宽后调用 `_wfopen`;
* 非 Windows 平台直接 `std::fopen`。`mode` 字符串(`"w"`/`"wb"`/`"r+b"` 等)
* 的语义原样交给 CRT,不做任何改写。
*
* @param value UTF-8 编码路径(空串返回 nullptr,与原 `fopen` 打开失败一致)
* @param mode CRT 打开模式字符串(与 `std::fopen` 完全一致)
* @return 打开的文件流;失败返回 nullptr,不抛异常
*/
inline std::FILE* open_file_utf8(std::string_view value, const char* mode) {
if (value.empty() || mode == nullptr) return nullptr;
#if defined(_WIN32)
std::wstring wide_mode;
for (const char* p = mode; *p != '\0'; ++p) {
wide_mode.push_back(
static_cast<wchar_t>(static_cast<unsigned char>(*p)));
}
return ::_wfopen(path_from_utf8(value).c_str(), wide_mode.c_str());
#else
return std::fopen(std::string(value).c_str(), mode);
#endif
}
/**
* @brief 把 `std::filesystem::path` 转回 UTF-8 字节串
*
* 用于把 `std::filesystem::directory_iterator` 枚举到的文件名拼回调用方使用的
* UTF-8 路径字符串(例如 STEP 外部引用同目录解析)。
*
* @param path 文件系统路径
* @return UTF-8 字节串;Windows 下由宽字符原生路径无损转换
*/
inline std::string path_to_utf8(const std::filesystem::path& path) {
#if defined(__cpp_char8_t)
const std::u8string u8 = path.u8string();
return std::string(reinterpret_cast<const char*>(u8.data()), u8.size());
#else
return path.u8string();
#endif
}
} // namespace vde::foundation