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 #include <stl/c_locale.h> 36 37 #ifdef _STLP_REAL_LOCALE_IMPLEMENTED 38 # if defined (_STLP_USE_GLIBC) && !defined (__CYGWIN__) 39 # include <nl_types.h> 40 # endif 41 #endif 42 43 /* 44 * A number: the maximum length of a simple locale name. 45 * (i.e. a name like like en_US, as opposed to a name like 46 * en_US/de_AT/de_AT/es_MX/en_US/en_US) */ 47 #define _Locale_MAX_SIMPLE_NAME 256 48 49 /* 50 * Maximum length of a composite locale. 51 */ 52 #define _Locale_MAX_COMPOSITE_NAME 6*(_Locale_MAX_SIMPLE_NAME+3) 53 54 #ifdef __cplusplus 55 extern "C" { 56 #endif 57 58 /* 59 * Typedefs: 60 */ 61 #if (defined (__GNUC__) && !defined (__MINGW32__)) || defined (_KCC) || defined (__ICC) 62 typedef unsigned short int _Locale_mask_t; 63 #else 64 typedef unsigned int _Locale_mask_t; 65 #endif 66 67 /* Function called during STLport library load phase. Might contain any 68 * code necessary to the platform localization layer. 69 */ 70 void _Locale_init(); 71 72 /* Function called during STLport library unload. Might contain any 73 * code necessary to the platform localization layer. 74 */ 75 void _Locale_final(); 76 77 /* Create a category of the locale with the given name. 78 * 79 * The char* argument is a simple (not a composite) locale name, which may 80 * neither be an empty string nor a null pointer. 81 * 82 * These functions return NULL to indicate failure. 83 * 84 * Note These functions return a void* instead of the appropriate 85 * _Locale_* struct because they are used with __acquire_category which 86 * requires that all functions have the same signature. 87 */ 88 void * _Locale_ctype_create(const char *, struct _Locale_name_hint*); 89 void * _Locale_numeric_create(const char *, struct _Locale_name_hint*); 90 void * _Locale_time_create(const char *, struct _Locale_name_hint*); 91 void * _Locale_collate_create(const char *, struct _Locale_name_hint*); 92 void * _Locale_monetary_create(const char *, struct _Locale_name_hint*); 93 void * _Locale_messages_create(const char *, struct _Locale_name_hint*); 94 95 96 /* Release a category of a locale 97 * 98 * These functions are used to release a category acquired with the 99 * according _Locale_*_create() functions. 100 * 101 * Note: For the same reasons as for the *_create functions, these 102 * take void* instead of the correct types so that they can be used 103 * with __release_category. 104 */ 105 void _Locale_ctype_destroy(void *); 106 void _Locale_numeric_destroy(void *); 107 void _Locale_time_destroy(void *); 108 void _Locale_collate_destroy(void *); 109 void _Locale_monetary_destroy(void *); 110 void _Locale_messages_destroy(void *); 111 112 113 /* 114 * Returns the name of the user's default locale in each 115 * category, as a null-terminated string. A NULL value 116 * means the default "C" locale. 117 */ 118 const char * _Locale_ctype_default(char * __buf); 119 const char * _Locale_numeric_default(char * __buf); 120 const char * _Locale_time_default(char * __buf); 121 const char * _Locale_collate_default(char * __buf); 122 const char * _Locale_monetary_default(char * __buf); 123 const char * _Locale_messages_default(char * __buf); 124 125 126 /* Retrieve the name of the given category 127 * 128 * __buf points to a buffer that can hold at least _Locale_MAX_SIMPLE_NAME 129 * characters. These functions store the name, as a null-terminated 130 * string, in __buf. 131 * TODO: can this fail? How is that signalled then? 132 */ 133 char const* _Locale_ctype_name(const void *, char* __buf); 134 char const* _Locale_numeric_name(const void *, char* __buf); 135 char const* _Locale_time_name(const void *, char* __buf); 136 char const* _Locale_collate_name(const void *, char* __buf); 137 char const* _Locale_monetary_name(const void *, char* __buf); 138 char const* _Locale_messages_name(const void *, char* __buf); 139 140 141 /* 142 * cname is a (possibly composite) locale name---i.e. a name that can 143 * be passed to setlocale. __buf points to an array large enough to 144 * store at least _Locale_MAX_SIMPLE_NAME characters, and each of these 145 * functions extracts the name of a single category, stores it in buf 146 * as a null-terminated string, and returns buf. 147 */ 148 char const* _Locale_extract_ctype_name(const char *cname, char *__buf, struct _Locale_name_hint* __hint); 149 char const* _Locale_extract_numeric_name(const char *cname, char *__buf, struct _Locale_name_hint* __hint); 150 char const* _Locale_extract_time_name(const char *cname, char *__buf, struct _Locale_name_hint* __hint); 151 char const* _Locale_extract_collate_name(const char *cname, char *__buf, struct _Locale_name_hint* __hint); 152 char const* _Locale_extract_monetary_name(const char *cname, char *__buf, struct _Locale_name_hint* __hint); 153 char const* _Locale_extract_messages_name(const char *cname, char *__buf, struct _Locale_name_hint* __hint); 154 155 /* 156 * The inputs to this function are six null-terminated strings: the 157 * names of a locale's six categories. Locale names for non-standard 158 * categories are taken from __DefaultName. 159 * __buf is a pointer to an array large enough to store at least 160 * _Locale_MAX_COMPOSITE_NAME characters. 161 * This function constructs a (possibly composite) name describing the 162 * locale as a whole, stores that name in buf as a null-terminated 163 * string, and returns buf. 164 */ 165 char const* _Locale_compose_name(char *__buf, 166 const char *__Ctype, const char *__Numeric, 167 const char *__Time, const char *__Collate, 168 const char *__Monetary, const char *__Messages, 169 const char *__DefaultName); 170 171 /* Funstions to improve locale creation process. For some locale API (Win32) 172 * you need to find a locale identification from the name which can be a 173 * rather expensive operation especially if you do so for all facets of a 174 * locale. Those functions can be used to extract from a API dependent facet 175 * struct the information necessary to skip this lookup process for other 176 * facets creation. If not supported those function should return NULL. 177 */ 178 struct _Locale_name_hint* _Locale_get_ctype_hint(struct _Locale_ctype*); 179 struct _Locale_name_hint* _Locale_get_numeric_hint(struct _Locale_numeric*); 180 struct _Locale_name_hint* _Locale_get_time_hint(struct _Locale_time*); 181 struct _Locale_name_hint* _Locale_get_collate_hint(struct _Locale_collate*); 182 struct _Locale_name_hint* _Locale_get_monetary_hint(struct _Locale_monetary*); 183 struct _Locale_name_hint* _Locale_get_messages_hint(struct _Locale_messages*); 184 185 /* 186 * FUNCTIONS THAT USE CTYPE 187 */ 188 189 /* 190 * Narrow character functions: 191 */ 192 193 /* 194 * Returns a pointer to the beginning of the ctype table. The table is 195 * at least 257 bytes long; if p is the pointer returned by this 196 * function, then p[c] is valid if c is EOF or if p is any value of 197 * type unsigned char. 198 */ 199 const _Locale_mask_t * _Locale_ctype_table(struct _Locale_ctype *); 200 201 /* 202 * c is either EOF, or an unsigned char value. 203 */ 204 int _Locale_toupper(struct _Locale_ctype *, int); 205 int _Locale_tolower(struct _Locale_ctype *, int); 206 207 208 # ifndef _STLP_NO_WCHAR_T 209 /* 210 * Wide character functions: 211 */ 212 _Locale_mask_t _Locale_wchar_ctype(struct _Locale_ctype *, wint_t, 213 _Locale_mask_t); 214 wint_t _Locale_wchar_tolower(struct _Locale_ctype *, wint_t); 215 wint_t _Locale_wchar_toupper(struct _Locale_ctype *, wint_t); 216 # endif 217 218 # if !defined ( _STLP_NO_MBSTATE_T ) 219 220 /* 221 * Multibyte functions: 222 */ 223 224 int _Locale_mb_cur_max (struct _Locale_ctype *); 225 /* 226 * Returns the number of bytes of the longest allowed multibyte 227 * character in the current encoding. 228 */ 229 230 int _Locale_mb_cur_min (struct _Locale_ctype *); 231 /* 232 * Returns the number of bytes of the shortest allowed multibyte 233 * character in the current encoding. 234 */ 235 236 int _Locale_is_stateless (struct _Locale_ctype *); 237 /* 238 * Returns 1 if the current multibyte encoding is stateless 239 * and does not require the use of an mbstate_t value. 240 */ 241 242 # ifndef _STLP_NO_WCHAR_T 243 wint_t _Locale_btowc(struct _Locale_ctype *, int); 244 int _Locale_wctob(struct _Locale_ctype *, wint_t); 245 246 /* 247 * Just like btowc and wctob, from 4.6.5.1 of the C standard, Normative 248 * Addendum 1. (And just like widen/narrow, from clause 22 of the C++ 249 * standard.) 250 */ 251 252 size_t _Locale_mbtowc(struct _Locale_ctype *, 253 wchar_t *, 254 const char *, size_t, 255 mbstate_t *); 256 257 /* 258 * Almost identical to mbrtowc, from 4.6.5.3.2 of NA1. The only 259 * important difference is that mbrtowc treats null wide characters 260 * as special, and we don't. Specifically: examines the characters 261 * in [from, from + n), extracts a single wide character, and stores 262 * it in *to. Modifies shift_state if appropriate. The return value, 263 * which is always positive, is the number of characters extracted from 264 * the input sequence. Return value is (size_t) -1 if there was an 265 * encoding error in the input sequence, and (size_t) -2 if 266 * [from, from + n) is correct but not complete. None of the pointer 267 * arguments may be null pointers. 268 */ 269 270 size_t _Locale_wctomb(struct _Locale_ctype *, 271 char *, size_t, 272 const wchar_t, 273 mbstate_t *); 274 275 /* 276 * Again, very similar to wcrtomb. The differences are that (1) it 277 * doesn't treat null characters as special; and (2) it stores at most 278 * n characters. Converts c to a multibyte sequence, stores that 279 * sequence in the array 'to', and returns the length of the sequence. 280 * Modifies shift_state if appropriate. The return value is (size_t) -1 281 * if c is not a valid wide character, and (size_t) -2 if the length of 282 * the multibyte character sequence is greater than n. 283 */ 284 # endif 285 286 size_t _Locale_unshift(struct _Locale_ctype *, 287 mbstate_t *, 288 char *, size_t, char **); 289 290 /* 291 * Inserts whatever characters are necessary to restore st to an 292 * initial shift state. Sets *next to buf + m, where m is the number 293 * of characters inserted. (0 <= m <= n.) Returns m to indicate 294 * success, (size_t) -1 to indicate error, (size_t) -2 to indicate 295 * partial success (more than n characters needed). For success or partial 296 * success, sets *next to buf + m. 297 */ 298 299 # endif /* _STLP_NO_MBSTATE_T */ 300 301 /* 302 * FUNCTIONS THAT USE COLLATE 303 */ 304 305 int _Locale_strcmp(struct _Locale_collate *, 306 const char *, size_t, 307 const char *, size_t); 308 # ifndef _STLP_NO_WCHAR_T 309 int _Locale_strwcmp(struct _Locale_collate *, 310 const wchar_t *, size_t, 311 const wchar_t *, size_t); 312 # endif 313 /* 314 * Compares the two sequences [s1, s1 + n1) and [s2, s2 + n2). Neither 315 * sequence is assumed to be null-terminated, and null characters 316 * aren't special. If the two sequences are the same up through 317 * min(n1, n2), then the sequence that compares less is whichever one 318 * is shorter. 319 */ 320 321 size_t _Locale_strxfrm(struct _Locale_collate *, 322 char *, size_t, 323 const char *, size_t); 324 325 # ifndef _STLP_NO_WCHAR_T 326 size_t _Locale_strwxfrm(struct _Locale_collate *, 327 wchar_t *, size_t, 328 const wchar_t *, size_t); 329 # endif 330 331 /* 332 * Creates a transformed version of the string [s2, s2 + n2). The 333 * string may contain embedded null characters; nulls aren't special. 334 * The transformed string begins at s1, and contains at most n1 335 * characters. The return value is the length of the transformed 336 * string. If the return value is greater than n1 then this is an 337 * error condition: it indicates that there wasn't enough space. In 338 * that case, the contents of [s1, s1 + n1) is unspecified. 339 */ 340 341 /* 342 * FUNCTIONS THAT USE NUMERIC 343 */ 344 345 /* 346 * Equivalent to the first three fields in struct lconv. (C standard, 347 * section 7.4.) 348 */ 349 char _Locale_decimal_point(struct _Locale_numeric *); 350 char _Locale_thousands_sep(struct _Locale_numeric *); 351 const char * _Locale_grouping(struct _Locale_numeric *); 352 353 354 /* 355 * Return "true" and "false" in English locales, and something 356 * appropriate in non-English locales. 357 */ 358 const char * _Locale_true(struct _Locale_numeric *); 359 const char * _Locale_false(struct _Locale_numeric *); 360 361 362 /* 363 * FUNCTIONS THAT USE MONETARY 364 */ 365 366 /* 367 * Return the obvious fields of struct lconv. 368 */ 369 const char * _Locale_int_curr_symbol(struct _Locale_monetary *); 370 const char * _Locale_currency_symbol(struct _Locale_monetary *); 371 char _Locale_mon_decimal_point(struct _Locale_monetary *); 372 char _Locale_mon_thousands_sep(struct _Locale_monetary *); 373 const char * _Locale_mon_grouping(struct _Locale_monetary *); 374 const char * _Locale_positive_sign(struct _Locale_monetary *); 375 const char * _Locale_negative_sign(struct _Locale_monetary *); 376 char _Locale_int_frac_digits(struct _Locale_monetary *); 377 char _Locale_frac_digits(struct _Locale_monetary *); 378 int _Locale_p_cs_precedes(struct _Locale_monetary *); 379 int _Locale_p_sep_by_space(struct _Locale_monetary *); 380 int _Locale_p_sign_posn(struct _Locale_monetary *); 381 int _Locale_n_cs_precedes(struct _Locale_monetary *); 382 int _Locale_n_sep_by_space(struct _Locale_monetary *); 383 int _Locale_n_sign_posn(struct _Locale_monetary *); 384 385 386 /* 387 * FUNCTIONS THAT USE TIME 388 */ 389 390 /* 391 * month is in the range [0, 12). 392 */ 393 const char * _Locale_full_monthname(struct _Locale_time *, int); 394 const char * _Locale_abbrev_monthname(struct _Locale_time *, int); 395 396 397 /* 398 * day is in the range [0, 7). Sunday is 0. 399 */ 400 const char * _Locale_full_dayofweek(struct _Locale_time *, int); 401 const char * _Locale_abbrev_dayofweek(struct _Locale_time *, int); 402 403 404 const char * _Locale_d_t_fmt(struct _Locale_time *); 405 const char * _Locale_d_fmt(struct _Locale_time *); 406 const char * _Locale_t_fmt(struct _Locale_time *); 407 const char * _Locale_long_d_t_fmt(struct _Locale_time*); 408 const char * _Locale_long_d_fmt(struct _Locale_time*); 409 410 const char * _Locale_am_str(struct _Locale_time *); 411 const char * _Locale_pm_str(struct _Locale_time *); 412 const char * _Locale_t_fmt_ampm(struct _Locale_time *); 413 414 415 /* 416 * FUNCTIONS THAT USE MESSAGES 417 */ 418 419 # ifdef _STLP_REAL_LOCALE_IMPLEMENTED 420 # if defined (WIN32) || defined (_WIN32) 421 typedef int nl_catd_type; 422 # elif defined (_STLP_USE_GLIBC) && !defined (__CYGWIN__) 423 typedef nl_catd nl_catd_type; 424 # else 425 typedef int nl_catd_type; 426 # endif 427 # else 428 typedef int nl_catd_type; 429 # endif /* _STLP_REAL_LOCALE_IMPLEMENTED */ 430 431 432 /* 433 * Very similar to catopen, except that it uses the given message 434 * category to determine which catalog to open. 435 */ 436 nl_catd_type _Locale_catopen(struct _Locale_messages*, const char*); 437 438 /* Complementary to _Locale_catopen. 439 * The catalog must be a value that was returned by a previous call 440 * to _Locale_catopen. 441 */ 442 void _Locale_catclose(struct _Locale_messages*, nl_catd_type); 443 444 /* 445 * Returns a string, identified by a set index and a message index, 446 * from an opened message catalog. Returns the supplied default if 447 * no such string exists. 448 */ 449 const char * _Locale_catgets(struct _Locale_messages *, nl_catd_type, 450 int, int,const char *); 451 452 #ifdef __cplusplus 453 } 454 #endif 455 456 #endif /* _STLP_C_LOCALE_IMPL_H */ 457