update for HEAD-2003091401
[reactos.git] / lib / msvcrt / stdlib / wctomb.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 <windows.h>
20 #include <msvcrt/stdlib.h>
21 #include <msvcrt/ctype.h>
22 #include <msvcrt/wchar.h>
23 #include <msvcrt/errno.h>
24 #include <msvcrt/internal/file.h>
25
26
27 int
28 STDCALL
29 WideCharToMultiByte(
30     UINT     CodePage,
31     DWORD    dwFlags,
32     LPCWSTR  lpWideCharStr,
33     int      cchWideChar,
34     LPSTR    lpMultiByteStr,
35     int      cchMultiByte,
36     LPCSTR   lpDefaultChar,
37     LPBOOL   lpUsedDefaultChar);
38
39
40 /*
41  * @unimplemented
42  */
43 int wctomb(char* dst, wchar_t ch)
44 {
45 #if 0
46     return WideCharToMultiByte(CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL);
47 #else
48     if (dst == NULL) {
49         return 1;
50     }
51     *dst = ch;
52     return 1;
53 #endif
54 }
55
56
57 #if 0
58
59 #ifndef EILSEQ
60 #define EILSEQ EINVAL
61 #endif
62
63 static const wchar_t encoding_mask[] =
64 {
65     /* This reflects the sources *nix origin where type wchar_t 
66        was 32 bits wide. Since our type wchar_t is only 16 bits
67        wide all this module will need to be reviewed.
68        Simplest option may well be to forward this modules work 
69        on to the kernel which already has support for this.
70       */
71     ~0x7ff, ~0xffff, ~0x1fffff, ~0x3ffffff
72     //~0x0000-07ff, ~0x0000-ffff, ~0x001f-ffff, ~0x03ff-ffff
73 };
74
75 static const unsigned char encoding_byte[] =
76 {
77     0xc0, 0xe0, 0xf0, 0xf8, 0xfc
78 };
79
80 /* The state is for this UTF8 encoding not used.  */
81 //static mbstate_t internal;
82 //extern mbstate_t __no_r_state;  /* Defined in mbtowc.c.  */
83
84 size_t __wcrtomb(char *s, wchar_t wc);
85
86 /* Convert WCHAR into its multibyte character representation,
87    putting this in S and returning its length.
88
89    Attention: this function should NEVER be intentionally used.
90    The interface is completely stupid.  The state is shared between
91    all conversion functions.  You should use instead the restartable
92    version `wcrtomb'.  */
93
94 int wctomb(char *s, wchar_t wchar)
95 {
96   /* If S is NULL the function has to return null or not null
97      depending on the encoding having a state depending encoding or
98      not.  This is nonsense because any multibyte encoding has a
99      state.  The ISO C amendment 1 corrects this while introducing the
100      restartable functions.  We simply say here all encodings have a
101      state.  */
102     if (s == NULL) {
103         return 1;
104     }
105     return __wcrtomb(s, wchar);
106 }
107
108 size_t __wcrtomb(char *s, wchar_t wc)
109 {
110     char fake[1];
111     size_t written = 0;
112
113     if (s == NULL) {
114         s = fake;
115         wc = L'\0';
116     }
117     /* Store the UTF8 representation of WC.  */
118     //if (wc < 0 || wc > 0x7fffffff) {
119     if (wc < 0 || wc > 0x7fff) {
120         /* This is no correct ISO 10646 character.  */
121         __set_errno (EILSEQ);
122         return (size_t) -1;
123     }
124     if (wc < 0x80) {
125         /* It's a one byte sequence.  */
126         if (s != NULL) {
127             *s = (char)wc;
128         }
129         return 1;
130     }
131     for (written = 2; written < 6; ++written) {
132         if ((wc & encoding_mask[written - 2]) == 0) {
133             break;
134         }
135     }
136     if (s != NULL) {
137         size_t cnt = written;
138         s[0] = encoding_byte[cnt - 2];
139         --cnt;
140         do {
141             s[cnt] = 0x80 | (wc & 0x3f);
142             wc >>= 6;
143         } while (--cnt > 0);
144         s[0] |= wc;
145     }
146     return written;
147 }
148
149 #endif