locale.c 45.2 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll locale functions
 *
 * Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 */
20 21 22 23

#include "config.h"
#include "wine/port.h"

24
#include <limits.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
25
#include <locale.h>
26
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
27 28
#include <stdio.h>

29
#include "windef.h"
30 31
#include "winbase.h"
#include "winuser.h"
32
#include "winnls.h"
Francois Gouget's avatar
Francois Gouget committed
33

34
#include "msvcrt.h"
35
#include "mtdll.h"
36

37 38 39
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
40 41 42

#define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
#define MAX_LOCALE_LENGTH 256
43
MSVCRT__locale_t MSVCRT_locale = NULL;
44 45
int MSVCRT___lc_codepage = 0;
int MSVCRT___lc_collate_cp = 0;
46 47
LCID MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1] = { 0 };
static unsigned char charmax = CHAR_MAX;
48 49

/* MT */
50 51
#define LOCK_LOCALE   _mlock(_SETLOCALE_LOCK);
#define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
52 53

#define MSVCRT_LEADBYTE  0x8000
54
#define MSVCRT_C1_DEFINED 0x200
55

56
/* Friendly country strings & language names abbreviations. */
57
static const char * const _country_synonyms[] =
58
{
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    "american", "enu",
    "american english", "enu",
    "american-english", "enu",
    "english-american", "enu",
    "english-us", "enu",
    "english-usa", "enu",
    "us", "enu",
    "usa", "enu",
    "australian", "ena",
    "english-aus", "ena",
    "belgian", "nlb",
    "french-belgian", "frb",
    "canadian", "enc",
    "english-can", "enc",
    "french-canadian", "frc",
    "chinese", "chs",
    "chinese-simplified", "chs",
    "chinese-traditional", "cht",
    "dutch-belgian", "nlb",
    "english-nz", "enz",
    "uk", "eng",
    "english-uk", "eng",
    "french-swiss", "frs",
    "swiss", "des",
    "german-swiss", "des",
    "italian-swiss", "its",
    "german-austrian", "dea",
    "portugese", "ptb",
    "portuguese-brazil", "ptb",
    "spanish-mexican", "esm",
    "norwegian-bokmal", "nor",
    "norwegian-nynorsk", "non",
    "spanish-modern", "esn"
92 93 94 95 96
};

/* INTERNAL: Map a synonym to an ISO code */
static void remap_synonym(char *name)
{
97
  unsigned int i;
98 99 100 101 102
  for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
  {
    if (!strcasecmp(_country_synonyms[i],name))
    {
      TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
103
      memcpy(name, _country_synonyms[i+1], sizeof(_country_synonyms[i+1]));
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
      return;
    }
  }
}

/* Note: Flags are weighted in order of matching importance */
#define FOUND_LANGUAGE         0x4
#define FOUND_COUNTRY          0x2
#define FOUND_CODEPAGE         0x1

typedef struct {
  char search_language[MAX_ELEM_LEN];
  char search_country[MAX_ELEM_LEN];
  char search_codepage[MAX_ELEM_LEN];
  char found_codepage[MAX_ELEM_LEN];
  unsigned int match_flags;
  LANGID found_lang_id;
} locale_search_t;

#define CONTINUE_LOOKING TRUE
#define STOP_LOOKING     FALSE

/* INTERNAL: Get and compare locale info with a given string */
127
static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp, BOOL exact)
128
{
129 130
  int len;

131
  buff[0] = 0;
132
  GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE, buff, MAX_ELEM_LEN);
133 134
  if (!buff[0] || !cmp[0])
    return 0;
135 136 137 138 139 140 141

  /* Partial matches are only allowed on language/country names */
  len = strlen(cmp);
  if(exact || len<=3)
    return !strcasecmp(cmp, buff);
  else
    return !strncasecmp(cmp, buff, len);
142 143 144
}

static BOOL CALLBACK
145
find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
146 147 148 149 150 151 152 153 154 155
{
  locale_search_t *res = (locale_search_t *)lParam;
  const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
  char buff[MAX_ELEM_LEN];
  unsigned int flags = 0;

  if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
    return CONTINUE_LOOKING;

  /* Check Language */
156 157 158
  if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language, TRUE) ||
      compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language, TRUE) ||
      compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language, FALSE))
159 160 161 162 163 164 165 166 167 168
  {
    TRACE(":Found language: %s->%s\n", res->search_language, buff);
    flags |= FOUND_LANGUAGE;
  }
  else if (res->match_flags & FOUND_LANGUAGE)
  {
    return CONTINUE_LOOKING;
  }

  /* Check Country */
169 170 171
  if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country, TRUE) ||
      compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country, TRUE) ||
      compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country, FALSE))
172 173 174 175 176 177 178 179 180 181
  {
    TRACE("Found country:%s->%s\n", res->search_country, buff);
    flags |= FOUND_COUNTRY;
  }
  else if (res->match_flags & FOUND_COUNTRY)
  {
    return CONTINUE_LOOKING;
  }

  /* Check codepage */
