|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * win32setlocale.c |
| 4 | + * Wrapper to work around bugs in Windows setlocale() implementation |
| 5 | + * |
| 6 | + * Copyright (c) 2011, PostgreSQL Global Development Group |
| 7 | + * |
| 8 | + * IDENTIFICATION |
| 9 | + * src/port/win32setlocale.c |
| 10 | + * |
| 11 | + * |
| 12 | + * Windows has a problem with locale names that have a dot in the country |
| 13 | + * name. For example: |
| 14 | + * |
| 15 | + * "Chinese (Traditional)_Hong Kong S.A.R..950" |
| 16 | + * |
| 17 | + * For some reason, setlocale() doesn't accept that. Fortunately, Windows' |
| 18 | + * setlocale() accepts various alternative names for such countries, so we |
| 19 | + * provide a wrapper setlocale() function that maps the troublemaking locale |
| 20 | + * names to accepted aliases. |
| 21 | + *------------------------------------------------------------------------- |
| 22 | + */ |
| 23 | + |
| 24 | +#include "c.h" |
| 25 | + |
| 26 | +#undef setlocale |
| 27 | + |
| 28 | +struct locale_map |
| 29 | +{ |
| 30 | + const char *locale_name_part; /* string in locale name to replace */ |
| 31 | + const char *replacement; /* string to replace it with */ |
| 32 | +}; |
| 33 | + |
| 34 | +static const struct locale_map locale_map_list[] = { |
| 35 | + |
| 36 | + /* |
| 37 | + * "HKG" is listed here: |
| 38 | + * http://msdn.microsoft.com/en-us/library/cdax410z%28v=vs.71%29.aspx |
| 39 | + * (Country/Region Strings). |
| 40 | + * |
| 41 | + * "ARE" is the ISO-3166 three-letter code for U.A.E. It is not on the |
| 42 | + * above list, but seems to work anyway. |
| 43 | + */ |
| 44 | + { "Hong Kong S.A.R.", "HKG" }, |
| 45 | + { "U.A.E.", "ARE" }, |
| 46 | + |
| 47 | + /* |
| 48 | + * The ISO-3166 country code for Macau S.A.R. is MAC, but Windows doesn't |
| 49 | + * seem to recognize that. And Macau isn't listed in the table of |
| 50 | + * accepted abbreviations linked above. Fortunately, "ZHM" seems to be |
| 51 | + * accepted as an alias for "Chinese (Traditional)_Macau S.A.R..950". I'm |
| 52 | + * not sure where "ZHM" comes from, must be some legacy naming scheme. But |
| 53 | + * hey, it works. |
| 54 | + * |
| 55 | + * Note that unlike HKG and ARE, ZHM is an alias for the *whole* locale |
| 56 | + * name, not just the country part. |
| 57 | + * |
| 58 | + * Some versions of Windows spell it "Macau", others "Macao". |
| 59 | + */ |
| 60 | + { "Chinese (Traditional)_Macau S.A.R..950", "ZHM" }, |
| 61 | + { "Chinese_Macau S.A.R..950", "ZHM" }, |
| 62 | + { "Chinese (Traditional)_Macao S.A.R..950", "ZHM" }, |
| 63 | + { "Chinese_Macao S.A.R..950", "ZHM" } |
| 64 | +}; |
| 65 | + |
| 66 | +char * |
| 67 | +pgwin32_setlocale(int category, const char *locale) |
| 68 | +{ |
| 69 | + char *result; |
| 70 | + char *alias; |
| 71 | + int i; |
| 72 | + |
| 73 | + if (locale == NULL) |
| 74 | + return setlocale(category, locale); |
| 75 | + |
| 76 | + /* Check if the locale name matches any of the problematic ones. */ |
| 77 | + alias = NULL; |
| 78 | + for (i = 0; i < lengthof(locale_map_list); i++) |
| 79 | + { |
| 80 | + const char *needle = locale_map_list[i].locale_name_part; |
| 81 | + const char *replacement = locale_map_list[i].replacement; |
| 82 | + char *match; |
| 83 | + |
| 84 | + match = strstr(locale, needle); |
| 85 | + if (match != NULL) |
| 86 | + { |
| 87 | + /* Found a match. Replace the matched string. */ |
| 88 | + int matchpos = match - locale; |
| 89 | + int replacementlen = strlen(replacement); |
| 90 | + char *rest = match + strlen(needle); |
| 91 | + int restlen = strlen(rest); |
| 92 | + |
| 93 | + alias = malloc(matchpos + replacementlen + restlen + 1); |
| 94 | + if (!alias) |
| 95 | + return NULL; |
| 96 | + |
| 97 | + memcpy(&alias[0], &locale[0], matchpos); |
| 98 | + memcpy(&alias[matchpos], replacement, replacementlen); |
| 99 | + memcpy(&alias[matchpos + replacementlen], rest, restlen + 1); /* includes null terminator */ |
| 100 | + |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /* Call the real setlocale() function */ |
| 106 | + if (alias) |
| 107 | + { |
| 108 | + result = setlocale(category, alias); |
| 109 | + free(alias); |
| 110 | + } |
| 111 | + else |
| 112 | + result = setlocale(category, locale); |
| 113 | + |
| 114 | + return result; |
| 115 | +} |
0 commit comments