Update.
[glibc.git] / locale / localeinfo.h
blob3ec58f3b219cbbb8545816e9fd5eeaedd59b0ff0
1 /* Declarations for internal libc locale interfaces
2 Copyright (C) 1995-2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _LOCALEINFO_H
21 #define _LOCALEINFO_H 1
23 #include <stddef.h>
24 #include <langinfo.h>
25 #include <limits.h>
26 #include <locale.h>
27 #include <time.h>
28 #include <stdint.h>
29 #include <sys/types.h>
31 #include <intl/loadinfo.h> /* For loaded_l10nfile definition. */
33 /* Magic number at the beginning of a locale data file for CATEGORY. */
34 #define LIMAGIC(category) ((unsigned int) (0x20000828 ^ (category)))
36 /* Two special weight constants for the collation data. */
37 #define IGNORE_CHAR 2
39 /* We use a special value for the usage counter in `locale_data' to
40 signal that this data must never be removed anymore. */
41 #define MAX_USAGE_COUNT (UINT_MAX - 1)
42 #define UNDELETABLE UINT_MAX
44 /* Structure describing locale data in core for a category. */
45 struct locale_data
47 const char *name;
48 const char *filedata; /* Region mapping the file data. */
49 off_t filesize; /* Size of the file (and the region). */
50 enum /* Flavor of storage used for those. */
52 ld_malloced, /* Both are malloc'd. */
53 ld_mapped, /* name is malloc'd, filedata mmap'd */
54 ld_archive /* Both point into mmap'd archive regions. */
55 } alloc;
57 /* This provides a slot for category-specific code to cache data computed
58 about this locale. That code can set a cleanup function to deallocate
59 the data. */
60 struct
62 void (*cleanup) (struct locale_data *) internal_function;
63 union
65 void *data;
66 struct lc_time_data *time;
68 } private;
70 unsigned int usage_count; /* Counter for users. */
72 int use_translit; /* Nonzero if the mb*towv*() and wc*tomb()
73 functions should use transliteration. */
75 unsigned int nstrings; /* Number of strings below. */
76 union locale_data_value
78 const uint32_t *wstr;
79 const char *string;
80 unsigned int word;
82 values __flexarr; /* Items, usually pointers into `filedata'. */
85 /* We know three kinds of collation sorting rules. */
86 enum coll_sort_rule
88 illegal_0__,
89 sort_forward,
90 sort_backward,
91 illegal_3__,
92 sort_position,
93 sort_forward_position,
94 sort_backward_position,
95 sort_mask
98 /* We can map the types of the entries into a few categories. */
99 enum value_type
101 none,
102 string,
103 stringarray,
104 byte,
105 bytearray,
106 word,
107 stringlist,
108 wordarray,
109 wstring,
110 wstringarray,
111 wstringlist
115 /* Definitions for `era' information from LC_TIME. */
116 #define ERA_NAME_FORMAT_MEMBERS 4
117 #define ERA_M_NAME 0
118 #define ERA_M_FORMAT 1
119 #define ERA_W_NAME 2
120 #define ERA_W_FORMAT 3
123 /* Structure to access `era' information from LC_TIME. */
124 struct era_entry
126 uint32_t direction; /* Contains '+' or '-'. */
127 int32_t offset;
128 int32_t start_date[3];
129 int32_t stop_date[3];
130 const char *era_name;
131 const char *era_format;
132 const wchar_t *era_wname;
133 const wchar_t *era_wformat;
134 int absolute_direction;
135 /* absolute direction:
136 +1 indicates that year number is higher in the future. (like A.D.)
137 -1 indicates that year number is higher in the past. (like B.C.) */
140 /* Structure caching computed data about information from LC_TIME.
141 The `private.time' member of `struct locale_data' points to this. */
142 struct lc_time_data
144 struct era_entry *eras;
145 size_t num_eras;
146 int era_initialized;
148 const char **alt_digits;
149 const wchar_t **walt_digits;
150 int alt_digits_initialized;
151 int walt_digits_initialized;
155 /* LC_CTYPE specific:
156 Hardwired indices for standard wide character translation mappings. */
157 enum
159 __TOW_toupper = 0,
160 __TOW_tolower = 1
164 /* LC_CTYPE specific:
165 Access a wide character class with a single character index.
166 _ISCTYPE (c, desc) = iswctype (btowc (c), desc).
167 c must be an `unsigned char'. desc must be a nonzero wctype_t. */
168 #define _ISCTYPE(c, desc) \
169 (((((const uint32_t *) (desc)) - 8)[(c) >> 5] >> ((c) & 0x1f)) & 1)
171 extern const char *const _nl_category_names[__LC_LAST] attribute_hidden;
172 extern const size_t _nl_category_name_sizes[__LC_LAST] attribute_hidden;
174 /* Name of the standard locales. */
175 extern const char _nl_C_name[] attribute_hidden;
176 extern const char _nl_POSIX_name[] attribute_hidden;
178 /* The standard codeset. */
179 extern const char _nl_C_codeset[] attribute_hidden;
181 /* This is the internal locale_t object that holds the global locale
182 controlled by calls to setlocale. A thread's TSD locale pointer
183 points to this when `uselocale (LC_GLOBAL_LOCALE)' is in effect. */
184 extern struct __locale_struct _nl_global_locale attribute_hidden;
186 /* This fetches the thread-local locale_t pointer, either one set with
187 uselocale or &_nl_global_locale. */
188 #define _NL_CURRENT_LOCALE ((__locale_t) __libc_tsd_get (LOCALE))
189 #include <bits/libc-tsd.h>
190 __libc_tsd_define (extern, LOCALE)
193 /* For static linking it is desireable to avoid always linking in the code
194 and data for every category when we can tell at link time that they are
195 unused. We can manage this playing some tricks with weak references.
196 But with thread-local locale settings, it becomes quite ungainly unless
197 we can use __thread variables. So only in that case do we attempt this. */
198 #if !defined SHARED && defined HAVE___THREAD && defined HAVE_WEAK_SYMBOLS
199 # include <tls.h>
200 # if USE_TLS
201 # define NL_CURRENT_INDIRECT 1
202 # endif
203 #endif
205 #ifdef NL_CURRENT_INDIRECT
207 /* For each category declare the thread-local variable for the current
208 locale data. This has an extra indirection so it points at the
209 __locales[CATEGORY] element in either _nl_global_locale or the current
210 locale object set by uselocale, which points at the actual data. The
211 reason for having these variables is so that references to particular
212 categories will link in the lc-CATEGORY.c module to define this symbol,
213 and we arrange that linking that module is what brings in all the code
214 associated with this category. */
215 #define DEFINE_CATEGORY(category, category_name, items, a) \
216 extern __thread struct locale_data *const *_nl_current_##category \
217 attribute_hidden;
218 #include "categories.def"
219 #undef DEFINE_CATEGORY
221 /* Return a pointer to the current `struct locale_data' for CATEGORY. */
222 #define _NL_CURRENT_DATA(category) (*_nl_current_##category)
224 /* Extract the current CATEGORY locale's string for ITEM. */
225 #define _NL_CURRENT(category, item) \
226 ((*_nl_current_##category)->values[_NL_ITEM_INDEX (item)].string)
228 /* Extract the current CATEGORY locale's string for ITEM. */
229 #define _NL_CURRENT_WSTR(category, item) \
230 ((wchar_t *) (*_nl_current_##category)->values[_NL_ITEM_INDEX (item)].wstr)
232 /* Extract the current CATEGORY locale's word for ITEM. */
233 #define _NL_CURRENT_WORD(category, item) \
234 ((*_nl_current_##category)->values[_NL_ITEM_INDEX (item)].word)
236 /* This is used in lc-CATEGORY.c to define _nl_current_CATEGORY. */
237 #define _NL_CURRENT_DEFINE(category) \
238 __thread struct locale_data *const *_nl_current_##category \
239 attribute_hidden = &_nl_global_locale.__locales[category]; \
240 asm (_NL_CURRENT_DEFINE_STRINGIFY (ASM_GLOBAL_DIRECTIVE) \
241 " " __SYMBOL_PREFIX "_nl_current_" #category "_used\n" \
242 _NL_CURRENT_DEFINE_ABS (_nl_current_##category##_used, 1));
243 #define _NL_CURRENT_DEFINE_STRINGIFY(x) _NL_CURRENT_DEFINE_STRINGIFY_1 (x)
244 #define _NL_CURRENT_DEFINE_STRINGIFY_1(x) #x
245 #ifdef HAVE_ASM_SET_DIRECTIVE
246 # define _NL_CURRENT_DEFINE_ABS(sym, val) ".set " #sym ", " #val
247 #else
248 # define _NL_CURRENT_DEFINE_ABS(sym, val) #sym " = " #val
249 #endif
251 #else
253 /* All categories are always loaded in the shared library, so there is no
254 point in having lots of separate symbols for linking. */
256 /* Return a pointer to the current `struct locale_data' for CATEGORY. */
257 # define _NL_CURRENT_DATA(category) \
258 (_NL_CURRENT_LOCALE->__locales[category])
260 /* Extract the current CATEGORY locale's string for ITEM. */
261 # define _NL_CURRENT(category, item) \
262 (_NL_CURRENT_DATA (category)->values[_NL_ITEM_INDEX (item)].string)
264 /* Extract the current CATEGORY locale's string for ITEM. */
265 # define _NL_CURRENT_WSTR(category, item) \
266 ((wchar_t *) _NL_CURRENT_DATA (category)->values[_NL_ITEM_INDEX (item)].wstr)
268 /* Extract the current CATEGORY locale's word for ITEM. */
269 # define _NL_CURRENT_WORD(category, item) \
270 (_NL_CURRENT_DATA (category)->values[_NL_ITEM_INDEX (item)].word)
272 /* This is used in lc-CATEGORY.c to define _nl_current_CATEGORY. */
273 # define _NL_CURRENT_DEFINE(category) \
274 /* No per-category variable here. */
276 #endif
279 /* Default search path if no LOCPATH environment variable. */
280 extern const char _nl_default_locale_path[] attribute_hidden;
282 /* Load the locale data for CATEGORY from the file specified by *NAME.
283 If *NAME is "", use environment variables as specified by POSIX, and
284 fill in *NAME with the actual name used. If LOCALE_PATH is not null,
285 those directories are searched for the locale files. If it's null,
286 the locale archive is checked first and then _nl_default_locale_path
287 is searched for locale files. */
288 extern struct locale_data *_nl_find_locale (const char *locale_path,
289 size_t locale_path_len,
290 int category, const char **name)
291 internal_function attribute_hidden;
293 /* Try to load the file described by FILE. */
294 extern void _nl_load_locale (struct loaded_l10nfile *file, int category)
295 internal_function attribute_hidden;
297 /* Free all resource. */
298 extern void _nl_unload_locale (struct locale_data *locale)
299 internal_function attribute_hidden;
301 /* Free the locale and give back all memory if the usage count is one. */
302 extern void _nl_remove_locale (int locale, struct locale_data *data)
303 internal_function attribute_hidden;
305 /* Find the locale *NAMEP in the locale archive, and return the
306 internalized data structure for its CATEGORY data. If this locale has
307 already been loaded from the archive, just returns the existing data
308 structure. If successful, sets *NAMEP to point directly into the mapped
309 archive string table; that way, the next call can short-circuit strcmp. */
310 extern struct locale_data *_nl_load_locale_from_archive (int category,
311 const char **namep)
312 internal_function attribute_hidden;
314 /* Subroutine of setlocale's __libc_subfreeres hook. */
315 extern void _nl_archive_subfreeres (void) attribute_hidden;
317 /* Validate the contents of a locale file and set up the in-core
318 data structure to point into the data. This leaves the `alloc'
319 and `name' fields uninitialized, for the caller to fill in.
320 If any bogons are detected in the data, this will refuse to
321 intern it, and return a null pointer instead. */
322 extern struct locale_data *_nl_intern_locale_data (int category,
323 const void *data,
324 size_t datasize)
325 internal_function attribute_hidden;
328 /* Return `era' entry which corresponds to TP. Used in strftime. */
329 extern struct era_entry *_nl_get_era_entry (const struct tm *tp,
330 struct locale_data *lc_time)
331 internal_function attribute_hidden;
333 /* Return `era' cnt'th entry . Used in strptime. */
334 extern struct era_entry *_nl_select_era_entry (int cnt,
335 struct locale_data *lc_time)
336 internal_function attribute_hidden;
338 /* Return `alt_digit' which corresponds to NUMBER. Used in strftime. */
339 extern const char *_nl_get_alt_digit (unsigned int number,
340 struct locale_data *lc_time)
341 internal_function attribute_hidden;
343 /* Similar, but now for wide characters. */
344 extern const wchar_t *_nl_get_walt_digit (unsigned int number,
345 struct locale_data *lc_time)
346 internal_function attribute_hidden;
348 /* Parse string as alternative digit and return numeric value. */
349 extern int _nl_parse_alt_digit (const char **strp,
350 struct locale_data *lc_time)
351 internal_function attribute_hidden;
353 /* Postload processing. */
354 extern void _nl_postload_ctype (void);
356 /* Functions used for the `private.cleanup' hook. */
357 extern void _nl_cleanup_time (struct locale_data *)
358 internal_function attribute_hidden;
361 #endif /* localeinfo.h */