Rename to xy_unix_path_to_win_if_on_win

This commit is contained in:
Aoran Zeng 2023-09-27 18:14:43 +08:00
parent b92c68ebed
commit 87858ed7a8
2 changed files with 21 additions and 10 deletions

View File

@ -90,6 +90,9 @@ main (int argc, char const *argv[])
assert(xy_file_exist("chsrc.png")); assert(xy_file_exist("chsrc.png"));
assert(xy_file_exist(xy_win_powershell_profile)); assert(xy_file_exist(xy_win_powershell_profile));
assert(false==xy_file_exist(xy_win_powershellv5_profile)); assert(false==xy_file_exist(xy_win_powershellv5_profile));
puts (xy_unix_path_to_win_if_on_win(" \n ~/haha/test/123 \n\r "));
xy_success("测试全部通过"); xy_success("测试全部通过");
return 0; return 0;
} }

28
xy.h
View File

@ -517,9 +517,9 @@ _xy_win_powershellv5_profile ()
* @note Windows上`path` access() * @note Windows上`path` access()
*/ */
static bool static bool
xy_file_exist (char* path) xy_file_exist (const char* path)
{ {
char* newpath = path; const char* newpath = path;
if (xy_on_windows) if (xy_on_windows)
{ {
if (xy_str_start_with(path, "~")) { if (xy_str_start_with(path, "~")) {
@ -530,16 +530,24 @@ xy_file_exist (char* path)
} }
static const char* /**
xy_unix_path_to_win (const char* path) * 使Windows下也可调用
*/
static char*
xy_unix_path_to_win_if_on_win (const char* path)
{ {
path = xy_str_strip(path); // 防止开发者多写了空格 char* new = xy_str_strip(path); // 防止开发者多写了空白符
if (xy_str_start_with(path, "~/")){
// 或 %USERPROFILE% // 这个函数仅在Windows上才进行替换
path = xy_strjoin(3, xy_os_home, "\\", xy_str_delete_prefix(path, "~/")); if (xy_on_windows) {
path = xy_str_gsub(path, "/", "\\"); if (xy_str_start_with(new, "~/")){
// 或 %USERPROFILE%
new = xy_strjoin(3, xy_os_home, "\\", xy_str_delete_prefix(new, "~/"));
}
new = xy_str_gsub(new, "/", "\\");
} }
return path;
return new;
} }
#endif #endif