summaryrefslogtreecommitdiffstats
path: root/libc/stdlib/wchar.c
blob: d805333a02a586e6c9d4ac079420f4ca8ea17c10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright (C) 2008 The Android Open Source Project
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <wchar.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

/* stubs for wide-char functions */
wint_t  btowc(int c)
{
  return (c == EOF) ? WEOF : c;
}

int fwprintf(FILE *stream, const wchar_t *format, ...)
{
    va_list  args;
    int      result;

    va_start(args, format);
    result = vfwprintf(stream, format, args);
    va_end(args);
    return result;
}

int wprintf(const wchar_t *format, ...)
{
    va_list  args;
    int      result;

    va_start(args, format);
    result = vwprintf(format, args);
    va_end(args);
    return result;
}

int swprintf(wchar_t *s, size_t n, const wchar_t *format, ...)
{
    va_list  args;
    int      result;

    va_start(args, format);
    result = vswprintf(s, n, format, args);
    va_end(args);
    return result;
}

int vwprintf(const wchar_t *format, va_list arg)
{
    return vprintf((const char*)format, arg);
}

int vfwprintf(FILE *stream, const wchar_t *format, va_list arg)
{
    return vfprintf(stream, (const char*)format, arg);
}

int vswprintf(wchar_t *s, size_t n, const wchar_t *format, va_list arg)
{
    return vsnprintf( (char*)s, n, (const char*)format, arg );
}

int fwscanf(FILE *stream, const wchar_t *format, ... )
{
    va_list  args;
    int      result;

    va_start (args, format);
    result = vfscanf( stream, (const char*)format, args );
    va_end (args);
    return result;
}

int wscanf(const wchar_t *format, ... )
{
    va_list  args;
    int      result;

    va_start (args, format);
    result = vscanf( (const char*)format, args );
    va_end (args);
    return result;
}

int swscanf(const wchar_t *s, const wchar_t *format, ... )
{
    va_list  args;
    int      result;

    va_start (args, format);
    result = vscanf( (const char*)format, args );
    va_end (args);
    return result;
}

int iswalnum(wint_t wc) { return isalnum(wc); }
int iswalpha(wint_t wc) { return isalpha(wc); }
int iswcntrl(wint_t wc) { return iscntrl(wc); }
int iswdigit(wint_t wc) { return isdigit(wc); }
int iswgraph(wint_t wc) { return isgraph(wc); }
int iswlower(wint_t wc) { return islower(wc); }
int iswprint(wint_t wc) { return isprint(wc); }
int iswpunct(wint_t wc) { return ispunct(wc); }
int iswspace(wint_t wc) { return isspace(wc); }
int iswupper(wint_t wc) { return isupper(wc); }
int iswxdigit(wint_t wc) { return isxdigit(wc); }

int iswctype(wint_t wc, wctype_t charclass)
{
    switch (charclass) {
        case WC_TYPE_ALNUM: return isalnum(wc);
        case WC_TYPE_ALPHA: return isalpha(wc);
        case WC_TYPE_BLANK: return isblank(wc);
        case WC_TYPE_CNTRL: return iscntrl(wc);
        case WC_TYPE_DIGIT: return isdigit(wc);
        case WC_TYPE_GRAPH: return isgraph(wc);
        case WC_TYPE_LOWER: return islower(wc);
        case WC_TYPE_PRINT: return isprint(wc);
        case WC_TYPE_PUNCT: return ispunct(wc);
        case WC_TYPE_SPACE: return isspace(wc);
        case WC_TYPE_UPPER: return isupper(wc);
        case WC_TYPE_XDIGIT: return isxdigit(wc);
        default: return 0;
    };
}

wint_t fgetwc(FILE *stream)
{
    return fgetc(stream);
}

wchar_t *fgetws(wchar_t *ws, int n, FILE *stream)
{
    return (wchar_t*) fgets((char*)ws, n, stream);
}

wint_t   fputwc(wchar_t wc, FILE *stream)
{
    return (wint_t)fputc((char)wc, stream);
}

int  fputws(const wchar_t *str, FILE *stream)
{
    return fputs( (const char*)str, stream );
}

int  fwide(FILE *stream, int  mode)
{
    stream=stream;
    return (mode);
}

wint_t  getwc(FILE *stream)
{
    return getc(stream);
}

wint_t  getwchar(void)
{
    return getchar();
}

int mbsinit(const mbstate_t *ps)
{
    ps=ps;
    return 1;
}

size_t mbrlen(const char *s, size_t n, mbstate_t *ps)
{
    return (n != 0);
}

size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
{
    if (s == NULL) {
        s   = "";
        pwc = NULL;
    }
    if (n == 0) {
        if (pwc)
            *pwc = 0;
        return 0;
    }
    if (pwc)
        *pwc = *s;

    return (*s != 0);
}

size_t mbsrtowcs(wchar_t *dst, const char **src, size_t len, mbstate_t *ps)
{
    const char*  s  = *src;
    const char*  s2 = memchr( s, 0, len );

    if (s2 != NULL)
        len = (size_t)(s2 - s) + 1U;

    if (dst)
        memcpy( (char*)dst, s, len );

    *src = s + len;
    return len;
}

wint_t  putwc(wchar_t wc, FILE *stream)
{
    return fputc((char)wc, stream);
}

wint_t  putwchar(wchar_t wc)
{
    return  putchar((char)wc);
}

wint_t  towlower(wint_t wc)
{
    return tolower(wc);
}

wint_t  towupper(wint_t  wc)
{
    return toupper(wc);
}

wint_t  ungetwc(wint_t wc, FILE *stream)
{
    return ungetc((char)wc, stream);
}

