Home | History | Annotate | Download | only in stdlib
      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 size_t mbstowcs(wchar_t *dst, const char *src, size_t len)
    231 {
    232     return mbsrtowcs(dst, &src, len, NULL);
    233 }
    234 
    235 wint_t  putwc(wchar_t wc, FILE *stream)
    236 {
    237     return fputc((char)wc, stream);
    238 }
    239 
    240 wint_t  putwchar(wchar_t wc)
    241 {
    242     return  putchar((char)wc);
    243 }
    244 
    245 wint_t  towlower(wint_t wc)
    246 {
    247     return tolower(wc);
    248 }
    249 
    250 wint_t  towupper(wint_t  wc)
    251 {
    252     return toupper(wc);
    253 }
    254 
    255 wint_t  ungetwc(wint_t wc, FILE *stream)
    256 {
    257     return ungetc((char)wc, stream);
    258 }
    259 
    260 size_t wcrtomb(char *s, wchar_t wc, mbstate_t *ps)
    261 {
    262     if (s != NULL)
    263         *s = 1;
    264     return 1;
    265 }
    266 
    267 wchar_t *wcscat(wchar_t *ws1, const wchar_t *ws2)
    268 {
    269     return (wchar_t*) strcat((char*)ws1, (const char*)ws2);
    270 }
    271 
    272 wchar_t *wcschr(const wchar_t *ws, wchar_t wc)
    273 {
    274     return (wchar_t*)strchr( (const char*)ws, (char)wc );
    275 }
    276 
    277 int wcscmp(const wchar_t *ws1, const wchar_t *ws2)
    278 {
    279     return strcmp( (const char*)ws1, (const char*)ws2 );
    280 }
    281 
    282 int wcscoll(const wchar_t *ws1, const wchar_t *ws2)
    283 {
    284     return strcmp( (const char*)ws1, (const char*)ws2 );
    285 }
    286 
    287 wchar_t *wcscpy(wchar_t *ws1, const wchar_t *ws2)
    288 {
    289     return (wchar_t*) strcpy( (char*)ws1, (const char*)ws2 );
    290 }
    291 
    292 size_t wcscspn(const wchar_t *ws1, const wchar_t *ws2)
    293 {
    294     return strspn( (const char*)ws1, (const char*)ws2 );
    295 }
    296 
    297 size_t wcslen(const wchar_t *ws)
    298 {
    299     return (size_t)strlen( (const char*)ws );
    300 }
    301 
    302 size_t wcsftime(wchar_t *wcs, size_t maxsize, const wchar_t *format,  const struct tm *timptr)
    303 {
    304     return strftime( (char*)wcs, maxsize, (const char*)format, timptr );
    305 }
    306 
    307 wchar_t *wcsncat(wchar_t *ws1, const wchar_t *ws2, size_t n)
    308 {
    309     return (wchar_t*) strncat( (char*)ws1, (const char*)ws2, n );
    310 }
    311 
    312 int wcsncmp(const wchar_t *ws1, const wchar_t *ws2, size_t n)
    313 {
    314     return strncmp( (const char*)ws1, (const char*)ws2, n );
    315 }
    316 
    317 wchar_t *wcsncpy(wchar_t *ws1, const wchar_t *ws2, size_t n)
    318 {
    319     return (wchar_t*) strncpy( (char*)ws1, (const char*)ws2, n );
    320 }
    321 
    322 wchar_t *wcspbrk(const wchar_t *ws1, const wchar_t *ws2)
    323 {
    324     return (wchar_t*) strpbrk( (const char*)ws1, (const char*)ws2 );
    325 }
    326 
    327 wchar_t *wcsrchr(const wchar_t *ws, wchar_t wc)
    328 {
    329     return (wchar_t*) strrchr( (const char*)ws, (int)wc );
    330 }
    331 
    332 size_t wcsrtombs(char *dst, const wchar_t **src, size_t len, mbstate_t *ps)
    333 {
    334     const char*  s = (const char*)*src;
    335     const char*  s2 = memchr( s, 0, len );
    336 
    337     if (s2 != NULL)
    338         len = (s2 - s)+1;
    339 
    340     if (dst != NULL)
    341         memcpy( dst, s, len );
    342 
    343     *src = (wchar_t*)(s + len);
    344     return len;
    345 }
    346 
    347 size_t wcstombs(char *dst, const wchar_t *src, size_t len)
    348 {
    349     return wcsrtombs(dst, &src, len, NULL);
    350 }
    351 
    352 size_t wcsspn(const wchar_t *ws1, const wchar_t *ws2)
    353 {
    354     return strspn( (const char*)ws1, (const char*)ws2 );
    355 }
    356 
    357 wchar_t *wcsstr(const wchar_t *ws1, const wchar_t *ws2)
    358 {
    359     return (wchar_t*) strstr( (const char*)ws1, (const char*)ws2 );
    360 }
    361 
    362 double wcstod(const wchar_t *nptr, wchar_t **endptr)
    363 {
    364     return strtod( (const char*)nptr, (char**)endptr );
    365 }
    366 
    367 wchar_t *wcstok(wchar_t *ws1, const wchar_t *ws2, wchar_t **ptr)
    368 {
    369     return (wchar_t*) strtok_r( (char*)ws1, (const char*)ws2, (char**)ptr );
    370 }
    371 
    372 long int wcstol(const wchar_t *nptr, wchar_t **endptr, int base)
    373 {
    374     return strtol( (const char*)nptr, (char**)endptr, base );
    375 }
    376 
    377 unsigned long int wcstoul(const wchar_t *nptr, wchar_t **endptr, int base)
    378 {
    379     return strtoul( (const char*)nptr, (char**)endptr, base );
    380 }
    381 
    382 wchar_t *wcswcs(const wchar_t *ws1, const wchar_t *ws2)
    383 {
    384     return (wchar_t*) strstr( (const char*)ws1, (const char*)ws2 );
    385 }
    386 
    387 int wcswidth(const wchar_t *pwcs, size_t n)
    388 {
    389     return  strnlen( (const char*)pwcs, n );
    390 }
    391 
    392 size_t wcsxfrm(wchar_t *ws1, const wchar_t *ws2, size_t n)
    393 {
    394     memcpy( (char*)ws1, (const char*)ws2, n );
    395     return n;
    396 }
    397 
    398 int wctob(wint_t c)
    399 {
    400   return c;
    401 }
    402 
    403 wctype_t wctype(const char *property)
    404 {
    405     static const char* const  properties[WC_TYPE_MAX] =
    406     {
    407         "<invalid>",
    408         "alnum", "alpha", "blank", "cntrl", "digit", "graph",
    409         "lower", "print", "punct", "space", "upper", "xdigit"
    410     };
    411     int  nn;
    412 
    413     for ( nn = 0; nn < WC_TYPE_MAX; nn++ )
    414         if ( !strcmp( properties[nn], property ) )
    415             return (wctype_t)(nn);
    416 
    417     return 0;
    418 }
    419 
    420 int wcwidth(wchar_t wc)
    421 {
    422     return (wc > 0);
    423 }
    424 
    425 wchar_t *wmemchr(const wchar_t *ws, wchar_t wc, size_t n)
    426 {
    427     return (wchar_t*)  memchr( (const char*)ws, (int)wc, n );
    428 }
    429 
    430 int wmemcmp(const wchar_t *ws1, const wchar_t *ws2, size_t n)
    431 {
    432     return  memcmp( (const char*)ws1, (const char*)ws2, n );
    433 }
    434 
    435 wchar_t *wmemcpy(wchar_t *ws1, const wchar_t *ws2, size_t n)
    436 {
    437     return (wchar_t*) memcpy( (char*)ws1, (const char*)ws2, n );
    438 }
    439 
    440 wchar_t *wmemmove(wchar_t *ws1, const wchar_t *ws2, size_t n)
    441 {
    442     return (wchar_t*)memmove( (char*)ws1, (const char*)ws2, n );
    443 }
    444 
    445 wchar_t *wmemset(wchar_t *ws, wchar_t wc, size_t n)
    446 {
    447     return (wchar_t*) memset( (char*)ws, (int)wc, n );
    448 }
    449