annotate intl/localcharset.c @ 1448:3b1c464cbbb0 trunk

[svn] Seems safer to me to free this way, assuming I'm not misreading. Hey. At least it doesn't crash.
author nemo
date Fri, 28 Jul 2006 07:21:20 -0700
parents cb178e5ad177
children f12d7e208b43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
1 /* Determine a canonical name for the current locale's character encoding.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
2
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
3 Copyright (C) 2000-2003 Free Software Foundation, Inc.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
4
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify it
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
6 under the terms of the GNU Library General Public License as published
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
7 by the Free Software Foundation; either version 2, or (at your option)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
8 any later version.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
9
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
13 Library General Public License for more details.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
14
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
15 You should have received a copy of the GNU Library General Public
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
16 License along with this program; if not, write to the Free Software
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
18 USA. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
19
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
20 /* Written by Bruno Haible <bruno@clisp.org>. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
21
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
22 #ifdef HAVE_CONFIG_H
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
23 # include <config.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
24 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
25
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
26 /* Specification. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
27 #include "localcharset.h"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
28
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
29 #if HAVE_STDDEF_H
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
30 # include <stddef.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
31 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
32
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
33 #include <stdio.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
34 #if HAVE_STRING_H
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
35 # include <string.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
36 #else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
37 # include <strings.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
38 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
39 #if HAVE_STDLIB_H
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
40 # include <stdlib.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
41 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
42
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
43 #if defined _WIN32 || defined __WIN32__
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
44 # undef WIN32 /* avoid warning on mingw32 */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
45 # define WIN32
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
46 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
47
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
48 #if defined __EMX__
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
49 /* Assume EMX program runs on OS/2, even if compiled under DOS. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
50 # define OS2
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
51 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
52
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
53 #if !defined WIN32
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
54 # if HAVE_LANGINFO_CODESET
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
55 # include <langinfo.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
56 # else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
57 # if HAVE_SETLOCALE
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
58 # include <locale.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
59 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
60 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
61 #elif defined WIN32
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
62 # define WIN32_LEAN_AND_MEAN
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
63 # include <windows.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
64 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
65 #if defined OS2
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
66 # define INCL_DOS
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
67 # include <os2.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
68 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
69
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
70 #if ENABLE_RELOCATABLE
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
71 # include "relocatable.h"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
72 #else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
73 # define relocate(pathname) (pathname)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
74 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
75
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
76 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
77 /* Win32, OS/2, DOS */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
78 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
79 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
80
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
81 #ifndef DIRECTORY_SEPARATOR
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
82 # define DIRECTORY_SEPARATOR '/'
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
83 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
84
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
85 #ifndef ISSLASH
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
86 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
87 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
88
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
89 #ifdef HAVE_GETC_UNLOCKED
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
90 # undef getc
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
91 # define getc getc_unlocked
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
92 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
93
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
94 /* The following static variable is declared 'volatile' to avoid a
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
95 possible multithread problem in the function get_charset_aliases. If we
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
96 are running in a threaded environment, and if two threads initialize
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
97 'charset_aliases' simultaneously, both will produce the same value,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
98 and everything will be ok if the two assignments to 'charset_aliases'
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
99 are atomic. But I don't know what will happen if the two assignments mix. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
100 #if __STDC__ != 1
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
101 # define volatile /* empty */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
102 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
103 /* Pointer to the contents of the charset.alias file, if it has already been
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
104 read, else NULL. Its format is:
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
105 ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0' */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
106 static const char * volatile charset_aliases;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
107
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
108 /* Return a pointer to the contents of the charset.alias file. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
109 static const char *
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
110 get_charset_aliases ()
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
111 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
112 const char *cp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
113
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
114 cp = charset_aliases;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
115 if (cp == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
116 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
117 #if !(defined VMS || defined WIN32)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
118 FILE *fp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
119 const char *dir = relocate (LIBDIR);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
120 const char *base = "charset.alias";
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
121 char *file_name;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
122
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
123 /* Concatenate dir and base into freshly allocated file_name. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
124 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
125 size_t dir_len = strlen (dir);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
126 size_t base_len = strlen (base);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
127 int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
128 file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
129 if (file_name != NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
130 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
131 memcpy (file_name, dir, dir_len);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
132 if (add_slash)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
133 file_name[dir_len] = DIRECTORY_SEPARATOR;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
134 memcpy (file_name + dir_len + add_slash, base, base_len + 1);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
135 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
136 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
137
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
138 if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
139 /* Out of memory or file not found, treat it as empty. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
140 cp = "";
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
141 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
142 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
143 /* Parse the file's contents. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
144 int c;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
145 char buf1[50+1];
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
146 char buf2[50+1];
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
147 char *res_ptr = NULL;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
148 size_t res_size = 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
149 size_t l1, l2;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
150
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
151 for (;;)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
152 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
153 c = getc (fp);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
154 if (c == EOF)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
155 break;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
156 if (c == '\n' || c == ' ' || c == '\t')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
157 continue;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
158 if (c == '#')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
159 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
160 /* Skip comment, to end of line. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
161 do
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
162 c = getc (fp);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
163 while (!(c == EOF || c == '\n'));
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
164 if (c == EOF)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
165 break;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
166 continue;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
167 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
168 ungetc (c, fp);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
169 if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
170 break;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
171 l1 = strlen (buf1);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
172 l2 = strlen (buf2);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
173 if (res_size == 0)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
174 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
175 res_size = l1 + 1 + l2 + 1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
176 res_ptr = (char *) malloc (res_size + 1);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
177 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
178 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
179 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
180 res_size += l1 + 1 + l2 + 1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
181 res_ptr = (char *) realloc (res_ptr, res_size + 1);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
182 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
183 if (res_ptr == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
184 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
185 /* Out of memory. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
186 res_size = 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
187 break;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
188 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
189 strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
190 strcpy (res_ptr + res_size - (l2 + 1), buf2);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
191 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
192 fclose (fp);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
193 if (res_size == 0)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
194 cp = "";
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
195 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
196 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
197 *(res_ptr + res_size) = '\0';
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
198 cp = res_ptr;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
199 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
200 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
201
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
202 if (file_name != NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
203 free (file_name);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
204
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
205 #else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
206
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
207 # if defined VMS
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
208 /* To avoid the troubles of an extra file charset.alias_vms in the
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
209 sources of many GNU packages, simply inline the aliases here. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
210 /* The list of encodings is taken from the OpenVMS 7.3-1 documentation
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
211 "Compaq C Run-Time Library Reference Manual for OpenVMS systems"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
212 section 10.7 "Handling Different Character Sets". */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
213 cp = "ISO8859-1" "\0" "ISO-8859-1" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
214 "ISO8859-2" "\0" "ISO-8859-2" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
215 "ISO8859-5" "\0" "ISO-8859-5" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
216 "ISO8859-7" "\0" "ISO-8859-7" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
217 "ISO8859-8" "\0" "ISO-8859-8" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
218 "ISO8859-9" "\0" "ISO-8859-9" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
219 /* Japanese */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
220 "eucJP" "\0" "EUC-JP" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
221 "SJIS" "\0" "SHIFT_JIS" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
222 "DECKANJI" "\0" "DEC-KANJI" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
223 "SDECKANJI" "\0" "EUC-JP" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
224 /* Chinese */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
225 "eucTW" "\0" "EUC-TW" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
226 "DECHANYU" "\0" "DEC-HANYU" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
227 "DECHANZI" "\0" "GB2312" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
228 /* Korean */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
229 "DECKOREAN" "\0" "EUC-KR" "\0";
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
230 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
231
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
232 # if defined WIN32
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
233 /* To avoid the troubles of installing a separate file in the same
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
234 directory as the DLL and of retrieving the DLL's directory at
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
235 runtime, simply inline the aliases here. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
236
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
237 cp = "CP936" "\0" "GBK" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
238 "CP1361" "\0" "JOHAB" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
239 "CP20127" "\0" "ASCII" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
240 "CP20866" "\0" "KOI8-R" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
241 "CP21866" "\0" "KOI8-RU" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
242 "CP28591" "\0" "ISO-8859-1" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
243 "CP28592" "\0" "ISO-8859-2" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
244 "CP28593" "\0" "ISO-8859-3" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
245 "CP28594" "\0" "ISO-8859-4" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
246 "CP28595" "\0" "ISO-8859-5" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
247 "CP28596" "\0" "ISO-8859-6" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
248 "CP28597" "\0" "ISO-8859-7" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
249 "CP28598" "\0" "ISO-8859-8" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
250 "CP28599" "\0" "ISO-8859-9" "\0"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
251 "CP28605" "\0" "ISO-8859-15" "\0";
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
252 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
253 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
254
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
255 charset_aliases = cp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
256 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
257
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
258 return cp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
259 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
260
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
261 /* Determine the current locale's character encoding, and canonicalize it
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
262 into one of the canonical names listed in config.charset.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
263 The result must not be freed; it is statically allocated.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
264 If the canonical name cannot be determined, the result is a non-canonical
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
265 name. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
266
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
267 #ifdef STATIC
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
268 STATIC
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
269 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
270 const char *
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
271 locale_charset ()
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
272 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
273 const char *codeset;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
274 const char *aliases;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
275
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
276 #if !(defined WIN32 || defined OS2)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
277
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
278 # if HAVE_LANGINFO_CODESET
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
279
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
280 /* Most systems support nl_langinfo (CODESET) nowadays. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
281 codeset = nl_langinfo (CODESET);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
282
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
283 # else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
284
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
285 /* On old systems which lack it, use setlocale or getenv. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
286 const char *locale = NULL;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
287
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
288 /* But most old systems don't have a complete set of locales. Some
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
289 (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
290 use setlocale here; it would return "C" when it doesn't support the
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
291 locale name the user has set. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
292 # if HAVE_SETLOCALE && 0
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
293 locale = setlocale (LC_CTYPE, NULL);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
294 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
295 if (locale == NULL || locale[0] == '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
296 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
297 locale = getenv ("LC_ALL");
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
298 if (locale == NULL || locale[0] == '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
299 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
300 locale = getenv ("LC_CTYPE");
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
301 if (locale == NULL || locale[0] == '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
302 locale = getenv ("LANG");
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
303 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
304 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
305
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
306 /* On some old systems, one used to set locale = "iso8859_1". On others,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
307 you set it to "language_COUNTRY.charset". In any case, we resolve it
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
308 through the charset.alias file. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
309 codeset = locale;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
310
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
311 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
312
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
313 #elif defined WIN32
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
314
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
315 static char buf[2 + 10 + 1];
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
316
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
317 /* Woe32 has a function returning the locale's codepage as a number. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
318 sprintf (buf, "CP%u", GetACP ());
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
319 codeset = buf;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
320
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
321 #elif defined OS2
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
322
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
323 const char *locale;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
324 static char buf[2 + 10 + 1];
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
325 ULONG cp[3];
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
326 ULONG cplen;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
327
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
328 /* Allow user to override the codeset, as set in the operating system,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
329 with standard language environment variables. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
330 locale = getenv ("LC_ALL");
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
331 if (locale == NULL || locale[0] == '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
332 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
333 locale = getenv ("LC_CTYPE");
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
334 if (locale == NULL || locale[0] == '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
335 locale = getenv ("LANG");
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
336 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
337 if (locale != NULL && locale[0] != '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
338 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
339 /* If the locale name contains an encoding after the dot, return it. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
340 const char *dot = strchr (locale, '.');
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
341
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
342 if (dot != NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
343 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
344 const char *modifier;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
345
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
346 dot++;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
347 /* Look for the possible @... trailer and remove it, if any. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
348 modifier = strchr (dot, '@');
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
349 if (modifier == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
350 return dot;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
351 if (modifier - dot < sizeof (buf))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
352 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
353 memcpy (buf, dot, modifier - dot);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
354 buf [modifier - dot] = '\0';
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
355 return buf;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
356 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
357 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
358
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
359 /* Resolve through the charset.alias file. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
360 codeset = locale;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
361 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
362 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
363 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
364 /* OS/2 has a function returning the locale's codepage as a number. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
365 if (DosQueryCp (sizeof (cp), cp, &cplen))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
366 codeset = "";
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
367 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
368 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
369 sprintf (buf, "CP%u", cp[0]);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
370 codeset = buf;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
371 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
372 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
373
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
374 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
375
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
376 if (codeset == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
377 /* The canonical name cannot be determined. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
378 codeset = "";
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
379
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
380 /* Resolve alias. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
381 for (aliases = get_charset_aliases ();
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
382 *aliases != '\0';
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
383 aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
384 if (strcmp (codeset, aliases) == 0
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
385 || (aliases[0] == '*' && aliases[1] == '\0'))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
386 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
387 codeset = aliases + strlen (aliases) + 1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
388 break;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
389 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
390
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
391 /* Don't return an empty string. GNU libc and GNU libiconv interpret
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
392 the empty string as denoting "the locale's character encoding",
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
393 thus GNU libiconv would call this function a second time. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
394 if (codeset[0] == '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
395 codeset = "ASCII";
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
396
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
397 return codeset;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
398 }