- Cleanup the /lib directory, by putting more 3rd-party libs in /3rdparty, and by...
[reactos.git] / reactos / lib / sdk / crt / stdio / getc.c
1 /* $Id$
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 <precomp.h>
25
26 //getc can be a macro
27 #undef getc
28 #undef getwc
29
30 #ifndef MB_CUR_MAX
31 #define MB_CUR_MAX 10
32 #endif
33
34 int getc(FILE *fp)
35 {
36 int c = -1;
37
38 // check for invalid stream
39 if ( !__validfp (fp) ) {
40 __set_errno(EINVAL);
41 return EOF;
42 }
43 // check for read access on stream
44 if ( !OPEN4READING(fp) ) {
45 __set_errno(EINVAL);
46 return EOF;
47 }
48 if(fp->_cnt > 0) {
49 fp->_cnt--;
50 c = (int)(*fp->_ptr++ & 0377);
51 } else {
52 c = _filbuf(fp);
53 }
54 return c;
55 }
56
57 /*
58 * @implemented
59 */
60 wint_t getwc(FILE *fp)
61 {
62 wint_t c = -1;
63
64 // check for invalid stream
65 if (!__validfp(fp)) {
66 __set_errno(EINVAL);
67 return WEOF;
68 }
69 // check for read access on stream
70 //#define OPEN4READING(f) ((((f)->_flag & _IOREAD) == _IOREAD ) )
71 if (!OPEN4READING(fp)) {
72 __set_errno(EINVAL);
73 return WEOF;
74 }
75 // might check on multi bytes if text mode
76 if (fp->_flag & _IOBINARY) {
77 if (fp->_cnt > 1) {
78 fp->_cnt -= sizeof(wchar_t);
79 c = *((wchar_t*)fp->_ptr);
80 fp->_ptr += sizeof(wchar_t);
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++ & 0377;
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++ &0377;
123 } else {
124 c = _filbuf(fp);
125 }
126 #endif
127 }
128 return c;
129 }
130