4.6.2. Selecting locale at run time

The C library function setlocale() selects a locale at runtime for the locale category, or categories, specified in its arguments. It does this by selecting the requested locale separately in each locale category. In effect, each locale category is a small filing system containing an entry for each locale.

Each locale category is processed by a function like _get_lc_category, for example:

void const *_get_lc_time (void *null, char const *locale_name)

_get_lc_time() returns the address of the time filing system entry for the locale named locale_name, or NULL if the entry was not found.

The implementation of each locale category must supply a selection function as shown in Table 4.7.

Table 4.7. Locale categories

FunctionDescription
_get_lc_ctype()Returns a pointer to the first element in a user-defined array that holds character attributes. See _get_lc_ctype().
_get_lc_collate()Returns a pointer to the first element in a user-defined array that holds sorting attributes. See _get_lc_collate().
_get_lc_monetary()Returns a pointer to the user-defined __lc_monetary_blk structure. See _get_lc_monetary().
_get_lc_numeric()Returns a pointer to the user-defined __lc_numeric_blk structure. See _get_lc_numeric().
_get_lc_time()Returns a pointer to the user-defined __lc_time_blk structure. See _get_lc_time().

C header files describing what must be implemented, and providing some useful support macros, are given in locale.h and rt_locale.h.

Implementation

For each category, changing locale is achieved by changing a pointer into the read-only data for the locale category. Except for default locales, the data must be user-supplied.

All locale blocks for a category are collected into a read-only, position-independent, in-memory file system structure. The C library provides a set of macros to create the blocks and the _findlocale() function to search the file system.

You can define a set of runtime selectable locales by using the supplied re-implementations as a starting point. Your application will not call _get_lc_category functions directly. _get_lc_category functions are called by setlocale() and __rt_lib_init(). You implement new locales by providing new locale definition blocks and re-implementations of _get_lc_category for setlocale() to use as in Example 4.2.

Example 4.2. get_lc_ctype

void const *_get_lc_ctype(void const *null, char const *name) {
    return _findlocale(&lcctype_c_index, name);
}
Copyright © 1999-2001 ARM Limited. All rights reserved.ARM DUI 0067D
Non-Confidential