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