Sync scanf to current wine sources, correct msvcrt cpp sync date
[reactos.git] / reactos / 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 <precomp.h>
27 #include <ctype.h>
28
29 // HACK for LIBCNT
30 #ifndef debugstr_a
31 #define debugstr_a
32 #endif
33
34 //extern FILE _iob[];
35
36 /* helper function for *scanf. Returns the value of character c in the
37 * given base, or -1 if the given character is not a digit of the base.
38 */
39 static int char2digit(char c, int base) {
40 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
41 if (base<=10) return -1;
42 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
43 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
44 return -1;
45 }
46
47 /* helper function for *wscanf. Returns the value of character c in the
48 * given base, or -1 if the given character is not a digit of the base.
49 */
50 static int wchar2digit(wchar_t c, int base) {
51 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
52 if (base<=10) return -1;
53 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
54 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
55 return -1;
56 }
57
58 #ifndef _LIBCNT_
59 /* vfscanf */
60 #undef WIDE_SCANF
61 #undef CONSOLE
62 #undef STRING
63 #include "scanf.h"
64
65 /* vfwscanf */
66 #define WIDE_SCANF 1
67 #undef CONSOLE
68 #undef STRING
69 #include "scanf.h"
70 #endif
71
72 /* vsscanf */
73 #undef WIDE_SCANF
74 #undef CONSOLE
75 #define STRING 1
76 #include "scanf.h"
77
78 /* vswscanf */
79 #define WIDE_SCANF 1
80 #undef CONSOLE
81 #define STRING 1
82 #include "scanf.h"
83
84 #ifndef _LIBCNT_
85 /* vcscanf */
86 #undef WIDE_SCANF
87 #define CONSOLE 1
88 #undef STRING
89 #include "scanf.h"
90
91
92 /*********************************************************************
93 * fscanf (MSVCRT.@)
94 */
95 int fscanf(FILE *file, const char *format, ...)
96 {
97 va_list valist;
98 int res;
99
100 va_start(valist, format);
101 res = vfscanf(file, format, valist);
102 va_end(valist);
103 return res;
104 }
105
106 /*********************************************************************
107 * scanf (MSVCRT.@)
108 */
109 int scanf(const char *format, ...)
110 {
111 va_list valist;
112 int res;
113
114 va_start(valist, format);
115 res = vfscanf(stdin, format, valist);
116 va_end(valist);
117 return res;
118 }
119
120 /*********************************************************************
121 * fwscanf (MSVCRT.@)
122 */
123 int fwscanf(FILE *file, const wchar_t *format, ...)
124 {
125 va_list valist;
126 int res;
127
128 va_start(valist, format);
129 res = vfwscanf(file, format, valist);
130 va_end(valist);
131 return res;
132 }
133
134
135 /*********************************************************************
136 * wscanf (MSVCRT.@)
137 */
138 int wscanf(const wchar_t *format, ...)
139 {
140 va_list valist;
141 int res;
142
143 va_start(valist, format);
144 res = vfwscanf(stdin, format, valist);
145 va_end(valist);
146 return res;
147 }
148 #endif
149
150
151 /*********************************************************************
152 * sscanf (MSVCRT.@)
153 */
154 int sscanf(const char *str, const char *format, ...)
155 {
156 va_list valist;
157 int res;
158
159 va_start(valist, format);
160 res = vsscanf(str, format, valist);
161 va_end(valist);
162 return res;
163 }
164
165
166 /*********************************************************************
167 * swscanf (MSVCRT.@)
168 */
169 int CDECL swscanf(const wchar_t *str, const wchar_t *format, ...)
170 {
171 va_list valist;
172 int res;
173
174 va_start(valist, format);
175 res = vswscanf(str, format, valist);
176 va_end(valist);
177 return res;
178 }
179
180 #ifndef _LIBCNT_
181 /*********************************************************************
182 * _cscanf (MSVCRT.@)
183 */
184 int CDECL _cscanf(const char *format, ...)
185 {
186 va_list valist;
187 int res;
188
189 va_start(valist, format);
190 res = vcscanf(format, valist);
191 va_end(valist);
192 return res;
193 }
194 #endif