182 183
  if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage, TRUE) ||
      (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage, TRUE)))
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  {
    TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
    flags |= FOUND_CODEPAGE;
    memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
  }
  else if (res->match_flags & FOUND_CODEPAGE)
  {
    return CONTINUE_LOOKING;
  }

  if (flags > res->match_flags)
  {
    /* Found a better match than previously */
    res->match_flags = flags;
    res->found_lang_id = LangID;
  }
200 201
  if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
        (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
202 203 204 205 206 207 208 209 210 211
  {
    TRACE(":found exact locale match\n");
    return STOP_LOOKING;
  }
  return CONTINUE_LOOKING;
}

extern int atoi(const char *);

/* Internal: Find the LCID for a locale specification */
212
static LCID MSVCRT_locale_to_LCID(const char *locale)
213
{
214 215
    LCID lcid;
    locale_search_t search;
216
    const char *cp, *region;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

    memset(&search, 0, sizeof(locale_search_t));

    cp = strchr(locale, '.');
    region = strchr(locale, '_');

    lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
    if(region) {
        lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
        if(region-locale < MAX_ELEM_LEN)
            search.search_language[region-locale] = '\0';
    } else
        search.search_country[0] = '\0';

    if(cp) {
        lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
233 234
        if(cp-region-1 < MAX_ELEM_LEN)
          search.search_country[cp-region-1] = '\0';
235 236 237 238 239
        if(cp-locale < MAX_ELEM_LEN)
            search.search_language[cp-locale] = '\0';
    } else
        search.search_codepage[0] = '\0';

240 241
    if(!search.search_country[0] && !search.search_codepage[0])
        remap_synonym(search.search_language);
242 243 244 245 246 247

    EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
            (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
            (LONG_PTR)&search);

    if (!search.match_flags)
248
        return -1;
249 250 251

    /* If we were given something that didn't match, fail */
    if (search.search_country[0] && !(search.match_flags & FOUND_COUNTRY))
252
        return -1;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    lcid =  MAKELCID(search.found_lang_id, SORT_DEFAULT);

    /* Populate partial locale, translating LCID to locale string elements */
    if (!search.found_codepage[0]) {
        /* Even if a codepage is not enumerated for a locale
         * it can be set if valid */
        if (search.search_codepage[0]) {
            if (IsValidCodePage(atoi(search.search_codepage)))
                memcpy(search.found_codepage,search.search_codepage,MAX_ELEM_LEN);
            else {
                /* Special codepage values: OEM & ANSI */
                if (strcasecmp(search.search_codepage,"OCP")) {
                    GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
                            search.found_codepage, MAX_ELEM_LEN);
                } else if (strcasecmp(search.search_codepage,"ACP")) {
                    GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
                            search.found_codepage, MAX_ELEM_LEN);
                } else
272
                    return -1;
273 274

                if (!atoi(search.found_codepage))
275
                    return -1;
276 277 278 279 280 281 282 283
            }
        } else {
            /* Prefer ANSI codepages if present */
            GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
                    search.found_codepage, MAX_ELEM_LEN);
            if (!search.found_codepage[0] || !atoi(search.found_codepage))
                GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
                        search.found_codepage, MAX_ELEM_LEN);
284 285
        }
    }
286 287

    return lcid;
288 289
}

290
/* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
291
static BOOL update_threadlocinfo_category(LCID lcid, MSVCRT__locale_t loc, int category)
292 293 294 295
{
    char buf[256], *p;
    int len;

296
    if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE|LOCALE_NOUSEROVERRIDE, buf, 256)) {
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
        p = buf;

        loc->locinfo->lc_id[category].wLanguage = 0;
        while(*p) {
            loc->locinfo->lc_id[category].wLanguage *= 16;

            if(*p <= '9')
                loc->locinfo->lc_id[category].wLanguage += *p-'0';
            else
                loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;

            p++;
        }

        loc->locinfo->lc_id[category].wCountry =
            loc->locinfo->lc_id[category].wLanguage;
    }

315 316
    if(GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE
                |LOCALE_NOUSEROVERRIDE, buf, 256))
317 318 319 320 321
        loc->locinfo->lc_id[category].wCodePage = atoi(buf);

    loc->locinfo->lc_handle[category] = lcid;

    len = 0;
322 323
    len += GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE
            |LOCALE_NOUSEROVERRIDE, buf, 256);
324
    buf[len-1] = '_';
325 326
    len += GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY
            |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
327
    buf[len-1] = '.';
328 329
    len += GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE
            |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

    loc->locinfo->lc_category[category].locale = MSVCRT_malloc(sizeof(char[len]));
    loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
    if(!loc->locinfo->lc_category[category].locale
            || !loc->locinfo->lc_category[category].refcount) {
        MSVCRT_free(loc->locinfo->lc_category[category].locale);
        MSVCRT_free(loc->locinfo->lc_category[category].refcount);
        loc->locinfo->lc_category[category].locale = NULL;
        loc->locinfo->lc_category[category].refcount = NULL;
        return TRUE;
    }
    memcpy(loc->locinfo->lc_category[category].locale, buf, sizeof(char[len]));
    *loc->locinfo->lc_category[category].refcount = 1;

    return FALSE;
}

347 348 349
/* INTERNAL: swap pointers values */
static inline void swap_pointers(void **p1, void **p2) {
    void *hlp;
350

351 352 353
    hlp = *p1;
    *p1 = *p2;
    *p2 = hlp;
354 355
}

