-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix #3171 : 去掉生成绝对路径函数中的窄字符WindowsAPI调用,改用宽字符API和C++17 std::filesystem来避免win平台中文路径问题 #3255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
391b62a
750f6a3
3e6f259
25c5a4e
f27c320
43db5d0
01881d3
989488c
853d5fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,24 +5,28 @@ | |
| #include "sherpa-onnx/csrc/file-utils.h" | ||
|
|
||
| #include <fstream> | ||
| #include <filesystem> | ||
| #include <memory> | ||
| #include <sstream> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #ifdef _WIN32 | ||
| #include <windows.h> | ||
| #else | ||
| #include <limits.h> | ||
| #include <stdlib.h> | ||
| #endif | ||
|
|
||
| #include "sherpa-onnx/csrc/macros.h" | ||
|
|
||
| namespace sherpa_onnx { | ||
| std::wstring ToWideString(const std::string &s); | ||
| } // namespace sherpa_onnx | ||
|
|
||
| namespace sherpa_onnx { | ||
|
||
|
|
||
| bool FileExists(const std::string &filename) { | ||
| #ifdef _WIN32 | ||
| std::wstring wide_path = ToWideString(filename); | ||
| std::ifstream file(wide_path); | ||
| return file.good(); | ||
| #else | ||
| return std::ifstream(filename).good(); | ||
| #endif | ||
| } | ||
|
|
||
| void AssertFileExists(const std::string &filename) { | ||
|
|
@@ -33,7 +37,12 @@ void AssertFileExists(const std::string &filename) { | |
| } | ||
|
|
||
| std::vector<char> ReadFile(const std::string &filename) { | ||
| #ifdef _WIN32 | ||
| std::wstring wide_path = ToWideString(filename); | ||
| std::ifstream file(wide_path, std::ios::binary | std::ios::ate); | ||
| #else | ||
| std::ifstream file(filename, std::ios::binary | std::ios::ate); | ||
| #endif | ||
| if (!file.is_open()) { | ||
| return {}; | ||
| } | ||
|
|
@@ -119,33 +128,23 @@ std::string ResolveAbsolutePath(const std::string &path) { | |
| return path; | ||
| } | ||
|
|
||
| #ifdef _WIN32 | ||
| // Check if path is already absolute (drive letter or UNC path) | ||
| if ((path.size() > 1 && path[1] == ':') || | ||
| (path.size() > 1 && path[0] == '\\' && path[1] == '\\')) { | ||
| return path; | ||
| } | ||
|
|
||
| char buffer[MAX_PATH]; | ||
| if (GetFullPathNameA(path.c_str(), MAX_PATH, buffer, nullptr)) { | ||
| return std::string(buffer); | ||
| } | ||
|
|
||
| return path; // fallback on failure | ||
|
|
||
| #else | ||
| // POSIX: absolute paths start with '/' | ||
| if (path[0] == '/') { | ||
| try { | ||
| std::filesystem::path fs_path(path); | ||
|
||
|
|
||
| // If already absolute, return normalized path | ||
| if (fs_path.is_absolute()) { | ||
| return fs_path.lexically_normal().u8string(); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Convert to absolute path and normalize | ||
| std::filesystem::path abs_path = std::filesystem::absolute(fs_path); | ||
| abs_path = abs_path.lexically_normal(); | ||
|
|
||
| return abs_path.u8string(); | ||
| } catch (const std::filesystem::filesystem_error&) { | ||
| // If conversion fails, return original path | ||
| return path; | ||
| } | ||
|
|
||
| char buffer[PATH_MAX]; | ||
| if (realpath(path.c_str(), buffer)) { | ||
| return std::string(buffer); | ||
| } | ||
|
|
||
| return path; // fallback on failure | ||
| #endif | ||
| } | ||
|
|
||
| } // namespace sherpa_onnx | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你好,我们现在避免使用 filesystem 这个头文件. 详见 #2998
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的谢谢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
您好,看ai审阅还有超过4GB的大文件的读取问题,需要考虑循环分块读取吗?