osString.c 4.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);

27
int64_t taosStr2int64(const char *str) {
S
Shengliang Guan 已提交
28 29 30 31
  char *endptr = NULL;
  return strtoll(str, &endptr, 10);
}

wafwerar's avatar
wafwerar 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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 已提交
47

wafwerar's avatar
wafwerar 已提交
48 49 50 51
  return 0;

#if 0
  int32_t ucs4_max_len = bytes + 4;
wafwerar's avatar
wafwerar 已提交
52 53
  char *f1_mbs = taosMemoryCalloc(bytes, 1);
  char *f2_mbs = taosMemoryCalloc(bytes, 1);
wafwerar's avatar
wafwerar 已提交
54 55 56 57 58 59 60
  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 已提交
61 62
  taosMemoryFree(f1_mbs);
  taosMemoryFree(f2_mbs);
wafwerar's avatar
wafwerar 已提交
63 64 65 66
  return ret;
#endif
}

wafwerar's avatar
wafwerar 已提交
67 68

TdUcs4* tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4) {
wafwerar's avatar
wafwerar 已提交
69
  assert(taosMemorySize(target_ucs4)>=len_ucs4*sizeof(TdUcs4));
70
  return memcpy(target_ucs4, source_ucs4, len_ucs4*sizeof(TdUcs4));
wafwerar's avatar
wafwerar 已提交
71 72
}

wafwerar's avatar
wafwerar 已提交
73 74
int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs) {
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
75
  printf("Nchar cannot be read and written without iconv, please install iconv library and recompile TDengine.\n");
wafwerar's avatar
wafwerar 已提交
76 77
  return -1;
#else
S
Shengliang Guan 已提交
78 79 80 81 82 83 84 85 86 87
  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 已提交
88
#endif
S
Shengliang Guan 已提交
89 90
}

wafwerar's avatar
wafwerar 已提交
91 92
bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4_max_len, int32_t *len) { 
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
93
  printf("Nchar cannot be read and written without iconv, please install iconv library and recompile TDengine.\n");
wafwerar's avatar
wafwerar 已提交
94 95
  return -1;
#else
S
Shengliang Guan 已提交
96 97 98 99
  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 已提交
100
  if (iconv(cd, (char**)&mbs, &ucs4_input_len, (char**)&ucs4, &outLeft) == -1) {
S
Shengliang Guan 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113
    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 已提交
114
#endif
S
Shengliang Guan 已提交
115 116 117
}

bool taosValidateEncodec(const char *encodec) {
wafwerar's avatar
wafwerar 已提交
118
#ifdef DISALLOW_NCHAR_WITHOUT_ICONV
119 120
  printf("Nchar cannot be read and written without iconv, please install iconv library and recompile TDengine.\n");
  return true;
wafwerar's avatar
wafwerar 已提交
121
#else
S
Shengliang Guan 已提交
122 123 124 125 126 127 128 129 130 131
  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 已提交
132 133
int32_t taosUcs4len(TdUcs4 *ucs4) {
  TdUcs4 *wstr = (TdUcs4 *)ucs4;
S
Shengliang Guan 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  if (NULL == wstr) {
    return 0;
  }

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

  return n;
}

wafwerar's avatar
wafwerar 已提交
149 150 151 152 153 154 155 156 157 158 159
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); }