356 357
/* INTERNAL: returns pthreadlocinfo struct */
MSVCRT_pthreadlocinfo get_locinfo(void) {
358 359
    thread_data_t *data = msvcrt_get_thread_data();

360 361
    if(!data || !data->have_locale)
        return MSVCRT_locale->locinfo;
362

363
    return data->locinfo;
364 365
}

366
/* INTERNAL: returns pthreadlocinfo struct */
367
static MSVCRT_pthreadmbcinfo get_mbcinfo(void) {
368 369
    thread_data_t *data = msvcrt_get_thread_data();

370 371
    if(!data || !data->have_locale)
        return MSVCRT_locale->mbcinfo;
372

373
    return data->mbcinfo;
374 375
}

376
/* INTERNAL: constructs string returned by setlocale */
377
static inline char* construct_lc_all(MSVCRT_pthreadlocinfo locinfo) {
378 379 380 381 382
    static char current_lc_all[MAX_LOCALE_LENGTH];

    int i;

    for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
383 384
        if(strcmp(locinfo->lc_category[i].locale,
                    locinfo->lc_category[i+1].locale))
385 386 387 388
            break;
    }

    if(i==MSVCRT_LC_MAX)
389
        return locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
390 391 392

    sprintf(current_lc_all,
            "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
393 394 395 396 397
            locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
            locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
            locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
            locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
            locinfo->lc_category[MSVCRT_LC_TIME].locale);
398 399 400 401

    return current_lc_all;
}

402

403
/*********************************************************************
André Hentschel's avatar
André Hentschel committed
404
 *		wsetlocale (MSVCRT.@)
405
 */
406
MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
407 408 409 410 411 412 413 414 415
{
  static MSVCRT_wchar_t fake[] = {
    'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
    'S','t','a','t','e','s','.','1','2','5','2',0 };

  FIXME("%d %s\n", category, debugstr_w(locale));

  return fake;
}
416 417 418 419

/*********************************************************************
 *		_Getdays (MSVCRT.@)
 */
420
const char* CDECL _Getdays(void)
421
{
422
  static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
423 424
                            "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
  /* FIXME: Use locale */
425
  TRACE("(void) semi-stub\n");
426 427 428 429 430 431
  return MSVCRT_days;
}

/*********************************************************************
 *		_Getmonths (MSVCRT.@)
 */
432
const char* CDECL _Getmonths(void)
433
{
434
  static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
435 436 437
                "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
                "October:Nov:November:Dec:December";
  /* FIXME: Use locale */
438
  TRACE("(void) semi-stub\n");
439 440 441 442
  return MSVCRT_months;
}

/*********************************************************************
443
 *		_Gettnames (MSVCRT.@)
444
 */
445
const char* CDECL _Gettnames(void)
446 447
{
  /* FIXME: */
448
  TRACE("(void) stub\n");
449 450 451 452 453 454
  return "";
}

/*********************************************************************
 *		_Strftime (MSVCRT.@)
 */
455 456
const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
                            const void *tm, void *foo)
457 458
{
  /* FIXME: */
459
  TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
460 461 462
  return "";
}

463 464 465
/*********************************************************************
 *		__crtLCMapStringA (MSVCRT.@)
 */
466
int CDECL __crtLCMapStringA(
467 468 469
  LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
  int dstlen, unsigned int codepage, int xflag
) {
470
  FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
471 472 473 474 475 476
        lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
  /* FIXME: A bit incorrect. But msvcrt itself just converts its
   * arguments to wide strings and then calls LCMapStringW
   */
  return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
}
477

478 479 480 481 482 483 484 485 486 487 488 489
/*********************************************************************
 *              __crtLCMapStringW (MSVCRT.@)
 */
int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
        int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
{
    FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
            lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);

    return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
/*********************************************************************
 *		__crtCompareStringA (MSVCRT.@)
 */
int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
                               const char *src2, int len2 )
{
    FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
          lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
    /* FIXME: probably not entirely right */
    return CompareStringA( lcid, flags, src1, len1, src2, len2 );
}

/*********************************************************************
 *		__crtCompareStringW (MSVCRT.@)
 */
int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
                               const MSVCRT_wchar_t *src2, int len2 )
{
    FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
          lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
    /* FIXME: probably not entirely right */
    return CompareStringW( lcid, flags, src1, len1, src2, len2 );
}

/*********************************************************************
 *		__crtGetLocaleInfoW (MSVCRT.@)
 */
int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
{
    FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
    /* FIXME: probably not entirely right */
    return GetLocaleInfoW( lcid, type, buffer, len );
}

524 525 526 527 528 529 530 531
/*********************************************************************
 *              btowc(MSVCRT.@)
 */
MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
{
    unsigned char letter = c;
    MSVCRT_wchar_t ret;

532
    if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
533 534 535 536 537 538
                0, (LPCSTR)&letter, 1, &ret, 1))
        return 0;

    return ret;
}

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
/*********************************************************************
 *              __crtGetStringTypeW(MSVCRT.@)
 *
 * This function was accepting different number of arguments in older
 * versions of msvcrt.
 */
BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
        MSVCRT_wchar_t *buffer, int len, WORD *out)
{
    FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
            unk, type, buffer, len, out);

    return GetStringTypeW(type, buffer, len, out);
}

554 555 556
/*********************************************************************
 *		localeconv (MSVCRT.@)
 */
557 558
struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
{
559
    return get_locinfo()->lconv;
560
}
561

562 563 564
/*********************************************************************
 *		__lconv_init (MSVCRT.@)
 */
565
int CDECL __lconv_init(void)
566
{
567 568
    /* this is used to make chars unsigned */
    charmax = 255;
569
    return 0;
570
}
571 572 573 574

/*********************************************************************
 *      ___lc_handle_func (MSVCRT.@)
 */
575
LCID* CDECL ___lc_handle_func(void)
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
{
    return MSVCRT___lc_handle;
}

/*********************************************************************
 *      ___lc_codepage_func (MSVCRT.@)
 */
int CDECL ___lc_codepage_func(void)
{
    return MSVCRT___lc_codepage;
}

/*********************************************************************
 *      ___lc_collate_cp_func (MSVCRT.@)
 */
int CDECL ___lc_collate_cp_func(void)
{
593
    return get_locinfo()->lc_collate_cp;
594
}
595

596 597
/* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
598 599 600
{
    int i;

601
    if(!locinfo)
602 603
        return;

604
    for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
605 606
        MSVCRT_free(locinfo->lc_category[i].locale);
        MSVCRT_free(locinfo->lc_category[i].refcount);
607 608
    }

609 610 611 612 613 614 615 616 617 618 619
    if(locinfo->lconv) {
        MSVCRT_free(locinfo->lconv->decimal_point);
        MSVCRT_free(locinfo->lconv->thousands_sep);
        MSVCRT_free(locinfo->lconv->grouping);
        MSVCRT_free(locinfo->lconv->int_curr_symbol);
        MSVCRT_free(locinfo->lconv->currency_symbol);
        MSVCRT_free(locinfo->lconv->mon_decimal_point);
        MSVCRT_free(locinfo->lconv->mon_thousands_sep);
        MSVCRT_free(locinfo->lconv->mon_grouping);
        MSVCRT_free(locinfo->lconv->positive_sign);
        MSVCRT_free(locinfo->lconv->negative_sign);
620
    }
621 622 623 624 625 626 627
    MSVCRT_free(locinfo->lconv_intl_refcount);
    MSVCRT_free(locinfo->lconv_num_refcount);
    MSVCRT_free(locinfo->lconv_mon_refcount);
    MSVCRT_free(locinfo->lconv);

    MSVCRT_free(locinfo->ctype1_refcount);
    MSVCRT_free(locinfo->ctype1);
628

629 630
    MSVCRT_free(locinfo->pclmap);
    MSVCRT_free(locinfo->pcumap);
631

632 633
    MSVCRT_free(locinfo);
}
634

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
/* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
{
    if(!mbcinfo)
        return;

    MSVCRT_free(mbcinfo);
}

/* _free_locale - not exported in native msvcrt */
void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
{
    if (!locale)
        return;

    free_locinfo(locale->locinfo);
    free_mbcinfo(locale->mbcinfo);
652 653 654 655
    MSVCRT_free(locale);
}

/* _create_locale - not exported in native msvcrt */
656
MSVCRT__locale_t MSVCRT__create_locale(int category, const char *locale)
657
{
658 659 660 661 662 663
    static const char collate[] = "COLLATE=";
    static const char ctype[] = "CTYPE=";
    static const char monetary[] = "MONETARY=";
    static const char numeric[] = "NUMERIC=";
    static const char time[] = "TIME=";

664
    MSVCRT__locale_t loc;
665
    LCID lcid[6] = { 0 };
666
    char buf[256];
667 668 669 670 671 672 673 674
    int i;

    TRACE("(%d %s)\n", category, locale);

    if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
        return NULL;

    if(locale[0]=='C' && !locale[1])
675
        lcid[0] = CP_ACP;
676
    else if(!locale[0])
677
        lcid[0] = GetSystemDefaultLCID();
678
    else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
679
        const char *p;
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701

        while(1) {
            locale += 3; /* LC_ */
            if(!memcmp(locale, collate, sizeof(collate)-1)) {
                i = MSVCRT_LC_COLLATE;
                locale += sizeof(collate)-1;
            } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
                i = MSVCRT_LC_CTYPE;
                locale += sizeof(ctype)-1;
            } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
                i = MSVCRT_LC_MONETARY;
                locale += sizeof(monetary)-1;
            } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
                i = MSVCRT_LC_NUMERIC;
                locale += sizeof(numeric)-1;
            } else if(!memcmp(locale, time, sizeof(time)-1)) {
                i = MSVCRT_LC_TIME;
                locale += sizeof(time)-1;
            } else
                return NULL;

            p = strchr(locale, ';');
