086a148f15b13e804eb6dd4e5fe38e56f99fc08a
[reactos.git] / reactos / lib / msvcrt / stdio / putc.c
1 /* $Id: putc.c,v 1.5 2002/11/24 18:42:24 robd Exp $
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 int i;
68 int mb_cnt;
69 char mbchar[MB_CUR_MAX_CONST];
70
71 // valid stream macro should check that fp is dword aligned
72 if (!__validfp(fp)) {
73 __set_errno(EINVAL);
74 return WEOF;
75 }
76 // check for write access on fp
77 if (!OPEN4WRITING(fp)) {
78 __set_errno(EINVAL);
79 return WEOF;
80 }
81 // might check on multi bytes if text mode
82 if (fp->_flag & _IOBINARY) {
83 //if (1) {
84
85 fp->_flag |= _IODIRTY;
86 if (fp->_cnt > 0 ) {
87 fp->_cnt -= sizeof(wchar_t);
88 //*((wchar_t*)(fp->_ptr))++ = c;
89 *((wchar_t*)(fp->_ptr)) = c;
90 ++((wchar_t*)(fp->_ptr));
91 return (wint_t)c;
92 } else {
93 return _flswbuf(c, fp);
94 }
95
96 } else {
97 // Convert wide character to the corresponding multibyte character.
98 mb_cnt = wctomb(mbchar, (wchar_t)c);
99 if (mb_cnt == -1) {
100 fp->_flag |= _IOERR;
101 return WEOF;
102 }
103 if (mb_cnt > MB_CUR_MAX_CONST) {
104 // BARF();
105 }
106 for (i = 0; i < mb_cnt; i++) {
107 fp->_flag |= _IODIRTY;
108 #if 0
109 // convert lf's into a cr/lf pair.
110 if (mbchar[i] == '\n') {
111 if (fp->_cnt > 0) {
112 fp->_cnt--;
113 *(fp)->_ptr++ = (unsigned char)'\r';
114 } else {
115 if (_flsbuf((unsigned char)'\r', fp) == EOF) {
116 return WEOF;
117 }
118 }
119 }
120 #endif
121 if (fp->_cnt > 0) {
122 fp->_cnt--;
123 *(fp)->_ptr++ = (unsigned char)mbchar[i];
124 } else {
125 if (_flsbuf((unsigned char)mbchar[i], fp) == EOF) {
126 return WEOF;
127 }
128 }
129 }
130 return c;
131 }
132 return WEOF;
133 }