1/*
2 * Copyright (c) 1999
3 * Silicon Graphics Computer Systems, Inc.
4 *
5 * Copyright (c) 1999
6 * Boris Fomitchev
7 *
8 * Written 2000
9 * Anton Lapach
10 *
11 * This material is provided "as is", with absolutely no warranty expressed
12 * or implied. Any use is at your own risk.
13 *
14 * Permission to use or copy this software for any purpose is hereby granted
15 * without fee, provided the above notices are retained on all copies.
16 * Permission to modify the code and to distribute modified code is granted,
17 * provided the above notices are retained, and a notice that the code was
18 * modified is included with the above copyright notice.
19 *
20 */
21
22#include <limits.h>
23#if defined (_STLP_MSVC) || defined (__ICL)
24#  include <memory.h>
25#endif
26#include <string.h>
27#include <locale.h>
28#include <stdlib.h>
29#include <stdio.h>
30
31#if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
32#  define _STLP_STRCPY(D, DS, S) strcpy_s(D, DS, S)
33#  define _STLP_STRNCPY(D, DS, S, C) strncpy_s(D, DS, S, C)
34#  define _STLP_STRCAT(D, DS, S) strcat_s(D, DS, S)
35#else
36#  define _STLP_STRCPY(D, DS, S) strcpy(D, S)
37#  define _STLP_STRNCPY(D, DS, S, C) strncpy(D, S, C)
38#  define _STLP_STRCAT(D, DS, S) strcat(D, S)
39#endif
40
41#if defined (__cplusplus)
42extern "C" {
43#endif
44
45/* Framework functions */
46/*
47  locale :: "lang[_country[.code_page]]"
48  | ".code_page"
49  | ""
50  | NULL
51
52*/
53
54typedef struct _LOCALECONV {
55  const char* name;
56  const char* abbrev;
57} LOCALECONV;
58
59#define MAX_LANG_LEN        64  /* max language name length */
60#define MAX_CTRY_LEN        64  /* max country name length */
61#define MAX_MODIFIER_LEN    0   /* max modifier name length - n/a */
62#define MAX_LC_LEN          (MAX_LANG_LEN+MAX_CTRY_LEN+MAX_MODIFIER_LEN+3)
63                                /* max entire locale string length */
64#define MAX_CP_LEN          5   /* max code page name length */
65
66#if !defined (LANG_INVARIANT)
67#  define LANG_INVARIANT 0x7f
68#  define _STLP_LANG_INVARIANT_DEFINED
69#endif
70
71#ifndef CP_UTF7
72#  define CP_UTF7 65000
73#endif
74
75#ifndef CP_UTF8
76#  define CP_UTF8 65001
77#endif
78
79#define INVARIANT_LCID MAKELCID(MAKELANGID(LANG_INVARIANT, SUBLANG_NEUTRAL), SORT_DEFAULT)
80
81static const char *_C_name = "C";
82
83/*  non-NLS language string table */
84static LOCALECONV __rg_language[] = {
85  {"american",                    "ENU"},
86  {"american english",            "ENU"},
87  {"american-english",            "ENU"},
88  {"australian",                  "ENA"},
89  {"belgian",                     "NLB"},
90  {"canadian",                    "ENC"},
91  {"chh",                         "ZHH"},
92  {"chi",                         "ZHI"},
93  {"chinese",                     "CHS"},
94  {"chinese-hongkong",            "ZHH"},
95  {"chinese-simplified",          "CHS"},
96  {"chinese-singapore",           "ZHI"},
97  {"chinese-traditional",         "CHT"},
98  {"dutch-belgian",               "NLB"},
99  {"english-american",            "ENU"},
100  {"english-aus",                 "ENA"},
101  {"english-belize",              "ENL"},
102  {"english-can",                 "ENC"},
103  {"english-caribbean",           "ENB"},
104  {"english-ire",                 "ENI"},
105  {"english-jamaica",             "ENJ"},
106  {"english-nz",                  "ENZ"},
107  {"english-south africa",        "ENS"},
108  {"english-trinidad y tobago",   "ENT"},
109  {"english-uk",                  "ENG"},
110  {"english-us",                  "ENU"},
111  {"english-usa",                 "ENU"},
112  {"french-belgian",              "FRB"},
113  {"french-canadian",             "FRC"},
114  {"french-luxembourg",           "FRL"},
115  {"french-swiss",                "FRS"},
116  {"german-austrian",             "DEA"},
117  {"german-lichtenstein",         "DEC"},
118  {"german-luxembourg",           "DEL"},
119  {"german-swiss",                "DES"},
120  {"irish-english",               "ENI"},
121  {"italian-swiss",               "ITS"},
122  {"norwegian",                   "NOR"},
123  {"norwegian-bokmal",            "NOR"},
124  {"norwegian-nynorsk",           "NON"},
125  {"portuguese-brazilian",        "PTB"},
126  {"spanish-argentina",           "ESS"},
127  {"spanish-bolivia",             "ESB"},
128  {"spanish-chile",               "ESL"},
129  {"spanish-colombia",            "ESO"},
130  {"spanish-costa rica",          "ESC"},
131  {"spanish-dominican republic",  "ESD"},
132  {"spanish-ecuador",             "ESF"},
133  {"spanish-el salvador",         "ESE"},
134  {"spanish-guatemala",           "ESG"},
135  {"spanish-honduras",            "ESH"},
136  {"spanish-mexican",             "ESM"},
137  {"spanish-modern",              "ESN"},
138  {"spanish-nicaragua",           "ESI"},
139  {"spanish-panama",              "ESA"},
140  {"spanish-paraguay",            "ESZ"},
141  {"spanish-peru",                "ESR"},
142  {"spanish-puerto rico",         "ESU"},
143  {"spanish-uruguay",             "ESY"},
144  {"spanish-venezuela",           "ESV"},
145  {"swedish-finland",             "SVF"},
146  {"swiss",                       "DES"},
147  {"uk",                          "ENG"},
148  {"us",                          "ENU"},
149  {"usa",                         "ENU"}
150};
151
152/*  non-NLS country string table */
153static LOCALECONV __rg_country[] = {
154  {"america",                     "USA"},
155  {"britain",                     "GBR"},
156  {"china",                       "CHN"},
157  {"czech",                       "CZE"},
158  {"england",                     "GBR"},
159  {"great britain",               "GBR"},
160  {"holland",                     "NLD"},
161  {"hong-kong",                   "HKG"},
162  {"new-zealand",                 "NZL"},
163  {"nz",                          "NZL"},
164  {"pr china",                    "CHN"},
165  {"pr-china",                    "CHN"},
166  {"puerto-rico",                 "PRI"},
167  {"slovak",                      "SVK"},
168  {"south africa",                "ZAF"},
169  {"south korea",                 "KOR"},
170  {"south-africa",                "ZAF"},
171  {"south-korea",                 "KOR"},
172  {"trinidad & tobago",           "TTO"},
173  {"uk",                          "GBR"},
174  {"united-kingdom",              "GBR"},
175  {"united-states",               "USA"},
176  {"us",                          "USA"},
177};
178
179typedef struct _Locale_name_hint {
180  LCID id;
181} _Locale_lcid_t;
182
183typedef struct _Locale_ctype {
184  _Locale_lcid_t lc;
185  UINT cp;
186  unsigned short ctable[256];
187} _Locale_ctype_t;
188
189typedef struct _Locale_numeric {
190  _Locale_lcid_t lc;
191  char cp[MAX_CP_LEN + 1];
192  char decimal_point[4];
193  char thousands_sep[4];
194  char *grouping;
195} _Locale_numeric_t;
196
197typedef struct _Locale_time {
198  _Locale_lcid_t lc;
199  char cp[MAX_CP_LEN + 1];
200  char *month[12];
201  char *abbrev_month[12];
202  char *dayofweek[7];
203  char *abbrev_dayofweek[7];
204  char *date_time_format;
205  char *long_date_time_format;
206  char *date_format;
207  char *long_date_format;
208  char *time_format;
209  char am[9];
210  char pm[9];
211} _Locale_time_t;
212
213typedef struct _Locale_collate {
214  _Locale_lcid_t lc;
215  char cp[MAX_CP_LEN + 1];
216} _Locale_collate_t;
217
218typedef struct _Locale_monetary {
219  _Locale_lcid_t lc;
220  char cp[MAX_CP_LEN + 1];
221  char decimal_point[4];
222  char thousands_sep[4];
223  char *grouping;
224  char int_curr_symbol[5]; /* 3 + 1 + 1 */
225  char curr_symbol[6];
226  char negative_sign[5];
227  char positive_sign[5];
228  int frac_digits;
229  int int_frac_digits;
230} _Locale_monetary_t;
231
232/* Internal function */
233static void __FixGrouping(char *grouping);
234static const char* __ConvertName(const char* lname, LOCALECONV* ConvTable, int TableSize);
235static int __ParseLocaleString(const char* lname, char* lang, char* ctry, char* page);
236static int __GetLCID(const char* lang, const char* ctry, LCID* lcid);
237static int __GetLCIDFromName(const char* lname, LCID* lcid, char *cp, _Locale_lcid_t *hint);
238static char const* __GetLocaleName(LCID lcid, const char* cp, char* buf);
239static char const* __Extract_locale_name(const char* loc, const char* category, char* buf);
240static char const* __TranslateToSystem(const char* lname, char* buf, _Locale_lcid_t* hint, int *__err_code);
241static void __GetLocaleInfoUsingACP(LCID lcid, const char* cp, LCTYPE lctype, char* buf, int buf_size, wchar_t* wbuf, int wbuf_size);
242static int __intGetACP(LCID lcid);
243static int __intGetOCP(LCID lcid);
244static int __GetDefaultCP(LCID lcid);
245static char* __ConvertToCP(int from_cp, int to_cp, const char *from, size_t size, size_t *ret_buf_size);
246static void my_ltoa(long __x, char* buf);
247
248void my_ltoa(long __x, char* buf) {
249  char rbuf[64];
250  char* ptr = rbuf;
251
252  if (__x == 0)
253    *ptr++ = '0';
254  else {
255    for (; __x != 0; __x /= 10)
256      *ptr++ = (char)(__x % 10) + '0';
257  }
258  while(ptr > rbuf) *buf++ = *--ptr;
259  /* psw */
260  *buf = '\0';
261}
262
263#if defined (__cplusplus)
264_STLP_BEGIN_NAMESPACE
265extern "C" {
266#endif
267
268_Locale_lcid_t* _Locale_get_ctype_hint(_Locale_ctype_t* ltype)
269{ return (ltype != 0) ? &ltype->lc : 0; }
270_Locale_lcid_t* _Locale_get_numeric_hint(_Locale_numeric_t* lnumeric)
271{ return (lnumeric != 0) ? &lnumeric->lc : 0; }
272_Locale_lcid_t* _Locale_get_time_hint(_Locale_time_t* ltime)
273{ return (ltime != 0) ? &ltime->lc : 0; }
274_Locale_lcid_t* _Locale_get_collate_hint(_Locale_collate_t* lcollate)
275{ return (lcollate != 0) ? &lcollate->lc : 0; }
276_Locale_lcid_t* _Locale_get_monetary_hint(_Locale_monetary_t* lmonetary)
277{ return (lmonetary != 0) ? &lmonetary->lc : 0; }
278_Locale_lcid_t* _Locale_get_messages_hint(struct _Locale_messages* lmessages) {
279  _STLP_MARK_PARAMETER_AS_UNUSED(lmessages)
280  return 0;
281}
282
283#define MAP(x, y) if ((mask & x) != 0) ret |= (y)
284unsigned short MapCtypeMask(unsigned short mask) {
285  unsigned short ret = 0;
286  MAP(C1_UPPER, _Locale_UPPER | _Locale_PRINT);
287  MAP(C1_LOWER, _Locale_LOWER | _Locale_PRINT);
288  MAP(C1_DIGIT, _Locale_DIGIT | _Locale_PRINT);
289  MAP(C1_SPACE, _Locale_SPACE | _Locale_PRINT);
290  MAP(C1_PUNCT, _Locale_PUNCT | _Locale_PRINT);
291  /* MAP(C1_BLANK, ?); */
292  MAP(C1_XDIGIT, _Locale_XDIGIT | _Locale_PRINT);
293  MAP(C1_ALPHA, _Locale_ALPHA | _Locale_PRINT);
294  if ((mask & C1_CNTRL) != 0) { ret |= _Locale_CNTRL; ret &= ~_Locale_PRINT; }
295  return ret;
296}
297
298static void MapCtypeMasks(unsigned short *cur, unsigned short *end) {
299  for (; cur != end; ++cur) {
300    *cur = MapCtypeMask(*cur);
301  }
302}
303
304_Locale_ctype_t* _Locale_ctype_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) {
305  char cp_name[MAX_CP_LEN + 1];
306  int NativeCP;
307  unsigned char Buffer[256];
308  unsigned char *ptr;
309  CPINFO CPInfo;
310  int i;
311  wchar_t *wbuffer;
312  int BufferSize;
313
314  _Locale_ctype_t *ltype = (_Locale_ctype_t*)malloc(sizeof(_Locale_ctype_t));
315
316  if (!ltype) { *__err_code = _STLP_LOC_NO_MEMORY; return ltype; }
317  memset(ltype, 0, sizeof(_Locale_ctype_t));
318
319  if (__GetLCIDFromName(name, &ltype->lc.id, cp_name, lc_hint) == -1)
320  { free(ltype); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }
321
322#if defined (__BORLANDC__)
323  if ( ltype->lc.id == INVARIANT_LCID && name[0] == 'C' && name[1] == 0 )
324  { ltype->lc.id = 0x409; }
325#endif
326
327  ltype->cp = atoi(cp_name);
328
329  NativeCP = __GetDefaultCP(ltype->lc.id);
330
331  /* Make table with all characters. */
332  for (i = 0; i < 256; ++i) Buffer[i] = (unsigned char)i;
333
334  if (!GetCPInfo(NativeCP, &CPInfo)) { free(ltype); return NULL; }
335
336  if (CPInfo.MaxCharSize > 1) {
337    for (ptr = (unsigned char*)CPInfo.LeadByte; *ptr && *(ptr + 1); ptr+=2)
338      for (i = *ptr; i <= *(ptr + 1); ++i) Buffer[i] = 0;
339  }
340
341  if ((UINT)NativeCP != ltype->cp) {
342    OSVERSIONINFO ver_info;
343    ver_info.dwOSVersionInfoSize = sizeof(ver_info);
344    GetVersionEx(&ver_info);
345    if (ver_info.dwPlatformId == VER_PLATFORM_WIN32_NT) {
346      /* Convert character sequence to Unicode. */
347      BufferSize = MultiByteToWideChar(ltype->cp, MB_PRECOMPOSED, (const char*)Buffer, 256, NULL, 0);
348      if (!BufferSize) { free(ltype); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }
349      wbuffer = (wchar_t*)malloc(BufferSize * sizeof(wchar_t));
350      if (!wbuffer) { free(ltype); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
351      MultiByteToWideChar(ltype->cp, MB_PRECOMPOSED, (const char*)Buffer, 256, wbuffer, BufferSize);
352
353      GetStringTypeW(CT_CTYPE1, wbuffer, 256, ltype->ctable);
354      MapCtypeMasks(ltype->ctable, ltype->ctable + 256);
355      free(wbuffer);
356    }
357    else {
358      unsigned short ctable[256];
359      unsigned char TargetBuffer[256];
360      GetStringTypeA(ltype->lc.id, CT_CTYPE1, (const char*)Buffer, 256, ctable);
361
362      /* Convert character sequence to target code page. */
363      BufferSize = MultiByteToWideChar(NativeCP, MB_PRECOMPOSED, (const char*)Buffer, 256, NULL, 0);
364      if (!BufferSize) { free(ltype); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }
365      wbuffer = (wchar_t*)malloc(BufferSize * sizeof(wchar_t));
366      if (!wbuffer) { free(ltype); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
367      MultiByteToWideChar(NativeCP, MB_PRECOMPOSED, (const char*)Buffer, 256, wbuffer, BufferSize);
368      if (!WideCharToMultiByte(ltype->cp, WC_COMPOSITECHECK | WC_SEPCHARS, wbuffer, BufferSize, (char*)TargetBuffer, 256, NULL, FALSE))
369      { free(wbuffer); free(ltype); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }
370
371      free(wbuffer);
372
373      /* Translate ctype table. */
374      for (i = 0; i < 256; ++i) {
375        if (!TargetBuffer[i]) continue;
376        ltype->ctable[TargetBuffer[i]] = MapCtypeMask(ctable[i]);
377      }
378    }
379  }
380  else {
381    GetStringTypeA(ltype->lc.id, CT_CTYPE1, (const char*)Buffer, 256, ltype->ctable);
382    MapCtypeMasks(ltype->ctable, ltype->ctable + 256);
383  }
384  return ltype;
385}
386
387_Locale_numeric_t* _Locale_numeric_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) {
388  wchar_t wbuf[4];
389  char *GroupingBuffer;
390  int BufferSize;
391
392  _Locale_numeric_t *lnum = (_Locale_numeric_t*)malloc(sizeof(_Locale_numeric_t));
393  if (!lnum) { *__err_code = _STLP_LOC_NO_MEMORY; return lnum; }
394  memset(lnum, 0, sizeof(_Locale_numeric_t));
395
396  if (__GetLCIDFromName(name, &lnum->lc.id, lnum->cp, lc_hint) == -1)
397  { free(lnum); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }
398
399#if defined (__BORLANDC__)
400  if (lnum->lc.id != INVARIANT_LCID) {
401#endif
402  __GetLocaleInfoUsingACP(lnum->lc.id, lnum->cp, LOCALE_SDECIMAL, lnum->decimal_point, 4, wbuf, 4);
403  __GetLocaleInfoUsingACP(lnum->lc.id, lnum->cp, LOCALE_STHOUSAND, lnum->thousands_sep, 4, wbuf, 4);
404#if defined (__BORLANDC__)
405  }
406  else
407    lnum->decimal_point[0] = '.';
408#endif
409
410  if (lnum->lc.id != INVARIANT_LCID) {
411    BufferSize = GetLocaleInfoA(lnum->lc.id, LOCALE_SGROUPING, NULL, 0);
412    GroupingBuffer = (char*)malloc(BufferSize);
413    if (!GroupingBuffer) { free(lnum); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
414    GetLocaleInfoA(lnum->lc.id, LOCALE_SGROUPING, GroupingBuffer, BufferSize);
415    __FixGrouping(GroupingBuffer);
416    lnum->grouping = GroupingBuffer;
417  }
418  else {
419    lnum->grouping = (char*)malloc(1);
420    if (!lnum->grouping) { free(lnum); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
421    lnum->grouping[0] = 0;
422  }
423
424  return lnum;
425}
426
427static int __ConvertDate(const char *NTDate, char *buffer, int buf_size) {
428  /* This function will return an incomplete buffer if buffer is not long enough */
429  const char *cur_char;
430  char *cur_output, *end_output;
431
432  /* Correct time format. */
433  cur_char = NTDate;
434  cur_output = buffer;
435  end_output = cur_output + buf_size;
436  buf_size = 0;
437  while (*cur_char) {
438    if (cur_output && (cur_output == end_output)) break;
439    switch (*cur_char) {
440    case 'd':
441    {
442      if (*(cur_char + 1) == 'd') {
443        if (cur_output && (cur_output + 2 > end_output)) {
444          *cur_output = 0;
445          return ++buf_size;
446        }
447        if (*(cur_char + 2) == 'd') {
448          if (*(cur_char + 3) == 'd') {
449            if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'A'; }
450            buf_size += 2;
451            cur_char += 3;
452          }
453          else {
454            if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'a'; }
455            buf_size += 2;
456            cur_char += 2;
457          }
458        }
459        else {
460          if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'd'; }
461          buf_size += 2;
462          cur_char++;
463        }
464      }
465      else {
466        if (cur_output && (cur_output + 3 > end_output)) {
467          *cur_output = 0;
468          return ++buf_size;
469        }
470        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'd'; }
471        buf_size += 3;
472      }
473    }
474    break;
475    case 'M':
476    {
477      if (*(cur_char + 1) == 'M') {
478        if (cur_output && (cur_output + 2 > end_output)) {
479          *cur_output = 0;
480          return ++buf_size;
481        }
482        if (*(cur_char + 2) == 'M') {
483          if (*(cur_char + 3) == 'M') {
484            if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'B'; }
485            buf_size += 2;
486            cur_char += 3;
487          }
488          else {
489            if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'b'; }
490            buf_size += 2;
491            cur_char += 2;
492          }
493        }
494        else {
495          if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'm'; }
496          buf_size += 2;
497          cur_char++;
498        }
499      }
500      else {
501        if (cur_output && (cur_output + 3 > end_output)) {
502          *cur_output = 0;
503          return ++buf_size;
504        }
505        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'm'; }
506        buf_size += 3;
507      }
508    }
509    break;
510    case 'y':
511    {
512      if (*(cur_char + 1) == 'y') {
513        if (cur_output && (cur_output + 2 > end_output)) {
514          *cur_output = 0;
515          return ++buf_size;
516        }
517        if (*(cur_char + 2) == 'y' && *(cur_char + 3) == 'y') {
518          if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'Y'; }
519          buf_size += 2;
520          cur_char += 3;
521        }
522        else {
523          if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'y'; }
524          buf_size += 2;
525          cur_char++;
526        }
527      }
528      else {
529        if (cur_output && (cur_output + 3 > end_output)) {
530          *cur_output = 0;
531          return ++buf_size;
532        }
533        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'y'; }
534        buf_size += 3;
535      }
536    }
537    break;
538    case '%':
539    {
540      if (cur_output && (cur_output + 2 > end_output)) {
541        *cur_output = 0;
542        return ++buf_size;
543      }
544      if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '%'; }
545      buf_size += 2;
546    }
547    break;
548    case '\'':
549    {
550      ++cur_char;
551      while (*cur_char != '\'' && *cur_char != 0 && (cur_output == NULL || cur_output != end_output)) {
552        if (cur_output) { *cur_output++ = *cur_char; }
553        ++cur_char;
554        buf_size += 1;
555      }
556    }
557    break;
558    default:
559    {
560      if (cur_output) { *(cur_output++) = *cur_char; }
561      buf_size += 1;
562    }
563    break;
564    }
565    if (*cur_char == 0) break;
566    ++cur_char;
567  }
568
569  if (!cur_output || cur_output != end_output) {
570    if (cur_output) *cur_output = 0;
571    buf_size += 1;
572  }
573  else {
574    /* We trunc result */
575    *(--cur_output) = 0;
576  }
577
578  return buf_size;
579}
580
581static int __ConvertTime(const char *NTTime, char *buffer, int buf_size) {
582  const char *cur_char;
583  char *cur_output, *end_output;
584  cur_char = NTTime;
585  cur_output = buffer;
586  end_output = cur_output + buf_size;
587  buf_size = 0;
588  while (*cur_char) {
589    switch(*cur_char) {
590    case 'h':
591      if (*(cur_char + 1) == 'h') {
592        if (cur_output && (cur_output + 2 > end_output)) {
593          *cur_output = 0;
594          return ++buf_size;
595        }
596        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'I'; }
597        buf_size += 2;
598        ++cur_char;
599      }
600      else {
601        if (cur_output && (cur_output + 3 > end_output)) {
602          *cur_output = 0;
603          return ++buf_size;
604        }
605        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'I'; }
606        buf_size += 3;
607      }
608      break;
609    case 'H':
610      if (*(cur_char + 1) == 'H') {
611        if (cur_output && (cur_output + 2 > end_output)) {
612          *cur_output = 0;
613          return ++buf_size;
614        }
615        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'H'; }
616        buf_size += 2;
617        ++cur_char;
618      }
619      else {
620        if (cur_output && (cur_output + 3 > end_output)) {
621          *cur_output = 0;
622          return ++buf_size;
623        }
624        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'H'; }
625        buf_size += 3;
626      }
627      break;
628    case 'm':
629      if (*(cur_char + 1) == 'm') {
630        if (cur_output && (cur_output + 2 > end_output)) {
631          *cur_output = 0;
632          return ++buf_size;
633        }
634        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'M'; }
635        buf_size += 2;
636        cur_char++;
637      }
638      else {
639        if (cur_output && (cur_output + 3 > end_output)) {
640          *cur_output = 0;
641          return ++buf_size;
642        }
643        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'M'; }
644        buf_size += 3;
645      }
646      break;
647    case 's':
648      if (*(cur_char + 1) == 's') {
649        if (cur_output && (cur_output + 2 > end_output)) {
650          *cur_output = 0;
651          return ++buf_size;
652        }
653        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'S'; }
654        buf_size += 2;
655        ++cur_char;
656      }
657      else {
658        if (cur_output && (cur_output + 3 > end_output)) {
659          *cur_output = 0;
660          return ++buf_size;
661        }
662        if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = '#'; *(cur_output++) = 'S'; }
663        buf_size += 3;
664      }
665      break;
666    case 't':
667      if (*(cur_char + 1) == 't')
668        ++cur_char;
669      if (cur_output && (cur_output + 2 > end_output)) {
670        *cur_output = 0;
671        return ++buf_size;
672      }
673      if (cur_output) { *(cur_output++) = '%'; *(cur_output++) = 'p'; }
674      buf_size += 2;
675      break;
676    case '%':
677      if (cur_output && (cur_output + 2 > end_output)) {
678        *cur_output = 0;
679        return ++buf_size;
680      }
681      if (cur_output) { *(cur_output++)='%'; *(cur_output++)='%'; }
682      buf_size += 2;
683      break;
684    case '\'':
685      ++cur_char;
686      while (*cur_char != '\'' && *cur_char != 0 && (!cur_output || (cur_output != end_output))) {
687        if (cur_output) *cur_output++ = *cur_char;
688        ++cur_char;
689        buf_size += 1;
690      }
691      break;
692    default:
693      if (cur_output) { *(cur_output++) = *cur_char; }
694      buf_size += 1;
695      break;
696    }
697    if (*cur_char == 0) break;
698    ++cur_char;
699  }
700
701  if (!cur_output || cur_output != end_output) {
702    if (cur_output) *cur_output = 0;
703    buf_size += 1;
704  }
705  else {
706    /* We trunc result */
707    *(--cur_output) = 0;
708  }
709
710  return buf_size;
711}
712
713_Locale_time_t* _Locale_time_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) {
714  int size, month, dayofweek;
715  size_t length;
716  char fmt80[80];
717  wchar_t wbuf80[80];
718
719  _Locale_time_t *ltime = (_Locale_time_t*)malloc(sizeof(_Locale_time_t));
720
721  if (!ltime) { *__err_code = _STLP_LOC_NO_MEMORY; return ltime; }
722  memset(ltime, 0, sizeof(_Locale_time_t));
723
724  if (__GetLCIDFromName(name, &ltime->lc.id, ltime->cp, lc_hint) == -1)
725  { free(ltime); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }
726
727#if defined (__BORLANDC__)
728  if ( ltime->lc.id == INVARIANT_LCID && name[0] == 'C' && name[1] == 0 )
729  { ltime->lc.id = 0x409; }
730#endif
731
732  for (month = LOCALE_SMONTHNAME1; month <= LOCALE_SMONTHNAME12; ++month) { /* Small hack :-) */
733    size = GetLocaleInfoA(ltime->lc.id, month, NULL, 0);
734    ltime->month[month - LOCALE_SMONTHNAME1] = (char*)malloc(size);
735    if (!ltime->month[month - LOCALE_SMONTHNAME1])
736    { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
737    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, month, ltime->month[month - LOCALE_SMONTHNAME1], size, wbuf80, 80);
738  }
739
740  for (month = LOCALE_SABBREVMONTHNAME1; month <= LOCALE_SABBREVMONTHNAME12; ++month) {
741    size = GetLocaleInfoA(ltime->lc.id, month, NULL, 0);
742    ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1] = (char*)malloc(size);
743    if (!ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1])
744    { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
745    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, month, ltime->abbrev_month[month - LOCALE_SABBREVMONTHNAME1], size, wbuf80, 80);
746  }
747
748  for (dayofweek = LOCALE_SDAYNAME1; dayofweek <= LOCALE_SDAYNAME7; ++dayofweek) {
749    int dayindex = ( dayofweek != LOCALE_SDAYNAME7 ) ? dayofweek - LOCALE_SDAYNAME1 + 1 : 0;
750    size = GetLocaleInfoA(ltime->lc.id, dayofweek, NULL, 0);
751    ltime->dayofweek[dayindex] = (char*)malloc(size);
752    if (!ltime->dayofweek[dayindex])
753    { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
754    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, dayofweek, ltime->dayofweek[dayindex], size, wbuf80, 80);
755  }
756
757  for (dayofweek = LOCALE_SABBREVDAYNAME1; dayofweek <= LOCALE_SABBREVDAYNAME7; ++dayofweek) {
758    int dayindex = ( dayofweek != LOCALE_SABBREVDAYNAME7 ) ? dayofweek - LOCALE_SABBREVDAYNAME1 + 1 : 0;
759    size = GetLocaleInfoA(ltime->lc.id, dayofweek, NULL, 0);
760    ltime->abbrev_dayofweek[dayindex] = (char*)malloc(size);
761    if (!ltime->abbrev_dayofweek[dayindex])
762    { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
763    __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, dayofweek, ltime->abbrev_dayofweek[dayindex], size, wbuf80, 80);
764  }
765
766  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_SSHORTDATE, fmt80, 80, wbuf80, 80);
767  size = __ConvertDate(fmt80, NULL, 0);
768  ltime->date_format = (char*)malloc(size);
769  if (!ltime->date_format)
770  { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
771  __ConvertDate(fmt80, ltime->date_format, size);
772
773  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_SLONGDATE, fmt80, 80, wbuf80, 80);
774  size = __ConvertDate(fmt80, NULL, 0);
775  ltime->long_date_format = (char*)malloc(size);
776  if (!ltime->long_date_format)
777  { _Locale_time_destroy(ltime);*__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
778  __ConvertDate(fmt80, ltime->long_date_format, size);
779
780  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_STIMEFORMAT, fmt80, 80, wbuf80, 80);
781  size = __ConvertTime(fmt80, NULL, 0);
782  ltime->time_format = (char*)malloc(size);
783  if (!ltime->time_format)
784  { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
785  __ConvertTime(fmt80, ltime->time_format, size);
786
787  /* NT doesn't provide this information, we must simulate. */
788  length = strlen(ltime->date_format) + strlen(ltime->time_format) + 1 /* space */ + 1 /* trailing 0 */;
789  ltime->date_time_format = (char*)malloc(length);
790  if (!ltime->date_time_format)
791  { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
792  _STLP_STRCPY(ltime->date_time_format, length, ltime->date_format);
793  _STLP_STRCAT(ltime->date_time_format, length, " ");
794  _STLP_STRCAT(ltime->date_time_format, length, ltime->time_format);
795
796  /* NT doesn't provide this information, we must simulate. */
797  length = strlen(ltime->long_date_format) + strlen(ltime->time_format) + 1 /* space */ + 1 /* trailing 0 */;
798  ltime->long_date_time_format = (char*)malloc(length);
799  if (!ltime->long_date_time_format)
800  { _Locale_time_destroy(ltime); *__err_code = _STLP_LOC_NO_MEMORY; return NULL; }
801  _STLP_STRCPY(ltime->long_date_time_format, length, ltime->long_date_format);
802  _STLP_STRCAT(ltime->long_date_time_format, length, " ");
803  _STLP_STRCAT(ltime->long_date_time_format, length, ltime->time_format);
804
805  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_S1159, ltime->am, 9, wbuf80, 80);
806  __GetLocaleInfoUsingACP(ltime->lc.id, ltime->cp, LOCALE_S2359, ltime->pm, 9, wbuf80, 80);
807
808  return ltime;
809}
810
811_Locale_collate_t* _Locale_collate_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) {
812  _Locale_collate_t *lcol = (_Locale_collate_t*)malloc(sizeof(_Locale_collate_t));
813  if (!lcol) { *__err_code = _STLP_LOC_NO_MEMORY; return lcol; }
814  memset(lcol, 0, sizeof(_Locale_collate_t));
815
816  if (__GetLCIDFromName(name, &lcol->lc.id, lcol->cp, lc_hint) == -1)
817  { free(lcol); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }
818
819#if defined (__BORLANDC__)
820  if ( lcol->lc.id == INVARIANT_LCID && name[0] == 'C' && name[1] == 0 )
821  { lcol->lc.id = 0x409; }
822#endif
823
824  return lcol;
825}
826
827_Locale_monetary_t* _Locale_monetary_create(const char * name, _Locale_lcid_t* lc_hint, int *__err_code) {
828  char *GroupingBuffer;
829  int BufferSize;
830  char FracDigits[3];
831  wchar_t wbuf[6];
832
833  _Locale_monetary_t *lmon = (_Locale_monetary_t*)malloc(sizeof(_Locale_monetary_t));
834  if (!lmon) { *__err_code = _STLP_LOC_NO_MEMORY; return lmon; }
835  memset(lmon, 0, sizeof(_Locale_monetary_t));
836
837  if (__GetLCIDFromName(name, &lmon->lc.id, lmon->cp, lc_hint) == -1)
838  { free(lmon); *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }
839
840  if (lmon->lc.id != INVARIANT_LCID) {
841    /* Extract information about monetary system */
842    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SDECIMAL, lmon->decimal_point, 4, wbuf, 6);
843    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_STHOUSAND, lmon->thousands_sep, 4, wbuf, 6);
844
845    BufferSize = GetLocaleInfoA(lmon->lc.id, LOCALE_SGROUPING, NULL, 0);
846    GroupingBuffer = (char*)malloc(BufferSize);
847    if (!GroupingBuffer)
848    { lmon->grouping = NULL; *__err_code = _STLP_LOC_NO_MEMORY; return lmon; }
849    GetLocaleInfoA(lmon->lc.id, LOCALE_SGROUPING, GroupingBuffer, BufferSize);
850    __FixGrouping(GroupingBuffer);
851    lmon->grouping = GroupingBuffer;
852
853    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SCURRENCY, lmon->curr_symbol, 6, wbuf, 6);
854    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SNEGATIVESIGN, lmon->negative_sign, 5, wbuf, 6);
855    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SPOSITIVESIGN, lmon->positive_sign, 5, wbuf, 6);
856
857    GetLocaleInfoA(lmon->lc.id, LOCALE_ICURRDIGITS, FracDigits, 3);
858    lmon->frac_digits = atoi(FracDigits);
859
860    GetLocaleInfoA(lmon->lc.id, LOCALE_IINTLCURRDIGITS, FracDigits, 3);
861    lmon->int_frac_digits = atoi(FracDigits);
862
863    __GetLocaleInfoUsingACP(lmon->lc.id, lmon->cp, LOCALE_SINTLSYMBOL, lmon->int_curr_symbol, 5, wbuf, 6);
864    /* Even if Platform SDK documentation says that the returned symbol should
865     * be a 3 letters symbol followed by a seperation character, experimentation
866     * has shown that no seperation character is ever appended. We are adding it
867     * ourself to conform to the POSIX specification.
868     */
869    if (lmon->int_curr_symbol[3] == 0) {
870      lmon->int_curr_symbol[3] = ' ';
871      lmon->int_curr_symbol[4] = 0;
872    }
873  }
874  /* else it is already ok */
875
876  return lmon;
877}
878
879struct _Locale_messages* _Locale_messages_create(const char *name, _Locale_lcid_t* lc_hint, int *__err_code) {
880  /* The Win32 API has no support for messages facet */
881  _STLP_MARK_PARAMETER_AS_UNUSED(name)
882  _STLP_MARK_PARAMETER_AS_UNUSED(lc_hint)
883  *__err_code = _STLP_LOC_UNSUPPORTED_FACET_CATEGORY;
884  return NULL;
885}
886
887static const char* _Locale_common_default(char* buf) {
888  char cp[MAX_CP_LEN + 1];
889  int CodePage = __GetDefaultCP(LOCALE_USER_DEFAULT);
890  my_ltoa(CodePage, cp);
891  return __GetLocaleName(LOCALE_USER_DEFAULT, cp, buf);
892}
893
894const char* _Locale_ctype_default(char* buf)
895{ return _Locale_common_default(buf); }
896
897const char* _Locale_numeric_default(char * buf)
898{ return _Locale_common_default(buf); }
899
900const char* _Locale_time_default(char* buf)
901{ return _Locale_common_default(buf); }
902
903const char* _Locale_collate_default(char* buf)
904{ return _Locale_common_default(buf); }
905
906const char* _Locale_monetary_default(char* buf)
907{ return _Locale_common_default(buf); }
908
909const char* _Locale_messages_default(char* buf)
910{ return _Locale_common_default(buf); }
911
912char const* _Locale_ctype_name(const _Locale_ctype_t* ltype, char* buf) {
913  char cp_buf[MAX_CP_LEN + 1];
914  my_ltoa(ltype->cp, cp_buf);
915  return __GetLocaleName(ltype->lc.id, cp_buf, buf);
916}
917
918char const* _Locale_numeric_name(const _Locale_numeric_t* lnum, char* buf)
919{ return __GetLocaleName(lnum->lc.id, lnum->cp, buf); }
920
921char const* _Locale_time_name(const _Locale_time_t* ltime, char* buf)
922{ return __GetLocaleName(ltime->lc.id, ltime->cp, buf); }
923
924char const* _Locale_collate_name(const _Locale_collate_t* lcol, char* buf)
925{ return __GetLocaleName(lcol->lc.id, lcol->cp, buf); }
926
927char const* _Locale_monetary_name(const _Locale_monetary_t* lmon, char* buf)
928{ return __GetLocaleName(lmon->lc.id, lmon->cp, buf); }
929
930char const* _Locale_messages_name(const struct _Locale_messages* lmes, char* buf) {
931  _STLP_MARK_PARAMETER_AS_UNUSED(lmes)
932  _STLP_MARK_PARAMETER_AS_UNUSED(buf)
933  return NULL;
934}
935
936void _Locale_ctype_destroy(_Locale_ctype_t* ltype) {
937  if (!ltype) return;
938
939  free(ltype);
940}
941
942void _Locale_numeric_destroy(_Locale_numeric_t* lnum) {
943  if (!lnum) return;
944
945  if (lnum->grouping) free(lnum->grouping);
946  free(lnum);
947}
948
949void _Locale_time_destroy(_Locale_time_t* ltime) {
950  int i;
951  if (!ltime) return;
952
953  for (i = 0; i < 12; ++i) {
954    if (ltime->month[i]) free(ltime->month[i]);
955    if (ltime->abbrev_month[i]) free(ltime->abbrev_month[i]);
956  }
957
958  for (i = 0; i < 7; ++i) {
959    if (ltime->dayofweek[i]) free(ltime->dayofweek[i]);
960    if (ltime->abbrev_dayofweek[i]) free(ltime->abbrev_dayofweek[i]);
961  }
962
963  if (ltime->date_format) free(ltime->date_format);
964  if (ltime->long_date_format) free(ltime->long_date_format);
965  if (ltime->time_format) free(ltime->time_format);
966  if (ltime->date_time_format) free(ltime->date_time_format);
967  if (ltime->long_date_time_format) free(ltime->long_date_time_format);
968
969  free(ltime);
970}
971
972void _Locale_collate_destroy(_Locale_collate_t* lcol) {
973  if (!lcol) return;
974
975  free(lcol);
976}
977
978void _Locale_monetary_destroy(_Locale_monetary_t* lmon) {
979  if (!lmon) return;
980
981  if (lmon->grouping) free(lmon->grouping);
982  free(lmon);
983}
984
985void _Locale_messages_destroy(struct _Locale_messages* lmes)
986{ _STLP_MARK_PARAMETER_AS_UNUSED(lmes) }
987
988static char const* _Locale_extract_category_name(const char* name, const char* category, char* buf,
989                                                 _Locale_lcid_t* hint, int *__err_code) {
990  const char* cname = __Extract_locale_name(name, category, buf);
991  if (cname == 0 || (cname[0] == 'C' && cname[1] == 0)) {
992    return cname;
993  }
994  return __TranslateToSystem(cname, buf, hint, __err_code);
995}
996
997char const* _Locale_extract_ctype_name(const char* cname, char* buf,
998                                       _Locale_lcid_t* hint, int *__err_code)
999{ return _Locale_extract_category_name(cname, "LC_CTYPE", buf, hint, __err_code); }
1000
1001char const* _Locale_extract_numeric_name(const char* cname, char* buf,
1002                                         _Locale_lcid_t* hint, int *__err_code)
1003{ return _Locale_extract_category_name(cname, "LC_NUMERIC", buf, hint, __err_code); }
1004
1005char const* _Locale_extract_time_name(const char* cname, char* buf,
1006                                      _Locale_lcid_t* hint, int *__err_code)
1007{ return _Locale_extract_category_name(cname, "LC_TIME", buf, hint, __err_code); }
1008
1009char const* _Locale_extract_collate_name(const char* cname, char* buf,
1010                                         _Locale_lcid_t* hint, int *__err_code)
1011{ return _Locale_extract_category_name(cname, "LC_COLLATE", buf, hint, __err_code); }
1012
1013char const* _Locale_extract_monetary_name(const char* cname, char* buf,
1014                                          _Locale_lcid_t* hint, int *__err_code)
1015{ return _Locale_extract_category_name(cname, "LC_MONETARY", buf, hint, __err_code); }
1016
1017char const* _Locale_extract_messages_name(const char* cname, char* buf,
1018                                          _Locale_lcid_t* hint, int *__err_code) {
1019  if (cname[0] == 'L' && cname[1] == 'C' && cname[2] == '_') {
1020    return _C_name;
1021  }
1022  if (cname[0] == 'C' && cname[1] == 0) {
1023    return _C_name;
1024  }
1025  return __TranslateToSystem(cname, buf, hint, __err_code);
1026}
1027
1028/* ctype */
1029
1030const _Locale_mask_t* _Locale_ctype_table(_Locale_ctype_t* ltype) {
1031  _STLP_STATIC_ASSERT(sizeof(_Locale_mask_t) == sizeof(ltype->ctable[0]))
1032  return (const _Locale_mask_t*)ltype->ctable;
1033}
1034
1035int _Locale_toupper(_Locale_ctype_t* ltype, int c) {
1036  char buf[2], out_buf[2];
1037  buf[0] = (char)c; buf[1] = 0;
1038  if ((UINT)__GetDefaultCP(ltype->lc.id) == ltype->cp) {
1039    LCMapStringA(ltype->lc.id, LCMAP_LINGUISTIC_CASING | LCMAP_UPPERCASE, buf, 2, out_buf, 2);
1040    return out_buf[0];
1041  }
1042  else {
1043    wchar_t wbuf[2];
1044    MultiByteToWideChar(ltype->cp, MB_PRECOMPOSED, buf, 2, wbuf, 2);
1045    WideCharToMultiByte(__GetDefaultCP(ltype->lc.id), WC_COMPOSITECHECK | WC_SEPCHARS, wbuf, 2, buf, 2, NULL, FALSE);
1046
1047    LCMapStringA(ltype->lc.id, LCMAP_LINGUISTIC_CASING | LCMAP_UPPERCASE, buf, 2, out_buf, 2);
1048
1049    MultiByteToWideChar(__GetDefaultCP(ltype->lc.id), MB_PRECOMPOSED, out_buf, 2, wbuf, 2);
1050    WideCharToMultiByte(ltype->cp, WC_COMPOSITECHECK | WC_SEPCHARS, wbuf, 2, out_buf, 2, NULL, FALSE);
1051    return out_buf[0];
1052  }
1053}
1054
1055int _Locale_tolower(_Locale_ctype_t* ltype, int c) {
1056  char buf[2], out_buf[2];
1057  buf[0] = (char)c; buf[1] = 0;
1058  if ((UINT)__GetDefaultCP(ltype->lc.id) == ltype->cp) {
1059    LCMapStringA(ltype->lc.id, LCMAP_LINGUISTIC_CASING | LCMAP_LOWERCASE, buf, 2, out_buf, 2);
1060    return out_buf[0];
1061  }
1062  else {
1063    wchar_t wbuf[2];
1064    MultiByteToWideChar(ltype->cp, MB_PRECOMPOSED, buf, 2, wbuf, 2);
1065    WideCharToMultiByte(__GetDefaultCP(ltype->lc.id), WC_COMPOSITECHECK | WC_SEPCHARS, wbuf, 2, buf, 2, NULL, FALSE);
1066
1067    LCMapStringA(ltype->lc.id, LCMAP_LINGUISTIC_CASING | LCMAP_LOWERCASE, buf, 2, out_buf, 2);
1068
1069    MultiByteToWideChar(__GetDefaultCP(ltype->lc.id), MB_PRECOMPOSED, out_buf, 2, wbuf, 2);
1070    WideCharToMultiByte(ltype->cp, WC_COMPOSITECHECK | WC_SEPCHARS, wbuf, 2, out_buf, 2, NULL, FALSE);
1071    return out_buf[0];
1072  }
1073}
1074
1075#ifndef CSTR_EQUAL /* VC5SP3*/
1076#  define CSTR_EQUAL 2
1077#endif
1078#ifndef CSTR_LESS_THAN /* VC5SP3 */
1079#  define CSTR_LESS_THAN 1
1080#endif
1081
1082static DWORD max_DWORD = 0xffffffff;
1083static DWORD trim_size_t_to_DWORD(size_t n) { return n < (size_t)max_DWORD ? (DWORD)n : max_DWORD; }
1084
1085/* Collate */
1086/* This function takes care of the potential size_t DWORD different size. */
1087static int _Locale_strcmp_auxA(_Locale_collate_t* lcol,
1088                               const char* s1, size_t n1,
1089                               const char* s2, size_t n2) {
1090  int result = CSTR_EQUAL;
1091  while (n1 > 0 || n2 > 0) {
1092    DWORD size1 = trim_size_t_to_DWORD(n1);
1093    DWORD size2 = trim_size_t_to_DWORD(n2);
1094    result = CompareStringA(lcol->lc.id, 0, s1, size1, s2, size2);
1095    if (result != CSTR_EQUAL)
1096      break;
1097    n1 -= size1;
1098    n2 -= size2;
1099  }
1100  return result;
1101}
1102
1103int _Locale_strcmp(_Locale_collate_t* lcol,
1104                   const char* s1, size_t n1,
1105                   const char* s2, size_t n2) {
1106  int result;
1107  if (__GetDefaultCP(lcol->lc.id) == atoi(lcol->cp)) {
1108    result = _Locale_strcmp_auxA(lcol, s1, n1, s2, n2);
1109  }
1110  else {
1111    char *buf1, *buf2;
1112    size_t size1, size2;
1113    buf1 = __ConvertToCP(atoi(lcol->cp), __GetDefaultCP(lcol->lc.id), s1, n1, &size1);
1114    buf2 = __ConvertToCP(atoi(lcol->cp), __GetDefaultCP(lcol->lc.id), s2, n2, &size2);
1115
1116    result = _Locale_strcmp_auxA(lcol, buf1, size1, buf2, size2);
1117    free(buf1); free(buf2);
1118  }
1119  return (result == CSTR_EQUAL) ? 0 : (result == CSTR_LESS_THAN) ? -1 : 1;
1120}
1121
1122size_t _Locale_strxfrm(_Locale_collate_t* lcol,
1123                       char* dst, size_t dst_size,
1124                       const char* src, size_t src_size) {
1125  int result;
1126
1127  /* The Windows API do not support transformation of very long strings (src_size > INT_MAX)
1128   * In this case the result will just be the input string:
1129   */
1130  if (src_size > INT_MAX) {
1131    if (dst != 0) {
1132      _STLP_STRNCPY(dst, dst_size, src, src_size);
1133    }
1134    return src_size;
1135  }
1136  if (dst_size > INT_MAX) {
1137    /* now that we know that src_size <= INT_MAX we can safely decrease dst_size to INT_MAX. */
1138    dst_size = INT_MAX;
1139  }
1140
1141  if (__GetDefaultCP(lcol->lc.id) == atoi(lcol->cp))
1142    result = LCMapStringA(lcol->lc.id, LCMAP_SORTKEY, src, (int)src_size, dst, (int)dst_size);
1143  else {
1144    char *buf;
1145    size_t size;
1146    buf = __ConvertToCP(atoi(lcol->cp), __GetDefaultCP(lcol->lc.id), src, src_size, &size);
1147
1148    result = LCMapStringA(lcol->lc.id, LCMAP_SORTKEY, buf, (int)size, dst, (int)dst_size);
1149    free(buf);
1150  }
1151  return result != 0 ? result - 1 : 0;
1152}
1153
1154/* Numeric */
1155static const char* __true_name = "true";
1156static const char* __false_name = "false";
1157
1158char _Locale_decimal_point(_Locale_numeric_t* lnum)
1159{ return lnum->decimal_point[0]; }
1160
1161char _Locale_thousands_sep(_Locale_numeric_t* lnum)
1162{ return lnum->thousands_sep[0]; }
1163
1164const char* _Locale_grouping(_Locale_numeric_t * lnum) {
1165  if (!lnum->grouping) return "";
1166  else return lnum->grouping;
1167}
1168
1169const char * _Locale_true(_Locale_numeric_t * lnum) {
1170  _STLP_MARK_PARAMETER_AS_UNUSED(lnum)
1171  return __true_name; /* NT does't provide information about this */
1172}
1173
1174const char * _Locale_false(_Locale_numeric_t * lnum) {
1175  _STLP_MARK_PARAMETER_AS_UNUSED(lnum)
1176  return __false_name; /* NT does't provide information about this */
1177}
1178
1179/* Monetary */
1180const char* _Locale_int_curr_symbol(_Locale_monetary_t * lmon)
1181{ return lmon->int_curr_symbol; }
1182
1183const char* _Locale_currency_symbol(_Locale_monetary_t * lmon)
1184{ return lmon->curr_symbol; }
1185
1186char _Locale_mon_decimal_point(_Locale_monetary_t * lmon)
1187{ return lmon->decimal_point[0]; }
1188
1189char _Locale_mon_thousands_sep(_Locale_monetary_t * lmon)
1190{ return lmon->thousands_sep[0]; }
1191
1192const char* _Locale_mon_grouping(_Locale_monetary_t * lmon) {
1193  if (!lmon->grouping) return "";
1194  else return lmon->grouping;
1195}
1196
1197const char* _Locale_positive_sign(_Locale_monetary_t * lmon)
1198{ return lmon->positive_sign; }
1199
1200const char* _Locale_negative_sign(_Locale_monetary_t * lmon)
1201{ return lmon->negative_sign; }
1202
1203char _Locale_int_frac_digits(_Locale_monetary_t * lmon)
1204{ return (char)lmon->int_frac_digits; }
1205
1206char _Locale_frac_digits(_Locale_monetary_t * lmon)
1207{ return (char)lmon->frac_digits; }
1208
1209int _Locale_p_cs_precedes(_Locale_monetary_t * lmon) {
1210  char loc_data[2];
1211  GetLocaleInfoA(lmon->lc.id, LOCALE_IPOSSYMPRECEDES, loc_data, 2);
1212  if (loc_data[0] == '0') return 0;
1213  else if (loc_data[0] == '1') return 1;
1214  else return -1;
1215}
1216
1217int _Locale_p_sep_by_space(_Locale_monetary_t * lmon) {
1218  char loc_data[2];
1219  GetLocaleInfoA(lmon->lc.id, LOCALE_IPOSSEPBYSPACE, loc_data, 2);
1220  if (loc_data[0] == '0') return 0;
1221  else if (loc_data[0] == '1') return 1;
1222  else return -1;
1223}
1224
1225int _Locale_p_sign_posn(_Locale_monetary_t * lmon) {
1226  char loc_data[2];
1227  if (lmon->lc.id != INVARIANT_LCID) {
1228    GetLocaleInfoA(lmon->lc.id, LOCALE_IPOSSIGNPOSN, loc_data, 2);
1229    return atoi(loc_data);
1230  }
1231  else {
1232    return CHAR_MAX;
1233  }
1234}
1235
1236int _Locale_n_cs_precedes(_Locale_monetary_t * lmon) {
1237  char loc_data[2];
1238  GetLocaleInfoA(lmon->lc.id, LOCALE_INEGSYMPRECEDES, loc_data, 2);
1239  if (loc_data[0] == '0') return 0;
1240  else if (loc_data[0] == '1') return 1;
1241  else return -1;
1242}
1243
1244int _Locale_n_sep_by_space(_Locale_monetary_t * lmon) {
1245  char loc_data[2];
1246  GetLocaleInfoA(lmon->lc.id, LOCALE_INEGSEPBYSPACE, loc_data, 2);
1247  if (loc_data[0] == '0') return 0;
1248  else if (loc_data[0] == '1') return 1;
1249  else return -1;
1250}
1251
1252int _Locale_n_sign_posn(_Locale_monetary_t * lmon) {
1253  char loc_data[2];
1254  if (lmon->lc.id != INVARIANT_LCID) {
1255    GetLocaleInfoA(lmon->lc.id, LOCALE_INEGSIGNPOSN, loc_data, 2);
1256    return atoi(loc_data);
1257  }
1258  else {
1259    return CHAR_MAX;
1260  }
1261}
1262
1263/* Time */
1264const char * _Locale_full_monthname(_Locale_time_t * ltime, int month) {
1265  const char **names = (const char**)ltime->month;
1266  return names[month];
1267}
1268
1269const char * _Locale_abbrev_monthname(_Locale_time_t * ltime, int month) {
1270  const char **names = (const char**)ltime->abbrev_month;
1271  return names[month];
1272}
1273
1274const char * _Locale_full_dayofweek(_Locale_time_t * ltime, int day) {
1275  const char **names = (const char**)ltime->dayofweek;
1276  return names[day];
1277}
1278
1279const char * _Locale_abbrev_dayofweek(_Locale_time_t * ltime, int day) {
1280  const char **names = (const char**)ltime->abbrev_dayofweek;
1281  return names[day];
1282}
1283
1284const char* _Locale_d_t_fmt(_Locale_time_t* ltime)
1285{ return ltime->date_time_format; }
1286
1287const char* _Locale_long_d_t_fmt(_Locale_time_t* ltime)
1288{ return ltime->long_date_time_format; }
1289
1290const char* _Locale_d_fmt(_Locale_time_t* ltime)
1291{ return ltime->date_format; }
1292
1293const char* _Locale_long_d_fmt(_Locale_time_t* ltime)
1294{ return ltime->long_date_format; }
1295
1296const char* _Locale_t_fmt(_Locale_time_t* ltime)
1297{ return ltime->time_format; }
1298
1299const char* _Locale_am_str(_Locale_time_t* ltime)
1300{ return ltime->am; }
1301
1302const char* _Locale_pm_str(_Locale_time_t* ltime)
1303{ return ltime->pm; }
1304
1305/* Messages */
1306nl_catd_type _Locale_catopen(struct _Locale_messages* lmes, const char* cat_name) {
1307  _STLP_MARK_PARAMETER_AS_UNUSED(lmes)
1308  _STLP_MARK_PARAMETER_AS_UNUSED(cat_name)
1309  return -1;
1310}
1311void _Locale_catclose(struct _Locale_messages* lmes, nl_catd_type cat) {
1312  _STLP_MARK_PARAMETER_AS_UNUSED(lmes)
1313  _STLP_MARK_PARAMETER_AS_UNUSED(&cat)
1314}
1315const char* _Locale_catgets(struct _Locale_messages* lmes, nl_catd_type cat,
1316                            int setid, int msgid, const char *dfault) {
1317  _STLP_MARK_PARAMETER_AS_UNUSED(lmes)
1318  _STLP_MARK_PARAMETER_AS_UNUSED(&cat)
1319  _STLP_MARK_PARAMETER_AS_UNUSED(&setid)
1320  _STLP_MARK_PARAMETER_AS_UNUSED(&msgid)
1321  return dfault;
1322}
1323
1324#ifdef __cplusplus
1325} /* extern C */
1326_STLP_END_NAMESPACE
1327#endif
1328
1329void __FixGrouping(char *grouping) {
1330  /* This converts NT version which uses '0' instead of 0, etc ; to ANSI */
1331  char *g = grouping;
1332  char building_group = 0;
1333  char repeat_last = 0;
1334  /* Check there is a grouping info otherwise we would add a useless CHAR_MAX */
1335  if (*g) {
1336    for (; *g; ++g) {
1337      if (*g > '0' && *g <= '9') {
1338        if (!building_group) {
1339          *grouping = *g - '0';
1340          building_group = 1;
1341        }
1342        else {
1343          /* Known issue: grouping might roll. */
1344          *grouping = *grouping * 10 + *g - '0';
1345        }
1346      }
1347      else if (*g == '0') {
1348        if (!building_group) {
1349          repeat_last = 1;
1350        }
1351        else
1352          /* Known issue: grouping might roll. */
1353          *grouping *= 10;
1354      }
1355      else if (*g == ';') {
1356        /* Stop adding to the current group */
1357        building_group = 0;
1358        ++grouping;
1359      }
1360      /* else we ignore the character */
1361    }
1362
1363    if (!repeat_last)
1364      *grouping++ = CHAR_MAX;
1365    *grouping = 0;
1366  }
1367}
1368
1369const char* __ConvertName(const char* lname, LOCALECONV* ConvTable, int TableSize) {
1370  int i;
1371  int cmp;
1372  int low = 0;
1373  int high = TableSize - 1;
1374
1375  /*  typical binary search - do until no more to search or match */
1376  while (low <= high) {
1377    i = (low + high) / 2;
1378
1379    if ((cmp = lstrcmpiA(lname, (*(ConvTable + i)).name)) == 0)
1380      return (*(ConvTable + i)).abbrev;
1381    else if (cmp < 0)
1382      high = i - 1;
1383    else
1384      low = i + 1;
1385  }
1386  return lname;
1387}
1388
1389int __ParseLocaleString(const char* lname,
1390                        char* lang, char* ctry, char* page) {
1391  int param = 0;
1392  size_t len;
1393  size_t tmpLen;
1394
1395  if (lname[0] == 0)
1396    return 0;
1397
1398  /* We look for the first country separator '_' */
1399  len = strcspn(lname, "_");
1400  if (lname[len] == '_') {
1401    if (len == 0 || len > MAX_LANG_LEN) return -1; /* empty lang is invalid*/
1402    _STLP_STRNCPY(lang, MAX_LANG_LEN + 1, lname, len);
1403    lname += len + 1;
1404    ++param;
1405  }
1406
1407  /* We look for the last code page separator '.' */
1408  len = -1;
1409  tmpLen = strcspn(lname, ".");
1410  while (lname[tmpLen] == '.') {
1411    len = tmpLen; ++tmpLen;
1412    tmpLen += strcspn(lname + tmpLen, ".");
1413  }
1414  if (len != -1) { /* Means that we found a '.' */
1415    if (param == 0) {
1416      /* We have no lang yet so we have to fill it first, no country */
1417      if (len > MAX_LANG_LEN) return -1;
1418      if (len == 0) {
1419        /* No language nor country, only code page */
1420        ++param;
1421      }
1422      else
1423      { _STLP_STRNCPY(lang, MAX_LANG_LEN + 1, lname, len); }
1424      ++param;
1425    }
1426    else {
1427      /* We already have a lang so we are now looking for the country: */
1428      if (len == 0) return -1; /* We forbid locale name with the "_." motif in it */
1429      if (len > MAX_CTRY_LEN) return -1;
1430      _STLP_STRNCPY(ctry, MAX_CTRY_LEN + 1, lname, len);
1431    }
1432    ++param;
1433    lname += len + 1;
1434  }
1435
1436  /* We look for ',' for compatibility with POSIX */
1437  len = strcspn(lname, ",");
1438  switch (param) {
1439    case 0:
1440      if (len > MAX_LANG_LEN) return -1;
1441      _STLP_STRNCPY(lang, MAX_LANG_LEN + 1, lname, len);
1442      break;
1443    case 1:
1444      if (len > MAX_CTRY_LEN) return -1;
1445      _STLP_STRNCPY(ctry, MAX_CTRY_LEN + 1, lname, len);
1446      break;
1447    default:
1448      if (len > MAX_CP_LEN) return -1;
1449      _STLP_STRNCPY(page, MAX_CP_LEN + 1, lname, len);
1450      break;
1451  }
1452
1453  /* ',' POSIX modifier is not used in NT */
1454  return 0;
1455}
1456
1457/* Data necessary for find LCID*/
1458static CRITICAL_SECTION __criticalSection;
1459static int __FindFlag;
1460static LCID __FndLCID;
1461static const char* __FndLang;
1462static const char* __FndCtry;
1463
1464void _Locale_init()
1465{ InitializeCriticalSection(&__criticalSection); }
1466
1467void _Locale_final()
1468{ DeleteCriticalSection(&__criticalSection); }
1469
1470static LCID LocaleFromHex(const char* locale) {
1471  unsigned long result = 0;
1472  int digit;
1473  while (*locale) {
1474    result <<= 4;
1475    digit = (*locale >= '0' && *locale <= '9') ? *locale - '0':
1476            (*locale >= 'A' && *locale <= 'F') ? (*locale - 'A') + 10
1477                                               : (*locale - 'a') + 10;
1478    result += digit;
1479    ++locale;
1480  }
1481  return (LCID)result;
1482}
1483
1484static BOOL CALLBACK EnumLocalesProcA(LPSTR locale) {
1485  LCID lcid = LocaleFromHex(locale);
1486  int LangFlag = 0, CtryFlag = !__FndCtry;
1487  static char Lang[MAX_LANG_LEN], Ctry[MAX_CTRY_LEN];
1488
1489  GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE, Lang, MAX_LANG_LEN);
1490  if (lstrcmpiA(Lang, __FndLang) != 0) {
1491    GetLocaleInfoA(lcid, LOCALE_SABBREVLANGNAME, Lang, MAX_LANG_LEN);
1492    if (lstrcmpiA(Lang, __FndLang) != 0) {
1493      GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME, Lang, MAX_LANG_LEN);
1494      if (lstrcmpiA(Lang, __FndLang) == 0) LangFlag = 1;
1495    }
1496    else LangFlag = 1;
1497  }
1498  else LangFlag = 1;
1499
1500  if (__FndCtry) {
1501    GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY, Ctry, MAX_CTRY_LEN);
1502    if (lstrcmpiA(Ctry, __FndCtry) != 0) {
1503      GetLocaleInfoA(lcid, LOCALE_SABBREVCTRYNAME, Ctry, MAX_CTRY_LEN);
1504      if (lstrcmpiA(Ctry, __FndCtry) != 0) {
1505        GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME, Ctry, MAX_CTRY_LEN);
1506        if (lstrcmpiA(Ctry, __FndCtry) == 0) CtryFlag = 1;
1507      }
1508      else CtryFlag = 1;
1509    }
1510    else
1511      CtryFlag = 1;
1512  }
1513
1514  if (LangFlag && CtryFlag) {
1515    __FindFlag = 1;
1516    __FndLCID = lcid;
1517    return FALSE;
1518  }
1519
1520  return TRUE;
1521}
1522
1523int __GetLCID(const char* lang, const char* ctry, LCID* lcid) {
1524  int ret;
1525  EnterCriticalSection(&__criticalSection);
1526
1527  __FindFlag = 0;
1528  __FndLang = lang;
1529  __FndCtry = ctry;
1530  EnumSystemLocalesA(EnumLocalesProcA, LCID_INSTALLED);
1531
1532  if (__FindFlag != 0) *lcid = __FndLCID;
1533  ret = __FindFlag != 0 ? 0 : -1;
1534
1535  LeaveCriticalSection(&__criticalSection);
1536  return ret;
1537}
1538
1539int __GetLCIDFromName(const char* lname, LCID* lcid, char* cp, _Locale_lcid_t *hint) {
1540  char lang[MAX_LANG_LEN + 1], ctry[MAX_CTRY_LEN + 1], page[MAX_CP_LEN + 1];
1541  int result = 0;
1542  if (lname == NULL || lname[0] == 0) {
1543    *lcid = LOCALE_USER_DEFAULT;
1544    return 0;
1545  }
1546
1547  memset(lang, 0, MAX_LANG_LEN + 1);
1548  memset(ctry, 0, MAX_CTRY_LEN + 1);
1549  memset(page, 0, MAX_CP_LEN + 1);
1550  if (__ParseLocaleString(lname, lang, ctry, page) == -1) return -1;
1551
1552  if (hint != 0) {
1553    *lcid = hint->id;
1554  }
1555  else {
1556    if (lang[0] == 0 && ctry[0] == 0)
1557      *lcid = LOCALE_USER_DEFAULT; /* Only code page given. */
1558    else {
1559      if (ctry[0] == 0) {
1560        result = __GetLCID(__ConvertName(lang, __rg_language, sizeof(__rg_language) / sizeof(LOCALECONV)), NULL, lcid);
1561        if (result != 0) {
1562          /* Check 'C' special case. Check is done after call to __GetLCID because normal programs do not
1563           * generate facet from 'C' name, they use the locale::classic() facets. */
1564          if (lang[0] == 'C' && lang[1] == 0) {
1565            *lcid = INVARIANT_LCID;
1566            result = 0;
1567          }
1568        }
1569      }
1570      else {
1571        result = __GetLCID(__ConvertName(lang, __rg_language, sizeof(__rg_language) / sizeof(LOCALECONV)),
1572                           __ConvertName(ctry, __rg_country, sizeof(__rg_country) / sizeof(LOCALECONV)),
1573                           lcid);
1574        if (result != 0) {
1575          /* Non NLS mapping might introduce problem with some locales when only one entry is mapped,
1576           * the lang or the country (example: chinese locales like 'chinese_taiwan' gives 'CHS_taiwan'
1577           * that do not exists in system). This is why we are giving this locale an other chance by
1578           * calling __GetLCID without the mapping. */
1579          result = __GetLCID(lang, ctry, lcid);
1580        }
1581      }
1582    }
1583  }
1584
1585  if (result == 0) {
1586    /* Handling code page */
1587    if (lstrcmpiA(page, "ACP") == 0 || page[0] == 0)
1588      my_ltoa(__intGetACP(*lcid), cp);
1589    else if (lstrcmpiA(page, "OCP") == 0)
1590      my_ltoa(__intGetOCP(*lcid), cp);
1591    else if (lstrcmpiA(page, "UTF7") == 0)
1592      my_ltoa(CP_UTF7, cp);
1593    else if (lstrcmpiA(page, "UTF8") == 0)
1594      my_ltoa(CP_UTF8, cp);
1595    else
1596      _STLP_STRNCPY(cp, MAX_CP_LEN + 1, page, 5);
1597
1598    /* Code page must be an integer value,
1599     * 0 returned by __intGetACP and 1 returned by __intGetOCP are invalid
1600     * values.
1601     */
1602    if (cp[1] == 0 && (cp[0] == '0' || cp[1] == '1'))
1603      return -1;
1604    else if (atoi(cp) == 0)
1605      return -1;
1606  }
1607
1608  return result;
1609}
1610
1611char const* __GetLocaleName(LCID lcid, const char* cp, char* buf) {
1612  if (lcid == INVARIANT_LCID) {
1613    return _C_name;
1614  }
1615  else {
1616    char lang[MAX_LANG_LEN + 1], ctry[MAX_CTRY_LEN + 1];
1617    GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE, lang, MAX_LANG_LEN);
1618    GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY, ctry, MAX_CTRY_LEN);
1619    _STLP_STRCPY(buf, _Locale_MAX_SIMPLE_NAME, lang);
1620    _STLP_STRCAT(buf, _Locale_MAX_SIMPLE_NAME, "_");
1621    _STLP_STRCAT(buf, _Locale_MAX_SIMPLE_NAME, ctry);
1622    _STLP_STRCAT(buf, _Locale_MAX_SIMPLE_NAME, ".");
1623    _STLP_STRCAT(buf, _Locale_MAX_SIMPLE_NAME, cp);
1624    return buf;
1625  }
1626}
1627
1628char const* __Extract_locale_name(const char* loc, const char* category, char* buf) {
1629  char *expr;
1630  size_t len_name;
1631
1632  if (loc[0] == 'L' && loc[1] == 'C' && loc[2] == '_') {
1633    expr = strstr((char*)loc, category);
1634    if (expr == NULL) return NULL; /* Category not found. */
1635    expr = strchr(expr, '=');
1636    if (expr == NULL) return NULL;
1637    ++expr;
1638    len_name = strcspn(expr, ";");
1639    len_name = len_name >= _Locale_MAX_SIMPLE_NAME ? _Locale_MAX_SIMPLE_NAME - 1
1640                                                   : len_name;
1641    _STLP_STRNCPY(buf, _Locale_MAX_SIMPLE_NAME, expr, len_name); buf[len_name] = 0;
1642    return buf;
1643  }
1644  else {
1645    return loc;
1646  }
1647}
1648
1649char const* __TranslateToSystem(const char* lname, char* buf, _Locale_lcid_t* hint,
1650                                int *__err_code) {
1651  LCID lcid;
1652  char cp[MAX_CP_LEN + 1];
1653  if (__GetLCIDFromName(lname, &lcid, cp, hint) != 0)
1654  { *__err_code = _STLP_LOC_UNKNOWN_NAME; return NULL; }
1655
1656  return __GetLocaleName(lcid, cp, buf);
1657}
1658
1659void __GetLocaleInfoUsingACP(LCID lcid, const char* cp, LCTYPE lctype, char* buf, int buf_size, wchar_t* wbuf, int wbuf_size) {
1660  wchar_t *Buffer;
1661  int BufferSize;
1662  int icp;
1663
1664  GetLocaleInfoA(lcid, lctype, buf, buf_size);
1665
1666  icp = atoi(cp);
1667  if (icp != CP_ACP && buf[0] != 0) {
1668    BufferSize = MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
1669    if (BufferSize > wbuf_size)
1670    {
1671      Buffer = (wchar_t*)malloc(sizeof(wchar_t) * BufferSize);
1672    }
1673    else
1674    {
1675      Buffer = wbuf;
1676    }
1677    MultiByteToWideChar(CP_ACP, 0, buf, -1, Buffer, BufferSize);
1678    WideCharToMultiByte(icp, 0, Buffer, -1, buf, buf_size, NULL, NULL);
1679    if (Buffer != wbuf)
1680    {
1681      free(Buffer);
1682    }
1683  }
1684}
1685
1686/* Return 0 if ANSI code page not used */
1687int __intGetACP(LCID lcid) {
1688  char cp[6];
1689  if (!GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE, cp, 6)) {
1690#if defined (_STLP_LANG_INVARIANT_DEFINED)
1691    if (lcid == INVARIANT_LCID) {
1692      /* We are using a limited PSDK, we rely on the most common code page */
1693      return 1252;
1694    }
1695#endif
1696    return 0;
1697  }
1698  return atoi(cp);
1699}
1700
1701/* Return 1 if OEM code page not used */
1702int __intGetOCP(LCID lcid) {
1703  char cp[6];
1704  if (!GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE, cp, 6))
1705    return 0;
1706  return atoi(cp);
1707}
1708
1709int __GetDefaultCP(LCID lcid) {
1710  int cp = __intGetACP(lcid);
1711  if (cp == 0) return __intGetOCP(lcid);
1712  else return cp;
1713}
1714
1715static int trim_size_t_to_int(size_t n) { return n < (size_t)INT_MAX ? (int)n : INT_MAX; }
1716
1717char* __ConvertToCP(int from_cp, int to_cp, const char *from, size_t size, size_t *ret_buf_size) {
1718  size_t wbuffer_size, buffer_size, from_offset, wbuf_offset;
1719  int from_size, to_size, wbuf_size;
1720  wchar_t *wbuffer;
1721  char* buffer;
1722
1723  size_t orig_size = size;
1724
1725  wbuffer_size = 0;
1726  from_offset = 0;
1727  while (size > 0) {
1728    from_size = trim_size_t_to_int(size);
1729    wbuffer_size += MultiByteToWideChar(from_cp, MB_PRECOMPOSED,
1730                                        from + from_offset, from_size, NULL, 0);
1731    from_offset += from_size;
1732    size -= from_size;
1733  }
1734
1735  wbuffer = (wchar_t*)malloc(sizeof(wchar_t)*wbuffer_size);
1736
1737  size = orig_size;
1738  wbuf_offset = 0;
1739  from_offset = 0;
1740  while (size > 0) {
1741    from_size = trim_size_t_to_int(size);
1742    wbuf_size = trim_size_t_to_int(wbuffer_size - wbuf_offset);
1743    wbuf_offset += MultiByteToWideChar(from_cp, MB_PRECOMPOSED,
1744                                       from + from_offset, from_size, wbuffer + wbuf_offset, wbuf_size);
1745    from_offset += from_size;
1746    size -= from_size;
1747  }
1748
1749  buffer_size = 0;
1750  wbuf_offset = 0;
1751  size = wbuffer_size;
1752  while (size > 0) {
1753    wbuf_size = trim_size_t_to_int(size);
1754    buffer_size += WideCharToMultiByte(to_cp, WC_COMPOSITECHECK | WC_SEPCHARS,
1755                                       wbuffer + wbuf_offset, wbuf_size,
1756                                       NULL, 0, NULL, FALSE);
1757    wbuf_offset += wbuf_size;
1758    size -= wbuf_size;
1759  }
1760
1761  buffer = (char*)malloc(buffer_size);
1762  *ret_buf_size = buffer_size;
1763
1764  size = wbuffer_size;
1765  wbuf_offset = 0;
1766  while (size > 0) {
1767    wbuf_size = trim_size_t_to_int(size);
1768    to_size = trim_size_t_to_int(buffer_size);
1769    buffer_size -= WideCharToMultiByte(to_cp, WC_COMPOSITECHECK | WC_SEPCHARS,
1770                                       wbuffer + wbuf_offset, wbuf_size,
1771                                       buffer, to_size, NULL, FALSE);
1772    wbuf_offset += wbuf_size;
1773    size -= wbuf_size;
1774  }
1775
1776  free(wbuffer);
1777  return buffer;
1778}
1779
1780#ifdef __cplusplus
1781}
1782#endif
1783
1784#ifndef _STLP_NO_WCHAR_T
1785#  include "c_wlocale_win32.c"
1786#endif
1787