Fix xy_normalize_path()

This commit is contained in:
Aoran Zeng 2025-08-18 19:23:16 +08:00
parent 4d813893e9
commit 0d19ee249f
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98

View File

@ -9,7 +9,7 @@
* | Mikachu2333 <mikachu.23333@zohomail.com>
* |
* Created On : <2023-08-28>
* Last Modified : <2025-08-17>
* Last Modified : <2025-08-18>
*
* xy:
* Corss-Platform C11 utilities for CLI applications in mixed
@ -19,7 +19,7 @@
#ifndef XY_H
#define XY_H
#define _XY_Version "v0.1.5.5-2025/08/17"
#define _XY_Version "v0.1.6.0-2025/08/18"
#define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
#define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
@ -904,37 +904,47 @@ xy_dir_exist (const char *path)
}
/**
* 1.
* 2. ~/
* @brief
*
* @details
* 1.
* - ( grep )
* -
* 2. ~
* 3. Windows上使 \
*/
static char *
xy_normalize_path (const char *path)
{
char *new = xy_str_strip (path); // 防止开发者多写了空白符
char *new = xy_str_strip (path);
if (xy_str_start_with (new, "~"))
{
new = xy_strjoin (3, xy_os_home, "/",
xy_str_delete_prefix (new, "~"));
new = xy_2strjoin (xy_os_home, xy_str_delete_prefix (new, "~"));
}
new = xy_str_gsub (new, "\\", "/");
new = xy_str_gsub (new, "//", "/");
if (xy_on_windows)
return xy_str_gsub (new, "/", "\\");
else
return new;
}
/**
* @note
* @brief
*
* @note
* - "目录名" () "路径"
* - Windows上使 \
*/
static char *
xy_parent_dir (const char *path)
{
char *dir = xy_normalize_path (path);
/* 不管是否为Windows全部统一使用 / 作为路径分隔符,方便后续处理 */
dir = xy_str_gsub (dir, "\\", "/");
if (xy_str_end_with (dir, "/"))
dir = xy_str_delete_suffix (dir, "/");
@ -943,11 +953,12 @@ xy_parent_dir (const char *path)
last = strrchr (dir, '/');
if (!last)
{
/* current dir */
/* 路径中没有一个 / 是很奇怪的,我们直接返回 . 作为当前目录 */
return ".";
}
*last = '\0';
/* Windows上重新使用 \ 作为路径分隔符 */
if (xy_on_windows)
return xy_str_gsub (dir, "/", "\\");
else