3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
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.
19 /* This is a "stub" implementation of the "c_locale.h" interface,
20 intended for operating systems where we have not yet written
21 a real implementation. A C++ library using this stub implementation
22 is still standard-conforming, since the C++ standard does not require
23 that any locales other than "C" be supported.
32 #if defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
33 # define _STLP_STRNCPY(D, DS, S, C) strncpy_s(D, DS, S, C)
34 # if !defined (_STLP_NO_WCHAR_T)
35 # define _STLP_WCSNCPY(D, DS, S, C) wcsncpy_s(D, DS, S, C)
38 # define _STLP_STRNCPY(D, DS, S, C) strncpy(D, S, C)
39 # if !defined (_STLP_NO_WCHAR_T)
40 # define _STLP_WCSNCPY(D, DS, S, C) wcsncpy(D, S, C)
44 static const char *_C_name = "C";
45 static const char *_empty_str = "";
46 #ifndef _STLP_NO_WCHAR_T
47 #if defined(WCHAR_MAX) && WCHAR_MAX == 255
48 static const wchar_t *_empty_wstr = "";
50 static const wchar_t *_empty_wstr = L"";
54 static _Locale_mask_t ctable[256];
56 /* Framework functions */
58 void _Locale_init(void) {
59 /* Ctype table for the ASCII character set. */
61 /* We might never reach 128 when char is signed. */
62 for (c = 0; /* c != 128 */; ++c) {
63 if (isalpha(c)) ctable[(unsigned char)c] |= _Locale_ALPHA;
64 if (iscntrl(c)) ctable[(unsigned char)c] |= _Locale_CNTRL;
65 if (isdigit(c)) ctable[(unsigned char)c] |= _Locale_DIGIT;
66 if (isprint(c)) ctable[(unsigned char)c] |= _Locale_PRINT;
67 if (ispunct(c)) ctable[(unsigned char)c] |= _Locale_PUNCT;
68 if (isspace(c)) ctable[(unsigned char)c] |= _Locale_SPACE;
69 if (isxdigit(c)) ctable[(unsigned char)c] |= _Locale_XDIGIT;
70 if (isupper(c)) ctable[(unsigned char)c] |= _Locale_UPPER;
71 if (islower(c)) ctable[(unsigned char)c] |= _Locale_LOWER;
75 /* ASCII is a 7-bit code, so everything else is non-ASCII. */
76 memset(&(ctable[128]), 0, 128 * sizeof(_Locale_mask_t));
79 void _Locale_final(void)
82 void* _Locale_create(const char* name, int *__err_code) {
83 if (name[0] == 'C' && name[1] == 0)
84 { return (void*)0x1; }
85 *__err_code = _STLP_LOC_NO_PLATFORM_SUPPORT; return 0;
88 struct _Locale_ctype* _Locale_ctype_create(const char *name,
89 struct _Locale_name_hint* hint, int *__err_code)
90 { return (struct _Locale_ctype*)_Locale_create(name, __err_code); }
92 struct _Locale_codecvt* _Locale_codecvt_create(const char *name,
93 struct _Locale_name_hint* hint, int *__err_code)
94 { return (struct _Locale_codecvt*)_Locale_create(name, __err_code); }
96 struct _Locale_numeric* _Locale_numeric_create(const char *name,
97 struct _Locale_name_hint* hint, int *__err_code)
98 { return (struct _Locale_numeric*)_Locale_create(name, __err_code); }
100 struct _Locale_time* _Locale_time_create(const char *name,
101 struct _Locale_name_hint* hint, int *__err_code)
102 { return (struct _Locale_time*)_Locale_create(name, __err_code); }
104 struct _Locale_collate* _Locale_collate_create(const char *name,
105 struct _Locale_name_hint* hint, int *__err_code)
106 { return (struct _Locale_collate*)_Locale_create(name, __err_code); }
108 struct _Locale_monetary* _Locale_monetary_create(const char *name,
109 struct _Locale_name_hint* hint, int *__err_code)
110 { return (struct _Locale_monetary*)_Locale_create(name, __err_code); }
112 struct _Locale_messages* _Locale_messages_create(const char *name,
113 struct _Locale_name_hint* hint, int *__err_code)
114 { return (struct _Locale_messages*)_Locale_create(name, __err_code); }
116 const char *_Locale_ctype_default(char* buf) { return _C_name; }
117 const char *_Locale_numeric_default(char * buf) { return _C_name; }
118 const char *_Locale_time_default(char* buf) { return _C_name; }
119 const char *_Locale_collate_default(char* buf) { return _C_name; }
120 const char *_Locale_monetary_default(char* buf) { return _C_name; }
121 const char *_Locale_messages_default(char* buf) { return _C_name; }
123 char const* _Locale_ctype_name(const struct _Locale_ctype *lctype, char* buf)
126 char const* _Locale_codecvt_name(const struct _Locale_codecvt *lcodecvt, char* buf)
129 char const* _Locale_numeric_name(const struct _Locale_numeric *lnum, char* buf)
132 char const* _Locale_time_name(const struct _Locale_time *ltime, char* buf)
135 char const* _Locale_collate_name(const struct _Locale_collate *lcol, char* buf)
138 char const* _Locale_monetary_name(const struct _Locale_monetary *lmon, char* buf)
141 char const* _Locale_messages_name(const struct _Locale_messages *lmes, char* buf)
144 void _Locale_ctype_destroy(struct _Locale_ctype *lctype) {}
145 void _Locale_codecvt_destroy(struct _Locale_codecvt *lcodecvt) {}
146 void _Locale_numeric_destroy(struct _Locale_numeric *lnum) {}
147 void _Locale_time_destroy(struct _Locale_time *ltime) {}
148 void _Locale_collate_destroy(struct _Locale_collate *lcol) {}
149 void _Locale_monetary_destroy(struct _Locale_monetary *lmon) {}
150 void _Locale_messages_destroy(struct _Locale_messages *lmes) {}
152 static char const* _Locale_extract_name(const char* name, int *__err_code) {
153 // When the request is the default locale or the "C" locale we answer "C".
155 (name[0] == 'C' && name[1] == 0))
157 *__err_code = _STLP_LOC_NO_PLATFORM_SUPPORT; return 0;
160 char const* _Locale_extract_ctype_name(const char *name, char *buf,
161 struct _Locale_name_hint* hint, int *__err_code)
162 { return _Locale_extract_name(name, __err_code); }
164 char const* _Locale_extract_numeric_name(const char *name, char *buf,
165 struct _Locale_name_hint* hint, int *__err_code)
166 { return _Locale_extract_name(name, __err_code); }
168 char const* _Locale_extract_time_name(const char*name, char *buf,
169 struct _Locale_name_hint* hint, int *__err_code)
170 { return _Locale_extract_name(name, __err_code); }
172 char const* _Locale_extract_collate_name(const char *name, char *buf,
173 struct _Locale_name_hint* hint, int *__err_code)
174 { return _Locale_extract_name(name, __err_code); }
176 char const* _Locale_extract_monetary_name(const char *name, char *buf,
177 struct _Locale_name_hint* hint, int *__err_code)
178 { return _Locale_extract_name(name, __err_code); }
180 char const* _Locale_extract_messages_name(const char *name, char *buf,
181 struct _Locale_name_hint* hint, int *__err_code)
182 { return _Locale_extract_name(name, __err_code); }
184 struct _Locale_name_hint* _Locale_get_ctype_hint(struct _Locale_ctype* ctype)
186 struct _Locale_name_hint* _Locale_get_numeric_hint(struct _Locale_numeric* numeric)
188 struct _Locale_name_hint* _Locale_get_time_hint(struct _Locale_time* time)
190 struct _Locale_name_hint* _Locale_get_collate_hint(struct _Locale_collate* collate)
192 struct _Locale_name_hint* _Locale_get_monetary_hint(struct _Locale_monetary* monetary)
194 struct _Locale_name_hint* _Locale_get_messages_hint(struct _Locale_messages* messages)
198 const _Locale_mask_t* _Locale_ctype_table(struct _Locale_ctype* lctype) {
199 _STLP_MARK_PARAMETER_AS_UNUSED(lctype)
203 int _Locale_toupper(struct _Locale_ctype*lctype, int c)
204 { return toupper(c); }
206 int _Locale_tolower(struct _Locale_ctype*lctype, int c)
207 { return tolower(c); }
209 #ifndef _STLP_NO_WCHAR_T
210 _Locale_mask_t _WLocale_ctype(struct _Locale_ctype *lctype, wint_t wc, _Locale_mask_t mask) {
211 _Locale_mask_t ret = 0;
212 if ((mask & _Locale_ALPHA) != 0 && iswalpha(wc))
213 ret |= _Locale_ALPHA;
215 if ((mask & _Locale_CNTRL) != 0 && iswcntrl(wc))
216 ret |= _Locale_CNTRL;
218 if ((mask & _Locale_DIGIT) != 0 && iswdigit(wc))
219 ret |= _Locale_DIGIT;
221 if ((mask & _Locale_PRINT) != 0 && iswprint(wc))
222 ret |= _Locale_PRINT;
224 if ((mask & _Locale_PUNCT) != 0 && iswpunct(wc))
225 ret |= _Locale_PUNCT;
227 if ((mask & _Locale_SPACE) != 0 && iswspace(wc))
228 ret |= _Locale_SPACE;
230 if ((mask & _Locale_XDIGIT) != 0 && iswxdigit(wc))
231 ret |= _Locale_XDIGIT;
233 if ((mask & _Locale_UPPER) != 0 && iswupper(wc))
234 ret |= _Locale_UPPER;
236 if ((mask & _Locale_LOWER) != 0 && iswlower(wc))
237 ret |= _Locale_LOWER;
242 wint_t _WLocale_tolower(struct _Locale_ctype *lctype, wint_t wc)
243 { return towlower(wc); }
245 wint_t _WLocale_toupper(struct _Locale_ctype *lctype, wint_t wc)
246 { return towupper(wc); }
248 int _WLocale_mb_cur_max (struct _Locale_codecvt *lcodecvt) { return 1; }
249 int _WLocale_mb_cur_min (struct _Locale_codecvt *lcodecvt) { return 1; }
250 int _WLocale_is_stateless (struct _Locale_codecvt *lcodecvt) { return 1; }
252 size_t _WLocale_mbtowc(struct _Locale_codecvt *lcodecvt,
254 const char *from, size_t n,
256 { *to = *from; return 1; }
258 size_t _WLocale_wctomb(struct _Locale_codecvt *lcodecvt,
262 { *to = (char)c; return 1; }
264 size_t _WLocale_unshift(struct _Locale_codecvt *lcodecvt,
266 char *buf, size_t n, char ** next)
267 { *next = buf; return 0; }
271 int _Locale_strcmp(struct _Locale_collate* lcol,
272 const char* s1, size_t n1, const char* s2, size_t n2) {
274 char buf1[64], buf2[64];
275 while (n1 > 0 || n2 > 0) {
276 size_t bufsize1 = n1 < 63 ? n1 : 63;
277 size_t bufsize2 = n2 < 63 ? n2 : 63;
278 _STLP_STRNCPY(buf1, 64, s1, bufsize1); buf1[bufsize1] = 0;
279 _STLP_STRNCPY(buf2, 64, s2, bufsize2); buf2[bufsize2] = 0;
281 ret = strcmp(buf1, buf2);
282 if (ret != 0) return ret < 0 ? -1 : 1;
283 s1 += bufsize1; n1 -= bufsize1;
284 s2 += bufsize2; n2 -= bufsize2;
286 return ret == 0 ? 0 : (ret < 0 ? -1 : 1);
289 #ifndef _STLP_NO_WCHAR_T
291 int _WLocale_strcmp(struct _Locale_collate* lcol,
292 const wchar_t* s1, size_t n1, const wchar_t* s2, size_t n2) {
294 wchar_t buf1[64], buf2[64];
295 while (n1 > 0 || n2 > 0) {
296 size_t bufsize1 = n1 < 63 ? n1 : 63;
297 size_t bufsize2 = n2 < 63 ? n2 : 63;
298 _STLP_WCSNCPY(buf1, 64, s1, bufsize1); buf1[bufsize1] = 0;
299 _STLP_WCSNCPY(buf2, 64, s2, bufsize2); buf2[bufsize2] = 0;
301 ret = wcscmp(buf1, buf2);
302 if (ret != 0) return ret < 0 ? -1 : 1;
303 s1 += bufsize1; n1 -= bufsize1;
304 s2 += bufsize2; n2 -= bufsize2;
306 return ret == 0 ? 0 : (ret < 0 ? -1 : 1);
311 size_t _Locale_strxfrm(struct _Locale_collate* lcol,
312 char* dest, size_t dest_n,
313 const char* src, size_t src_n) {
315 _STLP_STRNCPY(dest, dest_n, src, dest_n - 1); dest[dest_n - 1] = 0;
320 #ifndef _STLP_NO_WCHAR_T
322 size_t _WLocale_strxfrm(struct _Locale_collate* lcol,
323 wchar_t* dest, size_t dest_n,
324 const wchar_t* src, size_t src_n) {
326 _STLP_WCSNCPY(dest, dest_n, src, dest_n - 1); dest[dest_n - 1] = 0;
335 char _Locale_decimal_point(struct _Locale_numeric* lnum)
337 char _Locale_thousands_sep(struct _Locale_numeric* lnum)
339 const char* _Locale_grouping(struct _Locale_numeric * lnum)
340 { return _empty_str; }
341 const char * _Locale_true(struct _Locale_numeric * lnum)
343 const char * _Locale_false(struct _Locale_numeric * lnum)
346 #ifndef _STLP_NO_WCHAR_T
347 wchar_t _WLocale_decimal_point(struct _Locale_numeric* lnum)
349 wchar_t _WLocale_thousands_sep(struct _Locale_numeric* lnum)
351 #if defined(WCHAR_MAX) && WCHAR_MAX == 255
352 const wchar_t * _WLocale_true(struct _Locale_numeric* lnum, wchar_t* buf, size_t bufSize)
354 const wchar_t * _WLocale_false(struct _Locale_numeric* lnum, wchar_t* buf, size_t bufSize)
357 const wchar_t * _WLocale_true(struct _Locale_numeric* lnum, wchar_t* buf, size_t bufSize)
359 const wchar_t * _WLocale_false(struct _Locale_numeric* lnum, wchar_t* buf, size_t bufSize)
366 const char* _Locale_int_curr_symbol(struct _Locale_monetary * lmon)
367 { return _empty_str; }
368 const char* _Locale_currency_symbol(struct _Locale_monetary * lmon)
369 { return _empty_str; }
370 char _Locale_mon_decimal_point(struct _Locale_monetary * lmon)
372 char _Locale_mon_thousands_sep(struct _Locale_monetary * lmon)
374 const char* _Locale_mon_grouping(struct _Locale_monetary * lmon)
375 { return _empty_str; }
376 const char* _Locale_positive_sign(struct _Locale_monetary * lmon)
377 { return _empty_str; }
378 const char* _Locale_negative_sign(struct _Locale_monetary * lmon)
379 { return _empty_str; }
380 char _Locale_int_frac_digits(struct _Locale_monetary * lmon)
382 char _Locale_frac_digits(struct _Locale_monetary * lmon)
384 int _Locale_p_cs_precedes(struct _Locale_monetary * lmon)
386 int _Locale_p_sep_by_space(struct _Locale_monetary * lmon)
388 int _Locale_p_sign_posn(struct _Locale_monetary * lmon)
390 int _Locale_n_cs_precedes(struct _Locale_monetary * lmon)
392 int _Locale_n_sep_by_space(struct _Locale_monetary * lmon)
394 int _Locale_n_sign_posn(struct _Locale_monetary * lmon)
397 #ifndef _STLP_NO_WCHAR_T
398 const wchar_t* _WLocale_int_curr_symbol(struct _Locale_monetary * lmon,
399 wchar_t* buf, size_t bufSize)
400 { return _empty_wstr; }
401 const wchar_t* _WLocale_currency_symbol(struct _Locale_monetary * lmon,
402 wchar_t* buf, size_t bufSize)
403 { return _empty_wstr; }
404 wchar_t _WLocale_mon_decimal_point(struct _Locale_monetary * lmon)
406 wchar_t _WLocale_mon_thousands_sep(struct _Locale_monetary * lmon)
408 const wchar_t* _WLocale_positive_sign(struct _Locale_monetary * lmon,
409 wchar_t* buf, size_t bufSize)
410 { return _empty_wstr; }
411 const wchar_t* _WLocale_negative_sign(struct _Locale_monetary * lmon,
412 wchar_t* buf, size_t bufSize)
413 { return _empty_wstr; }
417 static const char* full_monthname[] =
418 { "January", "February", "March", "April", "May", "June",
419 "July", "August", "September", "October", "November", "December" };
420 const char * _Locale_full_monthname(struct _Locale_time * ltime, int n)
421 { return full_monthname[n]; }
423 static const char* abbrev_monthname[] =
424 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
425 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
426 const char * _Locale_abbrev_monthname(struct _Locale_time * ltime, int n)
427 { return abbrev_monthname[n]; }
429 static const char* full_dayname[] =
430 { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
431 const char * _Locale_full_dayofweek(struct _Locale_time * ltime, int n)
432 { return full_dayname[n]; }
434 static const char* abbrev_dayname[] =
435 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
436 const char * _Locale_abbrev_dayofweek(struct _Locale_time * ltime, int n)
437 { return abbrev_dayname[n]; }
439 const char* _Locale_d_t_fmt(struct _Locale_time* ltime)
440 { return "%m/%d/%y"; }
441 const char* _Locale_d_fmt(struct _Locale_time* ltime)
442 { return "%m/%d/%y"; }
443 const char* _Locale_t_fmt(struct _Locale_time* ltime)
444 { return "%H:%M:%S"; }
445 const char* _Locale_long_d_t_fmt(struct _Locale_time* ltime)
446 { return _empty_str; }
447 const char* _Locale_long_d_fmt(struct _Locale_time* ltime)
448 { return _empty_str; }
449 const char* _Locale_am_str(struct _Locale_time* ltime)
451 const char* _Locale_pm_str(struct _Locale_time* ltime)
454 #ifndef _STLP_NO_WCHAR_T
455 #if defined(WCHAR_MAX) && WCHAR_MAX == 255
456 static const wchar_t* full_wmonthname[] =
457 { "January", "February", "March", "April", "May", "June",
458 "July", "August", "September", "October", "November", "December" };
459 const wchar_t * _WLocale_full_monthname(struct _Locale_time * ltime, int n,
460 wchar_t* buf, size_t bufSize)
461 { return full_wmonthname[n]; }
463 static const wchar_t* abbrev_wmonthname[] =
464 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
465 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
466 const wchar_t * _WLocale_abbrev_monthname(struct _Locale_time * ltime, int n,
467 wchar_t* buf, size_t bufSize)
468 { return abbrev_wmonthname[n]; }
470 static const wchar_t* full_wdayname[] =
471 { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
472 const wchar_t * _WLocale_full_dayofweek(struct _Locale_time * ltime, int n,
473 wchar_t* buf, size_t bufSize)
474 { return full_wdayname[n]; }
476 static const wchar_t* abbrev_wdayname[] =
477 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
478 const wchar_t * _WLocale_abbrev_dayofweek(struct _Locale_time * ltime, int n,
479 wchar_t* buf, size_t bufSize)
480 { return abbrev_wdayname[n]; }
482 const wchar_t* _WLocale_am_str(struct _Locale_time* ltime,
483 wchar_t* buf, size_t bufSize)
485 const wchar_t* _WLocale_pm_str(struct _Locale_time* ltime,
486 wchar_t* buf, size_t bufSize)
488 #else /* WCHAR_MAX != 255 */
489 static const wchar_t* full_wmonthname[] =
490 { L"January", L"February", L"March", L"April", L"May", L"June",
491 L"July", L"August", L"September", L"October", L"November", L"December" };
492 const wchar_t * _WLocale_full_monthname(struct _Locale_time * ltime, int n,
493 wchar_t* buf, size_t bufSize)
494 { return full_wmonthname[n]; }
496 static const wchar_t* abbrev_wmonthname[] =
497 { L"Jan", L"Feb", L"Mar", L"Apr", L"May", L"Jun",
498 L"Jul", L"Aug", L"Sep", L"Oct", L"Nov", L"Dec" };
499 const wchar_t * _WLocale_abbrev_monthname(struct _Locale_time * ltime, int n,
500 wchar_t* buf, size_t bufSize)
501 { return abbrev_wmonthname[n]; }
503 static const wchar_t* full_wdayname[] =
504 { L"Sunday", L"Monday", L"Tuesday", L"Wednesday", L"Thursday", L"Friday", L"Saturday" };
505 const wchar_t * _WLocale_full_dayofweek(struct _Locale_time * ltime, int n,
506 wchar_t* buf, size_t bufSize)
507 { return full_wdayname[n]; }
509 static const wchar_t* abbrev_wdayname[] =
510 { L"Sun", L"Mon", L"Tue", L"Wed", L"Thu", L"Fri", L"Sat" };
511 const wchar_t * _WLocale_abbrev_dayofweek(struct _Locale_time * ltime, int n,
512 wchar_t* buf, size_t bufSize)
513 { return abbrev_wdayname[n]; }
515 const wchar_t* _WLocale_am_str(struct _Locale_time* ltime,
516 wchar_t* buf, size_t bufSize)
518 const wchar_t* _WLocale_pm_str(struct _Locale_time* ltime,
519 wchar_t* buf, size_t bufSize)
521 #endif /* WCHAR_MAX != 255 */
526 nl_catd_type _Locale_catopen(struct _Locale_messages* lmes, const char* name)
528 void _Locale_catclose(struct _Locale_messages* lmes, nl_catd_type cat) {}
529 const char* _Locale_catgets(struct _Locale_messages* lmes, nl_catd_type cat,
530 int setid, int msgid, const char *dfault)