From afe6f96611cf7cd09aa6b6293d27015f9cea22d0 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Fri, 29 Sep 2023 21:16:16 +0800 Subject: [PATCH] Add `xy_parent_dir()` --- test_xy.c | 3 ++- xy.h | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/test_xy.c b/test_xy.c index 631df5c..8997f8d 100644 --- a/test_xy.c +++ b/test_xy.c @@ -3,7 +3,7 @@ * License : MIT * Authors : Aoran Zeng * Created on : <2023-08-30> - * Last modified : <2023-09-27> + * Last modified : <2023-09-29> * * test_xy: * @@ -92,6 +92,7 @@ main (int argc, char const *argv[]) assert(false==xy_file_exist(xy_win_powershellv5_profile)); puts (xy_uniform_path(" \n ~/haha/test/123 \n\r ")); + assert_str (xy_uniform_path("~/haha/test"), xy_parent_dir (" ~/haha/test/123") ); xy_success("测试全部通过"); return 0; diff --git a/xy.h b/xy.h index 9bec834..3aad576 100644 --- a/xy.h +++ b/xy.h @@ -3,7 +3,7 @@ * License : MIT * Authors : Aoran Zeng * Created on : <2023-08-28> - * Last modified : <2023-09-27> + * Last modified : <2023-09-29> * * xy: * @@ -557,4 +557,20 @@ xy_uniform_path (const char* path) return new; } + +static char* +xy_parent_dir (const char* path) +{ + char* dir = xy_uniform_path (path); + char* last = NULL; + if (xy_on_windows) { + last = strrchr(dir, '\\'); + *last = '\0'; + } else { + last = strrchr(dir, '/'); + *last = '\0'; + } + return dir; +} + #endif