Improved unicode fileio support.
[reactos.git] / reactos / lib / msvcrt / stdio / getc.c
1 /* $Id: getc.c,v 1.6 2002/12/05 15:30:44 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 }
84 } else {
85 #if 0
86 BOOL get_bytes = 0;
87 int mb_cnt = 0;
88 int found_cr = 0;
89 //int count;
90 char mbchar[MB_CUR_MAX];
91
92 do {
93 if (fp->_cnt > 0) {
94 fp->_cnt--;
95 mbchar[mb_cnt] = *fp->_ptr++;
96 } else {
97 mbchar[mb_cnt] = _filbuf(fp);
98 }
99 if (isleadbyte(mbchar[mb_cnt])) {
100 get_bytes = 1;
101 } else {
102 get_bytes = 0;
103 }
104 if (_ismbblead(mbchar[mb_cnt])) {
105 }
106 ++mb_cnt;
107 //}
108 } while (get_bytes);
109
110 // Convert a multibyte character to a corresponding wide character.
111 mb_cnt = mbtowc(&c, mbchar, mb_cnt);
112 if (mb_cnt == -1) {
113 fp->_flag |= _IOERR;
114 return WEOF;
115 }
116 #else
117 if (fp->_cnt > 0) {
118 fp->_cnt--;
119 c = *fp->_ptr++;
120 } else {
121 c = _filbuf(fp);
122 }
123 #endif
124 }
125 return c;
126 }
127