@@ -984,60 +984,46 @@ xy_normalize_path (const char *path)
984984{
985985 char * new = xy_str_strip (path ); // 防止开发者多写了空白符
986986
987- if (xy_on_windows )
987+ if (xy_str_start_with ( new , "~" ) )
988988 {
989- if (xy_str_start_with (new , "~/" ))
990- {
991- // 或 %USERPROFILE%
992- new = xy_strjoin (3 , xy_os_home , "\\" ,
993- xy_str_delete_prefix (new , "~/" ));
994- }
995- new = xy_str_gsub (new , "/" , "\\" );
996- }
997- else
998- {
999- if (xy_str_start_with (new , "~/" ))
1000- {
1001- new = xy_strjoin (3 , xy_os_home , "/" ,
1002- xy_str_delete_prefix (new , "~/" ));
1003- }
989+ new = xy_strjoin (3 , xy_os_home , "/" ,
990+ xy_str_delete_prefix (new , "~" ));
1004991 }
1005992
1006- return new ;
1007- }
993+ new = xy_str_gsub ( new , "\\" , "/" ) ;
994+ new = xy_str_gsub ( new , "//" , "/" );
1008995
996+ if (xy_on_windows )
997+ return xy_str_gsub (new , "/" , "\\" );
998+ else
999+ return new ;
1000+ }
10091001
10101002/**
1011- * @warning 如果传入的路径以 `\` 或者 `/` 结尾,本函数将直接去除斜杠
1012- * 并且不会进一步处理为传入路径的父目录
1013- * @bug TODO 存在问题,后续修复
1003+ * @note 总是返回不含末尾斜杠的父目录路径
10141004 */
10151005static char *
10161006xy_parent_dir (const char * path )
10171007{
10181008 char * dir = xy_normalize_path (path );
1009+ dir = xy_str_gsub (dir , "\\" , "/" );
1010+ if (xy_str_end_with (dir , "/" ))
1011+ dir = xy_str_delete_suffix (dir , "/" );
1012+
10191013 char * last = NULL ;
1020- if (xy_on_windows )
1014+
1015+ last = strrchr (dir , '/' );
1016+ if (!last )
10211017 {
1022- last = strrchr (dir , '\\' );
1023- if (!last )
1024- {
1025- /* current dir */
1026- return "." ;
1027- }
1028- * last = '\0' ;
1018+ /* current dir */
1019+ return "." ;
10291020 }
1021+ * last = '\0' ;
1022+
1023+ if (xy_on_windows )
1024+ return xy_str_gsub (dir , "/" , "\\" );
10301025 else
1031- {
1032- last = strrchr (dir , '/' );
1033- if (!last )
1034- {
1035- /* current dir */
1036- return "." ;
1037- }
1038- * last = '\0' ;
1039- }
1040- return dir ;
1026+ return dir ;
10411027}
10421028
10431029
0 commit comments