1 | /* $Id: RTLocaleQueryNormalizedBaseLocaleName-win.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTLocaleQueryNormalizedBaseLocaleName, ring-3, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2017-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/win/windows.h>
|
---|
32 |
|
---|
33 | #include <iprt/locale.h>
|
---|
34 | #include "internal/iprt.h"
|
---|
35 |
|
---|
36 | #include <iprt/ctype.h>
|
---|
37 | #include <iprt/errcore.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | RTDECL(int) RTLocaleQueryNormalizedBaseLocaleName(char *pszName, size_t cbName)
|
---|
42 | {
|
---|
43 | /*
|
---|
44 | * Note! This part is duplicate of r3/generic/RTLocaleQueryNormalizedBaseLocaleName-r3-generic.cpp!
|
---|
45 | */
|
---|
46 | char szLocale[_1K];
|
---|
47 | int rc = RTLocaleQueryLocaleName(szLocale, sizeof(szLocale));
|
---|
48 | if (RT_SUCCESS(rc))
|
---|
49 | {
|
---|
50 | /*
|
---|
51 | * May return some complicated "LC_XXX=yyy;LC.." sequence if
|
---|
52 | * partially set (like IPRT does). Try get xx_YY sequence first
|
---|
53 | * because 'C' or 'POSIX' may be LC_xxx variants that haven't been
|
---|
54 | * set yet.
|
---|
55 | *
|
---|
56 | * ASSUMES complicated locale mangling is done in a certain way...
|
---|
57 | */
|
---|
58 | const char *pszLocale = strchr(szLocale, '=');
|
---|
59 | if (!pszLocale)
|
---|
60 | pszLocale = szLocale;
|
---|
61 | else
|
---|
62 | pszLocale++;
|
---|
63 | bool fSeenC = false;
|
---|
64 | bool fSeenPOSIX = false;
|
---|
65 | do
|
---|
66 | {
|
---|
67 | const char *pszEnd = strchr(pszLocale, ';');
|
---|
68 |
|
---|
69 | if ( RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(pszLocale)
|
---|
70 | && ( pszLocale[5] == '\0'
|
---|
71 | || RT_C_IS_PUNCT(pszLocale[5])) )
|
---|
72 | return RTStrCopyEx(pszName, cbName, pszLocale, 5);
|
---|
73 |
|
---|
74 | if ( pszLocale[0] == 'C'
|
---|
75 | && ( pszLocale[1] == '\0'
|
---|
76 | || RT_C_IS_PUNCT(pszLocale[1])) )
|
---|
77 | fSeenC = true;
|
---|
78 | else if ( strncmp(pszLocale, "POSIX", 5) == 0
|
---|
79 | && ( pszLocale[5] == '\0'
|
---|
80 | || RT_C_IS_PUNCT(pszLocale[5])) )
|
---|
81 | fSeenPOSIX = true;
|
---|
82 |
|
---|
83 | /* advance */
|
---|
84 | pszLocale = pszEnd ? strchr(pszEnd + 1, '=') : NULL;
|
---|
85 | } while (pszLocale++);
|
---|
86 |
|
---|
87 | if (fSeenC || fSeenPOSIX)
|
---|
88 | return RTStrCopy(pszName, cbName, "C"); /* C and POSIX should be identical IIRC, so keep it simple. */
|
---|
89 |
|
---|
90 | rc = VERR_NOT_AVAILABLE;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /*
|
---|
94 | * Fallback.
|
---|
95 | */
|
---|
96 | if ( GetLocaleInfoA(GetUserDefaultLCID(), LOCALE_SISO639LANGNAME, szLocale, sizeof(szLocale)) == 3
|
---|
97 | && GetLocaleInfoA(GetUserDefaultLCID(), LOCALE_SISO3166CTRYNAME, &szLocale[3], sizeof(szLocale) - 4) == 3)
|
---|
98 | {
|
---|
99 | szLocale[2] = '_';
|
---|
100 | Assert(RTLOCALE_IS_LANGUAGE2_UNDERSCORE_COUNTRY2(szLocale));
|
---|
101 | return RTStrCopy(pszName, cbName, szLocale);
|
---|
102 | }
|
---|
103 |
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 |
|
---|