xy_normalize_path change ~ for POSIX too

This commit is contained in:
Aoran Zeng 2024-11-22 00:32:15 +08:00
parent d7cc74c494
commit 1c239f00b0
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98

View File

@ -8,7 +8,7 @@
* Contributors : Nil Null <nil@null.org> * Contributors : Nil Null <nil@null.org>
* | * |
* Created On : <2023-08-28> * Created On : <2023-08-28>
* Last Modified : <2024-11-21> * Last Modified : <2024-11-22>
* *
* xy: * xy:
* Corss-Platform C utilities for CLI applications in Ruby flavor * Corss-Platform C utilities for CLI applications in Ruby flavor
@ -17,7 +17,7 @@
#ifndef XY_H #ifndef XY_H
#define XY_H #define XY_H
#define _XY_Version "v0.1.4.3-2024/11/21" #define _XY_Version "v0.1.4.3-2024/11/22"
#define _XY_Maintain_URL "https://gitee.com/RubyMetric/chsrc/blob/main/include/xy.h" #define _XY_Maintain_URL "https://gitee.com/RubyMetric/chsrc/blob/main/include/xy.h"
#include <assert.h> #include <assert.h>
@ -730,14 +730,14 @@ xy_dir_exist (const char *path)
} }
/** /**
* 使Windows下也可调用 * 1.
* 2. ~/
*/ */
static char * static char *
xy_normalize_path (const char *path) xy_normalize_path (const char *path)
{ {
char *new = xy_str_strip (path); // 防止开发者多写了空白符 char *new = xy_str_strip (path); // 防止开发者多写了空白符
// 这个函数仅在Windows上才进行替换
if (xy_on_windows) if (xy_on_windows)
{ {
if (xy_str_start_with (new, "~/")) if (xy_str_start_with (new, "~/"))
@ -748,6 +748,14 @@ xy_normalize_path (const char *path)
} }
new = xy_str_gsub (new, "/", "\\"); new = xy_str_gsub (new, "/", "\\");
} }
else
{
if (xy_str_start_with (new, "~/"))
{
new = xy_strjoin (3, xy_os_home, "/",
xy_str_delete_prefix (new, "~/"));
}
}
return new; return new;
} }