root/src/text/wincp.c
| Revision d666030b2349e8a710fcba4d2cabb912cc700580, 6.9 kB (checked in by Rémi Denis-Courmont <rdenis@simphalempin.com>, 4 months ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /***************************************************************************** |
| 2 | * wincp.c: Guessing "local" ANSI code page on Microsoft Windows® |
| 3 | ***************************************************************************** |
| 4 | * |
| 5 | * Copyright © 2006-2007 Rémi Denis-Courmont |
| 6 | * $Id$ |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
| 21 | *****************************************************************************/ |
| 22 | |
| 23 | /*** We need your help to complete this file!! Look for FIXME ***/ |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | # include "config.h" |
| 27 | #endif |
| 28 | |
| 29 | #include <vlc_common.h> |
| 30 | |
| 31 | #ifndef WIN32 |
| 32 | # include <locale.h> |
| 33 | #else |
| 34 | # include <windows.h> |
| 35 | #endif |
| 36 | |
| 37 | #ifdef __APPLE__ |
| 38 | # include <errno.h> |
| 39 | # include <string.h> |
| 40 | #endif |
| 41 | |
| 42 | #include <vlc_charset.h> |
| 43 | |
| 44 | |
| 45 | #ifndef WIN32 /* should work on Win32, but useless */ |
| 46 | static inline int locale_match (const char *tab, const char *locale) |
| 47 | { |
| 48 | for (;*tab; tab += 2) |
| 49 | if (memcmp (tab, locale, 2) == 0) |
| 50 | return 0; |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * @return a fallback characters encoding to be used, given a locale. |
| 57 | */ |
| 58 | static const char *FindFallbackEncoding (const char *locale) |
| 59 | { |
| 60 | if ((locale == NULL) || (strlen (locale) < 2) |
| 61 | || !strcasecmp (locale, "POSIX")) |
| 62 | return "CP1252"; /* Yeah, this is totally western-biased */ |
| 63 | |
| 64 | |
| 65 | /*** The ISO-8859 series (anything but Asia) ***/ |
| 66 | // Latin-1 Western-European languages (ISO-8859-1) |
| 67 | static const char western[] = |
| 68 | "aa" "af" "an" "br" "ca" "da" "de" "en" "es" "et" "eu" "fi" "fo" "fr" |
| 69 | "ga" "gd" "gl" "gv" "id" "is" "it" "kl" "kw" "mg" "ms" "nb" "nl" "nn" |
| 70 | "no" "oc" "om" "pt" "so" "sq" "st" "sv" "tl" "uz" "wa" "xh" "zu" |
| 71 | "eo" "mt" "cy"; |
| 72 | if (!locale_match (western, locale)) |
| 73 | return "CP1252"; // Compatible Microsoft superset |
| 74 | |
| 75 | // Latin-2 Slavic languages (ISO-8859-2) |
| 76 | static const char slavic[] = "bs" "cs" "hr" "hu" "pl" "ro" "sk" "sl"; |
| 77 | if (!locale_match (slavic, locale)) |
| 78 | return "CP1250"; // CP1250 is more common, but incompatible |
| 79 | |
| 80 | // Latin-3 Southern European languages (ISO-8859-3) |
| 81 | // "eo" and "mt" -> Latin-1 instead, I presume(?). |
| 82 | // "tr" -> ISO-8859-9 instead |
| 83 | |
| 84 | // Latin-4 North-European languages (ISO-8859-4) |
| 85 | // -> Latin-1 instead |
| 86 | |
| 87 | /* Cyrillic alphabet languages (ISO-8859-5) */ |
| 88 | static const char cyrillic[] = "be" "bg" "mk" "ru" "sr"; |
| 89 | if (!locale_match (cyrillic, locale)) |
| 90 | return "CP1251"; // KOI8, ISO-8859-5 and CP1251 are incompatible(?) |
| 91 | |
| 92 | /* Arabic (ISO-8859-6) */ |
| 93 | if (!locale_match ("ar", locale)) |
| 94 | // FIXME: someone check if we should return CP1256 or ISO-8859-6 |
| 95 | return "CP1256"; // CP1256 is(?) more common, but incompatible(?) |
| 96 | |
| 97 | /* Greek (ISO-8859-7) */ |
| 98 | if (!locale_match ("el", locale)) |
| 99 | // FIXME: someone check if we should return CP1253 or ISO-8859-7 |
| 100 | return "CP1253"; // CP1253 is(?) more common and less incompatible |
| 101 | |
| 102 | /* Hebrew (ISO-8859-8) */ |
| 103 | if (!locale_match ("he" "iw" "yi", locale)) |
| 104 | return "ISO-8859-8"; // CP1255 is reportedly screwed up |
| 105 | |
| 106 | /* Latin-5 Turkish (ISO-8859-9) */ |
| 107 | if (!locale_match ("tr" "ku", locale)) |
| 108 | return "CP1254"; // Compatible Microsoft superset |
| 109 | |
| 110 | /* Latin-6 “North-European” languages (ISO-8859-10) */ |
| 111 | /* It is so much north European that glibc only uses that for Luganda |
| 112 | * which is spoken in Uganda... unless someone complains, I'm not |
| 113 | * using this one; let's fallback to CP1252 here. */ |
| 114 | |
| 115 | // ISO-8859-11 does arguably not exist. Thai is handled below. |
| 116 | |
| 117 | // ISO-8859-12 really doesn't exist. |
| 118 | |
| 119 | // Latin-7 Baltic languages (ISO-8859-13) |
| 120 | if (!locale_match ("lt" "lv" "mi", locale)) |
| 121 | // FIXME: mi = New Zealand, doesn't sound baltic! |
| 122 | return "CP1257"; // Compatible Microsoft superset |
| 123 | |
| 124 | // Latin-8 Celtic languages (ISO-8859-14) |
| 125 | // "cy" -> use Latin-1 instead (most likely English or French) |
| 126 | |
| 127 | // Latin-9 (ISO-8859-15) -> see Latin-1 |
| 128 | |
| 129 | // Latin-10 (ISO-8859-16) does not seem to be used |
| 130 | |
| 131 | /*** KOI series ***/ |
| 132 | // For Russian, we use CP1251 |
| 133 | if (!locale_match ("uk", locale)) |
| 134 | return "KOI8-U"; |
| 135 | |
| 136 | if (!locale_match ("tg", locale)) |
| 137 | return "KOI8-T"; |
| 138 | |
| 139 | /*** Asia ***/ |
| 140 | // Japanese |
| 141 | if (!locale_match ("jp", locale)) |
| 142 | return "SHIFT-JIS"; // Shift-JIS is way more common than EUC-JP |
| 143 | |
| 144 | // Korean |
| 145 | if (!locale_match ("ko", locale)) |
| 146 | return "EUC-KR"; |
| 147 | |
| 148 | // Thai |
| 149 | if (!locale_match ("th", locale)) |
| 150 | return "TIS-620"; |
| 151 | |
| 152 | // Vietnamese (FIXME: more infos needed) |
| 153 | if (!locale_match ("vt", locale)) |
| 154 | /* VISCII is probably a bad idea as it is not extended ASCII */ |
| 155 | /* glibc has TCVN5712-1 */ |
| 156 | return "CP1258"; |
| 157 | |
| 158 | /* Kazakh (FIXME: more infos needed) */ |
| 159 | if (!locale_match ("kk", locale)) |
| 160 | return "PT154"; |
| 161 | |
| 162 | // Chinese. The politically incompatible character sets. |
| 163 | if (!locale_match ("zh", locale)) |
| 164 | { |
| 165 | if ((strlen (locale) >= 5) && (locale[2] != '_')) |
| 166 | locale += 3; |
| 167 | |
| 168 | // Hong Kong |
| 169 | if (!locale_match ("HK", locale)) |
| 170 | return "BIG5-HKSCS"; /* FIXME: use something else? */ |
| 171 | |
| 172 | // Taiwan island |
| 173 | if (!locale_match ("TW", locale)) |
| 174 | return "BIG5"; |
| 175 | |
| 176 | // People's Republic of China and Singapore |
| 177 | /* |
| 178 | * GB18030 can represent any Unicode code point |
| 179 | * (like UTF-8), while remaining compatible with GBK |
| 180 | * FIXME: is it compatible with GB2312? if not, should we |
| 181 | * use GB2312 instead? |
| 182 | */ |
| 183 | return "GB18030"; |
| 184 | } |
| 185 | |
| 186 | return "ASCII"; |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | /** |
| 191 | * GetFallbackEncoding() suggests an encoding to be used for non UTF-8 |
| 192 | * text files accord to the system's local settings. It is only a best |
| 193 | * guess. |
| 194 | */ |
| 195 | const char *GetFallbackEncoding( void ) |
| 196 | { |
| 197 | #ifndef WIN32 |
| 198 | const char *psz_lang; |
| 199 | |
| 200 | psz_lang = getenv ("LC_ALL"); |
| 201 | if ((psz_lang == NULL) || !*psz_lang) |
| 202 | { |
| 203 | psz_lang = getenv ("LC_CTYPE"); |
| 204 | if ((psz_lang == NULL) || !*psz_lang) |
| 205 | psz_lang = getenv ("LANG"); |
| 206 | } |
| 207 | |
| 208 | return FindFallbackEncoding (psz_lang); |
| 209 | #else |
| 210 | static char buf[16] = ""; |
| 211 | |
| 212 | if (buf[0] == 0) |
| 213 | { |
| 214 | int cp = GetACP (); |
| 215 | |
| 216 | switch (cp) |
| 217 | { |
| 218 | case 1255: // Hebrew, CP1255 screws up somewhat |
| 219 | strcpy (buf, "ISO-8859-8"); |
| 220 | break; |
| 221 | default: |
| 222 | snprintf (buf, sizeof (buf), "CP%u", cp); |
| 223 | } |
| 224 | } |
| 225 | return buf; |
| 226 | #endif |
| 227 | } |
Note: See TracBrowser for help on using the browser.
