annotate intl/localealias.c @ 1371:ca4f3115d33c trunk

[svn] - recursively locate album art, by external contributor Oliver Lumpton.
author nenolod
date Tue, 04 Jul 2006 04:59:27 -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 /* Handle aliases for locale names.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
2 Copyright (C) 1995-1999, 2000-2001, 2003 Free Software Foundation, Inc.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
3
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify it
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
5 under the terms of the GNU Library General Public License as published
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
6 by the Free Software Foundation; either version 2, or (at your option)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
7 any later version.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
8
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
12 Library General Public License for more details.
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
13
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
14 You should have received a copy of the GNU Library General Public
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
15 License along with this program; if not, write to the Free Software
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
17 USA. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
18
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
19 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
20 This must come before <config.h> because <config.h> may include
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
21 <features.h>, and once <features.h> has been included, it's too late. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
22 #ifndef _GNU_SOURCE
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
23 # define _GNU_SOURCE 1
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 #ifdef HAVE_CONFIG_H
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
27 # include <config.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
28 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
29
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
30 #include <ctype.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
31 #include <stdio.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
32 #if defined _LIBC || defined HAVE___FSETLOCKING
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
33 # include <stdio_ext.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
34 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
35 #include <sys/types.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
36
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
37 #ifdef __GNUC__
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
38 # undef alloca
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
39 # define alloca __builtin_alloca
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
40 # define HAVE_ALLOCA 1
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
41 #else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
42 # ifdef _MSC_VER
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
43 # include <malloc.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
44 # define alloca _alloca
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
45 # else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
46 # if defined HAVE_ALLOCA_H || defined _LIBC
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
47 # include <alloca.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
48 # else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
49 # ifdef _AIX
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
50 #pragma alloca
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
51 # else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
52 # ifndef alloca
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
53 char *alloca ();
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
54 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
55 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
56 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
57 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
58 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
59
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
60 #include <stdlib.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
61 #include <string.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
62
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
63 #include "gettextP.h"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
64
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
65 #if ENABLE_RELOCATABLE
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
66 # include "relocatable.h"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
67 #else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
68 # define relocate(pathname) (pathname)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
69 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
70
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
71 /* @@ end of prolog @@ */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
72
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
73 #ifdef _LIBC
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
74 /* Rename the non ANSI C functions. This is required by the standard
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
75 because some ANSI C functions will require linking with this object
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
76 file and the name space must not be polluted. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
77 # define strcasecmp __strcasecmp
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
78
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
79 # ifndef mempcpy
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
80 # define mempcpy __mempcpy
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
81 # endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
82 # define HAVE_MEMPCPY 1
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
83 # define HAVE___FSETLOCKING 1
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
84
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
85 /* We need locking here since we can be called from different places. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
86 # include <bits/libc-lock.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
87
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
88 __libc_lock_define_initialized (static, lock);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
89 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
90
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
91 #ifndef internal_function
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
92 # define internal_function
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
93 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
94
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
95 /* Some optimizations for glibc. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
96 #ifdef _LIBC
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
97 # define FEOF(fp) feof_unlocked (fp)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
98 # define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
99 #else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
100 # define FEOF(fp) feof (fp)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
101 # define FGETS(buf, n, fp) fgets (buf, n, fp)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
102 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
103
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
104 /* For those losing systems which don't have `alloca' we have to add
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
105 some additional code emulating it. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
106 #ifdef HAVE_ALLOCA
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
107 # define freea(p) /* nothing */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
108 #else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
109 # define alloca(n) malloc (n)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
110 # define freea(p) free (p)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
111 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
112
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
113 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
114 # undef fgets
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
115 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
116 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
117 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
118 # undef feof
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
119 # define feof(s) feof_unlocked (s)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
120 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
121
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
122
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
123 struct alias_map
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
124 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
125 const char *alias;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
126 const char *value;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
127 };
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
128
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
129
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
130 #ifndef _LIBC
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
131 # define libc_freeres_ptr(decl) decl
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
132 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
133
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
134 libc_freeres_ptr (static char *string_space);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
135 static size_t string_space_act;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
136 static size_t string_space_max;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
137 libc_freeres_ptr (static struct alias_map *map);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
138 static size_t nmap;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
139 static size_t maxmap;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
140
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
141
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
142 /* Prototypes for local functions. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
143 static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
144 internal_function;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
145 static int extend_alias_table PARAMS ((void));
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
146 static int alias_compare PARAMS ((const struct alias_map *map1,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
147 const struct alias_map *map2));
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
148
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
149
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
150 const char *
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
151 _nl_expand_alias (name)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
152 const char *name;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
153 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
154 static const char *locale_alias_path;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
155 struct alias_map *retval;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
156 const char *result = NULL;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
157 size_t added;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
158
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
159 #ifdef _LIBC
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
160 __libc_lock_lock (lock);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
161 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
162
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
163 if (locale_alias_path == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
164 locale_alias_path = LOCALE_ALIAS_PATH;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
165
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
166 do
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
167 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
168 struct alias_map item;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
169
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
170 item.alias = name;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
171
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
172 if (nmap > 0)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
173 retval = (struct alias_map *) bsearch (&item, map, nmap,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
174 sizeof (struct alias_map),
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
175 (int (*) PARAMS ((const void *,
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
176 const void *))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
177 ) alias_compare);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
178 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
179 retval = NULL;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
180
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
181 /* We really found an alias. Return the value. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
182 if (retval != NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
183 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
184 result = retval->value;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
185 break;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
186 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
187
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
188 /* Perhaps we can find another alias file. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
189 added = 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
190 while (added == 0 && locale_alias_path[0] != '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
191 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
192 const char *start;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
193
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
194 while (locale_alias_path[0] == PATH_SEPARATOR)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
195 ++locale_alias_path;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
196 start = locale_alias_path;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
197
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
198 while (locale_alias_path[0] != '\0'
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
199 && locale_alias_path[0] != PATH_SEPARATOR)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
200 ++locale_alias_path;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
201
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
202 if (start < locale_alias_path)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
203 added = read_alias_file (start, locale_alias_path - start);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
204 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
205 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
206 while (added != 0);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
207
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
208 #ifdef _LIBC
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
209 __libc_lock_unlock (lock);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
210 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
211
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
212 return result;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
213 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
214
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
215
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
216 static size_t
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
217 internal_function
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
218 read_alias_file (fname, fname_len)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
219 const char *fname;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
220 int fname_len;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
221 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
222 FILE *fp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
223 char *full_fname;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
224 size_t added;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
225 static const char aliasfile[] = "/locale.alias";
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
226
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
227 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
228 #ifdef HAVE_MEMPCPY
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
229 mempcpy (mempcpy (full_fname, fname, fname_len),
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
230 aliasfile, sizeof aliasfile);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
231 #else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
232 memcpy (full_fname, fname, fname_len);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
233 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
234 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
235
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
236 fp = fopen (relocate (full_fname), "r");
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
237 freea (full_fname);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
238 if (fp == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
239 return 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
240
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
241 #ifdef HAVE___FSETLOCKING
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
242 /* No threads present. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
243 __fsetlocking (fp, FSETLOCKING_BYCALLER);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
244 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
245
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
246 added = 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
247 while (!FEOF (fp))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
248 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
249 /* It is a reasonable approach to use a fix buffer here because
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
250 a) we are only interested in the first two fields
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
251 b) these fields must be usable as file names and so must not
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
252 be that long
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
253 We avoid a multi-kilobyte buffer here since this would use up
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
254 stack space which we might not have if the program ran out of
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
255 memory. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
256 char buf[400];
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
257 char *alias;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
258 char *value;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
259 char *cp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
260
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
261 if (FGETS (buf, sizeof buf, fp) == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
262 /* EOF reached. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
263 break;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
264
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
265 cp = buf;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
266 /* Ignore leading white space. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
267 while (isspace ((unsigned char) cp[0]))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
268 ++cp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
269
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
270 /* A leading '#' signals a comment line. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
271 if (cp[0] != '\0' && cp[0] != '#')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
272 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
273 alias = cp++;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
274 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
275 ++cp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
276 /* Terminate alias name. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
277 if (cp[0] != '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
278 *cp++ = '\0';
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
279
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
280 /* Now look for the beginning of the value. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
281 while (isspace ((unsigned char) cp[0]))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
282 ++cp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
283
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
284 if (cp[0] != '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
285 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
286 size_t alias_len;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
287 size_t value_len;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
288
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
289 value = cp++;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
290 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
291 ++cp;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
292 /* Terminate value. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
293 if (cp[0] == '\n')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
294 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
295 /* This has to be done to make the following test
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
296 for the end of line possible. We are looking for
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
297 the terminating '\n' which do not overwrite here. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
298 *cp++ = '\0';
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
299 *cp = '\n';
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
300 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
301 else if (cp[0] != '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
302 *cp++ = '\0';
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
303
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
304 if (nmap >= maxmap)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
305 if (__builtin_expect (extend_alias_table (), 0))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
306 return added;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
307
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
308 alias_len = strlen (alias) + 1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
309 value_len = strlen (value) + 1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
310
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
311 if (string_space_act + alias_len + value_len > string_space_max)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
312 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
313 /* Increase size of memory pool. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
314 size_t new_size = (string_space_max
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
315 + (alias_len + value_len > 1024
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
316 ? alias_len + value_len : 1024));
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
317 char *new_pool = (char *) realloc (string_space, new_size);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
318 if (new_pool == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
319 return added;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
320
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
321 if (__builtin_expect (string_space != new_pool, 0))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
322 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
323 size_t i;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
324
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
325 for (i = 0; i < nmap; i++)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
326 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
327 map[i].alias += new_pool - string_space;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
328 map[i].value += new_pool - string_space;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
329 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
330 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
331
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
332 string_space = new_pool;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
333 string_space_max = new_size;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
334 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
335
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
336 map[nmap].alias = memcpy (&string_space[string_space_act],
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
337 alias, alias_len);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
338 string_space_act += alias_len;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
339
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
340 map[nmap].value = memcpy (&string_space[string_space_act],
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
341 value, value_len);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
342 string_space_act += value_len;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
343
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
344 ++nmap;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
345 ++added;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
346 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
347 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
348
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
349 /* Possibly not the whole line fits into the buffer. Ignore
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
350 the rest of the line. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
351 while (strchr (buf, '\n') == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
352 if (FGETS (buf, sizeof buf, fp) == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
353 /* Make sure the inner loop will be left. The outer loop
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
354 will exit at the `feof' test. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
355 break;
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 /* Should we test for ferror()? I think we have to silently ignore
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
359 errors. --drepper */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
360 fclose (fp);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
361
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
362 if (added > 0)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
363 qsort (map, nmap, sizeof (struct alias_map),
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
364 (int (*) PARAMS ((const void *, const void *))) alias_compare);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
365
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
366 return added;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
367 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
368
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
369
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
370 static int
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
371 extend_alias_table ()
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
372 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
373 size_t new_size;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
374 struct alias_map *new_map;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
375
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
376 new_size = maxmap == 0 ? 100 : 2 * maxmap;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
377 new_map = (struct alias_map *) realloc (map, (new_size
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
378 * sizeof (struct alias_map)));
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
379 if (new_map == NULL)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
380 /* Simply don't extend: we don't have any more core. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
381 return -1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
382
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
383 map = new_map;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
384 maxmap = new_size;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
385 return 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
386 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
387
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
388
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
389 static int
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
390 alias_compare (map1, map2)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
391 const struct alias_map *map1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
392 const struct alias_map *map2;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
393 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
394 #if defined _LIBC || defined HAVE_STRCASECMP
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
395 return strcasecmp (map1->alias, map2->alias);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
396 #else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
397 const unsigned char *p1 = (const unsigned char *) map1->alias;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
398 const unsigned char *p2 = (const unsigned char *) map2->alias;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
399 unsigned char c1, c2;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
400
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
401 if (p1 == p2)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
402 return 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
403
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
404 do
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
405 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
406 /* I know this seems to be odd but the tolower() function in
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
407 some systems libc cannot handle nonalpha characters. */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
408 c1 = isupper (*p1) ? tolower (*p1) : *p1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
409 c2 = isupper (*p2) ? tolower (*p2) : *p2;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
410 if (c1 == '\0')
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
411 break;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
412 ++p1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
413 ++p2;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
414 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
415 while (c1 == c2);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
416
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
417 return c1 - c2;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
418 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
419 }