* Christmas edition *; fixed irc /os command; small cleanup in fd.c; improvements...
[ZeXOS.git] / libc / stdio / doscanf.c
blob5e4ab7d7c83e3e432c626bc4ae75f1f25d6af1dc
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <_printf.h> /* fnptr_t */
21 #include <string.h> /* strlen() */
22 #include <stdarg.h> /* va_list, va_arg() */
23 #include <stdio.h>
24 #include <stdlib.h>
26 static int scanf_getint (int *n)
28 char *str = (char *) malloc (sizeof (char) * 128);
30 if (!str)
31 return 0;
33 unsigned i = 0;
35 while (1) {
36 schedule ();
38 char c = getch ();
40 if (!c)
41 continue;
43 if (c == '\n' || i > 127)
44 break;
46 putch (c);
48 if (c == ' ' && i)
49 break;
51 str[i] = c;
53 if (c == '\b' && i)
54 i --;
55 else
56 i ++;
59 if (!i)
60 return 0;
62 str[i] = '\0';
64 i = 0;
66 *n = atoi (str);
68 free (str);
70 return 1;
73 /* TODO: implement real floatint point format */
74 static int scanf_getfloat (float *n)
76 char *str = (char *) malloc (sizeof (char) * 128);
78 if (!str)
79 return 0;
81 unsigned i = 0;
83 while (1) {
84 schedule ();
86 char c = getch ();
88 if (!c)
89 continue;
91 if (c == '\n' || i > 127)
92 break;
94 putch (c);
96 if (c == ' ' && i)
97 break;
99 str[i] = c;
101 if (c == '\b' && i)
102 i --;
103 else
104 i ++;
107 if (!i)
108 return 0;
110 str[i] = '\0';
112 i = 0;
114 *n = (float) atoi (str);
116 free (str);
118 return 1;
121 static int scanf_gethex (int *n)
123 char *str = (char *) malloc (sizeof (char) * 128);
125 if (!str)
126 return 0;
128 unsigned i = 0;
130 while (1) {
131 schedule ();
133 char c = getch ();
135 if (!c)
136 continue;
138 if (c == '\n' || i > 127)
139 break;
141 putch (c);
143 if (c == ' ' && i)
144 break;
146 str[i] = c;
148 if (c == '\b' && i)
149 i --;
150 else
151 i ++;
154 if (!i)
155 return 0;
157 str[i] = '\0';
159 i = 0;
161 char *endptr;
163 *n = strtol (str, &endptr, 16);
165 free (str);
167 return 1;
170 static int scanf_getstring (unsigned char *n)
172 unsigned i = 0;
174 while (1) {
175 schedule ();
177 char c = getch ();
179 if (!c)
180 continue;
182 if (c == '\n')
183 break;
185 putch (c);
187 if (c == ' ' && i)
188 break;
190 n[i] = c;
192 if (c == '\b' && i)
193 i --;
194 else
195 i ++;
198 if (!i)
199 return 0;
201 n[i] = '\0';
203 return 1;
206 static int scanf_getchar (unsigned char *n)
208 unsigned i = 0;
210 while (1) {
211 schedule ();
213 char c = getch ();
215 if (!c)
216 continue;
218 if (c == '\n')
219 break;
221 putch (c);
223 if (c == ' ' && i)
224 break;
226 if (i == 0)
227 n[i] = c;
229 if (c == '\b' && i)
230 i --;
231 else
232 i ++;
235 if (!i)
236 return 0;
238 return 1;
241 int do_scanf (const char *fmt, va_list args)
243 unsigned count = 0;
244 int *num = 0;
245 float *fp = 0;
246 unsigned char *buf = 0;
248 for (; *fmt; fmt ++) {
249 /* probably argument is here */
250 if (*fmt == '%') {
251 fmt ++;
253 if (!*fmt)
254 break;
256 if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
257 num = va_arg (args, int *);
259 if (scanf_getint (num))
260 count ++;
262 continue;
265 if (*fmt == 'x' || *fmt == 'X') {
266 num = va_arg (args, int *);
268 if (scanf_gethex (num))
269 count ++;
271 continue;
274 if (*fmt == 'c') {
275 buf = va_arg (args, unsigned char *);
277 if (scanf_getchar (buf))
278 count ++;
280 continue;
283 if (*fmt == 'f') {
284 fp = va_arg (args, float *);
286 if (scanf_getfloat (fp))
287 count ++;
289 continue;
292 if (*fmt == 's') {
293 buf = va_arg (args, unsigned char *);
295 if (scanf_getstring (buf))
296 count ++;
298 continue;
301 continue;
304 count ++;
308 return count;