702 703 704
            if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0'))
                lcid[i] = 0;
            else if(p) {
705
                memcpy(buf, locale, p-locale);
706
                buf[p-locale] = '\0';
707 708 709 710
                lcid[i] = MSVCRT_locale_to_LCID(buf);
            } else
                lcid[i] = MSVCRT_locale_to_LCID(locale);

711
            if(lcid[i] == -1)
712 713 714 715 716
                return NULL;

            if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
                break;

717
            locale = p+1;
718
        }
719
    } else {
720
        lcid[0] = MSVCRT_locale_to_LCID(locale);
721
        if(lcid[0] == -1)
722 723 724
            return NULL;
    }

725 726 727 728 729
    for(i=1; i<6; i++) {
        if(!lcid[i])
            lcid[i] = lcid[0];
    }

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
    if(!loc)
        return NULL;

    loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
    if(!loc->locinfo) {
        MSVCRT_free(loc);
        return NULL;
    }

    loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
    if(!loc->mbcinfo) {
        MSVCRT_free(loc->locinfo);
        MSVCRT_free(loc);
        return NULL;
    }

    memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
    memset(loc->mbcinfo, 0, sizeof(MSVCRT_threadmbcinfo));

    loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
    if(!loc->locinfo->lconv) {
752
        MSVCRT__free_locale(loc);
753 754 755 756 757 758 759
        return NULL;
    }
    memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));

    loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
    loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
    if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
760
        MSVCRT__free_locale(loc);
761 762 763 764 765
        return NULL;
    }

    loc->locinfo->refcount = 1;

766 767
    if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
        if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
768
            MSVCRT__free_locale(loc);
769 770
            return NULL;
        }
771
    } else
772
        loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = MSVCRT__strdup("C");
773

774
    if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
775
        CPINFO cp;
776
        int j;
777

778
        if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
779
            MSVCRT__free_locale(loc);
780 781 782 783
            return NULL;
        }

        loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
784
        loc->locinfo->lc_collate_cp = loc->locinfo->lc_codepage;
785 786
        loc->locinfo->lc_clike = 1;
        if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
787
            MSVCRT__free_locale(loc);
788 789 790 791 792 793 794
            return NULL;
        }
        loc->locinfo->mb_cur_max = cp.MaxCharSize;

        loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
        loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
        if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
795
            MSVCRT__free_locale(loc);
796 797 798 799 800 801 802 803 804 805 806
            return NULL;
        }

        *loc->locinfo->ctype1_refcount = 1;
        loc->locinfo->ctype1[0] = 0;
        loc->locinfo->pctype = loc->locinfo->ctype1+1;

        buf[1] = buf[2] = '\0';
        for(i=1; i<257; i++) {
            buf[0] = i-1;

807 808
            GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
                    1, loc->locinfo->ctype1+i);
809
        }
810 811 812 813

        for(i=0; cp.LeadByte[i+1]!=0; i+=2)
            for(j=cp.LeadByte[i]; j<=cp.LeadByte[i+1]; j++)
                loc->locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
814 815 816 817
    } else {
        loc->locinfo->lc_clike = 1;
        loc->locinfo->mb_cur_max = 1;
        loc->locinfo->pctype = MSVCRT__ctype+1;
818
        loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = MSVCRT__strdup("C");
819 820
    }

821 822 823 824 825 826 827
    for(i=0; i<256; i++) {
        if(loc->locinfo->pctype[i] & MSVCRT__LEADBYTE)
            buf[i] = ' ';
        else
            buf[i] = i;

    }
828

829 830 831 832 833 834 835 836 837 838 839
    if(lcid[MSVCRT_LC_CTYPE]) {
        LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
                (char*)loc->locinfo->pclmap, 256);
        LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
                (char*)loc->locinfo->pcumap, 256);
    } else {
        for(i=0; i<256; i++) {
            loc->locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
            loc->locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
        }
    }
840 841 842 843 844 845 846 847 848 849 850 851 852 853

    loc->mbcinfo->refcount = 1;
    loc->mbcinfo->mbcodepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;

    for(i=0; i<256; i++) {
        if(loc->locinfo->pclmap[i] != i) {
            loc->mbcinfo->mbctype[i+1] |= 0x10;
            loc->mbcinfo->mbcasemap[i] = loc->locinfo->pclmap[i];
        } else if(loc->locinfo->pcumap[i] != i) {
            loc->mbcinfo->mbctype[i+1] |= 0x20;
            loc->mbcinfo->mbcasemap[i] = loc->locinfo->pcumap[i];
        }
    }

854 855
    if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
        if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
856
            MSVCRT__free_locale(loc);
857 858 859 860 861 862
            return NULL;
        }

        loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
        loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
        if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
863
            MSVCRT__free_locale(loc);
864 865 866 867 868 869
            return NULL;
        }

        *loc->locinfo->lconv_intl_refcount = 1;
        *loc->locinfo->lconv_mon_refcount = 1;

