Sync with trunk r63502.
[reactos.git] / lib / sdk / crt / string / scanf.c
1 /*
2 * general implementation of scanf used by scanf, sscanf, fscanf,
3 * _cscanf, wscanf, swscanf and fwscanf
4 *
5 * Copyright 1996,1998 Marcus Meissner
6 * Copyright 1996 Jukka Iivonen
7 * Copyright 1997,2000 Uwe Bonnes
8 * Copyright 2000 Jon Griffiths
9 * Copyright 2002 Daniel Gudbjartsson
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26 #include <stdarg.h>
27 #include <limits.h>
28
29 #include <winternl.h>
30 #include <wine/debug.h>
31
32 #include "winesup.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35
36 //extern FILE _iob[];
37
38 /* helper function for *scanf. Returns the value of character c in the
39 * given base, or -1 if the given character is not a digit of the base.
40 */
41 static int char2digit(char c, int base) {
42 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
43 if (base<=10) return -1;
44 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
45 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
46 return -1;
47 }
48
49 /* helper function for *wscanf. Returns the value of character c in the
50 * given base, or -1 if the given character is not a digit of the base.
51 */
52 static int wchar2digit(wchar_t c, int base) {
53 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
54 if (base<=10) return -1;
55 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
56 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
57 return -1;
58 }
59
60 #ifndef _LIBCNT_
61 /* vfscanf_l */
62 #undef WIDE_SCANF
63 #undef CONSOLE
64 #undef STRING
65 #undef SECURE
66 #include "scanf.h"
67
68 /* vfwscanf_l */
69 #define WIDE_SCANF 1
70 #undef CONSOLE
71 #undef STRING
72 #undef SECURE
73 #include "scanf.h"
74 #endif /* !_LIBCNT_ */
75
76 /* vsscanf_l */
77 #undef WIDE_SCANF
78 #undef CONSOLE
79 #define STRING 1
80 #undef SECURE
81 #include "scanf.h"
82
83 /* vswscanf_l */
84 #define WIDE_SCANF 1
85 #undef CONSOLE
86 #define STRING 1
87 #undef SECURE
88 #include "scanf.h"
89
90 #ifndef _LIBCNT_
91 /* vcscanf_l */
92 #undef WIDE_SCANF
93 #define CONSOLE 1
94 #undef STRING
95 #undef SECURE
96 #include "scanf.h"
97
98
99 /*********************************************************************
100 * fscanf (MSVCRT.@)
101 */
102 int CDECL fscanf(FILE *file, const char *format, ...)
103 {
104 __ms_va_list valist;
105 int res;
106
107 __ms_va_start(valist, format);
108 res = vfscanf_l(file, format, NULL, valist);
109 __ms_va_end(valist);
110 return res;
111 }
112
113 /*********************************************************************
114 * scanf (MSVCRT.@)
115 */
116 int CDECL scanf(const char *format, ...)
117 {
118 __ms_va_list valist;
119 int res;
120
121 __ms_va_start(valist, format);
122 res = vfscanf_l(stdin, format, NULL, valist);
123 __ms_va_end(valist);
124 return res;
125 }
126
127 /*********************************************************************
128 * fwscanf (MSVCRT.@)
129 */
130 int CDECL fwscanf(FILE *file, const wchar_t *format, ...)
131 {
132 __ms_va_list valist;
133 int res;
134
135 __ms_va_start(valist, format);
136 res = vfwscanf_l(file, format, NULL, valist);
137 __ms_va_end(valist);
138 return res;
139 }
140
141
142 /*********************************************************************
143 * wscanf (MSVCRT.@)
144 */
145 int CDECL wscanf(const wchar_t *format, ...)
146 {
147 __ms_va_list valist;
148 int res;
149
150 __ms_va_start(valist, format);
151 res = vfwscanf_l(stdin, format, NULL, valist);
152 __ms_va_end(valist);
153 return res;
154 }
155 #endif /* !_LIBCNT_ */
156
157
158 /*********************************************************************
159 * sscanf (MSVCRT.@)
160 */
161 int CDECL sscanf(const char *str, const char *format, ...)
162 {
163 __ms_va_list valist;
164 int res;
165
166 __ms_va_start(valist, format);
167 res = vsscanf_l(str, format, NULL, valist);
168 __ms_va_end(valist);
169 return res;
170 }
171
172
173 /*********************************************************************
174 * swscanf (MSVCRT.@)
175 */
176 int CDECL swscanf(const wchar_t *str, const wchar_t *format, ...)
177 {
178 __ms_va_list valist;
179 int res;
180
181 __ms_va_start(valist, format);
182 res = vswscanf_l(str, format, NULL, valist);
183 __ms_va_end(valist);
184 return res;
185 }
186
187 #ifndef _LIBCNT_
188 /*********************************************************************
189 * _cscanf (MSVCRT.@)
190 */
191 int CDECL _cscanf(const char *format, ...)
192 {
193 __ms_va_list valist;
194 int res;
195
196 __ms_va_start(valist, format);
197 res = vcscanf_l(format, NULL, valist);
198 __ms_va_end(valist);
199 return res;
200 }
201 #endif