tutil.h 6.2 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TDENGINE_TUTIL_H
#define TDENGINE_TUTIL_H

#ifdef __cplusplus
extern "C" {
#endif

S
slguan 已提交
23 24
#include "os.h"
#include "tmd5.h"
H
hzcheng 已提交
25
#include "tcrc32c.h"
S
slguan 已提交
26
#include "taosdef.h"
H
hzcheng 已提交
27

S
slguan 已提交
28
#ifndef STDERR_FILENO
H
hjxilinx 已提交
29
#define STDERR_FILENO (2)
S
slguan 已提交
30
#endif
H
hzcheng 已提交
31

H
hjxilinx 已提交
32 33 34
#define FD_VALID(x) ((x) > STDERR_FILENO)
#define FD_INITIALIZER  ((int32_t)-1)

H
hzcheng 已提交
35
#define WCHAR wchar_t
H
hjxilinx 已提交
36

H
Haojun Liao 已提交
37 38 39 40 41 42
#define tfree(x)         \
  {                      \
    if (x) {             \
      free((void *)(x)); \
      x = 0;             \
    }                    \
H
hzcheng 已提交
43 44
  }

H
Haojun Liao 已提交
45 46
#define tstrncpy(dst, src, size)   \
  do {                             \
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
47
    strncpy((dst), (src), (size)); \
H
Haojun Liao 已提交
48 49
    (dst)[(size)-1] = 0;           \
  } while (0);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
50

H
hzcheng 已提交
51 52
#define tclose(x) taosCloseSocket(x)

H
TD-166  
hzcheng 已提交
53
// Pointer p drift right by b bytes
H
hzcheng 已提交
54
#define POINTER_SHIFT(p, b) ((void *)((char *)(p) + (b)))
H
Hui Li 已提交
55
#define POINTER_DISTANCE(p1, p2) ((char *)(p1) - (char *)(p2)) 
H
TD-166  
hzcheng 已提交
56

H
TD-166  
hzcheng 已提交
57
#ifndef NDEBUG
H
hzcheng 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#define ASSERT(x) assert(x)
#else
#define ASSERT(x)
#endif

#ifdef UNUSED
#undefine UNUSED
#endif
#define UNUSED(x) ((void)(x))

#ifdef UNUSED_FUNC
#undefine UNUSED_FUNC
#endif

#ifdef UNUSED_PARAM
#undef UNUSED_PARAM
#endif

#if defined(__GNUC__)
#define UNUSED_PARAM(x) _UNUSED##x __attribute__((unused))
#define UNUSED_FUNC __attribute__((unused))
#else
#define UNUSED_PARAM(x) x
#define UNUSED_FUNC
#endif

#ifdef tListLen
#undefine tListLen
#endif
#define tListLen(x) (sizeof(x) / sizeof((x)[0]))

#if defined(__GNUC__)
#define FORCE_INLINE inline __attribute__((always_inline))
#else
#define FORCE_INLINE
#endif

#define DEFAULT_UNICODE_ENCODEC "UCS-4LE"
S
slguan 已提交
96
  
H
hzcheng 已提交
97 98 99 100 101 102 103
#define DEFAULT_COMP(x, y)       \
  do {                           \
    if ((x) == (y)) {            \
      return 0;                  \
    } else {                     \
      return (x) < (y) ? -1 : 1; \
    }                            \
104
  } while (0)
H
hzcheng 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

#define ALIGN_NUM(n, align) (((n) + ((align)-1)) & (~((align)-1)))

// align to 8bytes
#define ALIGN8(n) ALIGN_NUM(n, 8)

#define MILLISECOND_PER_MINUTE (MILLISECOND_PER_SECOND * 60)
#define MILLISECOND_PER_HOUR   (MILLISECOND_PER_MINUTE * 60)
#define MILLISECOND_PER_DAY    (MILLISECOND_PER_HOUR * 24)
#define MILLISECOND_PER_WEEK   (MILLISECOND_PER_DAY * 7)
#define MILLISECOND_PER_MONTH  (MILLISECOND_PER_DAY * 30)
#define MILLISECOND_PER_YEAR   (MILLISECOND_PER_DAY * 365)

#define POW2(x) ((x) * (x))

120
uint32_t taosRand(void);
H
Hui Li 已提交
121

H
Hui Li 已提交
122 123
uint32_t trand(void);

H
[TD-92]  
Hui Li 已提交
124 125
size_t twcslen(const wchar_t *wcs);

H
hzcheng 已提交
126 127
int32_t strdequote(char *src);

H
Haojun Liao 已提交
128
size_t strtrim(char *src);
H
hzcheng 已提交
129

S
slguan 已提交
130
char *strnchr(char *haystack, char needle, int32_t len, bool skipquote);
H
hzcheng 已提交
131 132 133

char **strsplit(char *src, const char *delim, int32_t *num);

weixin_48148422's avatar
weixin_48148422 已提交
134
char* strtolower(char *dst, const char *src);
H
hzcheng 已提交
135 136 137

int64_t strnatoi(char *num, int32_t len);

H
Hui Li 已提交
138
//char* strreplace(const char* str, const char* pattern, const char* rep);
H
hzcheng 已提交
139

140 141
char *strbetween(char *string, char *begin, char *end);

H
hjxilinx 已提交
142
char *paGetToken(char *src, char **token, int32_t *tokenLen);
H
hzcheng 已提交
143 144 145 146 147 148 149 150 151

void taosMsleep(int32_t mseconds);

int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char hexstr[]);

int32_t taosHexStrToByteArray(char hexstr[], char bytes[]);

int32_t taosFileRename(char *fullPath, char *suffix, char delimiter, char **dstPath);

H
hjxilinx 已提交
152 153 154 155 156 157 158
/**
 *
 * @param fileNamePattern
 * @param dstPath
 */
void getTmpfilePath(const char *fileNamePattern, char *dstPath);

H
hjxilinx 已提交
159
bool taosMbsToUcs4(char *mbs, size_t mbs_len, char *ucs4, int32_t ucs4_max_len, size_t* len);
H
hzcheng 已提交
160

S
slguan 已提交
161 162
int tasoUcs4Compare(void* f1_ucs4, void *f2_ucs4, int bytes);

H
Haojun Liao 已提交
163 164
void taosRandStr(char* str, int32_t size);

165
int32_t taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs);
H
hzcheng 已提交
166

H
hjxilinx 已提交
167
bool taosValidateEncodec(const char *encodec);
H
hzcheng 已提交
168

S
slguan 已提交
169 170
bool taosGetVersionNumber(char *versionStr, int *versionNubmer);

171 172 173 174 175 176 177 178
static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, unsigned int inLen, char *target) {
  MD5_CTX context;
  MD5Init(&context);
  MD5Update(&context, inBuf, inLen);
  MD5Final(&context);
  memcpy(target, context.digest, TSDB_KEY_LEN);
}

S
slguan 已提交
179 180
int taosCheckVersion(char *input_client_version, char *input_server_version, int compared_segments);

S
slguan 已提交
181 182
char *taosIpStr(uint32_t ipInt);

S
slguan 已提交
183 184
uint32_t ip2uint(const char *const ip_addr);

S
slguan 已提交
185
void taosRemoveDir(char *rootDir);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
186
int  tmkdir(const char *pathname, mode_t mode); 
S
slguan 已提交
187

weixin_48148422's avatar
weixin_48148422 已提交
188 189 190 191 192 193
#define TAOS_ALLOC_MODE_DEFAULT 0
#define TAOS_ALLOC_MODE_RANDOM_FAIL 1
#define TAOS_ALLOC_MODE_DETECT_LEAK 2
void taosSetAllocMode(int mode, const char* path, bool autoDump);
void taosDumpMemoryLeak();

H
TD-100  
hzcheng 已提交
194 195 196 197 198 199 200
void * tmalloc(size_t size);
void * tcalloc(size_t nmemb, size_t size);
size_t tsizeof(void *ptr);
void   tmemset(void *ptr, int c);
void * trealloc(void *ptr, size_t size);
void   tzfree(void *ptr);

weixin_48148422's avatar
weixin_48148422 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
#ifdef TAOS_MEM_CHECK

void *  taos_malloc(size_t size, const char *file, uint32_t line);
void *  taos_calloc(size_t num, size_t size, const char *file, uint32_t line);
void *  taos_realloc(void *ptr, size_t size, const char *file, uint32_t line);
void    taos_free(void *ptr, const char *file, uint32_t line);
char *  taos_strdup(const char *str, const char *file, uint32_t line);
char *  taos_strndup(const char *str, size_t size, const char *file, uint32_t line);
ssize_t taos_getline(char **lineptr, size_t *n, FILE *stream, const char *file, uint32_t line);

#ifndef TAOS_MEM_CHECK_IMPL

#define malloc(size) taos_malloc(size, __FILE__, __LINE__)
#define calloc(num, size) taos_calloc(num, size, __FILE__, __LINE__)
#define realloc(ptr, size) taos_realloc(ptr, size, __FILE__, __LINE__)
#define free(ptr) taos_free(ptr, __FILE__, __LINE__)
#define strdup(str) taos_strdup(str, __FILE__, __LINE__)
#define strndup(str, size) taos_strndup(str, size, __FILE__, __LINE__)
weixin_48148422's avatar
weixin_48148422 已提交
219
#define getline(lineptr, n, stream) taos_getline(lineptr, n, stream, __FILE__, __LINE__)
S
slguan 已提交
220

weixin_48148422's avatar
weixin_48148422 已提交
221 222 223
#endif  // TAOS_MEM_CHECK_IMPL

#endif // TAOS_MEM_CHECK
S
slguan 已提交
224

H
hzcheng 已提交
225 226 227 228
#ifdef __cplusplus
}
#endif

S
slguan 已提交
229
#endif  // TDENGINE_TUTIL_H