PostgreSQL Source Code git master
Loading...
Searching...
No Matches
win32setlocale.c File Reference
#include "c.h"
Include dependency graph for win32setlocale.c:

Go to the source code of this file.

Data Structures

struct  locale_map
 

Macros

#define MAX_LOCALE_NAME_LEN   100
 

Functions

static const charmap_locale (const struct locale_map *map, const char *locale)
 
charpgwin32_setlocale (int category, const char *locale)
 

Variables

static const struct locale_map locale_map_argument []
 
static const struct locale_map locale_map_result []
 

Macro Definition Documentation

◆ MAX_LOCALE_NAME_LEN

#define MAX_LOCALE_NAME_LEN   100

Definition at line 108 of file win32setlocale.c.

Function Documentation

◆ map_locale()

static const char * map_locale ( const struct locale_map map,
const char locale 
)
static

Definition at line 111 of file win32setlocale.c.

112{
113 static char aliasbuf[MAX_LOCALE_NAME_LEN];
114 int i;
115
116 /* Check if the locale name matches any of the problematic ones. */
117 for (i = 0; map[i].locale_name_start != NULL; i++)
118 {
119 const char *needle_start = map[i].locale_name_start;
120 const char *needle_end = map[i].locale_name_end;
121 const char *replacement = map[i].replacement;
122 const char *match;
123 const char *match_start = NULL;
124 const char *match_end = NULL;
125
126 match = strstr(locale, needle_start);
127 if (match)
128 {
129 /*
130 * Found a match for the first part. If this was a two-part
131 * replacement, find the second part.
132 */
133 match_start = match;
134 if (needle_end)
135 {
137 if (match)
138 match_end = match + strlen(needle_end);
139 else
141 }
142 else
144 }
145
146 if (match_start)
147 {
148 /* Found a match. Replace the matched string. */
149 int matchpos = match_start - locale;
150 int replacementlen = strlen(replacement);
151 const char *rest = match_end;
152 int restlen = strlen(rest);
153
154 /* check that the result fits in the static buffer */
156 return NULL;
157
158 memcpy(&aliasbuf[0], &locale[0], matchpos);
159 memcpy(&aliasbuf[matchpos], replacement, replacementlen);
160 /* includes null terminator */
162
163 return aliasbuf;
164 }
165 }
166
167 /* no match, just return the original string */
168 return locale;
169}
int i
Definition isn.c:77
static int fb(int x)
const char * locale_name_end
const char * locale_name_start
const char * replacement
#define MAX_LOCALE_NAME_LEN

References fb(), i, locale_map::locale_name_end, locale_map::locale_name_start, MAX_LOCALE_NAME_LEN, and locale_map::replacement.

Referenced by pgwin32_setlocale().

◆ pgwin32_setlocale()

char * pgwin32_setlocale ( int  category,
const char locale 
)

Definition at line 172 of file win32setlocale.c.

173{
174 const char *argument;
175 char *result;
176
177 if (locale == NULL)
178 argument = NULL;
179 else
181
182 /* Call the real setlocale() function */
183 result = setlocale(category, argument);
184
185 /*
186 * setlocale() is specified to return a "char *" that the caller is
187 * forbidden to modify, so casting away the "const" is innocuous.
188 */
189 if (result)
190 result = unconstify(char *, map_locale(locale_map_result, result));
191
192 return result;
193}
#define unconstify(underlying_type, expr)
Definition c.h:1240
#define setlocale(a, b)
Definition win32_port.h:472
static const struct locale_map locale_map_argument[]
static const struct locale_map locale_map_result[]
static const char * map_locale(const struct locale_map *map, const char *locale)

References fb(), locale_map_argument, locale_map_result, map_locale(), setlocale, and unconstify.

Variable Documentation

◆ locale_map_argument

const struct locale_map locale_map_argument[]
static
Initial value:
= {
{"Hong Kong S.A.R.", NULL, "HKG"},
{"U.A.E.", NULL, "ARE"},
{"Chinese (Traditional)_Macau S.A.R..950", NULL, "ZHM"},
{"Chinese_Macau S.A.R..950", NULL, "ZHM"},
{"Chinese (Traditional)_Macao S.A.R..950", NULL, "ZHM"},
{"Chinese_Macao S.A.R..950", NULL, "ZHM"},
}

Definition at line 57 of file win32setlocale.c.

57 {
58 /*
59 * "HKG" is listed here:
60 * http://msdn.microsoft.com/en-us/library/cdax410z%28v=vs.71%29.aspx
61 * (Country/Region Strings).
62 *
63 * "ARE" is the ISO-3166 three-letter code for U.A.E. It is not on the
64 * above list, but seems to work anyway.
65 */
66 {"Hong Kong S.A.R.", NULL, "HKG"},
67 {"U.A.E.", NULL, "ARE"},
68
69 /*
70 * The ISO-3166 country code for Macau S.A.R. is MAC, but Windows doesn't
71 * seem to recognize that. And Macau isn't listed in the table of accepted
72 * abbreviations linked above. Fortunately, "ZHM" seems to be accepted as
73 * an alias for "Chinese (Traditional)_Macau S.A.R..950". I'm not sure
74 * where "ZHM" comes from, must be some legacy naming scheme. But hey, it
75 * works.
76 *
77 * Note that unlike HKG and ARE, ZHM is an alias for the *whole* locale
78 * name, not just the country part.
79 *
80 * Some versions of Windows spell it "Macau", others "Macao".
81 */
82 {"Chinese (Traditional)_Macau S.A.R..950", NULL, "ZHM"},
83 {"Chinese_Macau S.A.R..950", NULL, "ZHM"},
84 {"Chinese (Traditional)_Macao S.A.R..950", NULL, "ZHM"},
85 {"Chinese_Macao S.A.R..950", NULL, "ZHM"},
86 {NULL, NULL, NULL}
87};

Referenced by pgwin32_setlocale().

◆ locale_map_result

const struct locale_map locale_map_result[]
static
Initial value:
= {
{"Norwegian (Bokm", "l)_Norway", "Norwegian_Norway"},
{"Norwegian Bokm", "l_Norway", "Norwegian_Norway"},
}

Definition at line 92 of file win32setlocale.c.

92 {
93 /*
94 * "Norwegian (Bokmål)" locale name contains the a-ring character.
95 * Map it to a pure-ASCII alias.
96 *
97 * It's not clear what encoding setlocale() uses when it returns the
98 * locale name, so to play it safe, we search for "Norwegian (Bok*l)".
99 *
100 * Just to make life even more complicated, some versions of Windows spell
101 * the locale name without parentheses. Translate that too.
102 */
103 {"Norwegian (Bokm", "l)_Norway", "Norwegian_Norway"},
104 {"Norwegian Bokm", "l_Norway", "Norwegian_Norway"},
105 {NULL, NULL, NULL}
106};

Referenced by pgwin32_setlocale().