update for HEAD-2003021201
[reactos.git] / lib / msvcrt / stdio / putc.c
1 /* $Id$
2  *
3  *  ReactOS msvcrt library
4  *
5  *  putc.c
6  *
7  *  Copyright (C) 2002  Robert Dickenson <robd@reactos.org>
8  *
9  *  Based on original work Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
26
27 #include <windows.h>
28 #include <msvcrt/stdio.h>
29 #include <msvcrt/wchar.h>
30 #include <msvcrt/errno.h>
31 #include <msvcrt/internal/file.h>
32
33 // putc can be a macro
34 #undef putc
35 #undef putwc
36
37 #ifndef MB_CUR_MAX_CONST
38 #define MB_CUR_MAX_CONST 10
39 #endif
40
41 int putc(int c, FILE* fp)
42 {
43     // valid stream macro should check that fp is dword aligned
44     if (!__validfp(fp)) {
45         __set_errno(EINVAL);
46         return EOF;
47     }
48     // check for write access on fp
49     if (!OPEN4WRITING(fp)) {
50         __set_errno(EINVAL);
51         return EOF;
52     }
53     fp->_flag |= _IODIRTY;
54     if (fp->_cnt > 0) {
55         fp->_cnt--;
56         *(fp)->_ptr++ = (unsigned char)c;
57         return (int)(unsigned char)c; 
58     } else {
59         return _flsbuf((unsigned char)c, fp);
60     }
61     return EOF;
62 }
63
64 //wint_t putwc(wint_t c, FILE* fp)
65 int putwc(wint_t c, FILE* fp)
66 {
67     // valid stream macro should check that fp is dword aligned
68     if (!__validfp(fp)) {
69         __set_errno(EINVAL);
70         return WEOF;
71     }
72     // check for write access on fp
73     if (!OPEN4WRITING(fp)) {
74         __set_errno(EINVAL);
75         return WEOF;
76     }
77     // might check on multi bytes if text mode
78     if (fp->_flag & _IOBINARY) {
79     //if (1) {
80
81         fp->_flag |= _IODIRTY;
82         if (fp->_cnt > 0) {
83             fp->_cnt -= sizeof(wchar_t);
84             //*((wchar_t*)(fp->_ptr))++ = c;
85             *((wchar_t*)(fp->_ptr)) = c;
86             ++((wchar_t*)(fp->_ptr));
87             return (wint_t)c;
88         } else {
89 #if 1
90             wint_t result;
91             result = _flsbuf(c, fp);
92             if (result == EOF)
93                 return WEOF;
94             result = _flsbuf((int)(c >> 8), fp);
95             if (result == EOF)
96                 return WEOF;
97             return result;
98 #else
99             return _flswbuf(c, fp);
100 #endif
101         }
102
103     } else {
104 #if 0
105         int i;
106         int mb_cnt;
107         char mbchar[MB_CUR_MAX_CONST];
108
109         // Convert wide character to the corresponding multibyte character.
110         mb_cnt = wctomb(mbchar, (wchar_t)c);
111         if (mb_cnt == -1) {
112             fp->_flag |= _IOERR;
113             return WEOF;
114         }
115         if (mb_cnt > MB_CUR_MAX_CONST) {
116             // BARF();
117         }
118         for (i = 0; i < mb_cnt; i++) {
119             fp->_flag |= _IODIRTY;
120             if (fp->_cnt > 0) {
121                 fp->_cnt--;
122                 *(fp)->_ptr++ = (unsigned char)mbchar[i];
123             } else {
124                 if (_flsbuf((unsigned char)mbchar[i], fp) == EOF) {
125                     return WEOF;
126                 }
127             }
128         }
129 #else
130         fp->_flag |= _IODIRTY;
131         if (fp->_cnt > 0) {
132             fp->_cnt--;
133             *(fp)->_ptr++ = (unsigned char)c;
134         } else {
135             if (_flsbuf(c, fp) == EOF) {
136                 return WEOF;
137             }
138         }
139 #endif
140         return c; 
141     }
142     return WEOF;
143 }