forked from tusharjois/bscanf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bscanf.c
282 lines (239 loc) · 8.59 KB
/
bscanf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include "bscanf.h"
#include <stdio.h>
#define _BSCANF_CONSUME_WSPACE() while (isspace(*buf_ptr)) {buf_ptr++;}
#define _BSCANF_CHECK(x) if (!(x)) goto exit;
#define _BSCANF_MATCH() _BSCANF_CHECK(*buf_ptr == *fmt_ptr);
#define _BSCANF_CHECK_NULL(ptr) _BSCANF_CHECK(NULL != ptr);
#define _BSCANF_CHECK_STRING() _BSCANF_CHECK(0 != max_width);
#define _BSCANF_CHECK_BUFFER() _BSCANF_CHECK('\0' != *buffer);
#define _BSCANF_CHECK_STRTONUM() _BSCANF_CHECK(buf_ptr != end_ptr);
int bscanf(const char *buffer, const char *format, ...)
{
/* Our return value. On a conversion error, we return this immediately. */
int num_args_set = 0;
/* We use these to index into our buffer and format string. */
const char *buf_ptr = buffer;
const char *fmt_ptr = format;
/* Variadic arguments -- pointers in which we put our conversion results. */
va_list args;
long *long_ptr;
int *int_ptr;
short *short_ptr;
unsigned long *ulong_ptr;
unsigned short *ushort_ptr;
unsigned int *uint_ptr;
double *double_ptr;
float*float_ptr;
char *char_ptr;
wchar_t *wchar_ptr;
/* These are useful variables when doing string to number conversion. */
char* end_ptr;
int base;
/* These are flags that are used by different conversion specifiers. */
int is_suppressed = 0;
size_t max_width = 0;
char length_mod = '\0';
/* Return a special value when one of the arguments is NULL. */
if (NULL == buffer || NULL == format) {
return -1;
}
va_start(args, format);
while ('\0' != *fmt_ptr) {
/* We ignore spaces before specifiers. */
if (isspace(*fmt_ptr)) {
/* Any whitespace in the format consumes all of the whitespace in the
buffer. */
_BSCANF_CONSUME_WSPACE();
fmt_ptr++;
continue;
}
if ('%' == *fmt_ptr) {
/* Handle conversion specifier. */
fmt_ptr++;
/* Check for assignment-suppressing character. */
if ('*' == *fmt_ptr) {
is_suppressed = 1;
fmt_ptr++;
} else {
is_suppressed = 0;
}
/* Check for maximum field width. */
if (isdigit(*fmt_ptr)) {
max_width = strtoul(fmt_ptr, &end_ptr, 0);
/* Check if the sequence is a number > 0. */
_BSCANF_CHECK(fmt_ptr != end_ptr);
_BSCANF_CHECK(max_width > 0);
fmt_ptr = end_ptr;
}
/* Check for a length modifier. */
if ('h' == *fmt_ptr || 'l' == *fmt_ptr) {
length_mod = *fmt_ptr;
fmt_ptr++;
} else {
length_mod = '\0';
}
/* Handle the conversion format specifier. */
if ('n' == *fmt_ptr) {
/* 'n': number of characters read so far. */
/* 'n' conversion specifiers DO NOT consume whitespace. */
/* Technically undefined, but just stop here for safety. */
_BSCANF_CHECK(!is_suppressed);
if ('l' == length_mod) {
long_ptr = va_arg(args, long*);
_BSCANF_CHECK_NULL(long_ptr);
*long_ptr = (long) (buf_ptr - buffer);
} else if ('h' == length_mod) {
short_ptr = va_arg(args, short*);
_BSCANF_CHECK_NULL(short_ptr);
*short_ptr = (short) (buf_ptr - buffer);
} else {
int_ptr = va_arg(args, int*);
_BSCANF_CHECK_NULL(int_ptr);
*int_ptr = (int) (buf_ptr - buffer);
}
fmt_ptr++;
num_args_set++;
continue;
}
/* All other specifiers move the buffer pointer, so check that it's not
NUL. */
_BSCANF_CHECK_BUFFER();
if ('%' == *fmt_ptr) {
/* '%': match literal %. */
_BSCANF_CONSUME_WSPACE();
_BSCANF_MATCH();
buf_ptr++;
} else if ('c' == *fmt_ptr || 's' == *fmt_ptr) {
/* 'c'/'s': match a character sequence/string. */
/* String conversion requires a width. */
_BSCANF_CHECK_STRING();
/* 'c' conversion specifiers DO NOT consume whitespace. */
if ('c' != *fmt_ptr) {
_BSCANF_CONSUME_WSPACE();
}
if (is_suppressed) {
/* Consume the character (string) and ignore it in this case. */
for (; max_width > 0; max_width--) {
buf_ptr++;
if (*buf_ptr == '\0' || (isspace(*buf_ptr) && 's' == *fmt_ptr)) {
break;
}
}
fmt_ptr++;
continue;
} else if ('l' == length_mod) {
wchar_ptr = va_arg(args, wchar_t*);
_BSCANF_CHECK_NULL(char_ptr);
/* TODO: Implementation. */
_BSCANF_CHECK(0);
} else {
char_ptr = va_arg(args, char*);
_BSCANF_CHECK_NULL(char_ptr);
for (; max_width > 0; max_width--) {
*char_ptr = *buf_ptr;
if (*buf_ptr == '\0' || (isspace(*buf_ptr) && 's' == *fmt_ptr)) {
break;
}
char_ptr++;
buf_ptr++;
}
/* Strings are null-terminated. */
if ('s' == *fmt_ptr) {
*char_ptr = '\0';
}
num_args_set++;
}
} else if ('[' == *fmt_ptr) {
/* TODO: '[': match a non-empty sequence of characters from a set. */
_BSCANF_CHECK(0);
/* String conversion requires a width. */
_BSCANF_CHECK_STRING();
/* '[' conversion specifiers DO NOT consume whitespace. */
} else if ('i' == *fmt_ptr || 'd' == *fmt_ptr) {
/* 'i'/'d': match a integer/decimal integer. */
_BSCANF_CONSUME_WSPACE();
base = ('d' == *fmt_ptr) * 10;
if (is_suppressed) {
/* Consume the integer and ignore it in this case. */
strtol(buf_ptr, &end_ptr, base);
} else if ('l' == length_mod) {
long_ptr = va_arg(args, long*);
_BSCANF_CHECK_NULL(long_ptr);
*long_ptr = (long) strtol(buf_ptr, &end_ptr, base);
} else if ('h' == length_mod) {
short_ptr = va_arg(args, short*);
_BSCANF_CHECK_NULL(short_ptr);
*short_ptr = (short) (strtol(buf_ptr, &end_ptr, base));
} else {
int_ptr = va_arg(args, int*);
_BSCANF_CHECK_NULL(int_ptr);
*int_ptr = (int) (strtol(buf_ptr, &end_ptr, base));
}
_BSCANF_CHECK_STRTONUM();
buf_ptr = end_ptr;
num_args_set++;
} else if ('g' == *fmt_ptr || 'e' == *fmt_ptr || 'f' == *fmt_ptr ||
'G' == *fmt_ptr || 'E' == *fmt_ptr || 'F' == *fmt_ptr) {
/* 'g'/'e'/'f': match a float in strtod form. */
/* TODO: 'a': match a float in C99 binary floating-point form. */
_BSCANF_CONSUME_WSPACE();
if (is_suppressed) {
/* Consume the float and ignore it in this case. */
strtod(buf_ptr, &end_ptr);
} else if ('l' == length_mod) {
double_ptr = va_arg(args, double*);
_BSCANF_CHECK_NULL(double_ptr);
*double_ptr = (double) (strtod(buf_ptr, &end_ptr));
} else {
float_ptr = va_arg(args, float*);
_BSCANF_CHECK_NULL(float_ptr);
*float_ptr = (float) (strtod(buf_ptr, &end_ptr));
}
_BSCANF_CHECK_STRTONUM();
buf_ptr = end_ptr;
num_args_set++;
} else if ('u' == *fmt_ptr || 'o' == *fmt_ptr || 'x' == *fmt_ptr ||
'X' == *fmt_ptr) {
/* 'u'/'o'/'x': match a unsigned decimal/octal/hexadecimal integer */
_BSCANF_CONSUME_WSPACE();
base = ('u' == *fmt_ptr) * 10 + ('o' == *fmt_ptr) * 8 +
('x' == *fmt_ptr || 'X' == *fmt_ptr) * 16;
if (is_suppressed) {
/* Consume the unsigned integer and ignore it in this case. */
strtoul(buf_ptr, &end_ptr, base);
} else if ('l' == length_mod) {
ulong_ptr = va_arg(args, unsigned long*);
_BSCANF_CHECK_NULL(ulong_ptr);
*ulong_ptr = (unsigned long) strtoul(buf_ptr, &end_ptr, base);
} else if ('h' == length_mod) {
ushort_ptr = va_arg(args, unsigned short*);
_BSCANF_CHECK_NULL(ushort_ptr);
*ushort_ptr = (unsigned short) (strtoul(buf_ptr, &end_ptr, base));
} else {
uint_ptr = va_arg(args, unsigned int*);
_BSCANF_CHECK_NULL(uint_ptr);
*uint_ptr = (unsigned int) (strtoul(buf_ptr, &end_ptr, base));
}
_BSCANF_CHECK_STRTONUM();
buf_ptr = end_ptr;
num_args_set++;
} else {
/* Unknown conversion specifier. */
_BSCANF_CHECK(0);
}
/* TODO: 'p': match a (implementation-defined) pointer. */
} else {
/* Match character with that in buffer. */
_BSCANF_MATCH();
buf_ptr++;
}
/* Get the next format specifier. */
fmt_ptr++;
}
exit:
va_end(args);
return num_args_set;
}