size_t wcrtomb(char *s, wchar_t wc, mbstate_t *ps)
{
    if (s != NULL)
        *s = 1;
    return 1;
}

wchar_t *wcscat(wchar_t *ws1, const wchar_t *ws2)
{
    return (wchar_t*) strcat((char*)ws1, (const char*)ws2);
}

wchar_t *wcschr(const wchar_t *ws, wchar_t wc)
{
    return (wchar_t*)strchr( (const char*)ws, (char)wc );
}

int wcscmp(const wchar_t *ws1, const wchar_t *ws2)
{
    return strcmp( (const char*)ws1, (const char*)ws2 );
}

int wcscoll(const wchar_t *ws1, const wchar_t *ws2)
{
    return strcmp( (const char*)ws1, (const char*)ws2 );
}

wchar_t *wcscpy(wchar_t *ws1, const wchar_t *ws2)
{
    return (wchar_t*) strcpy( (char*)ws1, (const char*)ws2 );
}

size_t wcscspn(const wchar_t *ws1, const wchar_t *ws2)
{
    return strspn( (const char*)ws1, (const char*)ws2 );
}

size_t wcslen(const wchar_t *ws)
{
    return (size_t)strlen( (const char*)ws );
}

size_t wcsftime(wchar_t *wcs, size_t maxsize, const wchar_t *format,  const struct tm *timptr)
{
    return strftime( (char*)wcs, maxsize, (const char*)format, timptr );
}

wchar_t *wcsncat(wchar_t *ws1, const wchar_t *ws2, size_t n)
{
    return (wchar_t*) strncat( (char*)ws1, (const char*)ws2, n );
}

int wcsncmp(const wchar_t *ws1, const wchar_t *ws2, size_t n)
{
    return strncmp( (const char*)ws1, (const char*)ws2, n );
}

wchar_t *wcsncpy(wchar_t *ws1, const wchar_t *ws2, size_t n)
{
    return (wchar_t*) strncpy( (char*)ws1, (const char*)ws2, n );
}

wchar_t *wcspbrk(const wchar_t *ws1, const wchar_t *ws2)
{
    return (wchar_t*) strpbrk( (const char*)ws1, (const char*)ws2 );
}

wchar_t *wcsrchr(const wchar_t *ws, wchar_t wc)
{
    return (wchar_t*) strrchr( (const char*)ws, (int)wc );
}

size_t wcsrtombs(char *dst, const wchar_t **src, size_t len, mbstate_t *ps)
{
    const char*  s = (const char*)*src;
    const char*  s2 = memchr( s, 0, len );

    if (s2 != NULL)
        len = (s2 - s)+1;

    if (dst != NULL)
        memcpy( dst, s, len );

    *src = (wchar_t*)(s + len);
    return len;
}

size_t wcsspn(const wchar_t *ws1, const wchar_t *ws2)
{
    return strspn( (const char*)ws1, (const char*)ws2 );
}

wchar_t *wcsstr(const wchar_t *ws1, const wchar_t *ws2)
{
    return (wchar_t*) strstr( (const char*)ws1, (const char*)ws2 );
}

double wcstod(const wchar_t *nptr, wchar_t **endptr)
{
    return strtod( (const char*)nptr, (char**)endptr );
}

wchar_t *wcstok(wchar_t *ws1, const wchar_t *ws2, wchar_t **ptr)
{
    return (wchar_t*) strtok_r( (char*)ws1, (const char*)ws2, (char**)ptr );
}

long int wcstol(const wchar_t *nptr, wchar_t **endptr, int base)
{
    return strtol( (const char*)nptr, (char**)endptr, base );
}

unsigned long int wcstoul(const wchar_t *nptr, wchar_t **endptr, int base)
{
    return strtoul( (const char*)nptr, (char**)endptr, base );
}

wchar_t *wcswcs(const wchar_t *ws1, const wchar_t *ws2)
{
    return (wchar_t*) strstr( (const char*)ws1, (const char*)ws2 );
}

int wcswidth(const wchar_t *pwcs, size_t n)
{
    return  strnlen( (const char*)pwcs, n );
}

size_t wcsxfrm(wchar_t *ws1, const wchar_t *ws2, size_t n)
{
    memcpy( (char*)ws1, (const char*)ws2, n );
    return n;
}

int wctob(wint_t c)
{
  return c;
}

wctype_t wctype(const char *property)
{
    static const char* const  properties[WC_TYPE_MAX] =
    {
        "<invalid>",
        "alnum", "alpha", "blank", "cntrl", "digit", "graph",
        "lower", "print", "punct", "space", "upper", "xdigit"
    };
    int  nn;

    for ( nn = 0; nn < WC_TYPE_MAX; nn++ )
        if ( !strcmp( properties[nn], property ) )
            return (wctype_t)(nn);

    return 0;
}

int wcwidth(wchar_t wc)
{
    return (wc > 0);
}

wchar_t *wmemchr(const wchar_t *ws, wchar_t wc, size_t n)
{
    return (wchar_t*)  memchr( (const char*)ws, (int)wc, n );
}

int wmemcmp(const wchar_t *ws1, const wchar_t *ws2, size_t n)
{
    return  memcmp( (const char*)ws1, (const char*)ws2, n );
}

wchar_t *wmemcpy(wchar_t *ws1, const wchar_t *ws2, size_t n)
{
    return (wchar_t*) memcpy( (char*)ws1, (const char*)ws2, n );
}

wchar_t *wmemmove(wchar_t *ws1, const wchar_t *ws2, size_t n)
{
    return (wchar_t*)memmove( (char*)ws1, (const char*)ws2, n );
}

wchar_t *wmemset(wchar_t *ws, wchar_t wc, size_t n)
{
    return (wchar_t*) memset( (char*)ws, (int)wc, n );
}