c_locale_win32.c

来自「symbian 上的stl_port进过编译的。」· C语言 代码 · 共 1,983 行 · 第 1/5 页

C
1,983
字号
    int BufferSize;    _Locale_numeric_t *lnum = (_Locale_numeric_t*)malloc(sizeof(_Locale_numeric_t));    if (!lnum) return lnum; /* MS normal behavior for 'new' */    __Extract_locale_name(name, LC_NUMERIC, cname);    if (__GetLCIDFromName(cname, &lnum->lc.id, lnum->cp, lc_hint) == -1)    { free(lnum); return NULL; }    __GetLocaleInfoUsingACP(lnum->lc.id, lnum->cp, LOCALE_SDECIMAL, lnum->decimal_point, 4);    __GetLocaleInfoUsingACP(lnum->lc.id, lnum->cp, LOCALE_STHOUSAND, lnum->thousands_sep, 4);    BufferSize = GetLocaleInfoA(lnum->lc.id, LOCALE_SGROUPING, NULL, 0);    GroupingBuffer = (char*)malloc(BufferSize);    if (!GroupingBuffer) { lnum->grouping = NULL; return lnum; }    GetLocaleInfoA(lnum->lc.id, LOCALE_SGROUPING, GroupingBuffer, BufferSize);    __FixGrouping(GroupingBuffer);    lnum->grouping = GroupingBuffer;    return lnum;  }static int __ConvertDate(const char *NTDate, char *buffer, int buf_size) {  /* This function will return an incomplete buffer if buffer is not long enough */  const char *cur_char;  char *cur_output, *end_output;  /* Correct time format. */  cur_char = NTDate;  cur_output = buffer;  end_output = cur_output + buf_size;  buf_size = 0;  while (*cur_char) {    if (cur_output && (cur_output == end_output)) break;    switch (*cur_char) {    case 'd':    {      if (*(cur_char + 1) == 'd') {        if (cur_output && (cur_output + 2 > end_output)) {          *cur_output = 0;          return ++buf_size;        }        if (*(cur_char + 2) == 'd') {          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;}void* _Locale_time_create(const char * name, _Locale_lcid_t* lc_hint) {    int size, month, dayofweek;    size_t length;    char fmt80[80];    char cname[_Locale_MAX_SIMPLE_NAME];    _Locale_time_t *ltime=(_Locale_time_t*)malloc(sizeof(_Locale_time_t));    if (!ltime) return ltime;    memset(ltime, 0, sizeof(_Locale_time_t));    __Extract_locale_name(name, LC_TIME, cname);    if (__GetLCIDFromName(cname, &ltime->lc.id, ltime->cp, lc_hint) == -1)    { free(ltime); return NULL; }    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); return NULL; }      __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, month, ltime->month[month - LOCALE_SMONTHNAME1], size);    }    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); return NULL; }      __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, month, ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1], size);    }  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); return NULL; }      __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, dayofweek, ltime->dayofweek[dayindex], size);    }  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); return NULL; }      __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, dayofweek, ltime->abbrev_dayofweek[dayindex], size);    }    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_SSHORTDATE, fmt80, 80);    size = __ConvertDate(fmt80, NULL, 0);    ltime->date_format = (char*)malloc(size);    if (!ltime->date_format) { _Locale_time_destroy(ltime); return NULL; }    __ConvertDate(fmt80, ltime->date_format, size);    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_SLONGDATE, fmt80, 80);    size = __ConvertDate(fmt80, NULL, 0);    ltime->long_date_format = (char*)malloc(size);    if (!ltime->long_date_format) { _Locale_time_destroy(ltime); return NULL; }    __ConvertDate(fmt80, ltime->long_date_format, size);    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_STIMEFORMAT, fmt80, 80);    size = __ConvertTime(fmt80, NULL, 0);    ltime->time_format = (char*)malloc(size);    if (!ltime->time_format) { _Locale_time_destroy(ltime); 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); return NULL; }    _STLP_STRCPY2(ltime->date_time_format, length, ltime->date_format);    _STLP_STRCAT2(ltime->date_time_format, length, " ");    _STLP_STRCAT2(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); return NULL; }    _STLP_STRCPY2(ltime->long_date_time_format, length, ltime->long_date_format);    _STLP_STRCAT2(ltime->long_date_time_format, length, " ");    _STLP_STRCAT2(ltime->long_date_time_format, length, ltime->time_format);    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_S1159, ltime->am, 9);    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_S2359, ltime->pm, 9);    return ltime;  }  void* _Locale_collate_create(const char * name, _Locale_lcid_t* lc_hint) {    char cname[_Locale_MAX_SIMPLE_NAME];    _Locale_collate_t *lcol=(_Locale_collate_t*)malloc(sizeof(_Locale_collate_t));

⌨️ 快捷键说明

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