fced061c36ad372342f4bb6f378afb706337528f
[reactos.git] / reactos / lib / msvcrt / stdio / getc.c
1 /* $Id: getc.c,v 1.5 2002/11/24 18:42:24 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 // check for invalid stream
42
43 if ( !__validfp (fp) ) {
44 __set_errno(EINVAL);
45 return EOF;
46 }
47 // check for read access on stream
48
49 if ( !OPEN4READING(fp) ) {
50 __set_errno(EINVAL);
51 return EOF;
52 }
53
54 if(fp->_cnt > 0) {
55 fp->_cnt--;
56 c = (int)*fp->_ptr++;
57 } else {
58 c = _filbuf(fp);
59 }
60 return c;
61 }
62
63 wint_t getwc(FILE *fp)
64 {
65 wint_t c = -1;
66
67 // check for invalid stream
68 if (!__validfp(fp)) {
69 __set_errno(EINVAL);
70 return WEOF;
71 }
72
73 // check for read access on stream
74 //#define OPEN4READING(f) ((((f)->_flag & _IOREAD) == _IOREAD ) )
75 if (!OPEN4READING(fp)) {
76 __set_errno(EINVAL);
77 return WEOF;
78 }
79
80 // might check on multi bytes if text mode
81 if (fp->_flag & _IOBINARY) {
82
83 if (fp->_cnt > 0) {
84 fp->_cnt -= sizeof(wchar_t);
85 c = (wint_t)*((wchar_t*)(fp->_ptr))++;
86 } else {
87 c = _filwbuf(fp);
88 }
89
90 } else {
91 BOOL get_bytes = 0;
92 int mb_cnt = 0;
93 int found_cr = 0;
94 //int count;
95 char mbchar[MB_CUR_MAX];
96
97 do {
98
99 if (fp->_cnt > 0) {
100 fp->_cnt--;
101 mbchar[mb_cnt] = *fp->_ptr++;
102 } else {
103 mbchar[mb_cnt] = _filbuf(fp);
104 }
105 /*
106 // convert cr/lf pairs into a single lf.
107 if (mbchar[mb_cnt] == '\r') {
108 //found_cr = 1;
109 if (fp->_cnt > 0) {
110 fp->_cnt--;
111 mbchar[mb_cnt+1] = *fp->_ptr++;
112 } else {
113 mbchar[mb_cnt+1] = _filbuf(fp);
114 }
115 if (mbchar[mb_cnt+1] == '\n') {
116 mbchar[mb_cnt] = '\n';
117 } else {
118 ++mb_cnt;
119 }
120
121 }
122 */
123
124 if (isleadbyte(mbchar[mb_cnt])) {
125 get_bytes = 1;
126 } else {
127 get_bytes = 0;
128 }
129
130 if (_ismbblead(mbchar[mb_cnt])) {
131 }
132
133 ++mb_cnt;
134 //}
135 } while (get_bytes);
136
137 // Convert a multibyte character to a corresponding wide character.
138 mb_cnt = mbtowc(&c, mbchar, mb_cnt);
139 if (mb_cnt == -1) {
140 fp->_flag |= _IOERR;
141 return WEOF;
142 }
143 }
144 return c;
145 }
146
147 // MultiByteToWideChar
148 // WideCharToMultiByte