From 8d0a94e6bff3c1a5c1ca3b9d9bc594c2e2539732 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Fri, 16 Sep 2022 17:21:02 +0800 Subject: [PATCH] improve dirname --- docs/changes.md | 4 ++++ src/tkc/path.c | 12 +++++++++--- tests/path_test.cc | 20 +++++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/docs/changes.md b/docs/changes.md index 2c68ac11a..176f70e33 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,4 +1,8 @@ # 最新动态 + +2022/09/16 + * path_dirname 处理斜杠和反斜杠的混用的路径时候不正常的问题(感谢智明提供补丁) + 2022/09/14 * 支持在添加idle和添加timer是指定ID(感谢智明提供补丁) diff --git a/src/tkc/path.c b/src/tkc/path.c index 8ba7f857e..c5d09776e 100644 --- a/src/tkc/path.c +++ b/src/tkc/path.c @@ -80,13 +80,19 @@ ret_t path_extname(const char* path, char* result, int32_t size) { ret_t path_dirname(const char* path, char* result, int32_t size) { const char* p = NULL; + const char* p1 = NULL; int32_t real_size = 0; return_value_if_fail(path != NULL && result != NULL, RET_BAD_PARAMS); memset(result, 0x00, size); - p = strrchr(path, TK_PATH_SEP); - if (p == NULL) { - p = strrchr(path, TK_PATH_SEP == '/' ? '\\' : '/'); + p = strrchr(path, '\\'); + p1 = strrchr(path, '/'); + if (p == NULL && p1 != NULL) { + p = p1; + } else if (p != NULL && p1 != NULL) { + if (p - p1 < 0) { + p = p1; + } } if (p != NULL) { diff --git a/tests/path_test.cc b/tests/path_test.cc index 9ef2f0c75..9b1f58ea9 100644 --- a/tests/path_test.cc +++ b/tests/path_test.cc @@ -57,15 +57,33 @@ TEST(Path, extname) { } TEST(Path, dirname) { - char result[MAX_PATH + 1]; + char result[MAX_PATH + 1] = {0}; ASSERT_EQ(path_dirname("/a/test.bin", result, sizeof(result)), RET_OK); ASSERT_EQ(string(result), string("/a/")); + memset(result, 0x0, sizeof(result)); ASSERT_EQ(path_dirname("/a/b/test.bin", result, sizeof(result)), RET_OK); ASSERT_EQ(string(result), string("/a/b/")); + memset(result, 0x0, sizeof(result)); ASSERT_EQ(path_dirname("test.bin", result, sizeof(result)), RET_FAIL); ASSERT_EQ(string(result), string("")); + + memset(result, 0x0, sizeof(result)); + ASSERT_EQ(path_dirname("e:\\a\\b/c/d", result, sizeof(result)), RET_OK); + ASSERT_EQ(string(result), string("e:\\a\\b/c/")); + + memset(result, 0x0, sizeof(result)); + ASSERT_EQ(path_dirname("e:\\a\\b/c\\d", result, sizeof(result)), RET_OK); + ASSERT_EQ(string(result), string("e:\\a\\b/c\\")); + + memset(result, 0x0, sizeof(result)); + ASSERT_EQ(path_dirname("e:/a/b\\c\\d", result, sizeof(result)), RET_OK); + ASSERT_EQ(string(result), string("e:/a/b\\c\\")); + + memset(result, 0x0, sizeof(result)); + ASSERT_EQ(path_dirname("e:/a/b\\c/d", result, sizeof(result)), RET_OK); + ASSERT_EQ(string(result), string("e:/a/b\\c/")); } TEST(Path, exe) {