improve dirname

This commit is contained in:
lixianjing 2022-09-16 17:21:02 +08:00
parent 77d0c9ba65
commit 8d0a94e6bf
3 changed files with 32 additions and 4 deletions

View File

@ -1,4 +1,8 @@
# 最新动态
2022/09/16
* path_dirname 处理斜杠和反斜杠的混用的路径时候不正常的问题(感谢智明提供补丁)
2022/09/14
* 支持在添加idle和添加timer是指定ID(感谢智明提供补丁)

View File

@ -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) {

View File

@ -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) {