870 871
        i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
                |LOCALE_NOUSEROVERRIDE, buf, 256);
872 873 874
        if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char[i]))))
            memcpy(loc->locinfo->lconv->int_curr_symbol, buf, sizeof(char[i]));
        else {
875
            MSVCRT__free_locale(loc);
876 877 878
            return NULL;
        }

879 880
        i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
                |LOCALE_NOUSEROVERRIDE, buf, 256);
881 882 883
        if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char[i]))))
            memcpy(loc->locinfo->lconv->currency_symbol, buf, sizeof(char[i]));
        else {
884
            MSVCRT__free_locale(loc);
885 886 887
            return NULL;
        }

888 889
        i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
                |LOCALE_NOUSEROVERRIDE, buf, 256);
890 891 892
        if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char[i]))))
            memcpy(loc->locinfo->lconv->mon_decimal_point, buf, sizeof(char[i]));
        else {
893
            MSVCRT__free_locale(loc);
894 895 896
            return NULL;
        }

897 898
        i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
                |LOCALE_NOUSEROVERRIDE, buf, 256);
899 900 901
        if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
            memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, sizeof(char[i]));
        else {
902
            MSVCRT__free_locale(loc);
903 904 905
            return NULL;
        }

906 907
        i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
                |LOCALE_NOUSEROVERRIDE, buf, 256);
908 909 910 911 912 913 914 915 916
        if(i>1)
            i = i/2 + (buf[i-2]=='0'?0:1);
        if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char[i])))) {
            for(i=0; buf[i+1]==';'; i+=2)
                loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
            loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
            if(buf[i] != '0')
                loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
        } else {
917
            MSVCRT__free_locale(loc);
918 919 920
            return NULL;
        }

921 922
        i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
                |LOCALE_NOUSEROVERRIDE, buf, 256);
923 924 925
        if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char[i]))))
            memcpy(loc->locinfo->lconv->positive_sign, buf, sizeof(char[i]));
        else {
926
            MSVCRT__free_locale(loc);
927 928 929
            return NULL;
        }

930 931
        i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
                |LOCALE_NOUSEROVERRIDE, buf, 256);
932 933 934
        if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char[i]))))
            memcpy(loc->locinfo->lconv->negative_sign, buf, sizeof(char[i]));
        else {
935
            MSVCRT__free_locale(loc);
936 937 938
            return NULL;
        }

939 940
        if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
                    |LOCALE_NOUSEROVERRIDE, buf, 256))
941 942
            loc->locinfo->lconv->int_frac_digits = atoi(buf);
        else {
943
            MSVCRT__free_locale(loc);
944 945 946
            return NULL;
        }

947 948
        if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
                    |LOCALE_NOUSEROVERRIDE, buf, 256))
949 950
            loc->locinfo->lconv->frac_digits = atoi(buf);
        else {
951
            MSVCRT__free_locale(loc);
952 953 954
            return NULL;
        }

955 956
        if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
                    |LOCALE_NOUSEROVERRIDE, buf, 256))
957 958
            loc->locinfo->lconv->p_cs_precedes = atoi(buf);
        else {
959
            MSVCRT__free_locale(loc);
960 961 962
            return NULL;
        }

963 964
        if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
                    |LOCALE_NOUSEROVERRIDE, buf, 256))
965 966
            loc->locinfo->lconv->p_sep_by_space = atoi(buf);
        else {
967
            MSVCRT__free_locale(loc);
968 969 970
            return NULL;
        }

971 972
        if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
                    |LOCALE_NOUSEROVERRIDE, buf, 256))
973 974
            loc->locinfo->lconv->n_cs_precedes = atoi(buf);
        else {
975
            MSVCRT__free_locale(loc);
976 977 978
            return NULL;
        }

979 980
        if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
                    |LOCALE_NOUSEROVERRIDE, buf, 256))
981 982
            loc->locinfo->lconv->n_sep_by_space = atoi(buf);
        else {
983
            MSVCRT__free_locale(loc);
984 985 986
            return NULL;
        }

987 988
        if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
                    |LOCALE_NOUSEROVERRIDE, buf, 256))
989 990
            loc->locinfo->lconv->p_sign_posn = atoi(buf);
        else {
991
            MSVCRT__free_locale(loc);
992 993 994
            return NULL;
        }

995 996
        if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
                    |LOCALE_NOUSEROVERRIDE, buf, 256))
