c_locale_win32.c

来自「stl的源码」· C语言 代码 · 共 1,787 行 · 第 1/4 页

C
1,787
字号
          if (*(cur_char + 3) == 'd') {            if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'A'; }            buf_size += 2;            cur_char += 3;          }          else {            if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'a'; }            buf_size += 2;            cur_char += 2;          }        }        else {          if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'd'; }          buf_size += 2;          cur_char++;        }      }      else {        if (cur_output && (cur_output + 3 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'd'; }        buf_size += 3;      }    }    break;    case 'M':    {      if (*(cur_char + 1) == 'M') {        if (cur_output && (cur_output + 2 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (*(cur_char + 2) == 'M') {          if (*(cur_char + 3) == 'M') {            if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'B'; }            buf_size += 2;            cur_char += 3;          }          else {            if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'b'; }            buf_size += 2;            cur_char += 2;          }        }        else {          if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'm'; }          buf_size += 2;          cur_char++;        }      }      else {        if (cur_output && (cur_output + 3 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'm'; }        buf_size += 3;      }    }    break;    case 'y':    {      if (*(cur_char + 1) == 'y') {        if (cur_output && (cur_output + 2 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (*(cur_char + 2) == 'y' && *(cur_char + 3) == 'y') {          if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'Y'; }          buf_size += 2;          cur_char += 3;        }        else {          if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'y'; }          buf_size += 2;          cur_char++;        }      }      else {        if (cur_output && (cur_output + 3 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'y'; }        buf_size += 3;      }    }    break;    case '%':    {      if (cur_output && (cur_output + 2 > end_output)) {        *cur_output = 0;        return ++buf_size;      }      if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '%'; }      buf_size += 2;    }    break;    case '\'':    {      ++cur_char;      while (*cur_char != '\'' && *cur_char != 0 && (cur_output == NULL || cur_output != end_output)) {        if (cur_output) { *cur_output++ = *cur_char; }        ++cur_char;        buf_size += 1;      }    }    break;    default:    {      if (cur_output) { *(cur_output++) = *cur_char; }      buf_size += 1;    }    break;    }    if (*cur_char == 0) break;    ++cur_char;  }  if (!cur_output || cur_output != end_output) {    if (cur_output) *cur_output = 0;    buf_size += 1;  }  else {    /* We trunc result */    *(--cur_output) = 0;  }  return buf_size;}static int __ConvertTime(const char *NTTime, char *buffer, int buf_size) {  const char *cur_char;  char *cur_output, *end_output;  cur_char = NTTime;  cur_output = buffer;  end_output = cur_output + buf_size;  buf_size = 0;  while (*cur_char) {    switch(*cur_char) {    case 'h':      if (*(cur_char + 1) == 'h') {        if (cur_output && (cur_output + 2 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'I'; }        buf_size += 2;        ++cur_char;      }      else {        if (cur_output && (cur_output + 3 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'I'; }        buf_size += 3;      }      break;    case 'H':      if (*(cur_char + 1) == 'H') {        if (cur_output && (cur_output + 2 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'H'; }        buf_size += 2;        ++cur_char;      }      else {        if (cur_output && (cur_output + 3 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'H'; }        buf_size += 3;      }      break;    case 'm':      if (*(cur_char + 1) == 'm') {        if (cur_output && (cur_output + 2 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'M'; }        buf_size += 2;        cur_char++;      }      else {        if (cur_output && (cur_output + 3 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'M'; }        buf_size += 3;      }      break;    case 's':      if (*(cur_char + 1) == 's') {        if (cur_output && (cur_output + 2 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'S'; }        buf_size += 2;        ++cur_char;      }      else {        if (cur_output && (cur_output + 3 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'S'; }        buf_size += 3;      }      break;    case 't':      if (*(cur_char + 1) == 't')        ++cur_char;      if (cur_output && (cur_output + 2 > end_output)) {        *cur_output = 0;        return ++buf_size;      }      if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'p'; }      buf_size += 2;      break;    case '%':      if (cur_output && (cur_output + 2 > end_output)) {        *cur_output = 0;        return ++buf_size;      }      if (cur_output) { *(cur_output++)='%'; *(cur_output++)='%'; }      buf_size += 2;      break;    case '\'':      ++cur_char;      while (*cur_char != '\'' && *cur_char != 0 && (!cur_output || (cur_output != end_output))) {        if (cur_output) *cur_output++ = *cur_char;        ++cur_char;        buf_size += 1;      }      break;    default:      if (cur_output) { *(cur_output++) = *cur_char; }      buf_size += 1;      break;    }    if (*cur_char == 0) break;    ++cur_char;  }  if (!cur_output || cur_output != end_output) {    if (cur_output) *cur_output = 0;    buf_size += 1;  }  else {    /* We trunc result */    *(--cur_output) = 0;  }  return buf_size;}_Locale_time_t* _Locale_time_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) {  int size, month, dayofweek;  size_t length;  char fmt80[80];  wchar_t wbuf80[80];  _Locale_time_t *ltime = (_Locale_time_t*)malloc(sizeof(_Locale_time_t));    if (!ltime) { *__err_code = _STLP_LOC_NO_MEMORY; return ltime; }  memset(ltime, 0, sizeof(_Locale_time_t));  if (__GetLCIDFromName(name, &ltime->lc.id, ltime->cp, lc_hint) == -1)  { free(ltime); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }#if defined (__BORLANDC__)  if ( ltime->lc.id == INVARIANT_LCID && name[0] == 'C' && name[1] == 0 )  { ltime->lc.id = 0x409; }#endif  for (month = LOCALE_SMONTHNAME1; month <= LOCALE_SMONTHNAME12; ++month) { /* Small hack :-) */    size = GetLocaleInfoA(ltime->lc.id, month, NULL, 0);    ltime->month[month - LOCALE_SMONTHNAME1] = (char*)malloc(size);    if (!ltime->month[month - LOCALE_SMONTHNAME1])    { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, month, ltime->month[month - LOCALE_SMONTHNAME1], size, wbuf80, 80);  }  for (month = LOCALE_SABBREVMONTHNAME1; month <= LOCALE_SABBREVMONTHNAME12; ++month) {    size = GetLocaleInfoA(ltime->lc.id, month, NULL, 0);    ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1] = (char*)malloc(size);    if (!ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1])    { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, month, ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1], size, wbuf80, 80);  }  for (dayofweek = LOCALE_SDAYNAME1; dayofweek <= LOCALE_SDAYNAME7; ++dayofweek) {    int dayindex = ( dayofweek != LOCALE_SDAYNAME7 ) ? dayofweek - LOCALE_SDAYNAME1 + 1 : 0;    size = GetLocaleInfoA(ltime->lc.id, dayofweek, NULL, 0);    ltime->dayofweek[dayindex] = (char*)malloc(size);    if (!ltime->dayofweek[dayindex])    { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, dayofweek, ltime->dayofweek[dayindex], size, wbuf80, 80);  }  for (dayofweek = LOCALE_SABBREVDAYNAME1; dayofweek <= LOCALE_SABBREVDAYNAME7; ++dayofweek) {    int dayindex = ( dayofweek != LOCALE_SABBREVDAYNAME7 ) ? dayofweek - LOCALE_SABBREVDAYNAME1 + 1 : 0;    size = GetLocaleInfoA(ltime->lc.id, dayofweek, NULL, 0);    ltime->abbrev_dayofweek[dayindex] = (char*)malloc(size);    if (!ltime->abbrev_dayofweek[dayindex])    { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, dayofweek, ltime->abbrev_dayofweek[dayindex], size, wbuf80, 80);  }  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_SSHORTDATE, fmt80, 80, wbuf80, 80);  size = __ConvertDate(fmt80, NULL, 0);  ltime->date_format = (char*)malloc(size);  if (!ltime->date_format)  { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }  __ConvertDate(fmt80, ltime->date_format, size);  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_SLONGDATE, fmt80, 80, wbuf80, 80);  size = __ConvertDate(fmt80, NULL, 0);  ltime->long_date_format = (char*)malloc(size);  if (!ltime->long_date_format)  { _Locale_time_destroy(ltime);*__err_code = _STLP_LOC_NO_MEMORY; return NULL; }  __ConvertDate(fmt80, ltime->long_date_format, size);  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_STIMEFORMAT, fmt80, 80, wbuf80, 80);  size = __ConvertTime(fmt80, NULL, 0);  ltime->time_format = (char*)malloc(size);  if (!ltime->time_format)  { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }  __ConvertTime(fmt80, ltime->time_format, size);  /* NT doesn't provide this information, we must simulate. */  length = strlen(ltime->date_format) + strlen(ltime->time_format) + 1 /* space */ + 1 /* trailing 0 */;  ltime->date_time_format = (char*)malloc(length);  if (!ltime->date_time_format)  { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }  _STLP_STRCPY(ltime->date_time_format, length, ltime->date_format);  _STLP_STRCAT(ltime->date_time_format, length, " ");  _STLP_STRCAT(ltime->date_time_format, length, ltime->time_format);  /* NT doesn't provide this information, we must simulate. */  length = strlen(ltime->long_date_format) + strlen(ltime->time_format) + 1 /* space */ + 1 /* trailing 0 */;  ltime->long_date_time_format = (char*)malloc(length);  if (!ltime->long_date_time_format)  { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }  _STLP_STRCPY(ltime->long_date_time_format, length, ltime->long_date_format);  _STLP_STRCAT(ltime->long_date_time_format, length, " ");  _STLP_STRCAT(ltime->long_date_time_format, length, ltime->time_format);  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_S1159, ltime->am, 9, wbuf80, 80);  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_S2359, ltime->pm, 9, wbuf80, 80);  return ltime;}_Locale_collate_t* _Locale_collate_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) {  _Locale_collate_t *lcol = (_Locale_collate_t*)malloc(sizeof(_Locale_collate_t));  if (!lcol) { *__err_code = _STLP_LOC_NO_MEMORY; return lcol; }  memset(lcol, 0, sizeof(_Locale_collate_t));  if (__GetLCIDFromName(name, &lcol->lc.id, lcol->cp, lc_hint) == -1)  { free(lcol); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }#if defined (__BORLANDC__)  if ( lcol->lc.id == INVARIANT_LCID && name[0] == 'C' && name[1] == 0 )  { lcol->lc.id = 0x409; }#endif  return lcol;}_Locale_monetary_t* _Locale_monetary_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) {  char *GroupingBuffer;  int BufferSize;  char FracDigits[3];  wchar_t wbuf[6];  _Locale_monetary_t *lmon = (_Locale_monetary_t*)malloc(sizeof(_Locale_monetary_t));  if (!lmon) { *__err_code = _STLP_LOC_NO_MEMORY; return lmon; }  memset(lmon, 0, sizeof(_Locale_monetary_t));  if (__GetLCIDFromName(name, &lmon->lc.id, lmon->cp, lc_hint) == -1)  { free(lmon); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }  if (lmon->lc.id != INVARIANT_LCID) {    /* Extract information about monetary system */    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SDECIMAL, lmon->decimal_point, 4, wbuf, 6);    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_STHOUSAND, lmon->thousands_sep, 4, wbuf, 6);    BufferSize = GetLocaleInfoA(lmon->lc.id, LOCALE_SGROUPING, NULL, 0);    GroupingBuffer = (char*)malloc(BufferSize);    if (!GroupingBuffer)    { lmon->grouping = NULL; *__err_code = _STLP_LOC_NO_MEMORY; return lmon; }    GetLocaleInfoA(lmon->lc.id, LOCALE_SGROUPING, GroupingBuffer, BufferSize);    __FixGrouping(GroupingBuffer);    lmon->grouping = GroupingBuffer;    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SCURRENCY, lmon->curr_symbol, 6, wbuf, 6);    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SNEGATIVESIGN, lmon->negative_sign, 5, wbuf, 6);    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SPOSITIVESIGN, lmon->positive_sign, 5, wbuf, 6);    GetLocaleInfoA(lmon->lc.id, LOCALE_ICURRDIGITS, FracDigits, 3);    lmon->frac_digits = atoi(FracDigits);    GetLocaleInfoA(lmon->lc.id, LOCALE_IINTLCURRDIGITS, FracDigits, 3);    lmon->int_frac_digits = atoi(FracDigits);    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SINTLSYMBOL, lmon->int_curr_symbol, 5, wbuf, 6);    /* Even if Platform SDK documentation says that the returned symbol should     * be a 3 letters symbol followed by a seperation character, experimentation     * has shown that no seperation character is ever appended. We are adding it     * ourself to conform to the POSIX specification.     */    if (lmon->int_curr_symbol[3] == 0) {      lmon->int_curr_symbol[3] = ' ';      lmon->int_curr_symbol[4] = 0;    }  }  /* else it is already ok */  return lmon;}struct _Locale_messages* _Locale_messages_create(const char *name, _Locale_lcid_t* lc_hint, int *__err_code) {  /* The Win32 API has no support for messages facet */  _STLP_MARK_PARAMETER_AS_UNUSED(name)  _STLP_MARK_PARAMETER_AS_UNUSED(lc_hint)  *__err_code = _STLP_LOC_UNSUPPORTED_FACET_CATEGORY;  return NULL;}static const char* _Locale_common_default(char* buf) {  char cp[MAX_CP_LEN + 1];  int CodePage = __GetDefaultCP(LOCALE_USER_DEFAULT);  my_ltoa(CodePage, cp);  return __GetLocaleName(LOCALE_USER_DEFAULT, cp, buf);}const char* _Locale_ctype_default(char* buf)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?