The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | #include <wchar.h> |
| 29 | #include <ctype.h> |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | /* stubs for wide-char functions */ |
| 34 | wint_t btowc(int c) |
| 35 | { |
| 36 | return (c == EOF) ? WEOF : c; |
| 37 | } |
| 38 | |
| 39 | int fwprintf(FILE *stream, const wchar_t *format, ...) |
| 40 | { |
| 41 | va_list args; |
| 42 | int result; |
| 43 | |
| 44 | va_start(args, format); |
| 45 | result = vfwprintf(stream, format, args); |
| 46 | va_end(args); |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | int wprintf(const wchar_t *format, ...) |
| 51 | { |
| 52 | va_list args; |
| 53 | int result; |
| 54 | |
| 55 | va_start(args, format); |
| 56 | result = vwprintf(format, args); |
| 57 | va_end(args); |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | int swprintf(wchar_t *s, size_t n, const wchar_t *format, ...) |
| 62 | { |
| 63 | va_list args; |
| 64 | int result; |
| 65 | |
| 66 | va_start(args, format); |
| 67 | result = vswprintf(s, n, format, args); |
| 68 | va_end(args); |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | int vwprintf(const wchar_t *format, va_list arg) |
| 73 | { |
| 74 | return vprintf((const char*)format, arg); |
| 75 | } |
| 76 | |
| 77 | int vfwprintf(FILE *stream, const wchar_t *format, va_list arg) |
| 78 | { |
| 79 | return vfprintf(stream, (const char*)format, arg); |
| 80 | } |
| 81 | |
| 82 | int vswprintf(wchar_t *s, size_t n, const wchar_t *format, va_list arg) |
| 83 | { |
| 84 | return vsnprintf( (char*)s, n, (const char*)format, arg ); |
| 85 | } |
| 86 | |
| 87 | int fwscanf(FILE *stream, const wchar_t *format, ... ) |
| 88 | { |
| 89 | va_list args; |
| 90 | int result; |
| 91 | |
| 92 | va_start (args, format); |
| 93 | result = vfscanf( stream, (const char*)format, args ); |
| 94 | va_end (args); |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | int wscanf(const wchar_t *format, ... ) |
| 99 | { |
| 100 | va_list args; |
| 101 | int result; |
| 102 | |
| 103 | va_start (args, format); |
| 104 | result = vscanf( (const char*)format, args ); |
| 105 | va_end (args); |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | int swscanf(const wchar_t *s, const wchar_t *format, ... ) |
| 110 | { |
| 111 | va_list args; |
| 112 | int result; |
| 113 | |
| 114 | va_start (args, format); |
| 115 | result = vscanf( (const char*)format, args ); |
| 116 | va_end (args); |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | int iswalnum(wint_t wc) { return isalnum(wc); } |
| 121 | int iswalpha(wint_t wc) { return isalpha(wc); } |
| 122 | int iswcntrl(wint_t wc) { return iscntrl(wc); } |
| 123 | int iswdigit(wint_t wc) { return isdigit(wc); } |
| 124 | int iswgraph(wint_t wc) { return isgraph(wc); } |
| 125 | int iswlower(wint_t wc) { return islower(wc); } |
| 126 | int iswprint(wint_t wc) { return isprint(wc); } |
| 127 | int iswpunct(wint_t wc) { return ispunct(wc); } |
| 128 | int iswspace(wint_t wc) { return isspace(wc); } |
| 129 | int iswupper(wint_t wc) { return isupper(wc); } |
| 130 | int iswxdigit(wint_t wc) { return isxdigit(wc); } |
| 131 | |
| 132 | int iswctype(wint_t wc, wctype_t charclass) |
| 133 | { |
| 134 | switch (charclass) { |
| 135 | case WC_TYPE_ALNUM: return isalnum(wc); |
| 136 | case WC_TYPE_ALPHA: return isalpha(wc); |
| 137 | case WC_TYPE_BLANK: return isblank(wc); |
| 138 | case WC_TYPE_CNTRL: return iscntrl(wc); |
| 139 | case WC_TYPE_DIGIT: return isdigit(wc); |
| 140 | case WC_TYPE_GRAPH: return isgraph(wc); |
| 141 | case WC_TYPE_LOWER: return islower(wc); |
| 142 | case WC_TYPE_PRINT: return isprint(wc); |
| 143 | case WC_TYPE_PUNCT: return ispunct(wc); |
| 144 | case WC_TYPE_SPACE: return isspace(wc); |
| 145 | case WC_TYPE_UPPER: return isupper(wc); |
| 146 | case WC_TYPE_XDIGIT: return isxdigit(wc); |
| 147 | default: return 0; |
| 148 | }; |
| 149 | } |
| 150 | |
| 151 | wint_t fgetwc(FILE *stream) |
| 152 | { |
| 153 | return fgetc(stream); |
| 154 | } |
| 155 | |
| 156 | wchar_t *fgetws(wchar_t *ws, int n, FILE *stream) |
| 157 | { |
| 158 | return (wchar_t*) fgets((char*)ws, n, stream); |
| 159 | } |
| 160 | |
| 161 | wint_t fputwc(wchar_t wc, FILE *stream) |
| 162 | { |
| 163 | return (wint_t)fputc((char)wc, stream); |
| 164 | } |
| 165 | |
| 166 | int fputws(const wchar_t *str, FILE *stream) |
| 167 | { |
| 168 | return fputs( (const char*)str, stream ); |
| 169 | } |
| 170 | |
| 171 | int fwide(FILE *stream, int mode) |
| 172 | { |
| 173 | stream=stream; |
| 174 | return (mode); |
| 175 | } |
| 176 | |
| 177 | wint_t getwc(FILE *stream) |
| 178 | { |
| 179 | return getc(stream); |
| 180 | } |
| 181 | |
| 182 | wint_t getwchar(void) |
| 183 | { |
| 184 | return getchar(); |
| 185 | } |
| 186 | |
| 187 | int mbsinit(const mbstate_t *ps) |
| 188 | { |
| 189 | ps=ps; |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | size_t mbrlen(const char *s, size_t n, mbstate_t *ps) |
| 194 | { |
| 195 | return (n != 0); |
| 196 | } |
| 197 | |
| 198 | size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps) |
| 199 | { |
| 200 | if (s == NULL) { |
| 201 | s = ""; |
| 202 | pwc = NULL; |
| 203 | } |
| 204 | if (n == 0) { |
| 205 | if (pwc) |
| 206 | *pwc = 0; |
| 207 | return 0; |
| 208 | } |
| 209 | if (pwc) |
| 210 | *pwc = *s; |
| 211 | |
| 212 | return (*s != 0); |
| 213 | } |
| 214 | |
| 215 | size_t mbsrtowcs(wchar_t *dst, const char **src, size_t len, mbstate_t *ps) |
| 216 | { |
| 217 | const char* s = *src; |
| 218 | const char* s2 = memchr( s, 0, len ); |
| 219 | |
| 220 | if (s2 != NULL) |
| 221 | len = (size_t)(s2 - s) + 1U; |
| 222 | |
| 223 | if (dst) |
| 224 | memcpy( (char*)dst, s, len ); |
| 225 | |
| 226 | *src = s + len; |
| 227 | return len; |
| 228 | } |
| 229 | |
| 230 | wint_t putwc(wchar_t wc, FILE *stream) |
| 231 | { |
| 232 | return fputc((char)wc, stream); |
| 233 | } |
| 234 | |
| 235 | wint_t putwchar(wchar_t wc) |
| 236 | { |
| 237 | return putchar((char)wc); |
| 238 | } |
| 239 | |
| 240 | wint_t towlower(wint_t wc) |
| 241 | { |
| 242 | return tolower(wc); |
| 243 | } |
| 244 | |
| 245 | wint_t towupper(wint_t wc) |
| 246 | { |
| 247 | return toupper(wc); |
| 248 | } |
| 249 | |
| 250 | wint_t ungetwc(wint_t wc, FILE *stream) |
| 251 | { |
| 252 | return ungetc((char)wc, stream); |
| 253 | } |
| 254 | |
| 255 | size_t wcrtomb(char *s, wchar_t wc, mbstate_t *ps) |
| 256 | { |
| 257 | if (s != NULL) |
| 258 | *s = 1; |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | wchar_t *wcscat(wchar_t *ws1, const wchar_t *ws2) |
| 263 | { |
| 264 | return (wchar_t*) strcat((char*)ws1, (const char*)ws2); |
| 265 | } |
| 266 | |
| 267 | wchar_t *wcschr(const wchar_t *ws, wchar_t wc) |
| 268 | { |
| 269 | return (wchar_t*)strchr( (const char*)ws, (char)wc ); |
| 270 | } |
| 271 | |
| 272 | int wcscmp(const wchar_t *ws1, const wchar_t *ws2) |
| 273 | { |
| 274 | return strcmp( (const char*)ws1, (const char*)ws2 ); |
| 275 | } |
| 276 | |
| 277 | int wcscoll(const wchar_t *ws1, const wchar_t *ws2) |
| 278 | { |
| 279 | return strcmp( (const char*)ws1, (const char*)ws2 ); |
| 280 | } |
| 281 | |
| 282 | wchar_t *wcscpy(wchar_t *ws1, const wchar_t *ws2) |
| 283 | { |
| 284 | return (wchar_t*) strcpy( (char*)ws1, (const char*)ws2 ); |
| 285 | } |
| 286 | |
| 287 | size_t wcscspn(const wchar_t *ws1, const wchar_t *ws2) |
| 288 | { |
| 289 | return strspn( (const char*)ws1, (const char*)ws2 ); |
| 290 | } |
| 291 | |
| 292 | size_t wcslen(const wchar_t *ws) |
| 293 | { |
| 294 | return (size_t)strlen( (const char*)ws ); |
| 295 | } |
| 296 | |
| 297 | size_t wcsftime(wchar_t *wcs, size_t maxsize, const wchar_t *format, const struct tm *timptr) |
| 298 | { |
| 299 | return strftime( (char*)wcs, maxsize, (const char*)format, timptr ); |
| 300 | } |
| 301 | |
| 302 | wchar_t *wcsncat(wchar_t *ws1, const wchar_t *ws2, size_t n) |
| 303 | { |
| 304 | return (wchar_t*) strncat( (char*)ws1, (const char*)ws2, n ); |
| 305 | } |
| 306 | |
| 307 | int wcsncmp(const wchar_t *ws1, const wchar_t *ws2, size_t n) |
| 308 | { |
| 309 | return strncmp( (const char*)ws1, (const char*)ws2, n ); |
| 310 | } |
| 311 | |
| 312 | wchar_t *wcsncpy(wchar_t *ws1, const wchar_t *ws2, size_t n) |
| 313 | { |
| 314 | return (wchar_t*) strncpy( (char*)ws1, (const char*)ws2, n ); |
| 315 | } |
| 316 | |
| 317 | wchar_t *wcspbrk(const wchar_t *ws1, const wchar_t *ws2) |
| 318 | { |
| 319 | return (wchar_t*) strpbrk( (const char*)ws1, (const char*)ws2 ); |
| 320 | } |
| 321 | |
| 322 | wchar_t *wcsrchr(const wchar_t *ws, wchar_t wc) |
| 323 | { |
| 324 | return (wchar_t*) strrchr( (const char*)ws, (int)wc ); |
| 325 | } |
| 326 | |
| 327 | size_t wcsrtombs(char *dst, const wchar_t **src, size_t len, mbstate_t *ps) |
| 328 | { |
| 329 | const char* s = (const char*)*src; |
| 330 | const char* s2 = memchr( s, 0, len ); |
| 331 | |
| 332 | if (s2 != NULL) |
| 333 | len = (s2 - s)+1; |
| 334 | |
| 335 | if (dst != NULL) |
| 336 | memcpy( dst, s, len ); |
| 337 | |
| 338 | *src = (wchar_t*)(s + len); |
| 339 | return len; |
| 340 | } |
| 341 | |
| 342 | size_t wcsspn(const wchar_t *ws1, const wchar_t *ws2) |
| 343 | { |
| 344 | return strspn( (const char*)ws1, (const char*)ws2 ); |
| 345 | } |
| 346 | |
| 347 | wchar_t *wcsstr(const wchar_t *ws1, const wchar_t *ws2) |
| 348 | { |
| 349 | return (wchar_t*) strstr( (const char*)ws1, (const char*)ws2 ); |
| 350 | } |
| 351 | |
| 352 | double wcstod(const wchar_t *nptr, wchar_t **endptr) |
| 353 | { |
| 354 | return strtod( (const char*)nptr, (char**)endptr ); |
| 355 | } |
| 356 | |
| 357 | wchar_t *wcstok(wchar_t *ws1, const wchar_t *ws2, wchar_t **ptr) |
| 358 | { |
| 359 | return (wchar_t*) strtok_r( (char*)ws1, (const char*)ws2, (char**)ptr ); |
| 360 | } |
| 361 | |
| 362 | long int wcstol(const wchar_t *nptr, wchar_t **endptr, int base) |
| 363 | { |
| 364 | return strtol( (const char*)nptr, (char**)endptr, base ); |
| 365 | } |
| 366 | |
| 367 | unsigned long int wcstoul(const wchar_t *nptr, wchar_t **endptr, int base) |
| 368 | { |
| 369 | return strtoul( (const char*)nptr, (char**)endptr, base ); |
| 370 | } |
| 371 | |
| 372 | wchar_t *wcswcs(const wchar_t *ws1, const wchar_t *ws2) |
| 373 | { |
| 374 | return (wchar_t*) strstr( (const char*)ws1, (const char*)ws2 ); |
| 375 | } |
| 376 | |
| 377 | int wcswidth(const wchar_t *pwcs, size_t n) |
| 378 | { |
| 379 | return strnlen( (const char*)pwcs, n ); |
| 380 | } |
| 381 | |
| 382 | size_t wcsxfrm(wchar_t *ws1, const wchar_t *ws2, size_t n) |
| 383 | { |
| 384 | memcpy( (char*)ws1, (const char*)ws2, n ); |
| 385 | return n; |
| 386 | } |
| 387 | |
| 388 | int wctob(wint_t c) |
| 389 | { |
| 390 | return c; |
| 391 | } |
| 392 | |
| 393 | wctype_t wctype(const char *property) |
| 394 | { |
| 395 | static const char* const properties[WC_TYPE_MAX] = |
| 396 | { |
| 397 | "<invalid>", |
| 398 | "alnum", "alpha", "blank", "cntrl", "digit", "graph", |
| 399 | "lower", "print", "punct", "space", "upper", "xdigit" |
| 400 | }; |
| 401 | int nn; |
| 402 | |
| 403 | for ( nn = 0; nn < WC_TYPE_MAX; nn++ ) |
| 404 | if ( !strcmp( properties[nn], property ) ) |
| 405 | return (wctype_t)(nn); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int wcwidth(wchar_t wc) |
| 411 | { |
| 412 | return (wc > 0); |
| 413 | } |
| 414 | |
| 415 | wchar_t *wmemchr(const wchar_t *ws, wchar_t wc, size_t n) |
| 416 | { |
| 417 | return (wchar_t*) memchr( (const char*)ws, (int)wc, n ); |
| 418 | } |
| 419 | |
| 420 | int wmemcmp(const wchar_t *ws1, const wchar_t *ws2, size_t n) |
| 421 | { |
| 422 | return memcmp( (const char*)ws1, (const char*)ws2, n ); |
| 423 | } |
| 424 | |
| 425 | wchar_t *wmemcpy(wchar_t *ws1, const wchar_t *ws2, size_t n) |
| 426 | { |
| 427 | return (wchar_t*) memcpy( (char*)ws1, (const char*)ws2, n ); |
| 428 | } |
| 429 | |
| 430 | wchar_t *wmemmove(wchar_t *ws1, const wchar_t *ws2, size_t n) |
| 431 | { |
| 432 | return (wchar_t*)memmove( (char*)ws1, (const char*)ws2, n ); |
| 433 | } |
| 434 | |
| 435 | wchar_t *wmemset(wchar_t *ws, wchar_t wc, size_t n) |
| 436 | { |
| 437 | return (wchar_t*) memset( (char*)ws, (int)wc, n ); |
| 438 | } |