From 07ba689835de8c623c821782b4c01e5a7227f348 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Fri, 19 Mar 2021 09:17:11 +0800 Subject: [PATCH] add FilenameString type and macro --- HISTORY | 3 ++- src/common_define.h | 14 ++++++++++++ src/shared_func.c | 52 ++++++++++++++++++++++----------------------- src/system_info.c | 4 ++-- src/system_info.h | 2 +- 5 files changed, 45 insertions(+), 30 deletions(-) diff --git a/HISTORY b/HISTORY index b209cfe..1b4e216 100644 --- a/HISTORY +++ b/HISTORY @@ -1,8 +1,9 @@ -Version 1.49 2021-03-16 +Version 1.49 2021-03-19 * add macros: FC_ABS and FC_NEGATIVE * uniq_skiplist.c: add uniq_skiplist_pair struct and init function * add functions: fc_mkdirs and str_replace + * add FilenameString type and macro Version 1.48 2021-02-01 * fast_buffer.[hc]: add function fast_buffer_append_binary diff --git a/src/common_define.h b/src/common_define.h index 5f29f5f..32a9c71 100644 --- a/src/common_define.h +++ b/src/common_define.h @@ -21,6 +21,7 @@ #include #include #include +#include #ifdef WIN32 @@ -224,6 +225,12 @@ typedef struct pthread_cond_t cond; } pthread_lock_cond_pair_t; +typedef struct +{ + char buff[PATH_MAX]; + string_t s; +} FilenameString; + typedef void (*FreeDataFunc)(void *ptr); typedef int (*CompareFunc)(void *p1, void *p2); typedef void* (*MallocFunc)(size_t size); @@ -283,6 +290,13 @@ typedef void* (*MallocFunc)(size_t size); #define FC_IS_NULL_STRING(s) ((s)->str == NULL) #define FC_IS_EMPTY_STRING(s) ((s)->len == 0) +#define FC_INIT_FILENAME_STRING(filename) \ + FC_SET_STRING_EX((filename).s, (filename).buff, 0) + +#define FC_FILENAME_STRING_OBJ(filename) ((filename).s) +#define FC_FILENAME_STRING_PTR(filename) ((filename).buff) +#define FC_FILENAME_BUFFER_SIZE(filename) sizeof((filename).buff) + #define fc_compare_string(s1, s2) fc_string_compare(s1, s2) static inline int fc_string_compare(const string_t *s1, const string_t *s2) diff --git a/src/shared_func.c b/src/shared_func.c index df3cb3f..43c39be 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -516,34 +516,34 @@ char *bin2hex(const char *s, const int len, char *szHexBuff) char *hex2bin(const char *s, char *szBinBuff, int *nDestLen) { - char buff[3]; - char *pSrc; - int nSrcLen; - char *pDest; - char *pDestEnd; - - nSrcLen = strlen(s); - if (nSrcLen == 0) - { - *nDestLen = 0; - szBinBuff[0] = '\0'; - return szBinBuff; - } + char buff[3]; + char *pSrc; + int nSrcLen; + char *pDest; + char *pDestEnd; - *nDestLen = nSrcLen / 2; - pSrc = (char *)s; - buff[2] = '\0'; + nSrcLen = strlen(s); + if (nSrcLen == 0) + { + *nDestLen = 0; + szBinBuff[0] = '\0'; + return szBinBuff; + } - pDestEnd = szBinBuff + (*nDestLen); - for (pDest=szBinBuff; pDest