997 998
            loc->locinfo->lconv->n_sign_posn = atoi(buf);
        else {
999
            MSVCRT__free_locale(loc);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
            return NULL;
        }
    } else {
        loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
        loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
        loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
        loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
        loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
        loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
        loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));

        if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
                || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
                || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
                || !loc->locinfo->lconv->negative_sign) {
1015
            MSVCRT__free_locale(loc);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
            return NULL;
        }

        loc->locinfo->lconv->int_curr_symbol[0] = '\0';
        loc->locinfo->lconv->currency_symbol[0] = '\0';
        loc->locinfo->lconv->mon_decimal_point[0] = '\0';
        loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
        loc->locinfo->lconv->mon_grouping[0] = '\0';
        loc->locinfo->lconv->positive_sign[0] = '\0';
        loc->locinfo->lconv->negative_sign[0] = '\0';
        loc->locinfo->lconv->int_frac_digits = 127;
        loc->locinfo->lconv->frac_digits = 127;
        loc->locinfo->lconv->p_cs_precedes = 127;
        loc->locinfo->lconv->p_sep_by_space = 127;
        loc->locinfo->lconv->n_cs_precedes = 127;
        loc->locinfo->lconv->n_sep_by_space = 127;
        loc->locinfo->lconv->p_sign_posn = 127;
        loc->locinfo->lconv->n_sign_posn = 127;
1034

1035
        loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = MSVCRT__strdup("C");
1036 1037
    }

1038 1039
    if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
        if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
1040
            MSVCRT__free_locale(loc);
1041 1042 1043 1044 1045 1046 1047
            return NULL;
        }

        if(!loc->locinfo->lconv_intl_refcount)
            loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
        loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
        if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1048
            MSVCRT__free_locale(loc);
1049 1050 1051 1052 1053 1054
            return NULL;
        }

        *loc->locinfo->lconv_intl_refcount = 1;
        *loc->locinfo->lconv_num_refcount = 1;

1055 1056
        i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
                |LOCALE_NOUSEROVERRIDE, buf, 256);
1057 1058 1059
        if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[i]))))
            memcpy(loc->locinfo->lconv->decimal_point, buf, sizeof(char[i]));
        else {
1060
            MSVCRT__free_locale(loc);
1061 1062 1063
            return NULL;
        }

1064 1065
        i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
                |LOCALE_NOUSEROVERRIDE, buf, 256);
1066 1067 1068
        if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
            memcpy(loc->locinfo->lconv->thousands_sep, buf, sizeof(char[i]));
        else {
1069
            MSVCRT__free_locale(loc);
1070 1071 1072
            return NULL;
        }

1073 1074
        i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
                |LOCALE_NOUSEROVERRIDE, buf, 256);
1075 1076 1077 1078 1079 1080 1081 1082 1083
        if(i>1)
            i = i/2 + (buf[i-2]=='0'?0:1);
        if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char[i])))) {
            for(i=0; buf[i+1]==';'; i+=2)
                loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
            loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
            if(buf[i] != '0')
                loc->locinfo->lconv->grouping[i/2+1] = 127;
        } else {
1084
            MSVCRT__free_locale(loc);
1085 1086 1087 1088 1089 1090 1091 1092
            return NULL;
        }
    } else {
        loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
        loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
        loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
        if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
                || !loc->locinfo->lconv->grouping) {
1093
            MSVCRT__free_locale(loc);
1094 1095 1096 1097 1098 1099 1100
            return NULL;
        }

        loc->locinfo->lconv->decimal_point[0] = '.';
        loc->locinfo->lconv->decimal_point[1] = '\0';
        loc->locinfo->lconv->thousands_sep[0] = '\0';
        loc->locinfo->lconv->grouping[0] = '\0';
1101

1102
        loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = MSVCRT__strdup("C");
1103 1104
    }

1105 1106
    if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
        if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1107
            MSVCRT__free_locale(loc);
1108 1109
            return NULL;
        }
1110
    } else
1111
        loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = MSVCRT__strdup("C");
1112 1113 1114

    return loc;
}
1115 1116 1117 1118 1119 1120

/*********************************************************************
 *             setlocale (MSVCRT.@)
 */
char* CDECL MSVCRT_setlocale(int category, const char* locale)
{
1121 1122
    MSVCRT__locale_t loc;
    MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1123

1124 1125 1126 1127 1128
    if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
        return NULL;

    if(!locale) {
        if(category == MSVCRT_LC_ALL)
1129
            return construct_lc_all(locinfo);
1130

1131
        return locinfo->lc_category[category].locale;
1132 1133
    }

1134
    loc = MSVCRT__create_locale(category, locale);
1135 1136
    if(!loc) {
        WARN("%d %s failed\n", category, locale);
1137
        return NULL;
1138
    }
1139 1140 1141 1142 1143 1144

    LOCK_LOCALE;

    switch(category) {
        case MSVCRT_LC_ALL:
        case MSVCRT_LC_COLLATE:
1145
            locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1146
                loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1147
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1148
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1149
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1150 1151 1152 1153 1154
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);

            if(category != MSVCRT_LC_ALL)
                break;
        case MSVCRT_LC_CTYPE:
1155
            locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1156
                loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1157
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1158
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1159
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1160 1161
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);

1162 1163 1164 1165
            locinfo->lc_codepage = loc->locinfo->lc_codepage;
            locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
            locinfo->lc_clike = loc->locinfo->lc_clike;
            locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1166

1167
            swap_pointers((void**)&locinfo->ctype1_refcount,
1168
                    (void**)&loc->locinfo->ctype1_refcount);
