1 /*-------------------------------------------------------------------------
4 * Functions for handling locale-related info
7 * Copyright (c) 1996-2007, PostgreSQL Global Development Group
11 * $PostgreSQL: pgsql/src/port/chklocale.c,v 1.1 2007/09/28 22:25:49 tgl Exp $
13 *-------------------------------------------------------------------------
19 #include "postgres_fe.h"
23 #ifdef HAVE_LANGINFO_H
27 #include "mb/pg_wchar.h"
30 #if defined(HAVE_LANGINFO_H) && defined(CODESET)
33 * This table needs to recognize all the CODESET spellings for supported
34 * backend encodings. We don't need to handle frontend-only encodings.
35 * Note that we search the table with pg_strcasecmp(), so variant
36 * capitalizations don't need their own entries.
40 enum pg_enc pg_enc_code;
41 const char *system_enc_name;
44 static const struct encoding_match encoding_match_list[] = {
45 {PG_EUC_JP, "EUC-JP"},
47 {PG_EUC_JP, "IBM-eucJP"},
48 {PG_EUC_JP, "sdeckanji"},
50 {PG_EUC_CN, "EUC-CN"},
52 {PG_EUC_CN, "IBM-eucCN"},
53 {PG_EUC_CN, "GB2312"},
54 {PG_EUC_CN, "dechanzi"},
56 {PG_EUC_KR, "EUC-KR"},
58 {PG_EUC_KR, "IBM-eucKR"},
59 {PG_EUC_KR, "deckorean"},
62 {PG_EUC_TW, "EUC-TW"},
64 {PG_EUC_TW, "IBM-eucTW"},
65 {PG_EUC_TW, "cns11643"},
70 {PG_LATIN1, "ISO-8859-1"},
71 {PG_LATIN1, "ISO8859-1"},
72 {PG_LATIN1, "iso88591"},
74 {PG_LATIN2, "ISO-8859-2"},
75 {PG_LATIN2, "ISO8859-2"},
76 {PG_LATIN2, "iso88592"},
78 {PG_LATIN3, "ISO-8859-3"},
79 {PG_LATIN3, "ISO8859-3"},
80 {PG_LATIN3, "iso88593"},
82 {PG_LATIN4, "ISO-8859-4"},
83 {PG_LATIN4, "ISO8859-4"},
84 {PG_LATIN4, "iso88594"},
86 {PG_LATIN5, "ISO-8859-9"},
87 {PG_LATIN5, "ISO8859-9"},
88 {PG_LATIN5, "iso88599"},
90 {PG_LATIN6, "ISO-8859-10"},
91 {PG_LATIN6, "ISO8859-10"},
92 {PG_LATIN6, "iso885910"},
94 {PG_LATIN7, "ISO-8859-13"},
95 {PG_LATIN7, "ISO8859-13"},
96 {PG_LATIN7, "iso885913"},
98 {PG_LATIN8, "ISO-8859-14"},
99 {PG_LATIN8, "ISO8859-14"},
100 {PG_LATIN8, "iso885914"},
102 {PG_LATIN9, "ISO-8859-15"},
103 {PG_LATIN9, "ISO8859-15"},
104 {PG_LATIN9, "iso885915"},
106 {PG_LATIN10, "ISO-8859-16"},
107 {PG_LATIN10, "ISO8859-16"},
108 {PG_LATIN10, "iso885916"},
110 {PG_KOI8R, "KOI8-R"},
112 {PG_WIN1252, "CP1252"},
113 {PG_WIN1253, "CP1253"},
114 {PG_WIN1254, "CP1254"},
115 {PG_WIN1255, "CP1255"},
116 {PG_WIN1256, "CP1256"},
117 {PG_WIN1257, "CP1257"},
118 {PG_WIN1258, "CP1258"},
122 {PG_WIN1251, "CP1251"},
123 {PG_WIN866, "CP866"},
125 {PG_ISO_8859_5, "ISO-8859-5"},
126 {PG_ISO_8859_5, "ISO8859-5"},
127 {PG_ISO_8859_5, "iso88595"},
129 {PG_ISO_8859_6, "ISO-8859-6"},
130 {PG_ISO_8859_6, "ISO8859-6"},
131 {PG_ISO_8859_6, "iso88596"},
133 {PG_ISO_8859_7, "ISO-8859-7"},
134 {PG_ISO_8859_7, "ISO8859-7"},
135 {PG_ISO_8859_7, "iso88597"},
137 {PG_ISO_8859_8, "ISO-8859-8"},
138 {PG_ISO_8859_8, "ISO8859-8"},
139 {PG_ISO_8859_8, "iso88598"},
141 {PG_SQL_ASCII, NULL} /* end marker */
146 * Given a setting for LC_CTYPE, return the Postgres ID of the associated
147 * encoding, if we can determine it.
149 * Pass in NULL to get the encoding for the current locale setting.
151 * If the result is PG_SQL_ASCII, callers should treat it as being compatible
152 * with any desired encoding. We return this if the locale is C/POSIX or we
153 * can't determine the encoding.
156 pg_get_encoding_from_locale(const char *ctype)
165 save = setlocale(LC_CTYPE, NULL);
167 return PG_SQL_ASCII; /* setlocale() broken? */
168 /* must copy result, or it might change after setlocale */
171 return PG_SQL_ASCII; /* out of memory; unlikely */
173 if (!setlocale(LC_CTYPE, ctype))
176 return PG_SQL_ASCII; /* bogus ctype passed in? */
179 sys = nl_langinfo(CODESET);
183 setlocale(LC_CTYPE, save);
189 ctype = setlocale(LC_CTYPE, NULL);
191 return PG_SQL_ASCII; /* setlocale() broken? */
192 sys = nl_langinfo(CODESET);
198 return PG_SQL_ASCII; /* out of memory; unlikely */
200 if (pg_strcasecmp(ctype, "C") == 0 || pg_strcasecmp(ctype, "POSIX") == 0)
206 for (i = 0; encoding_match_list[i].system_enc_name; i++)
208 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
211 return encoding_match_list[i].pg_enc_code;
216 * We print a warning if we got a CODESET string but couldn't recognize
217 * it. This means we need another entry in the table.
220 fprintf(stderr, _("could not determine encoding for locale \"%s\": codeset is \"%s\""),
222 /* keep newline separate so there's only one translatable string */
226 (errmsg("could not determine encoding for locale \"%s\": codeset is \"%s\"",
228 errdetail("Please report this to <pgsql-bugs@postgresql.org>.")));
235 #else /* !(HAVE_LANGINFO_H && CODESET) */
238 * stub if no platform support
241 pg_get_encoding_from_locale(const char *ctype)
246 #endif /* HAVE_LANGINFO_H && CODESET */