From 80b751980bb7a555536710631a93c0d958ebea28 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Mon, 14 Mar 2022 17:10:43 +0800 Subject: [PATCH] add function fc_format_path --- HISTORY | 3 +++ src/shared_func.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ src/shared_func.h | 9 +++++++ 3 files changed, 73 insertions(+) diff --git a/HISTORY b/HISTORY index 3df4a2f..ed3fe86 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.57 2022-03-14 + * add function fc_format_path + Version 1.56 2022-03-09 * add function fc_gettid * function normalize_path: NULL from parameter for getcwd diff --git a/src/shared_func.c b/src/shared_func.c index 087703f..1d924f6 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -1136,6 +1136,67 @@ void chopPath(char *filePath) } } +int fc_format_path(const string_t *src, string_t *dest, const int size) +{ +#define MAX_SUBDIR_COUNT 128 + const bool ignore_empty = true; + string_t input; + string_t subdirs[MAX_SUBDIR_COUNT]; + char full_filename[PATH_MAX]; + string_t *sub; + string_t *end; + char *p; + int count; + + if (src->len == 0) + { + logError("file: "__FILE__", line: %d, " + "input path is empty!", __LINE__); + return EINVAL; + } + + if (src->str[0] == '/') + { + input = *src; + } + else + { + input.str = full_filename; + input.len = normalize_path(NULL, src->str, + full_filename, sizeof(full_filename)); + } + + if (size <= input.len) + { + logError("file: "__FILE__", line: %d, " + "dest buffer is too small! buffer size: %d <= " + "expected: %d", __LINE__, size, input.len); + return EOVERFLOW; + } + + p = dest->str; + count = split_string_ex(&input, '/', subdirs, + MAX_SUBDIR_COUNT, ignore_empty); + if (count == 0) + { + *p++ = '/'; + } + else + { + end = subdirs + count; + for (sub=subdirs; substr, sub->len); + p += sub->len; + } + } + + *p = '\0'; + dest->len = p - dest->str; + return 0; +} + int getFileContent1(int fd, const char *filename, char **buff, int64_t *file_size) { diff --git a/src/shared_func.h b/src/shared_func.h index 5945015..3ecf877 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -589,6 +589,15 @@ int get_time_item_from_str(const char *pValue, const char *item_name, */ void chopPath(char *filePath); +/** format the path + * parameters: + * src: the input path + * dest: the output path + * size: the max size of dest path + * return: error no , 0 success, != 0 fail +*/ +int fc_format_path(const string_t *src, string_t *dest, const int size); + /** get file content by fd * parameters: * fd: the file descriptor