PowerPC - logb[f|l] optimization for POWER7
[glibc.git] / intl / l10nflist.c
blobb67f8d423568a559459e749ec8b85b6051c76b8f
1 /* Copyright (C) 1995-2002, 2004, 2005, 2011 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
22 #ifndef _GNU_SOURCE
23 # define _GNU_SOURCE 1
24 #endif
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
30 #include <string.h>
32 #if defined _LIBC || defined HAVE_ARGZ_H
33 # include <argz.h>
34 #endif
35 #include <ctype.h>
36 #include <sys/types.h>
37 #include <stdlib.h>
39 #include "loadinfo.h"
41 /* On some strange systems still no definition of NULL is found. Sigh! */
42 #ifndef NULL
43 # if defined __STDC__ && __STDC__
44 # define NULL ((void *) 0)
45 # else
46 # define NULL 0
47 # endif
48 #endif
50 /* @@ end of prolog @@ */
52 #ifdef _LIBC
53 /* Rename the non ANSI C functions. This is required by the standard
54 because some ANSI C functions will require linking with this object
55 file and the name space must not be polluted. */
56 # ifndef stpcpy
57 # define stpcpy(dest, src) __stpcpy(dest, src)
58 # endif
59 #else
60 # ifndef HAVE_STPCPY
61 static char *stpcpy PARAMS ((char *dest, const char *src));
62 # endif
63 #endif
65 /* Define function which are usually not available. */
67 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
68 /* Returns the number of strings in ARGZ. */
69 static size_t argz_count__ PARAMS ((const char *argz, size_t len));
71 static size_t
72 argz_count__ (argz, len)
73 const char *argz;
74 size_t len;
76 size_t count = 0;
77 while (len > 0)
79 size_t part_len = strlen (argz);
80 argz += part_len + 1;
81 len -= part_len + 1;
82 count++;
84 return count;
86 # undef __argz_count
87 # define __argz_count(argz, len) argz_count__ (argz, len)
88 #else
89 # ifdef _LIBC
90 # define __argz_count(argz, len) INTUSE(__argz_count) (argz, len)
91 # endif
92 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
94 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
95 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
96 except the last into the character SEP. */
97 static void argz_stringify__ PARAMS ((char *argz, size_t len, int sep));
99 static void
100 argz_stringify__ (argz, len, sep)
101 char *argz;
102 size_t len;
103 int sep;
105 while (len > 0)
107 size_t part_len = strlen (argz);
108 argz += part_len;
109 len -= part_len + 1;
110 if (len > 0)
111 *argz++ = sep;
114 # undef __argz_stringify
115 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
116 #else
117 # ifdef _LIBC
118 # define __argz_stringify(argz, len, sep) \
119 INTUSE(__argz_stringify) (argz, len, sep)
120 # endif
121 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
123 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
124 static char *argz_next__ PARAMS ((char *argz, size_t argz_len,
125 const char *entry));
127 static char *
128 argz_next__ (argz, argz_len, entry)
129 char *argz;
130 size_t argz_len;
131 const char *entry;
133 if (entry)
135 if (entry < argz + argz_len)
136 entry = strchr (entry, '\0') + 1;
138 return entry >= argz + argz_len ? NULL : (char *) entry;
140 else
141 if (argz_len > 0)
142 return argz;
143 else
144 return 0;
146 # undef __argz_next
147 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
148 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
151 /* Return number of bits set in X. */
152 #ifndef ARCH_POP
153 static int pop PARAMS ((int x));
155 static inline int
156 pop (x)
157 int x;
159 /* We assume that no more than 16 bits are used. */
160 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
161 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
162 x = ((x >> 4) + x) & 0x0f0f;
163 x = ((x >> 8) + x) & 0xff;
165 return x;
167 #endif
170 struct loaded_l10nfile *
171 _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
172 territory, codeset, normalized_codeset, modifier,
173 filename, do_allocate)
174 struct loaded_l10nfile **l10nfile_list;
175 const char *dirlist;
176 size_t dirlist_len;
177 int mask;
178 const char *language;
179 const char *territory;
180 const char *codeset;
181 const char *normalized_codeset;
182 const char *modifier;
183 const char *filename;
184 int do_allocate;
186 char *abs_filename;
187 struct loaded_l10nfile *last = NULL;
188 struct loaded_l10nfile *retval;
189 char *cp;
190 size_t entries;
191 int cnt;
193 /* Allocate room for the full file name. */
194 abs_filename = (char *) malloc (dirlist_len
195 + strlen (language)
196 + ((mask & XPG_TERRITORY) != 0
197 ? strlen (territory) + 1 : 0)
198 + ((mask & XPG_CODESET) != 0
199 ? strlen (codeset) + 1 : 0)
200 + ((mask & XPG_NORM_CODESET) != 0
201 ? strlen (normalized_codeset) + 1 : 0)
202 + ((mask & XPG_MODIFIER) != 0
203 ? strlen (modifier) + 1 : 0)
204 + 1 + strlen (filename) + 1);
206 if (abs_filename == NULL)
207 return NULL;
209 retval = NULL;
210 last = NULL;
212 /* Construct file name. */
213 memcpy (abs_filename, dirlist, dirlist_len);
214 __argz_stringify (abs_filename, dirlist_len, ':');
215 cp = abs_filename + (dirlist_len - 1);
216 *cp++ = '/';
217 cp = stpcpy (cp, language);
219 if ((mask & XPG_TERRITORY) != 0)
221 *cp++ = '_';
222 cp = stpcpy (cp, territory);
224 if ((mask & XPG_CODESET) != 0)
226 *cp++ = '.';
227 cp = stpcpy (cp, codeset);
229 if ((mask & XPG_NORM_CODESET) != 0)
231 *cp++ = '.';
232 cp = stpcpy (cp, normalized_codeset);
234 if ((mask & XPG_MODIFIER) != 0)
236 *cp++ = '@';
237 cp = stpcpy (cp, modifier);
240 *cp++ = '/';
241 stpcpy (cp, filename);
243 /* Look in list of already loaded domains whether it is already
244 available. */
245 last = NULL;
246 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
247 if (retval->filename != NULL)
249 int compare = strcmp (retval->filename, abs_filename);
250 if (compare == 0)
251 /* We found it! */
252 break;
253 if (compare < 0)
255 /* It's not in the list. */
256 retval = NULL;
257 break;
260 last = retval;
263 if (retval != NULL || do_allocate == 0)
265 free (abs_filename);
266 return retval;
269 retval = (struct loaded_l10nfile *)
270 malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
271 * (1 << pop (mask))
272 * sizeof (struct loaded_l10nfile *)));
273 if (retval == NULL)
275 free (abs_filename);
276 return NULL;
279 retval->filename = abs_filename;
280 /* If more than one directory is in the list this is a pseudo-entry
281 which just references others. We do not try to load data for it,
282 ever. */
283 retval->decided = (__argz_count (dirlist, dirlist_len) != 1
284 || ((mask & XPG_CODESET) != 0
285 && (mask & XPG_NORM_CODESET) != 0));
286 retval->data = NULL;
288 if (last == NULL)
290 retval->next = *l10nfile_list;
291 *l10nfile_list = retval;
293 else
295 retval->next = last->next;
296 last->next = retval;
299 entries = 0;
300 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
301 a real file. So we have to use the DIRLIST separation mechanism
302 of the inner loop. */
303 cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
304 for (; cnt >= 0; --cnt)
305 if ((cnt & ~mask) == 0)
307 /* Iterate over all elements of the DIRLIST. */
308 char *dir = NULL;
310 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
311 != NULL)
312 retval->successor[entries++]
313 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
314 language, territory, codeset,
315 normalized_codeset, modifier, filename, 1);
317 retval->successor[entries] = NULL;
319 return retval;
322 /* Normalize codeset name. There is no standard for the codeset
323 names. Normalization allows the user to use any of the common
324 names. The return value is dynamically allocated and has to be
325 freed by the caller. */
326 const char *
327 _nl_normalize_codeset (codeset, name_len)
328 const char *codeset;
329 size_t name_len;
331 int len = 0;
332 int only_digit = 1;
333 char *retval;
334 char *wp;
335 size_t cnt;
336 #ifdef NOT_IN_libc
337 locale_t locale = newlocale (0, "C", NULL);
338 #else
339 # define locale _nl_C_locobj_ptr
340 #endif
342 for (cnt = 0; cnt < name_len; ++cnt)
343 if (__isalnum_l ((unsigned char) codeset[cnt], locale))
345 ++len;
347 if (! __isdigit_l ((unsigned char) codeset[cnt], locale))
348 only_digit = 0;
351 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
353 if (retval != NULL)
355 wp = retval;
356 if (only_digit)
357 wp = stpcpy (wp, "iso");
359 for (cnt = 0; cnt < name_len; ++cnt)
360 if (__isalpha_l ((unsigned char) codeset[cnt], locale))
361 *wp++ = __tolower_l ((unsigned char) codeset[cnt], locale);
362 else if (__isdigit_l ((unsigned char) codeset[cnt], locale))
363 *wp++ = codeset[cnt];
365 *wp = '\0';
368 return (const char *) retval;
372 /* @@ begin of epilog @@ */
374 /* We don't want libintl.a to depend on any other library. So we
375 avoid the non-standard function stpcpy. In GNU C Library this
376 function is available, though. Also allow the symbol HAVE_STPCPY
377 to be defined. */
378 #if !_LIBC && !HAVE_STPCPY
379 static char *
380 stpcpy (dest, src)
381 char *dest;
382 const char *src;
384 while ((*dest++ = *src++) != '\0')
385 /* Do nothing. */ ;
386 return dest - 1;
388 #endif