|
| 1 | +/** |
| 2 | + * @file locale_charset.c |
| 3 | + * @brief Detect the locale charset. |
| 4 | + * @copyright Copyright (C) 2024 The C++ Plus Project. |
| 5 | + */ |
| 6 | +/* |
| 7 | + * This file is part of the cppp-reiconv library. |
| 8 | + * |
| 9 | + * The cppp-reiconv library is free software; you can redistribute it |
| 10 | + * and/or modify it under the terms of the GNU Lesser General Public |
| 11 | + * License as published by the Free Software Foundation; either version 3 |
| 12 | + * of the License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * The cppp-reiconv library is distributed in the hope that it will be |
| 15 | + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with the cppp-reiconv library; see the file LICENSE. |
| 21 | + * If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | + |
| 24 | +#include "cppp/cppp-platform.h" |
| 25 | + |
| 26 | +#include <locale.h> |
| 27 | +#include <stddef.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <stdlib.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +#include "localecharset/lc_types.h" |
| 33 | +#include "localecharset/lc_utils.h" |
| 34 | + |
| 35 | +#if IS_DARWIN7 |
| 36 | +#include <xlocale.h> |
| 37 | +#endif // IS_DARWIN7 |
| 38 | + |
| 39 | +#if HAVE_LANGINFO_CODESET |
| 40 | +#include <langinfo.h> |
| 41 | +#endif // HAVE_LANGINFO_CODESET |
| 42 | + |
| 43 | +#if IS_WINDOWS_NATIVE || __has_cygwin__ |
| 44 | +#define WIN32_LEAN_AND_MEAN |
| 45 | +#include "localecharset/windows_getcp.h" |
| 46 | + |
| 47 | +#include <Windows.h> |
| 48 | + |
| 49 | +/* |
| 50 | + * For the use of setlocale() below, the Gnulib override in setlocale.c is |
| 51 | + * not needed; see the platform lists in setlocale_null.m4. |
| 52 | + */ |
| 53 | +#undef setlocale |
| 54 | +#endif // IS_WINDOWS_NATIVE || __has_cygwin__ |
| 55 | + |
| 56 | +#if !HAVE_LANGINFO_CODESET && !WINDOWS_NATIVE |
| 57 | +#include "localecharset/locale_table.h" |
| 58 | +#endif // !HAVE_LANGINFO_CODESET && !WINDOWS_NATIVE |
| 59 | + |
| 60 | +const char *locale_charset() |
| 61 | +{ |
| 62 | +#if IS_WINDOWS_NATIVE |
| 63 | + return windows_getcp(); |
| 64 | +#endif // IS_WINDOWS_NATIVE |
| 65 | + |
| 66 | +#if IS_MACOSX || __has_beos__ || __has_haiku__ || __has_android__ |
| 67 | + /* |
| 68 | + * On Mac OS X, all modern locales use the UTF-8 encoding. |
| 69 | + * BeOS, Haiku and Android have a single locale, and it has UTF-8 encoding. |
| 70 | + */ |
| 71 | + return "UTF-8"; |
| 72 | +#endif // IS_MACOSX || __has_beos__ || __has_haiku__ || __has_android__ |
| 73 | + |
| 74 | + const char *codeset = NULL; |
| 75 | + |
| 76 | +#if HAVE_LANGINFO_CODESET |
| 77 | + return nl_langinfo(CODESET); |
| 78 | +#endif // HAVE_LANGINFO_CODESET |
| 79 | + |
| 80 | +#if __has_cygwin__ |
| 81 | + return windows_getcp(); |
| 82 | +#endif // __has_cygwin__ |
| 83 | + |
| 84 | + const char *locale = NULL; |
| 85 | + GET_LOCALE(locale); |
| 86 | + |
| 87 | +#if locale_table_defined |
| 88 | + // The locale_table is sorted. Perform a binary search. |
| 89 | + size_t hi = LOCALE_TABLE_SIZE; |
| 90 | + size_t lo = 0; |
| 91 | + while (lo < hi) |
| 92 | + { |
| 93 | + /* |
| 94 | + * Invariant: |
| 95 | + * for i < lo, strcmp (locale_table[i].locale, locale) < 0, |
| 96 | + * for i >= hi, strcmp (locale_table[i].locale, locale) > 0. |
| 97 | + */ |
| 98 | + size_t mid = (hi + lo) >> 1; // >= lo, < hi |
| 99 | + int cmp = strcmp(locale_table[mid].locale, locale); |
| 100 | + if (cmp < 0) |
| 101 | + { |
| 102 | + lo = mid + 1; |
| 103 | + } |
| 104 | + else if (cmp > 0) |
| 105 | + { |
| 106 | + hi = mid; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + // Found an i with strcmp (locale_table[i].locale, locale) == 0. |
| 111 | + codeset = locale_table[mid].canonical; |
| 112 | + goto done_table_lookup; |
| 113 | + } |
| 114 | + } |
| 115 | +#endif // locale_table_defined |
| 116 | + // Cannot find the locale in the table, try to split it. |
| 117 | + const char *p = strchr(locale, '.'); |
| 118 | + if (p == NULL) |
| 119 | + { |
| 120 | + // No dot. |
| 121 | + return DEFAULT_CHARSET; |
| 122 | + } |
| 123 | + |
| 124 | + // Split the locale into two parts. |
| 125 | + codeset = p + 1; |
| 126 | + return codeset; |
| 127 | + |
| 128 | +done_table_lookup: |
| 129 | + |
| 130 | +#if IS_DARWIN7 |
| 131 | + /* Mac OS X sets MB_CUR_MAX to 1 when LC_ALL=C, and "UTF-8" |
| 132 | + (the default codeset) does not work when MB_CUR_MAX is 1. */ |
| 133 | + if (strcmp(codeset, "UTF-8") == 0 && MB_CUR_MAX_L(uselocale(NULL)) <= 1) |
| 134 | + { |
| 135 | + codeset = "ASCII"; |
| 136 | + } |
| 137 | +#endif // DARWIN7 |
| 138 | + |
| 139 | + return codeset; |
| 140 | +} |
0 commit comments