Skip to content

Commit

Permalink
add path_expand_vars
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Dec 23, 2023
1 parent 901dd67 commit f1092d2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 最新动态

2023/12/23
* 增加函数path\_expand\_vars

2023/12/22
* 增加data\_reader\_http。

Expand Down
23 changes: 23 additions & 0 deletions src/tkc/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,26 @@ const char* path_abs_normalize_with_root(const char* root, const char* rel_filen
return NULL;
}
}

ret_t path_expand_vars(const char* filename, char* result, int32_t size) {
str_t str;
char path[MAX_PATH + 1] = {0};
return_value_if_fail(filename != NULL && result != NULL && size > 0, RET_BAD_PARAMS);

str_init(&str, MAX_PATH+1);
str_set(&str, filename);

fs_get_user_storage_path(os_fs(), path);
str_replace(&str, "${user_dir}", path);

fs_get_temp_path(os_fs(), path);
str_replace(&str, "${temp_dir}", path);

path_app_root(path);
str_replace(&str, "${app_dir}", path);

tk_strncpy_s(result, size, str.str, str.size);
str_reset(&str);

return RET_OK;
}
16 changes: 16 additions & 0 deletions src/tkc/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ const char* path_prepend_user_storage_path(char full_path[MAX_PATH + 1], const c
*/
ret_t path_abs_normalize(const char* filename, char* result, int32_t size);

/**
* @method path_expand_vars
* 将路径中的变量展开。
*
*```
* ${app_dir} -> app 文件目录。
* ${temp_dir} -> 临时文件目录。
* ${user_dir} -> 用户目录。
*```
* @param {const char*} filename 相对路径。
* @param {char*} result 用于返回结果。
* @param {int32_t} size 缓冲区大小。
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
*/
ret_t path_expand_vars(const char* filename, char* result, int32_t size);

/**
* @method path_abs_normalize_with_root
* 将相对路径转换为绝对路径并规范路径字符形式。
Expand Down
28 changes: 28 additions & 0 deletions tests/path_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tkc/path.h"
#include "tkc/fs.h"
#include "gtest/gtest.h"
#include <string>

Expand Down Expand Up @@ -298,3 +299,30 @@ TEST(Path, abs_normalize_with_root) {
ASSERT_EQ(path_abs_normalize_with_root("/path", "abc", result), result);
ASSERT_EQ(strstr(result, "abc") != NULL, TRUE);
}

TEST(Path, expand_vars) {
char path[MAX_PATH + 1] = {0};
char result[MAX_PATH + 1] = {0};
ASSERT_EQ(path_expand_vars("abc", result, sizeof(result)), RET_OK);
ASSERT_EQ(strstr(result, "abc") != NULL, TRUE);

ASSERT_EQ(path_expand_vars("${app_dir}", result, sizeof(result)), RET_OK);
path_app_root(path);
ASSERT_STREQ(result, path);

ASSERT_EQ(path_expand_vars("${app_dir}/abc/test.txt", result, sizeof(result)), RET_OK);
path_app_root(path);
ASSERT_EQ(string(result), string(path) + "/abc/test.txt");

ASSERT_EQ(path_expand_vars("${temp_dir}", result, sizeof(result)), RET_OK);
fs_get_temp_path(os_fs(), path);
ASSERT_STREQ(result, path);

ASSERT_EQ(path_expand_vars("${user_dir}", result, sizeof(result)), RET_OK);
fs_get_user_storage_path(os_fs(), path);
ASSERT_STREQ(result, path);

ASSERT_EQ(path_expand_vars("${user_dir}/abc/test.txt", result, sizeof(result)), RET_OK);
fs_get_user_storage_path(os_fs(), path);
ASSERT_EQ(string(result), string(path) + "/abc/test.txt");
}

0 comments on commit f1092d2

Please sign in to comment.