From d77e905ea494cd866b7d67ae6bffde5c9b876b67 Mon Sep 17 00:00:00 2001 From: lixianjing Date: Mon, 25 Sep 2023 08:51:46 +0800 Subject: [PATCH] add path_prepend_xxx --- docs/changes.md | 4 ++++ src/tkc/path.c | 20 ++++++++++++++++++++ src/tkc/path.h | 20 ++++++++++++++++++++ tests/path_test.cc | 15 +++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/docs/changes.md b/docs/changes.md index 6bdcdb8bd..239686d1d 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -1,5 +1,9 @@ # 最新动态 +2023/09/25 + * 增加函数path\_prepend\_temp\_path + * 增加函数path\_prepend\_user\_storage\_path + 2023/09/24 * 增加函数tk\_iostream\_tcp\_create\_client * 增加函数hash\_table\_size diff --git a/src/tkc/path.c b/src/tkc/path.c index 0dd0121aa..0f26e2a9d 100644 --- a/src/tkc/path.c +++ b/src/tkc/path.c @@ -346,6 +346,26 @@ const char* path_prepend_app_root(char full_path[MAX_PATH + 1], const char* path return full_path; } +const char* path_prepend_temp_path(char full_path[MAX_PATH + 1], const char* path) { + char temp_path[MAX_PATH + 1] = {0}; + return_value_if_fail(path != NULL, NULL); + return_value_if_fail(fs_get_temp_path(os_fs(), temp_path) == RET_OK, NULL); + + path_build(full_path, MAX_PATH, temp_path, path, NULL); + + return full_path; +} + +const char* path_prepend_user_storage_path(char full_path[MAX_PATH + 1], const char* path) { + char user_storage_path[MAX_PATH + 1] = {0}; + return_value_if_fail(path != NULL, NULL); + return_value_if_fail(fs_get_user_storage_path(os_fs(), user_storage_path) == RET_OK, NULL); + + path_build(full_path, MAX_PATH, user_storage_path, path, NULL); + + return full_path; +} + ret_t path_abs_normalize(const char* filename, char* result, int32_t size) { char path[MAX_PATH + 1]; return_value_if_fail(filename != NULL && result != NULL && size > 0, RET_BAD_PARAMS); diff --git a/src/tkc/path.h b/src/tkc/path.h index fae4add14..3d5b616f8 100644 --- a/src/tkc/path.h +++ b/src/tkc/path.h @@ -257,6 +257,26 @@ ret_t path_remove_last_slash(char* path); */ const char* path_prepend_app_root(char full_path[MAX_PATH + 1], const char* path); +/** + * @method path_prepend_temp_path + * 将前面路径加上临时文件目录。 + * @param {char*} full_path 用于返回完整路径。 + * @param {const char*} path 路径。 + * + * @return {const char*} 返回完整路径。 + */ +const char* path_prepend_temp_path(char full_path[MAX_PATH + 1], const char* path); + +/** + * @method path_prepend_user_storage_path + * 将前面路径加上用户目录。 + * @param {char*} full_path 用于返回完整路径。 + * @param {const char*} path 路径。 + * + * @return {const char*} 返回完整路径。 + */ +const char* path_prepend_user_storage_path(char full_path[MAX_PATH + 1], const char* path); + /** * @method path_abs_normalize * 将相对路径转换为绝对路径并规范路径字符形式。 diff --git a/tests/path_test.cc b/tests/path_test.cc index ce7104ce4..eada12516 100644 --- a/tests/path_test.cc +++ b/tests/path_test.cc @@ -270,6 +270,21 @@ TEST(Path, path_prepend_app_root) { char result[MAX_PATH + 1] = {0}; ASSERT_EQ(path_prepend_app_root(result, "bin"), result); ASSERT_EQ(path_exist(result), TRUE); + log_debug("%s\n", result); +} + +TEST(Path, path_prepend_temp_path) { + char result[MAX_PATH + 1] = {0}; + ASSERT_EQ(path_prepend_temp_path(result, "test.txt"), result); + ASSERT_NE(strstr(result, "test.txt"), (char*)NULL); + log_debug("%s\n", result); +} + +TEST(Path, path_prepend_user_storage_path) { + char result[MAX_PATH + 1] = {0}; + ASSERT_EQ(path_prepend_user_storage_path(result, "test.txt"), result); + ASSERT_NE(strstr(result, "test.txt"), (char*)NULL); + log_debug("%s\n", result); } TEST(Path, abs_normalize) {