util
This commit is contained in:
parent
1398172370
commit
869d3c1742
|
@ -13,18 +13,18 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _TD_UTIL_UTIL_H
|
#ifndef _TD_UTIL_UTIL_H_
|
||||||
#define _TD_UTIL_UTIL_H
|
#define _TD_UTIL_UTIL_H_
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
#include "tcrc32c.h"
|
#include "tcrc32c.h"
|
||||||
#include "tdef.h"
|
#include "tdef.h"
|
||||||
#include "tmd5.h"
|
#include "tmd5.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
int32_t strdequote(char *src);
|
int32_t strdequote(char *src);
|
||||||
int32_t strndequote(char *dst, const char *z, int32_t len);
|
int32_t strndequote(char *dst, const char *z, int32_t len);
|
||||||
int32_t strRmquote(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);
|
tMD5Update(&context, inBuf, (unsigned int)len);
|
||||||
tMD5Final(&context);
|
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],
|
sprintf(target, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", context.digest[0],
|
||||||
context.digest[3], context.digest[4], context.digest[5], context.digest[6], context.digest[7],
|
context.digest[1], context.digest[2], context.digest[3], context.digest[4], context.digest[5],
|
||||||
context.digest[8], context.digest[9], context.digest[10], context.digest[11], context.digest[12],
|
context.digest[6], context.digest[7], context.digest[8], context.digest[9], context.digest[10],
|
||||||
context.digest[13], context.digest[14], context.digest[15]);
|
context.digest[11], context.digest[12], context.digest[13], context.digest[14], context.digest[15]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /*_TD_UTIL_UTIL_H*/
|
#endif /*_TD_UTIL_UTIL_H_*/
|
||||||
|
|
|
@ -13,8 +13,8 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "os.h"
|
#define _DEFAULT_SOURCE
|
||||||
#include "tdef.h"
|
#include "tutil.h"
|
||||||
|
|
||||||
int32_t strdequote(char *z) {
|
int32_t strdequote(char *z) {
|
||||||
if (z == NULL) {
|
if (z == NULL) {
|
||||||
|
@ -47,38 +47,38 @@ int32_t strdequote(char *z) {
|
||||||
return j + 1; // only one quote, do nothing
|
return j + 1; // only one quote, do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t strRmquote(char *z, int32_t len){
|
int32_t strRmquote(char *z, int32_t len) {
|
||||||
// delete escape character: \\, \', \"
|
// delete escape character: \\, \', \"
|
||||||
char delim = z[0];
|
char delim = z[0];
|
||||||
if (delim != '\'' && delim != '\"') {
|
if (delim != '\'' && delim != '\"') {
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t cnt = 0;
|
int32_t cnt = 0;
|
||||||
int32_t j = 0;
|
int32_t j = 0;
|
||||||
for (uint32_t k = 1; k < len - 1; ++k) {
|
for (uint32_t k = 1; k < len - 1; ++k) {
|
||||||
if (z[k] == '\\' || (z[k] == delim && z[k + 1] == delim)) {
|
if (z[k] == '\\' || (z[k] == delim && z[k + 1] == delim)) {
|
||||||
if (z[k] == '\\' && z[k + 1] == '_') {
|
if (z[k] == '\\' && z[k + 1] == '_') {
|
||||||
//match '_' self
|
// match '_' self
|
||||||
} else {
|
} else {
|
||||||
z[j] = z[k + 1];
|
z[j] = z[k + 1];
|
||||||
cnt++;
|
cnt++;
|
||||||
j++;
|
j++;
|
||||||
k++;
|
k++;
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
z[j] = z[k];
|
|
||||||
j++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
z[j] = 0;
|
z[j] = z[k];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
|
||||||
return len - 2 - cnt;
|
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);
|
assert(dst != NULL);
|
||||||
if (z == NULL || len == 0) {
|
if (z == NULL || len == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -90,7 +90,7 @@ int32_t strndequote(char *dst, const char* z, int32_t len) {
|
||||||
while (z[i] != 0) {
|
while (z[i] != 0) {
|
||||||
if (z[i] == quote) {
|
if (z[i] == quote) {
|
||||||
if (z[i + 1] == quote) {
|
if (z[i + 1] == quote) {
|
||||||
dst[j++] = (char) quote;
|
dst[j++] = (char)quote;
|
||||||
i++;
|
i++;
|
||||||
} else {
|
} else {
|
||||||
dst[j++] = 0;
|
dst[j++] = 0;
|
||||||
|
@ -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) {
|
char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote) {
|
||||||
for (int32_t i = 0; i < len; ++i) {
|
for (int32_t i = 0; i < len; ++i) {
|
||||||
|
|
||||||
// skip the needle in quote, jump to the end of quoted string
|
// skip the needle in quote, jump to the end of quoted string
|
||||||
if (skipquote && (haystack[i] == '\'' || haystack[i] == '"')) {
|
if (skipquote && (haystack[i] == '\'' || haystack[i] == '"')) {
|
||||||
char quote = haystack[i++];
|
char quote = haystack[i++];
|
||||||
while(i < len && haystack[i++] != quote);
|
while (i < len && haystack[i++] != quote)
|
||||||
|
;
|
||||||
if (i >= len) {
|
if (i >= len) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -186,9 +186,9 @@ char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* strtolower(char *dst, const char *src) {
|
char *strtolower(char *dst, const char *src) {
|
||||||
int32_t esc = 0;
|
int32_t esc = 0;
|
||||||
char quote = 0, *p = dst, c;
|
char quote = 0, *p = dst, c;
|
||||||
|
|
||||||
assert(dst != NULL);
|
assert(dst != NULL);
|
||||||
|
|
||||||
|
@ -213,9 +213,9 @@ char* strtolower(char *dst, const char *src) {
|
||||||
return dst;
|
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;
|
int32_t esc = 0;
|
||||||
char quote = 0, *p = dst, c;
|
char quote = 0, *p = dst, c;
|
||||||
|
|
||||||
assert(dst != NULL);
|
assert(dst != NULL);
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
|
@ -243,7 +243,7 @@ char* strntolower(char *dst, const char *src, int32_t n) {
|
||||||
return dst;
|
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;
|
char *p = dst, c;
|
||||||
|
|
||||||
assert(dst != NULL);
|
assert(dst != NULL);
|
||||||
|
@ -346,8 +346,8 @@ char *strbetween(char *string, char *begin, char *end) {
|
||||||
char *result = NULL;
|
char *result = NULL;
|
||||||
char *_begin = strstr(string, begin);
|
char *_begin = strstr(string, begin);
|
||||||
if (_begin != NULL) {
|
if (_begin != NULL) {
|
||||||
char *_end = strstr(_begin + strlen(begin), end);
|
char *_end = strstr(_begin + strlen(begin), end);
|
||||||
int32_t size = (int32_t)(_end - _begin);
|
int32_t size = (int32_t)(_end - _begin);
|
||||||
if (_end != NULL && size > 0) {
|
if (_end != NULL && size > 0) {
|
||||||
result = (char *)calloc(1, size);
|
result = (char *)calloc(1, size);
|
||||||
memcpy(result, _begin + strlen(begin), size - +strlen(begin));
|
memcpy(result, _begin + strlen(begin), size - +strlen(begin));
|
||||||
|
@ -401,11 +401,12 @@ int32_t taosHexStrToByteArray(char hexstr[], char bytes[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
char *taosIpStr(uint32_t ipInt) {
|
char *taosIpStr(uint32_t ipInt) {
|
||||||
static char ipStrArray[3][30];
|
static char ipStrArray[3][30];
|
||||||
static int32_t ipStrIndex = 0;
|
static int32_t ipStrIndex = 0;
|
||||||
|
|
||||||
char *ipStr = ipStrArray[(ipStrIndex++) % 3];
|
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));
|
sprintf(ipStr, "%u.%u.%u.%u", ipInt & 0xFF, (ipInt >> 8) & 0xFF, (ipInt >> 16) & 0xFF, (uint8_t)(ipInt >> 24));
|
||||||
return ipStr;
|
return ipStr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue