osString.c 5.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#define ALLOW_FORBID_FUNC
S
Shengliang Guan 已提交
17 18 19
#define _DEFAULT_SOURCE
#include "os.h"

wafwerar's avatar
wafwerar 已提交
20 21 22
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
#include "iconv.h"
#endif
S
Shengliang Guan 已提交
23

wafwerar's avatar
wafwerar 已提交
24 25 26
extern int wcwidth(wchar_t c);
extern int wcswidth(const wchar_t *s, size_t n);

wafwerar's avatar
wafwerar 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#ifdef WINDOWS
char *strsep(char **stringp, const char *delim) {
  char *      s;
  const char *spanp;
  int32_t     c, sc;
  char *tok;
  if ((s = *stringp) == NULL)
    return (NULL);
  for (tok = s;;) {
    c = *s++;
    spanp = delim;
    do {
      if ((sc = *spanp++) == c) {
        if (c == 0)
          s = NULL;
        else
          s[-1] = 0;
        *stringp = s;
        return (tok);
      }
    } while (sc != 0);
  }
  /* NOTREACHED */
}
/* Duplicate a string, up to at most size characters */
char *strndup(const char *s, size_t size) {
  size_t l;
  char *s2;
  l = strlen(s);
  if (l > size) l=size;
  s2 = malloc(l+1);
  if (s2) {
    strncpy(s2, s, l);
    s2[l] = '\0';
  }
  return s2;
}
/* Copy no more than N characters of SRC to DEST, returning the address of
   the terminating '\0' in DEST, if any, or else DEST + N.  */
char *stpncpy (char *dest, const char *src, size_t n) {
  size_t size = strnlen (src, n);
  memcpy (dest, src, size);
  dest += size;
  if (size == n)
    return dest;
  return memset (dest, '\0', n - size);
}
#endif

76
int64_t taosStr2int64(const char *str) {
S
Shengliang Guan 已提交
77 78 79 80
  char *endptr = NULL;
  return strtoll(str, &endptr, 10);
}

wafwerar's avatar
wafwerar 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes) {
  for (int32_t i = 0; i < bytes; i += sizeof(TdUcs4)) {
    int32_t f1 = *(int32_t *)((char *)f1_ucs4 + i);
    int32_t f2 = *(int32_t *)((char *)f2_ucs4 + i);

    if ((f1 == 0 && f2 != 0) || (f1 != 0 && f2 == 0)) {
      return f1 - f2;
    } else if (f1 == 0 && f2 == 0) {
      return 0;
    }

    if (f1 != f2) {
      return f1 - f2;
    }
  }
S
Shengliang Guan 已提交
96

wafwerar's avatar
wafwerar 已提交
97 98 99 100
  return 0;

#if 0
  int32_t ucs4_max_len = bytes + 4;
wafwerar's avatar
wafwerar 已提交
101 102
  char *f1_mbs = taosMemoryCalloc(bytes, 1);
  char *f2_mbs = taosMemoryCalloc(bytes, 1);
wafwerar's avatar
wafwerar 已提交
103 104 105 106 107 108 109
  if (taosUcs4ToMbs(f1_ucs4, ucs4_max_len, f1_mbs) < 0) {
    return -1;
  }
  if (taosUcs4ToMbs(f2_ucs4, ucs4_max_len, f2_mbs) < 0) {
    return -1;
  }
  int32_t ret = strcmp(f1_mbs, f2_mbs);
wafwerar's avatar
wafwerar 已提交
110 111
  taosMemoryFree(f1_mbs);
  taosMemoryFree(f2_mbs);
wafwerar's avatar
wafwerar 已提交
112 113 114 115
  return ret;
#endif
}

wafwerar's avatar
wafwerar 已提交
116 117

TdUcs4* tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4) {
wafwerar's avatar
wafwerar 已提交
118
  assert(taosMemorySize(target_ucs4)>=len_ucs4*sizeof(TdUcs4));
119
  return memcpy(target_ucs4, source_ucs4, len_ucs4*sizeof(TdUcs4));
wafwerar's avatar
wafwerar 已提交
120 121
}

wafwerar's avatar
wafwerar 已提交
122 123
int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs) {
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
124
  printf("Nchar cannot be read and written without iconv, please install iconv library and recompile TDengine.\n");
wafwerar's avatar
wafwerar 已提交
125 126
  return -1;
#else
S
Shengliang Guan 已提交
127 128 129 130 131 132 133 134 135 136
  iconv_t cd = iconv_open(tsCharset, DEFAULT_UNICODE_ENCODEC);
  size_t  ucs4_input_len = ucs4_max_len;
  size_t  outLen = ucs4_max_len;
  if (iconv(cd, (char **)&ucs4, &ucs4_input_len, &mbs, &outLen) == -1) {
    iconv_close(cd);
    return -1;
  }

  iconv_close(cd);
  return (int32_t)(ucs4_max_len - outLen);
wafwerar's avatar
wafwerar 已提交
137
#endif
S
Shengliang Guan 已提交
138 139
}

wafwerar's avatar
wafwerar 已提交
140 141
bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len) { 
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
142
  printf("Nchar cannot be read and written without iconv, please install iconv library and recompile TDengine.\n");
wafwerar's avatar
wafwerar 已提交
143 144
  return -1;
#else
S
Shengliang Guan 已提交
145 146 147 148
  memset(ucs4, 0, ucs4_max_len);
  iconv_t cd = iconv_open(DEFAULT_UNICODE_ENCODEC, tsCharset);
  size_t  ucs4_input_len = mbsLength;
  size_t  outLeft = ucs4_max_len;
wafwerar's avatar
wafwerar 已提交
149
  if (iconv(cd, (char**)&mbs, &ucs4_input_len, (char**)&ucs4, &outLeft) == -1) {
S
Shengliang Guan 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162
    iconv_close(cd);
    return false;
  }

  iconv_close(cd);
  if (len != NULL) {
    *len = (int32_t)(ucs4_max_len - outLeft);
    if (*len < 0) {
      return false;
    }
  }

  return true;
wafwerar's avatar
wafwerar 已提交
163
#endif
S
Shengliang Guan 已提交
164 165 166
}

bool taosValidateEncodec(const char *encodec) {
wafwerar's avatar
wafwerar 已提交
167
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
168 169
  printf("Nchar cannot be read and written without iconv, please install iconv library and recompile TDengine.\n");
  return true;
wafwerar's avatar
wafwerar 已提交
170
#else
S
Shengliang Guan 已提交
171 172 173 174 175 176 177 178 179 180
  iconv_t cd = iconv_open(encodec, DEFAULT_UNICODE_ENCODEC);
  if (cd == (iconv_t)(-1)) {
    return false;
  }

  iconv_close(cd);
  return true;
#endif
}

wafwerar's avatar
wafwerar 已提交
181 182
int32_t taosUcs4len(TdUcs4 *ucs4) {
  TdUcs4 *wstr = (TdUcs4 *)ucs4;
S
Shengliang Guan 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  if (NULL == wstr) {
    return 0;
  }

  int32_t n = 0;
  while (1) {
    if (0 == *wstr++) {
      break;
    }
    n++;
  }

  return n;
}

wafwerar's avatar
wafwerar 已提交
198 199 200 201 202 203 204 205 206 207 208
int32_t taosWcharWidth(TdWchar wchar) { return wcwidth(wchar); }

int32_t taosWcharsWidth(TdWchar *pWchar, int32_t size) { return wcswidth(pWchar, size); }

int32_t taosMbToWchar(TdWchar *pWchar, const char *pStr, int32_t size) { return mbtowc(pWchar, pStr, size); }

int32_t taosMbsToWchars(TdWchar *pWchars, const char *pStrs, int32_t size) { return mbstowcs(pWchars, pStrs, size); }

int32_t taosWcharToMb(char *pStr, TdWchar wchar) { return wctomb(pStr, wchar); }

int32_t taosWcharsToMbs(char *pStrs, TdWchar *pWchars, int32_t size) { return wcstombs(pStrs, pWchars, size); }