1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
11  * Permission to use or copy this software for any purpose is hereby granted
12  * without fee, provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  *
17  */
18 
19 /*
20  * It is impossible to write the C++ locale library in terms of locales
21  * as defined in the C standard.  Instead, we write the C++ locale and I/O
22  * library in terms of a low level C-like interface.  This file defines
23  * that interface.
24  *
25  * The low-level locale interface can't be written portably; there
26  * must be a version of it for each platform that the C++ library
27  * is ported to.  On many systems this interface may be a thin wrapper
28  * for existing functionality.
29  */
30 
31 #ifndef _STLP_C_LOCALE_IMPL_H
32 #define _STLP_C_LOCALE_IMPL_H
33 
34 #include "stlport_prefix.h"
35 
36 #include <wchar.h> /* for mbstate_t */
37 #include <stl/c_locale.h>
38 
39 struct _Locale_name_hint;
40 
41 #if defined (_GNU_SOURCE) && defined (__GLIBC__) && \
42     ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
43 #  define _STLP_USE_GLIBC2_LOCALIZATION
44 #  include <nl_types.h>
45 typedef nl_catd nl_catd_type;
46 #else
47 typedef int nl_catd_type;
48 #endif
49 
50 /*
51  * A number: the maximum length of a simple locale name.
52  * (i.e. a name like like en_US, as opposed to a name like
53  * en_US/de_AT/de_AT/es_MX/en_US/en_US) */
54 #define _Locale_MAX_SIMPLE_NAME 256
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 /*
61  * Typedefs:
62  */
63 typedef unsigned short int _Locale_mask_t;
64 
65 /* Function called during STLport library load phase. Might contain any
66  * code necessary to the platform localization layer.
67  */
68 void _Locale_init(void);
69 
70 /* Function called during STLport library unload. Might contain any
71  * code necessary to the platform localization layer.
72  */
73 void _Locale_final(void);
74 
75 /* Create a category of the locale with the given name.
76  *
77  * The char* argument is a simple (not a composite) locale name, which may
78  * neither be an empty string nor a null pointer.
79  *
80  * These functions return NULL to indicate failure. Failure reason should be reported
81  * using the __err_code pointer.
82  */
83 struct _Locale_ctype* _Locale_ctype_create(const char *, struct _Locale_name_hint*, int * /* __err_code */);
84 struct _Locale_codecvt* _Locale_codecvt_create(const char *, struct _Locale_name_hint*, int * /* __err_code */);
85 struct _Locale_numeric* _Locale_numeric_create(const char *, struct _Locale_name_hint*, int * /* __err_code */);
86 struct _Locale_time* _Locale_time_create(const char *, struct _Locale_name_hint*, int * /* __err_code */);
87 struct _Locale_collate* _Locale_collate_create(const char *, struct _Locale_name_hint*, int * /* __err_code */);
88 struct _Locale_monetary* _Locale_monetary_create(const char *, struct _Locale_name_hint*, int * /* __err_code */);
89 struct _Locale_messages* _Locale_messages_create(const char *, struct _Locale_name_hint*, int * /* __err_code */);
90 
91 /* Give error reason on failure of one of the _Locale_*_create functions. Available
92  * reasons are:
93  * 0: No specific error reason has been reported.
94  * 1: No platform support for the given facet.
95  * 2: Unknown locale name
96  * 3: No platform API for localization support.
97  * 4: No more memory
98  */
99 #define _STLP_LOC_UNDEFINED 0
100 #define _STLP_LOC_UNSUPPORTED_FACET_CATEGORY 1
101 #define _STLP_LOC_UNKNOWN_NAME 2
102 #define _STLP_LOC_NO_PLATFORM_SUPPORT 3
103 #define _STLP_LOC_NO_MEMORY 4
104 
105 /* Release a category of a locale
106  *
107  * These functions are used to release a category acquired with the
108  * according _Locale_*_create() functions.
109  */
110 void _Locale_ctype_destroy(struct _Locale_ctype *);
111 void _Locale_codecvt_destroy(struct _Locale_codecvt *);
112 void _Locale_numeric_destroy(struct _Locale_numeric *);
113 void _Locale_time_destroy(struct _Locale_time *);
114 void _Locale_collate_destroy(struct _Locale_collate *);
115 void _Locale_monetary_destroy(struct _Locale_monetary *);
116 void _Locale_messages_destroy(struct _Locale_messages *);
117 
118 /*
119  * Returns the name of the user's default locale in each
120  * category, as a null-terminated string.  A NULL value
121  * means the default "C" locale.
122  */
123 const char * _Locale_ctype_default(char * __buf);
124 const char * _Locale_numeric_default(char * __buf);
125 const char * _Locale_time_default(char * __buf);
126 const char * _Locale_collate_default(char * __buf);
127 const char * _Locale_monetary_default(char * __buf);
128 const char * _Locale_messages_default(char * __buf);
129 
130 /* Retrieve the name of the given category
131  *
132  * __buf points to a buffer that can hold at least _Locale_MAX_SIMPLE_NAME
133  * characters.  These functions store the name, as a null-terminated
134  * string, in __buf. This function can't fail, at worst name is truncated.
135  */
136 char const* _Locale_ctype_name(const struct _Locale_ctype *, char* __buf);
137 char const* _Locale_codecvt_name(const struct _Locale_codecvt *, char* __buf);
138 char const* _Locale_numeric_name(const struct _Locale_numeric *, char* __buf);
139 char const* _Locale_time_name(const struct _Locale_time *, char* __buf);
140 char const* _Locale_collate_name(const struct _Locale_collate *, char*  __buf);
141 char const* _Locale_monetary_name(const struct _Locale_monetary *, char* __buf);
142 char const* _Locale_messages_name(const struct _Locale_messages *, char* __buf);
143 
144 /*
145  * cname is a (possibly composite) locale name---i.e. a name that can
146  * be passed to setlocale. __buf points to an array large enough to
147  * store at least _Locale_MAX_SIMPLE_NAME characters, and each of these
148  * functions extracts the name of a single category, stores it in buf
149  * as a null-terminated string, and returns buf.
150  */
151 char const* _Locale_extract_ctype_name(const char *cname, char *__buf,
152                                        struct _Locale_name_hint* __hint, int *__err_code);
153 char const* _Locale_extract_numeric_name(const char *cname, char *__buf,
154                                          struct _Locale_name_hint* __hint, int *__err_code);
155 char const* _Locale_extract_time_name(const char *cname, char *__buf,
156                                       struct _Locale_name_hint* __hint, int *__err_code);
157 char const* _Locale_extract_collate_name(const char *cname, char *__buf,
158                                          struct _Locale_name_hint* __hint, int *__err_code);
159 char const* _Locale_extract_monetary_name(const char *cname, char *__buf,
160                                           struct _Locale_name_hint* __hint, int *__err_code);
161 char const* _Locale_extract_messages_name(const char *cname, char *__buf,
162                                           struct _Locale_name_hint* __hint, int *__err_code);
163 
164 /* Functions to improve locale creation process. For some locale API (Win32)
165  * you need to find a locale identification from the name which can be a
166  * rather expensive operation especially if you do so for all facets of a
167  * locale. Those functions can be used to extract from a API dependent facet
168  * struct the information necessary to skip this lookup process for other
169  * facets creation. If not supported those function should return NULL.
170  */
171 struct _Locale_name_hint* _Locale_get_ctype_hint(struct _Locale_ctype*);
172 struct _Locale_name_hint* _Locale_get_numeric_hint(struct _Locale_numeric*);
173 struct _Locale_name_hint* _Locale_get_time_hint(struct _Locale_time*);
174 struct _Locale_name_hint* _Locale_get_collate_hint(struct _Locale_collate*);
175 struct _Locale_name_hint* _Locale_get_monetary_hint(struct _Locale_monetary*);
176 struct _Locale_name_hint* _Locale_get_messages_hint(struct _Locale_messages*);
177 
178 /*
179  * FUNCTIONS THAT USE CTYPE
180  */
181 
182 /*
183  * Narrow character functions:
184  */
185 
186 /*
187  * Returns a pointer to the beginning of the ctype table.  The table is
188  * at least 257 bytes long; if p is the pointer returned by this
189  * function, then p[c] is valid if c is EOF or if p is any value of
190  * type unsigned char.
191  */
192 const _Locale_mask_t * _Locale_ctype_table(struct _Locale_ctype *);
193 
194 /*
195  * c is either EOF, or an unsigned char value.
196  */
197 int _Locale_toupper(struct _Locale_ctype *, int /* c */);
198 int _Locale_tolower(struct _Locale_ctype *, int /* c */);
199 
200 
201 #ifndef _STLP_NO_WCHAR_T
202 /*
203  * Wide character functions:
204  */
205 _Locale_mask_t _WLocale_ctype(struct _Locale_ctype *, wint_t, _Locale_mask_t);
206 wint_t _WLocale_tolower(struct _Locale_ctype *, wint_t);
207 wint_t _WLocale_toupper(struct _Locale_ctype *, wint_t);
208 
209 /*
210  * Multibyte functions:
211  */
212 
213 /*
214  * Returns the number of bytes of the longest allowed multibyte
215  * character in the current encoding.
216  */
217 int _WLocale_mb_cur_max(struct _Locale_codecvt *);
218 
219 /*
220  * Returns the number of bytes of the shortest allowed multibyte
221  * character in the current encoding.
222  */
223 int _WLocale_mb_cur_min(struct _Locale_codecvt *);
224 
225 /*
226  * Returns 1 if the current multibyte encoding is stateless
227  * and does not require the use of an mbstate_t value.
228  */
229 int _WLocale_is_stateless(struct _Locale_codecvt *);
230 
231 /*
232  * Almost identical to mbrtowc, from 4.6.5.3.2 of NA1.  The only
233  * important difference is that mbrtowc treats null wide characters
234  * as special, and we don't.  Specifically: examines the characters
235  * in [from, from + n), extracts a single wide character, and stores
236  * it in *to.  Modifies shift_state if appropriate.  The return value,
237  * which is always positive, is the number of characters extracted from
238  * the input sequence.  Return value is (size_t) -1 if there was an
239  * encoding error in the input sequence, and (size_t) -2 if
240  * [from, from + n) is correct but not complete.  None of the pointer
241  * arguments may be null pointers.
242  */
243 size_t _WLocale_mbtowc(struct _Locale_codecvt *,
244                        wchar_t * /* to */,
245                        const char * /* from */, size_t /* n */,
246                        mbstate_t *);
247 
248 /*
249  * Again, very similar to wcrtomb.  The differences are that (1) it
250  * doesn't treat null characters as special; and (2) it stores at most
251  * n characters.  Converts c to a multibyte sequence, stores that
252  * sequence in the array 'to', and returns the length of the sequence.
253  * Modifies shift_state if appropriate.  The return value is (size_t) -1
254  * if c is not a valid wide character, and (size_t) -2 if the length of
255  * the multibyte character sequence is greater than n.
256  */
257 size_t _WLocale_wctomb(struct _Locale_codecvt *,
258                        char *, size_t,
259                        const wchar_t,
260                        mbstate_t *);
261 
262 /*
263  * Inserts whatever characters are necessary to restore st to an
264  * initial shift state.  Sets *next to buf + m, where m is the number
265  * of characters inserted.  (0 <= m <= n.)  Returns m to indicate
266  * success, (size_t) -1 to indicate error, (size_t) -2 to indicate
267  * partial success (more than n characters needed).  For success or partial
268  * success, sets *next to buf + m.
269  */
270 size_t _WLocale_unshift(struct _Locale_codecvt *,
271                         mbstate_t *,
272                         char *, size_t, char **);
273 #endif
274 
275 /*
276  * FUNCTIONS THAT USE COLLATE
277  */
278 
279 /*
280  * Compares the two sequences [s1, s1 + n1) and [s2, s2 + n2).  Neither
281  * sequence is assumed to be null-terminated, and null characters
282  * aren't special.  If the two sequences are the same up through
283  * min(n1, n2), then the sequence that compares less is whichever one
284  * is shorter.
285  */
286 int _Locale_strcmp(struct _Locale_collate *,
287                    const char * /* s1 */, size_t /* n1 */,
288                    const char * /* s2 */, size_t /* n2 */);
289 #ifndef _STLP_NO_WCHAR_T
290 int _WLocale_strcmp(struct _Locale_collate *,
291                     const wchar_t * /* s1 */, size_t /* n1 */,
292                     const wchar_t * /* s2 */, size_t /* n2 */);
293 #endif
294 
295 /*
296  * Creates a transformed version of the string [s2, s2 + n2).  The
297  * string may contain embedded null characters; nulls aren't special.
298  * The transformed string begins at s1, and contains at most n1
299  * characters.  The return value is the length of the transformed
300  * string.  If the return value is greater than n1 then this is an
301  * error condition: it indicates that there wasn't enough space.  In
302  * that case, the contents of [s1, s1 + n1) is unspecified.
303 */
304 size_t _Locale_strxfrm(struct _Locale_collate *,
305                        char * /* s1 */, size_t /* n1 */,
306                        const char * /* s2 */, size_t /* n2 */);
307 
308 #ifndef _STLP_NO_WCHAR_T
309 size_t _WLocale_strxfrm(struct _Locale_collate *,
310                         wchar_t * /* s1 */, size_t /* n1 */,
311                         const wchar_t * /* s2 */, size_t /* n2 */);
312 #endif
313 
314 
315 /*
316  * FUNCTIONS THAT USE NUMERIC
317  */
318 
319 /*
320  * Equivalent to the first three fields in struct lconv.  (C standard,
321  * section 7.4.)
322  */
323 char _Locale_decimal_point(struct _Locale_numeric *);
324 char _Locale_thousands_sep(struct _Locale_numeric *);
325 const char * _Locale_grouping(struct _Locale_numeric *);
326 
327 #ifndef _STLP_NO_WCHAR_T
328 wchar_t _WLocale_decimal_point(struct _Locale_numeric *);
329 wchar_t _WLocale_thousands_sep(struct _Locale_numeric *);
330 #endif
331 
332 /*
333  * Return "true" and "false" in English locales, and something
334  * appropriate in non-English locales.
335  */
336 const char * _Locale_true(struct _Locale_numeric *);
337 const char * _Locale_false(struct _Locale_numeric *);
338 
339 #ifndef _STLP_NO_WCHAR_T
340 const wchar_t * _WLocale_true(struct _Locale_numeric *, wchar_t* /* buf */, size_t /* bufSize */);
341 const wchar_t * _WLocale_false(struct _Locale_numeric *, wchar_t* /* buf */, size_t /* bufSize */);
342 #endif
343 
344 /*
345  * FUNCTIONS THAT USE MONETARY
346  */
347 
348 /*
349  * Return the obvious fields of struct lconv.
350  */
351 const char * _Locale_int_curr_symbol(struct _Locale_monetary *);
352 const char * _Locale_currency_symbol(struct _Locale_monetary *);
353 char         _Locale_mon_decimal_point(struct _Locale_monetary *);
354 char         _Locale_mon_thousands_sep(struct _Locale_monetary *);
355 const char * _Locale_mon_grouping(struct _Locale_monetary *);
356 const char * _Locale_positive_sign(struct _Locale_monetary *);
357 const char * _Locale_negative_sign(struct _Locale_monetary *);
358 char         _Locale_int_frac_digits(struct _Locale_monetary *);
359 char         _Locale_frac_digits(struct _Locale_monetary *);
360 int          _Locale_p_cs_precedes(struct _Locale_monetary *);
361 int          _Locale_p_sep_by_space(struct _Locale_monetary *);
362 int          _Locale_p_sign_posn(struct _Locale_monetary *);
363 int          _Locale_n_cs_precedes(struct _Locale_monetary *);
364 int          _Locale_n_sep_by_space(struct _Locale_monetary *);
365 int          _Locale_n_sign_posn(struct _Locale_monetary *);
366 
367 #ifndef _STLP_NO_WCHAR_T
368 const wchar_t * _WLocale_int_curr_symbol(struct _Locale_monetary *, wchar_t* /* buf */, size_t /* bufSize */);
369 const wchar_t * _WLocale_currency_symbol(struct _Locale_monetary *, wchar_t* /* buf */, size_t /* bufSize */);
370 wchar_t         _WLocale_mon_decimal_point(struct _Locale_monetary *);
371 wchar_t         _WLocale_mon_thousands_sep(struct _Locale_monetary *);
372 const wchar_t * _WLocale_positive_sign(struct _Locale_monetary *, wchar_t* /* buf */, size_t /* bufSize */);
373 const wchar_t * _WLocale_negative_sign(struct _Locale_monetary *, wchar_t* /* buf */, size_t /* bufSize */);
374 #endif
375 
376 /*
377  * FUNCTIONS THAT USE TIME
378  */
379 
380 /*
381  * month is in the range [0, 12).
382  */
383 const char * _Locale_full_monthname(struct _Locale_time *, int /* month */);
384 const char * _Locale_abbrev_monthname(struct _Locale_time *, int /* month */);
385 
386 #ifndef _STLP_NO_WCHAR_T
387 const wchar_t * _WLocale_full_monthname(struct _Locale_time *, int /* month */,
388                                         wchar_t* /* buf */, size_t /* bufSize */);
389 const wchar_t * _WLocale_abbrev_monthname(struct _Locale_time *, int /* month */,
390                                           wchar_t* /* buf */, size_t /* bufSize */);
391 #endif
392 
393 /*
394  * day is in the range [0, 7).  Sunday is 0.
395  */
396 const char * _Locale_full_dayofweek(struct _Locale_time *, int /* day */);
397 const char * _Locale_abbrev_dayofweek(struct _Locale_time *, int /* day */);
398 
399 #ifndef _STLP_NO_WCHAR_T
400 const wchar_t * _WLocale_full_dayofweek(struct _Locale_time *, int /* day */,
401                                         wchar_t* /* buf */, size_t /* bufSize */);
402 const wchar_t * _WLocale_abbrev_dayofweek(struct _Locale_time *, int /* day */,
403                                           wchar_t* /* buf */, size_t /* bufSize */);
404 #endif
405 
406 const char * _Locale_d_t_fmt(struct _Locale_time *);
407 const char * _Locale_d_fmt(struct _Locale_time *);
408 const char * _Locale_t_fmt(struct _Locale_time *);
409 const char * _Locale_long_d_t_fmt(struct _Locale_time*);
410 const char * _Locale_long_d_fmt(struct _Locale_time*);
411 
412 const char * _Locale_am_str(struct _Locale_time *);
413 const char * _Locale_pm_str(struct _Locale_time *);
414 
415 #ifndef _STLP_NO_WCHAR_T
416 const wchar_t * _WLocale_am_str(struct _Locale_time *,
417                                 wchar_t* /* buf */, size_t /* bufSize */);
418 const wchar_t * _WLocale_pm_str(struct _Locale_time *,
419                                 wchar_t* /* buf */, size_t /* bufSize */);
420 #endif
421 
422 /*
423  * FUNCTIONS THAT USE MESSAGES
424  */
425 
426 /*
427  * Very similar to catopen, except that it uses the given message
428  * category to determine which catalog to open.
429  */
430 nl_catd_type _Locale_catopen(struct _Locale_messages*, const char*);
431 
432 /* Complementary to _Locale_catopen.
433  * The catalog must be a value that was returned by a previous call
434  * to _Locale_catopen.
435  */
436 void _Locale_catclose(struct _Locale_messages*, nl_catd_type);
437 
438 /*
439  * Returns a string, identified by a set index and a message index,
440  * from an opened message catalog.  Returns the supplied default if
441  * no such string exists.
442  */
443 const char * _Locale_catgets(struct _Locale_messages *, nl_catd_type,
444                              int, int,const char *);
445 
446 #ifdef __cplusplus
447 }
448 #endif
449 
450 #endif /* _STLP_C_LOCALE_IMPL_H */
451