Fixed ZDE build - missing header file
[ZeXOS.git] / libc / stdio / dosscanf.c
blobb8ebb70eb105727ba827a10703ce046fd8d2959d
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
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>
25 #include <errno.h>
27 static int scanf_getint (const char *str, int *n)
29 *n = atoi (str);
31 return 1;
34 /* TODO: implement real floatint point format */
35 static int scanf_getfloat (const char *str, float *n)
37 *n = (float) atoi (str);
39 return 1;
42 static int scanf_gethex (const char *str, int *n)
44 char *endptr;
46 *n = strtol (str, &endptr, 16);
48 return 1;
51 static int scanf_getstring (const char *str, unsigned char *n)
53 unsigned i = strlen (str);
55 memcpy (n, str, i);
56 n[i] = '\0';
58 return 1;
61 static int scanf_getchar (const char *str, unsigned char *n)
63 *n = *str;
65 return 1;
68 int vsscanf (const char *str, const char *fmt, va_list args)
70 unsigned count = 0;
71 int *num = 0;
72 float *fp = 0;
73 unsigned char *buf = 0;
74 unsigned i = 0;
76 unsigned s_len = strlen (str);
77 char *s = (char *) malloc (sizeof (char) * s_len);
79 if (!s) {
80 errno = ENOMEM;
81 return 0;
84 for (count = 0; count < s_len; count ++) {
85 if (str[count] != ' ')
86 s[count] = str[count];
87 else
88 s[count] = '\0';
91 count = 0;
93 for (; *fmt; fmt ++) {
94 /* probably argument is here */
95 if (*fmt == '%') {
96 fmt ++;
98 if (!*fmt)
99 break;
101 if (*fmt == 'd' || *fmt == 'i' || *fmt == 'u') {
102 num = va_arg (args, int *);
104 if (scanf_getint (s, num)) {
105 count ++;
106 i += strlen (s+i)+1;
109 continue;
112 if (*fmt == 'x' || *fmt == 'X') {
113 num = va_arg (args, int *);
115 if (scanf_gethex (s+i, num)){
116 count ++;
117 i += strlen (s+i)+1;
120 continue;
123 if (*fmt == 'c') {
124 buf = va_arg (args, unsigned char *);
126 if (scanf_getchar (s+i, buf)) {
127 count ++;
128 i += strlen (s+i)+1;
131 continue;
134 if (*fmt == 'f') {
135 fp = va_arg (args, float *);
137 if (scanf_getfloat (s+i, fp)) {
138 count ++;
139 i += strlen (s+i)+1;
142 continue;
145 if (*fmt == 's') {
146 buf = va_arg (args, unsigned char *);
148 if (scanf_getstring (s+i, buf)) {
149 count ++;
150 i += strlen (s+i)+1;
153 continue;
156 continue;
160 free (s);
162 return count;