]> gcc.gnu.org Git - gcc.git/blame - gcc/intl/localcharset.c
900227_01.C: short and pointer are the same size, so no warning should be produced...
[gcc.git] / gcc / intl / localcharset.c
CommitLineData
71a94577
ZW
1/* Determine a canonical name for the current locale's character encoding.
2
3 Copyright (C) 2000-2001 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published
7 by the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 USA. */
19
20/* Written by Bruno Haible <haible@clisp.cons.org>. */
21
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
25
26#if HAVE_STDDEF_H
27# include <stddef.h>
28#endif
29
30#include <stdio.h>
31#if HAVE_STRING_H
32# include <string.h>
33#else
34# include <strings.h>
35#endif
36#if HAVE_STDLIB_H
37# include <stdlib.h>
38#endif
39
40#if defined _WIN32 || defined __WIN32__
41# undef WIN32 /* avoid warning on mingw32 */
42# define WIN32
43#endif
44
45#ifndef WIN32
46# if HAVE_LANGINFO_CODESET
47# include <langinfo.h>
48# else
49# if HAVE_SETLOCALE
50# include <locale.h>
51# endif
52# endif
53#else /* WIN32 */
54# define WIN32_LEAN_AND_MEAN
55# include <windows.h>
56#endif
57
58#ifndef DIRECTORY_SEPARATOR
59# define DIRECTORY_SEPARATOR '/'
60#endif
61
62#ifndef ISSLASH
63# define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
64#endif
65
66/* The following static variable is declared 'volatile' to avoid a
67 possible multithread problem in the function get_charset_aliases. If we
68 are running in a threaded environment, and if two threads initialize
69 'charset_aliases' simultaneously, both will produce the same value,
70 and everything will be ok if the two assignments to 'charset_aliases'
71 are atomic. But I don't know what will happen if the two assignments mix. */
72#if __STDC__ != 1
73# define volatile /* empty */
74#endif
75/* Pointer to the contents of the charset.alias file, if it has already been
76 read, else NULL. Its format is:
77 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
78static const char * volatile charset_aliases;
79
77c06844
ZW
80/* GCC LOCAL: Static function prototypes. */
81static const char *get_charset_aliases PARAMS ((void));
82
71a94577
ZW
83/* Return a pointer to the contents of the charset.alias file. */
84static const char *
85get_charset_aliases ()
86{
87 const char *cp;
88
89 cp = charset_aliases;
90 if (cp == NULL)
91 {
92#ifndef WIN32
93 FILE *fp;
94 const char *dir = LIBDIR;
95 const char *base = "charset.alias";
96 char *file_name;
97
98 /* Concatenate dir and base into freshly allocated file_name. */
99 {
100 size_t dir_len = strlen (dir);
101 size_t base_len = strlen (base);
102 int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
103 file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
104 if (file_name != NULL)
105 {
106 memcpy (file_name, dir, dir_len);
107 if (add_slash)
108 file_name[dir_len] = DIRECTORY_SEPARATOR;
109 memcpy (file_name + dir_len + add_slash, base, base_len + 1);
110 }
111 }
112
113 if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
114 /* Out of memory or file not found, treat it as empty. */
115 cp = "";
116 else
117 {
118 /* Parse the file's contents. */
119 int c;
120 char buf1[50+1];
121 char buf2[50+1];
122 char *res_ptr = NULL;
123 size_t res_size = 0;
124 size_t l1, l2;
125
126 for (;;)
127 {
128 c = getc (fp);
129 if (c == EOF)
130 break;
131 if (c == '\n' || c == ' ' || c == '\t')
132 continue;
133 if (c == '#')
134 {
135 /* Skip comment, to end of line. */
136 do
137 c = getc (fp);
138 while (!(c == EOF || c == '\n'));
139 if (c == EOF)
140 break;
141 continue;
142 }
143 ungetc (c, fp);
144 if (fscanf(fp, "%50s %50s", buf1, buf2) < 2)
145 break;
146 l1 = strlen (buf1);
147 l2 = strlen (buf2);
148 if (res_size == 0)
149 {
150 res_size = l1 + 1 + l2 + 1;
151 res_ptr = malloc (res_size + 1);
152 }
153 else
154 {
155 res_size += l1 + 1 + l2 + 1;
156 res_ptr = realloc (res_ptr, res_size + 1);
157 }
158 if (res_ptr == NULL)
159 {
160 /* Out of memory. */
161 res_size = 0;
162 break;
163 }
164 strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
165 strcpy (res_ptr + res_size - (l2 + 1), buf2);
166 }
167 fclose (fp);
168 if (res_size == 0)
169 cp = "";
170 else
171 {
172 *(res_ptr + res_size) = '\0';
173 cp = res_ptr;
174 }
175 }
176
177 if (file_name != NULL)
178 free (file_name);
179
180#else /* WIN32 */
181
182 /* To avoid the troubles of installing a separate file in the same
183 directory as the DLL and of retrieving the DLL's directory at
184 runtime, simply inline the aliases here. */
185
186 cp = "CP936" "\0" "GBK" "\0"
187 "CP1361" "\0" "JOHAB" "\0";
188#endif
189
190 charset_aliases = cp;
191 }
192
193 return cp;
194}
195
196/* Determine the current locale's character encoding, and canonicalize it
197 into one of the canonical names listed in config.charset.
198 The result must not be freed; it is statically allocated.
199 If the canonical name cannot be determined, the result is a non-canonical
77c06844
ZW
200 name.
201 GCC LOCAL: Get rid of STATIC nonsense. */
71a94577 202
71a94577
ZW
203const char *
204locale_charset ()
205{
206 const char *codeset;
207 const char *aliases;
208
209#ifndef WIN32
210
211# if HAVE_LANGINFO_CODESET
212
213 /* Most systems support nl_langinfo (CODESET) nowadays. */
214 codeset = nl_langinfo (CODESET);
215
216# else
217
218 /* On old systems which lack it, use setlocale or getenv. */
219 const char *locale = NULL;
220
221 /* But most old systems don't have a complete set of locales. Some
222 (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
223 use setlocale here; it would return "C" when it doesn't support the
224 locale name the user has set. */
225# if HAVE_SETLOCALE && 0
226 locale = setlocale (LC_CTYPE, NULL);
227# endif
228 if (locale == NULL || locale[0] == '\0')
229 {
230 locale = getenv ("LC_ALL");
231 if (locale == NULL || locale[0] == '\0')
232 {
233 locale = getenv ("LC_CTYPE");
234 if (locale == NULL || locale[0] == '\0')
235 locale = getenv ("LANG");
236 }
237 }
238
239 /* On some old systems, one used to set locale = "iso8859_1". On others,
240 you set it to "language_COUNTRY.charset". In any case, we resolve it
241 through the charset.alias file. */
242 codeset = locale;
243
244# endif
245
246#else /* WIN32 */
247
248 static char buf[2 + 10 + 1];
249
250 /* Win32 has a function returning the locale's codepage as a number. */
251 sprintf (buf, "CP%u", GetACP ());
252 codeset = buf;
253
254#endif
255
256 if (codeset == NULL)
257 /* The canonical name cannot be determined. */
258 codeset = "";
259
260 /* Resolve alias. */
261 for (aliases = get_charset_aliases ();
262 *aliases != '\0';
263 aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
264 if (strcmp (codeset, aliases) == 0
265 || (aliases[0] == '*' && aliases[1] == '\0'))
266 {
267 codeset = aliases + strlen (aliases) + 1;
268 break;
269 }
270
271 return codeset;
272}
This page took 0.078574 seconds and 5 git commands to generate.