f0020082ea2e8f5189bd8855d4741488b629cfee
[reactos.git] / reactos / lib / crtdll / old cruft / stdlib / wcstomb.c
1 /* Copyright (C) 1991, 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19 #include <msvcrt/stdlib.h>
20 #include <msvcrt/wchar.h>
21
22 #include <msvcrt/errno.h>
23 #include <msvcrt/wchar.h>
24
25 #ifndef EILSEQ
26 #define EILSEQ EINVAL
27 #endif
28
29 static const wchar_t encoding_mask[] =
30 {
31 (wchar_t)~0x7ff, (wchar_t)~0xffff, (wchar_t)~0x1fffff, (wchar_t)~0x3ffffff
32 };
33
34 static const unsigned char encoding_byte[] =
35 {
36 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
37 };
38
39 /* The state is for this UTF8 encoding not used. */
40 //static mbstate_t internal;
41
42
43 //extern mbstate_t __no_r_state; /* Defined in mbtowc.c. */
44
45 size_t
46 __wcrtomb (char *s, wchar_t wc);
47
48 /*
49 * Convert WCHAR into its multibyte character representation,
50 * putting this in S and returning its length.
51 *
52 * Attention: this function should NEVER be intentionally used.
53 * The interface is completely stupid. The state is shared between
54 * all conversion functions. You should use instead the restartable
55 * version `wcrtomb'.
56 *
57 * @implemented
58 */
59 int
60 wctomb (char *s, wchar_t wchar)
61 {
62 /* If S is NULL the function has to return null or not null
63 depending on the encoding having a state depending encoding or
64 not. This is nonsense because any multibyte encoding has a
65 state. The ISO C amendment 1 corrects this while introducing the
66 restartable functions. We simply say here all encodings have a
67 state. */
68 if (s == NULL)
69 return 1;
70
71 return __wcrtomb (s, wchar);
72 }
73
74
75 size_t
76 __wcrtomb (char *s, wchar_t wc)
77 {
78 char fake[1];
79 size_t written = 0;
80
81 if (s == NULL)
82 {
83 s = fake;
84 wc = L'\0';
85 }
86
87 if (wc < 0x80)
88 {
89 /* It's a one byte sequence. */
90 if (s != NULL)
91 *s = (char) wc;
92 return 1;
93 }
94
95 for (written = 2; written < 6; ++written)
96 if ((wc & encoding_mask[written - 2]) == 0)
97 break;
98
99 if (s != NULL)
100 {
101 size_t cnt = written;
102 s[0] = encoding_byte[cnt - 2];
103
104 --cnt;
105 do
106 {
107 s[cnt] = 0x80 | (wc & 0x3f);
108 wc >>= 6;
109 }
110 while (--cnt > 0);
111 s[0] |= wc;
112 }
113
114 return written;
115 }