diff --git a/include/util/tutil.h b/include/util/tutil.h index c315948702..e4123d585b 100644 --- a/include/util/tutil.h +++ b/include/util/tutil.h @@ -13,18 +13,18 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_UTIL_H -#define _TD_UTIL_UTIL_H - -#ifdef __cplusplus -extern "C" { -#endif +#ifndef _TD_UTIL_UTIL_H_ +#define _TD_UTIL_UTIL_H_ #include "os.h" #include "tcrc32c.h" #include "tdef.h" #include "tmd5.h" +#ifdef __cplusplus +extern "C" { +#endif + int32_t strdequote(char *src); int32_t strndequote(char *dst, const char *z, int32_t len); int32_t strRmquote(char *z, int32_t len); @@ -60,14 +60,14 @@ static FORCE_INLINE void taosEncryptPass_c(uint8_t *inBuf, size_t len, char *tar tMD5Update(&context, inBuf, (unsigned int)len); tMD5Final(&context); - sprintf(target, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", context.digest[0], context.digest[1], context.digest[2], - context.digest[3], context.digest[4], context.digest[5], context.digest[6], context.digest[7], - context.digest[8], context.digest[9], context.digest[10], context.digest[11], context.digest[12], - context.digest[13], context.digest[14], context.digest[15]); + sprintf(target, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", context.digest[0], + context.digest[1], context.digest[2], context.digest[3], context.digest[4], context.digest[5], + context.digest[6], context.digest[7], context.digest[8], context.digest[9], context.digest[10], + context.digest[11], context.digest[12], context.digest[13], context.digest[14], context.digest[15]); } #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_UTIL_H*/ +#endif /*_TD_UTIL_UTIL_H_*/ diff --git a/source/util/src/tutil.c b/source/util/src/tutil.c index 58a2e57f7c..7bd671a56c 100644 --- a/source/util/src/tutil.c +++ b/source/util/src/tutil.c @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#include "os.h" -#include "tdef.h" +#define _DEFAULT_SOURCE +#include "tutil.h" int32_t strdequote(char *z) { if (z == NULL) { @@ -47,38 +47,38 @@ int32_t strdequote(char *z) { return j + 1; // only one quote, do nothing } -int32_t strRmquote(char *z, int32_t len){ - // delete escape character: \\, \', \" - char delim = z[0]; - if (delim != '\'' && delim != '\"') { - return len; - } - - int32_t cnt = 0; - int32_t j = 0; - for (uint32_t k = 1; k < len - 1; ++k) { - if (z[k] == '\\' || (z[k] == delim && z[k + 1] == delim)) { - if (z[k] == '\\' && z[k + 1] == '_') { - //match '_' self - } else { - z[j] = z[k + 1]; - cnt++; - j++; - k++; - continue; - } +int32_t strRmquote(char *z, int32_t len) { + // delete escape character: \\, \', \" + char delim = z[0]; + if (delim != '\'' && delim != '\"') { + return len; + } + + int32_t cnt = 0; + int32_t j = 0; + for (uint32_t k = 1; k < len - 1; ++k) { + if (z[k] == '\\' || (z[k] == delim && z[k + 1] == delim)) { + if (z[k] == '\\' && z[k + 1] == '_') { + // match '_' self + } else { + z[j] = z[k + 1]; + cnt++; + j++; + k++; + continue; } - - z[j] = z[k]; - j++; } - - z[j] = 0; - - return len - 2 - cnt; + + z[j] = z[k]; + j++; + } + + z[j] = 0; + + return len - 2 - cnt; } -int32_t strndequote(char *dst, const char* z, int32_t len) { +int32_t strndequote(char *dst, const char *z, int32_t len) { assert(dst != NULL); if (z == NULL || len == 0) { return 0; @@ -90,7 +90,7 @@ int32_t strndequote(char *dst, const char* z, int32_t len) { while (z[i] != 0) { if (z[i] == quote) { if (z[i + 1] == quote) { - dst[j++] = (char) quote; + dst[j++] = (char)quote; i++; } else { dst[j++] = 0; @@ -139,7 +139,7 @@ size_t strtrim(char *z) { } else if (j != i) { z[i] = 0; } - + return i; } @@ -168,11 +168,11 @@ char **strsplit(char *z, const char *delim, int32_t *num) { char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote) { for (int32_t i = 0; i < len; ++i) { - // skip the needle in quote, jump to the end of quoted string if (skipquote && (haystack[i] == '\'' || haystack[i] == '"')) { char quote = haystack[i++]; - while(i < len && haystack[i++] != quote); + while (i < len && haystack[i++] != quote) + ; if (i >= len) { return NULL; } @@ -186,9 +186,9 @@ char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote) { return NULL; } -char* strtolower(char *dst, const char *src) { +char *strtolower(char *dst, const char *src) { int32_t esc = 0; - char quote = 0, *p = dst, c; + char quote = 0, *p = dst, c; assert(dst != NULL); @@ -213,9 +213,9 @@ char* strtolower(char *dst, const char *src) { return dst; } -char* strntolower(char *dst, const char *src, int32_t n) { +char *strntolower(char *dst, const char *src, int32_t n) { int32_t esc = 0; - char quote = 0, *p = dst, c; + char quote = 0, *p = dst, c; assert(dst != NULL); if (n == 0) { @@ -243,7 +243,7 @@ char* strntolower(char *dst, const char *src, int32_t n) { return dst; } -char* strntolower_s(char *dst, const char *src, int32_t n) { +char *strntolower_s(char *dst, const char *src, int32_t n) { char *p = dst, c; assert(dst != NULL); @@ -265,7 +265,7 @@ char* strntolower_s(char *dst, const char *src, int32_t n) { char *paGetToken(char *string, char **token, int32_t *tokenLen) { char quote = 0; - + while (*string != 0) { if (*string == ' ' || *string == '\t') { ++string; @@ -346,8 +346,8 @@ char *strbetween(char *string, char *begin, char *end) { char *result = NULL; char *_begin = strstr(string, begin); if (_begin != NULL) { - char *_end = strstr(_begin + strlen(begin), end); - int32_t size = (int32_t)(_end - _begin); + char *_end = strstr(_begin + strlen(begin), end); + int32_t size = (int32_t)(_end - _begin); if (_end != NULL && size > 0) { result = (char *)calloc(1, size); memcpy(result, _begin + strlen(begin), size - +strlen(begin)); @@ -401,11 +401,12 @@ int32_t taosHexStrToByteArray(char hexstr[], char bytes[]) { } char *taosIpStr(uint32_t ipInt) { - static char ipStrArray[3][30]; + static char ipStrArray[3][30]; static int32_t ipStrIndex = 0; char *ipStr = ipStrArray[(ipStrIndex++) % 3]; - //sprintf(ipStr, "0x%x:%u.%u.%u.%u", ipInt, ipInt & 0xFF, (ipInt >> 8) & 0xFF, (ipInt >> 16) & 0xFF, (uint8_t)(ipInt >> 24)); + // sprintf(ipStr, "0x%x:%u.%u.%u.%u", ipInt, ipInt & 0xFF, (ipInt >> 8) & 0xFF, (ipInt >> 16) & 0xFF, (uint8_t)(ipInt + // >> 24)); sprintf(ipStr, "%u.%u.%u.%u", ipInt & 0xFF, (ipInt >> 8) & 0xFF, (ipInt >> 16) & 0xFF, (uint8_t)(ipInt >> 24)); return ipStr; }