1169 1170 1171 1172
            swap_pointers((void**)&locinfo->ctype1, (void**)&loc->locinfo->ctype1);
            swap_pointers((void**)&locinfo->pctype, (void**)&loc->locinfo->pctype);
            swap_pointers((void**)&locinfo->pclmap, (void**)&loc->locinfo->pclmap);
            swap_pointers((void**)&locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1173

1174
            memcpy(get_mbcinfo(), loc->mbcinfo, sizeof(MSVCRT_threadmbcinfo));
1175 1176 1177 1178

            if(category != MSVCRT_LC_ALL)
                break;
        case MSVCRT_LC_MONETARY:
1179
            locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1180
                loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1181
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1182
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1183
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1184 1185
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);

1186
            swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1187
                    (void**)&loc->locinfo->lconv->int_curr_symbol);
1188
            swap_pointers((void**)&locinfo->lconv->currency_symbol,
1189
                    (void**)&loc->locinfo->lconv->currency_symbol);
1190
            swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1191
                    (void**)&loc->locinfo->lconv->mon_decimal_point);
1192
            swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1193
                    (void**)&loc->locinfo->lconv->mon_thousands_sep);
1194
            swap_pointers((void**)&locinfo->lconv->mon_grouping,
1195
                    (void**)&loc->locinfo->lconv->mon_grouping);
1196
            swap_pointers((void**)&locinfo->lconv->positive_sign,
1197
                    (void**)&loc->locinfo->lconv->positive_sign);
1198
            swap_pointers((void**)&locinfo->lconv->negative_sign,
1199
                    (void**)&loc->locinfo->lconv->negative_sign);
1200 1201 1202 1203 1204 1205 1206 1207
            locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
            locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
            locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
            locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
            locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
            locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
            locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
            locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1208 1209 1210 1211

            if(category != MSVCRT_LC_ALL)
                break;
        case MSVCRT_LC_NUMERIC:
1212
            locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1213
                loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1214
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1215
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1216
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1217 1218
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);

1219
            swap_pointers((void**)&locinfo->lconv->decimal_point,
1220
                    (void**)&loc->locinfo->lconv->decimal_point);
1221
            swap_pointers((void**)&locinfo->lconv->thousands_sep,
1222
                    (void**)&loc->locinfo->lconv->thousands_sep);
1223
            swap_pointers((void**)&locinfo->lconv->grouping,
1224 1225 1226 1227 1228
                    (void**)&loc->locinfo->lconv->grouping);

            if(category != MSVCRT_LC_ALL)
                break;
        case MSVCRT_LC_TIME:
1229
            locinfo->lc_handle[MSVCRT_LC_TIME] =
1230
                loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1231
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1232
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1233
            swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1234 1235 1236 1237 1238 1239
                    (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);

            if(category != MSVCRT_LC_ALL)
                break;
    }

1240
    MSVCRT__free_locale(loc);
1241 1242
    UNLOCK_LOCALE;

1243
    if(locinfo == MSVCRT_locale->locinfo) {
1244 1245
        int i;

1246 1247 1248 1249
        MSVCRT___lc_codepage = locinfo->lc_codepage;
        MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
        MSVCRT___mb_cur_max = locinfo->mb_cur_max;
        MSVCRT__pctype = locinfo->pctype;
1250 1251
        for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
            MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1252
    }
1253 1254

    if(category == MSVCRT_LC_ALL)
1255
        return construct_lc_all(locinfo);
1256

1257
    return locinfo->lc_category[category].locale;
1258
}
1259 1260 1261 1262 1263

/* _configthreadlocale - not exported in native msvcrt */
int CDECL _configthreadlocale(int type)
{
    thread_data_t *data = msvcrt_get_thread_data();
1264
    MSVCRT__locale_t locale;
1265 1266 1267 1268 1269
    int ret;

    if(!data)
        return -1;

1270
    ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1271 1272

    if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1273
        if(!data->have_locale) {
1274
            /* Copy current global locale */
1275 1276
            locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
            if(!locale)
1277
                return -1;
1278 1279 1280 1281 1282

            data->locinfo = locale->locinfo;
            data->mbcinfo = locale->mbcinfo;
            data->have_locale = TRUE;
            MSVCRT_free(locale);
1283 1284 1285 1286 1287 1288
        }

        return ret;
    }

    if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1289 1290 1291 1292 1293 1294
        if(data->have_locale) {
            free_locinfo(data->locinfo);
            free_mbcinfo(data->mbcinfo);
            data->locinfo = MSVCRT_locale->locinfo;
            data->mbcinfo = MSVCRT_locale->mbcinfo;
            data->have_locale = FALSE;
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
        }

        return ret;
    }

    if(!type)
        return ret;

    return -1;
}
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323

BOOL msvcrt_init_locale(void)
{
    int i;

    LOCK_LOCALE;
    MSVCRT_locale = MSVCRT__create_locale(0, "C");
    UNLOCK_LOCALE;
    if(!MSVCRT_locale)
        return FALSE;

    MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
    MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
    MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
    MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
    for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
        MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
    return TRUE;
}