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 /* 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. 24 */ 25 26 #include <string.h> 27 #include <wchar.h> 28 #include <ctype.h> 29 #include <wctype.h> 30 #include <limits.h> 31 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) 36 # endif 37 #else 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) 41 # endif 42 #endif 43 44 static const char *_C_name = "C"; 45 static const char *_empty_str = ""; 46 #ifndef _STLP_NO_WCHAR_T 47 static const wchar_t *_empty_wstr = L""; 48 #endif 49 50 static _Locale_mask_t ctable[256]; 51 52 /* Framework functions */ 53 54 void _Locale_init(void) { 55 /* Ctype table for the ASCII character set. */ 56 char c; 57 /* We might never reach 128 when char is signed. */ 58 for (c = 0; /* c != 128 */; ++c) { 59 if (isalpha(c)) ctable[(unsigned char)c] |= _Locale_ALPHA; 60 if (iscntrl(c)) ctable[(unsigned char)c] |= _Locale_CNTRL; 61 if (isdigit(c)) ctable[(unsigned char)c] |= _Locale_DIGIT; 62 if (isprint(c)) ctable[(unsigned char)c] |= _Locale_PRINT; 63 if (ispunct(c)) ctable[(unsigned char)c] |= _Locale_PUNCT; 64 if (isspace(c)) ctable[(unsigned char)c] |= _Locale_SPACE; 65 if (isxdigit(c)) ctable[(unsigned char)c] |= _Locale_XDIGIT; 66 if (isupper(c)) ctable[(unsigned char)c] |= _Locale_UPPER; 67 if (islower(c)) ctable[(unsigned char)c] |= _Locale_LOWER; 68 if (c == 127) break; 69 } 70 71 /* ASCII is a 7-bit code, so everything else is non-ASCII. */ 72 memset(&(ctable[128]), 0, 128 * sizeof(_Locale_mask_t)); 73 } 74 75 void _Locale_final(void) 76 {} 77 78 void* _Locale_create(const char* name, int *__err_code) { 79 if (name[0] == 'C' && name[1] == 0) 80 { return (void*)0x1; } 81 *__err_code = _STLP_LOC_NO_PLATFORM_SUPPORT; return 0; 82 } 83 84 struct _Locale_ctype* _Locale_ctype_create(const char *name, 85 struct _Locale_name_hint* hint, int *__err_code) 86 { return (struct _Locale_ctype*)_Locale_create(name, __err_code); } 87 88 struct _Locale_codecvt* _Locale_codecvt_create(const char *name, 89 struct _Locale_name_hint* hint, int *__err_code) 90 { return (struct _Locale_codecvt*)_Locale_create(name, __err_code); } 91 92 struct _Locale_numeric* _Locale_numeric_create(const char *name, 93 struct _Locale_name_hint* hint, int *__err_code) 94 { return (struct _Locale_numeric*)_Locale_create(name, __err_code); } 95 96 struct _Locale_time* _Locale_time_create(const char *name, 97 struct _Locale_name_hint* hint, int *__err_code) 98 { return (struct _Locale_time*)_Locale_create(name, __err_code); } 99 100 struct _Locale_collate* _Locale_collate_create(const char *name, 101 struct _Locale_name_hint* hint, int *__err_code) 102 { return (struct _Locale_collate*)_Locale_create(name, __err_code); } 103 104 struct _Locale_monetary* _Locale_monetary_create(const char *name, 105 struct _Locale_name_hint* hint, int *__err_code) 106 { return (struct _Locale_monetary*)_Locale_create(name, __err_code); } 107 108 struct _Locale_messages* _Locale_messages_create(const char *name, 109 struct _Locale_name_hint* hint, int *__err_code) 110 { return (struct _Locale_messages*)_Locale_create(name, __err_code); } 111 112 const char *_Locale_ctype_default(char* buf) { return _C_name; } 113 const char *_Locale_numeric_default(char * buf) { return _C_name; } 114 const char *_Locale_time_default(char* buf) { return _C_name; } 115 const char *_Locale_collate_default(char* buf) { return _C_name; } 116 const char *_Locale_monetary_default(char* buf) { return _C_name; } 117 const char *_Locale_messages_default(char* buf) { return _C_name; } 118 119 char const* _Locale_ctype_name(const struct _Locale_ctype *lctype, char* buf) 120 { return _C_name; } 121 122 char const* _Locale_codecvt_name(const struct _Locale_codecvt *lcodecvt, char* buf) 123 { return _C_name; } 124 125 char const* _Locale_numeric_name(const struct _Locale_numeric *lnum, char* buf) 126 { return _C_name; } 127 128 char const* _Locale_time_name(const struct _Locale_time *ltime, char* buf) 129 { return _C_name; } 130 131 char const* _Locale_collate_name(const struct _Locale_collate *lcol, char* buf) 132 { return _C_name; } 133 134 char const* _Locale_monetary_name(const struct _Locale_monetary *lmon, char* buf) 135 { return _C_name; } 136 137 char const* _Locale_messages_name(const struct _Locale_messages *lmes, char* buf) 138 { return _C_name; } 139 140 void _Locale_ctype_destroy(struct _Locale_ctype *lctype) {} 141 void _Locale_codecvt_destroy(struct _Locale_codecvt *lcodecvt) {} 142 void _Locale_numeric_destroy(struct _Locale_numeric *lnum) {} 143 void _Locale_time_destroy(struct _Locale_time *ltime) {} 144 void _Locale_collate_destroy(struct _Locale_collate *lcol) {} 145 void _Locale_monetary_destroy(struct _Locale_monetary *lmon) {} 146 void _Locale_messages_destroy(struct _Locale_messages *lmes) {} 147 148 static char const* _Locale_extract_name(const char* name, int *__err_code) { 149 // When the request is the default locale or the "C" locale we answer "C". 150 if (name[0] == 0 || 151 (name[0] == 'C' && name[1] == 0)) 152 { return _C_name; } 153 *__err_code = _STLP_LOC_NO_PLATFORM_SUPPORT; return 0; 154 } 155 156 char const* _Locale_extract_ctype_name(const char *name, char *buf, 157 struct _Locale_name_hint* hint, int *__err_code) 158 { return _Locale_extract_name(name, __err_code); } 159 160 char const* _Locale_extract_numeric_name(const char *name, char *buf, 161 struct _Locale_name_hint* hint, int *__err_code) 162 { return _Locale_extract_name(name, __err_code); } 163 164 char const* _Locale_extract_time_name(const char*name, char *buf, 165 struct _Locale_name_hint* hint, int *__err_code) 166 { return _Locale_extract_name(name, __err_code); } 167 168 char const* _Locale_extract_collate_name(const char *name, char *buf, 169 struct _Locale_name_hint* hint, int *__err_code) 170 { return _Locale_extract_name(name, __err_code); } 171 172 char const* _Locale_extract_monetary_name(const char *name, char *buf, 173 struct _Locale_name_hint* hint, int *__err_code) 174 { return _Locale_extract_name(name, __err_code); } 175 176 char const* _Locale_extract_messages_name(const char *name, char *buf, 177 struct _Locale_name_hint* hint, int *__err_code) 178 { return _Locale_extract_name(name, __err_code); } 179 180 struct _Locale_name_hint* _Locale_get_ctype_hint(struct _Locale_ctype* ctype) 181 { return 0; } 182 struct _Locale_name_hint* _Locale_get_numeric_hint(struct _Locale_numeric* numeric) 183 { return 0; } 184 struct _Locale_name_hint* _Locale_get_time_hint(struct _Locale_time* time) 185 { return 0; } 186 struct _Locale_name_hint* _Locale_get_collate_hint(struct _Locale_collate* collate) 187 { return 0; } 188 struct _Locale_name_hint* _Locale_get_monetary_hint(struct _Locale_monetary* monetary) 189 { return 0; } 190 struct _Locale_name_hint* _Locale_get_messages_hint(struct _Locale_messages* messages) 191 { return 0; } 192 193 /* ctype */ 194 const _Locale_mask_t* _Locale_ctype_table(struct _Locale_ctype* lctype) { 195 _STLP_MARK_PARAMETER_AS_UNUSED(lctype) 196 return ctable; 197 } 198 199 int _Locale_toupper(struct _Locale_ctype*lctype, int c) 200 { return toupper(c); } 201 202 int _Locale_tolower(struct _Locale_ctype*lctype, int c) 203 { return tolower(c); } 204 205 #ifndef _STLP_NO_WCHAR_T 206 _Locale_mask_t _WLocale_ctype(struct _Locale_ctype *lctype, wint_t wc, _Locale_mask_t mask) { 207 _Locale_mask_t ret = 0; 208 if ((mask & _Locale_ALPHA) != 0 && iswalpha(wc)) 209 ret |= _Locale_ALPHA; 210 211 if ((mask & _Locale_CNTRL) != 0 && iswcntrl(wc)) 212 ret |= _Locale_CNTRL; 213 214 if ((mask & _Locale_DIGIT) != 0 && iswdigit(wc)) 215 ret |= _Locale_DIGIT; 216 217 if ((mask & _Locale_PRINT) != 0 && iswprint(wc)) 218 ret |= _Locale_PRINT; 219 220 if ((mask & _Locale_PUNCT) != 0 && iswpunct(wc)) 221 ret |= _Locale_PUNCT; 222 223 if ((mask & _Locale_SPACE) != 0 && iswspace(wc)) 224 ret |= _Locale_SPACE; 225 226 if ((mask & _Locale_XDIGIT) != 0 && iswxdigit(wc)) 227 ret |= _Locale_XDIGIT; 228 229 if ((mask & _Locale_UPPER) != 0 && iswupper(wc)) 230 ret |= _Locale_UPPER; 231 232 if ((mask & _Locale_LOWER) != 0 && iswlower(wc)) 233 ret |= _Locale_LOWER; 234 235 return ret; 236 } 237 238 wint_t _WLocale_tolower(struct _Locale_ctype *lctype, wint_t wc) 239 { return towlower(wc); } 240 241 wint_t _WLocale_toupper(struct _Locale_ctype *lctype, wint_t wc) 242 { return towupper(wc); } 243 244 int _WLocale_mb_cur_max (struct _Locale_codecvt *lcodecvt) { return 1; } 245 int _WLocale_mb_cur_min (struct _Locale_codecvt *lcodecvt) { return 1; } 246 int _WLocale_is_stateless (struct _Locale_codecvt *lcodecvt) { return 1; } 247 248 size_t _WLocale_mbtowc(struct _Locale_codecvt *lcodecvt, 249 wchar_t *to, 250 const char *from, size_t n, 251 mbstate_t *st) 252 { *to = *from; return 1; } 253 254 size_t _WLocale_wctomb(struct _Locale_codecvt *lcodecvt, 255 char *to, size_t n, 256 const wchar_t c, 257 mbstate_t *st) 258 { *to = (char)c; return 1; } 259 260 size_t _WLocale_unshift(struct _Locale_codecvt *lcodecvt, 261 mbstate_t *st, 262 char *buf, size_t n, char ** next) 263 { *next = buf; return 0; } 264 #endif 265 266 /* Collate */ 267 int _Locale_strcmp(struct _Locale_collate* lcol, 268 const char* s1, size_t n1, const char* s2, size_t n2) { 269 int ret = 0; 270 char buf1[64], buf2[64]; 271 while (n1 > 0 || n2 > 0) { 272 size_t bufsize1 = n1 < 63 ? n1 : 63; 273 size_t bufsize2 = n2 < 63 ? n2 : 63; 274 _STLP_STRNCPY(buf1, 64, s1, bufsize1); buf1[bufsize1] = 0; 275 _STLP_STRNCPY(buf2, 64, s2, bufsize2); buf2[bufsize2] = 0; 276 277 ret = strcmp(buf1, buf2); 278 if (ret != 0) return ret < 0 ? -1 : 1; 279 s1 += bufsize1; n1 -= bufsize1; 280 s2 += bufsize2; n2 -= bufsize2; 281 } 282 return ret == 0 ? 0 : (ret < 0 ? -1 : 1); 283 } 284 285 #ifndef _STLP_NO_WCHAR_T 286 287 int _WLocale_strcmp(struct _Locale_collate* lcol, 288 const wchar_t* s1, size_t n1, const wchar_t* s2, size_t n2) { 289 int ret = 0; 290 wchar_t buf1[64], buf2[64]; 291 while (n1 > 0 || n2 > 0) { 292 size_t bufsize1 = n1 < 63 ? n1 : 63; 293 size_t bufsize2 = n2 < 63 ? n2 : 63; 294 _STLP_WCSNCPY(buf1, 64, s1, bufsize1); buf1[bufsize1] = 0; 295 _STLP_WCSNCPY(buf2, 64, s2, bufsize2); buf2[bufsize2] = 0; 296 297 ret = wcscmp(buf1, buf2); 298 if (ret != 0) return ret < 0 ? -1 : 1; 299 s1 += bufsize1; n1 -= bufsize1; 300 s2 += bufsize2; n2 -= bufsize2; 301 } 302 return ret == 0 ? 0 : (ret < 0 ? -1 : 1); 303 } 304 305 #endif 306 307 size_t _Locale_strxfrm(struct _Locale_collate* lcol, 308 char* dest, size_t dest_n, 309 const char* src, size_t src_n) { 310 if (dest != 0) { 311 _STLP_STRNCPY(dest, dest_n, src, dest_n - 1); dest[dest_n - 1] = 0; 312 } 313 return src_n; 314 } 315 316 #ifndef _STLP_NO_WCHAR_T 317 318 size_t _WLocale_strxfrm(struct _Locale_collate* lcol, 319 wchar_t* dest, size_t dest_n, 320 const wchar_t* src, size_t src_n) { 321 if (dest != 0) { 322 _STLP_WCSNCPY(dest, dest_n, src, dest_n - 1); dest[dest_n - 1] = 0; 323 } 324 return src_n; 325 } 326 327 #endif 328 329 /* Numeric */ 330 331 char _Locale_decimal_point(struct _Locale_numeric* lnum) 332 { return '.'; } 333 char _Locale_thousands_sep(struct _Locale_numeric* lnum) 334 { return ','; } 335 const char* _Locale_grouping(struct _Locale_numeric * lnum) 336 { return _empty_str; } 337 const char * _Locale_true(struct _Locale_numeric * lnum) 338 { return "true"; } 339 const char * _Locale_false(struct _Locale_numeric * lnum) 340 { return "false"; } 341 342 #ifndef _STLP_NO_WCHAR_T 343 wchar_t _WLocale_decimal_point(struct _Locale_numeric* lnum) 344 { return L'.'; } 345 wchar_t _WLocale_thousands_sep(struct _Locale_numeric* lnum) 346 { return L','; } 347 const wchar_t * _WLocale_true(struct _Locale_numeric* lnum, wchar_t* buf, size_t bufSize) 348 { return L"true"; } 349 const wchar_t * _WLocale_false(struct _Locale_numeric* lnum, wchar_t* buf, size_t bufSize) 350 { return L"false"; } 351 #endif 352 353 /* Monetary */ 354 355 const char* _Locale_int_curr_symbol(struct _Locale_monetary * lmon) 356 { return _empty_str; } 357 const char* _Locale_currency_symbol(struct _Locale_monetary * lmon) 358 { return _empty_str; } 359 char _Locale_mon_decimal_point(struct _Locale_monetary * lmon) 360 { return '.'; } 361 char _Locale_mon_thousands_sep(struct _Locale_monetary * lmon) 362 { return ','; } 363 const char* _Locale_mon_grouping(struct _Locale_monetary * lmon) 364 { return _empty_str; } 365 const char* _Locale_positive_sign(struct _Locale_monetary * lmon) 366 { return _empty_str; } 367 const char* _Locale_negative_sign(struct _Locale_monetary * lmon) 368 { return _empty_str; } 369 char _Locale_int_frac_digits(struct _Locale_monetary * lmon) 370 { return 0; } 371 char _Locale_frac_digits(struct _Locale_monetary * lmon) 372 { return 0; } 373 int _Locale_p_cs_precedes(struct _Locale_monetary * lmon) 374 { return CHAR_MAX; } 375 int _Locale_p_sep_by_space(struct _Locale_monetary * lmon) 376 { return CHAR_MAX; } 377 int _Locale_p_sign_posn(struct _Locale_monetary * lmon) 378 { return CHAR_MAX; } 379 int _Locale_n_cs_precedes(struct _Locale_monetary * lmon) 380 { return CHAR_MAX; } 381 int _Locale_n_sep_by_space(struct _Locale_monetary * lmon) 382 { return CHAR_MAX; } 383 int _Locale_n_sign_posn(struct _Locale_monetary * lmon) 384 { return CHAR_MAX; } 385 386 #ifndef _STLP_NO_WCHAR_T 387 const wchar_t* _WLocale_int_curr_symbol(struct _Locale_monetary * lmon, 388 wchar_t* buf, size_t bufSize) 389 { return _empty_wstr; } 390 const wchar_t* _WLocale_currency_symbol(struct _Locale_monetary * lmon, 391 wchar_t* buf, size_t bufSize) 392 { return _empty_wstr; } 393 wchar_t _WLocale_mon_decimal_point(struct _Locale_monetary * lmon) 394 { return L'.'; } 395 wchar_t _WLocale_mon_thousands_sep(struct _Locale_monetary * lmon) 396 { return L','; } 397 const wchar_t* _WLocale_positive_sign(struct _Locale_monetary * lmon, 398 wchar_t* buf, size_t bufSize) 399 { return _empty_wstr; } 400 const wchar_t* _WLocale_negative_sign(struct _Locale_monetary * lmon, 401 wchar_t* buf, size_t bufSize) 402 { return _empty_wstr; } 403 #endif 404 405 /* Time */ 406 static const char* full_monthname[] = 407 { "January", "February", "March", "April", "May", "June", 408 "July", "August", "September", "October", "November", "December" }; 409 const char * _Locale_full_monthname(struct _Locale_time * ltime, int n) 410 { return full_monthname[n]; } 411 412 static const char* abbrev_monthname[] = 413 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 414 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 415 const char * _Locale_abbrev_monthname(struct _Locale_time * ltime, int n) 416 { return abbrev_monthname[n]; } 417 418 static const char* full_dayname[] = 419 { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; 420 const char * _Locale_full_dayofweek(struct _Locale_time * ltime, int n) 421 { return full_dayname[n]; } 422 423 static const char* abbrev_dayname[] = 424 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; 425 const char * _Locale_abbrev_dayofweek(struct _Locale_time * ltime, int n) 426 { return abbrev_dayname[n]; } 427 428 const char* _Locale_d_t_fmt(struct _Locale_time* ltime) 429 { return "%m/%d/%y"; } 430 const char* _Locale_d_fmt(struct _Locale_time* ltime) 431 { return "%m/%d/%y"; } 432 const char* _Locale_t_fmt(struct _Locale_time* ltime) 433 { return "%H:%M:%S"; } 434 const char* _Locale_long_d_t_fmt(struct _Locale_time* ltime) 435 { return _empty_str; } 436 const char* _Locale_long_d_fmt(struct _Locale_time* ltime) 437 { return _empty_str; } 438 const char* _Locale_am_str(struct _Locale_time* ltime) 439 { return "AM"; } 440 const char* _Locale_pm_str(struct _Locale_time* ltime) 441 { return "PM"; } 442 443 #ifndef _STLP_NO_WCHAR_T 444 static const wchar_t* full_wmonthname[] = 445 { L"January", L"February", L"March", L"April", L"May", L"June", 446 L"July", L"August", L"September", L"October", L"November", L"December" }; 447 const wchar_t * _WLocale_full_monthname(struct _Locale_time * ltime, int n, 448 wchar_t* buf, size_t bufSize) 449 { return full_wmonthname[n]; } 450 451 static const wchar_t* abbrev_wmonthname[] = 452 { L"Jan", L"Feb", L"Mar", L"Apr", L"May", L"Jun", 453 L"Jul", L"Aug", L"Sep", L"Oct", L"Nov", L"Dec" }; 454 const wchar_t * _WLocale_abbrev_monthname(struct _Locale_time * ltime, int n, 455 wchar_t* buf, size_t bufSize) 456 { return abbrev_wmonthname[n]; } 457 458 static const wchar_t* full_wdayname[] = 459 { L"Sunday", L"Monday", L"Tuesday", L"Wednesday", L"Thursday", L"Friday", L"Saturday" }; 460 const wchar_t * _WLocale_full_dayofweek(struct _Locale_time * ltime, int n, 461 wchar_t* buf, size_t bufSize) 462 { return full_wdayname[n]; } 463 464 static const wchar_t* abbrev_wdayname[] = 465 { L"Sun", L"Mon", L"Tue", L"Wed", L"Thu", L"Fri", L"Sat" }; 466 const wchar_t * _WLocale_abbrev_dayofweek(struct _Locale_time * ltime, int n, 467 wchar_t* buf, size_t bufSize) 468 { return abbrev_wdayname[n]; } 469 470 const wchar_t* _WLocale_am_str(struct _Locale_time* ltime, 471 wchar_t* buf, size_t bufSize) 472 { return L"AM"; } 473 const wchar_t* _WLocale_pm_str(struct _Locale_time* ltime, 474 wchar_t* buf, size_t bufSize) 475 { return L"PM"; } 476 #endif 477 478 /* Messages */ 479 480 nl_catd_type _Locale_catopen(struct _Locale_messages* lmes, const char* name) 481 { return -1; } 482 void _Locale_catclose(struct _Locale_messages* lmes, nl_catd_type cat) {} 483 const char* _Locale_catgets(struct _Locale_messages* lmes, nl_catd_type cat, 484 int setid, int msgid, const char *dfault) 485 { return dfault; } 486