stdio: more syntaxic refactoring of scanf

This commit is contained in:
Lephenixnoir 2024-01-14 21:27:48 +01:00
parent 9f6e0c8039
commit 527c2e48fc
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495

View file

@ -273,39 +273,28 @@ int __scanf(
char const * __restrict__ format,
va_list *args)
{
in->bytes_read = 0; // we haven't started to read char from the input stream
int validrets = 0; // to be incremented each time we successfully read and store an input as per the format
int err = 0; // err control on __strto_xx( ) functions
int pos = 0; // current pos in the format string
/* Number of successful assignments */
int validrets = 0;
__scanf_start( in );
// TODO: No __scanf_end() in any of the "return validrets"!!
for(; format[pos]; pos++) {
for(int pos = 0; format[pos]; pos++) {
if(format[pos] == ' ') {
__purge_space(in);
continue;
}
else if(format[pos] != '%') {
// if the next char of the stream is corresponding, we validate the read and go to the following char
if(format[pos] == __scanf_peek( in )) {
else if(format[pos] != '%' || format[pos + 1] == '%') {
/* Expect this specific character */
if(__scanf_peek(in) != format[pos])
return validrets;
__scanf_in(in);
pos++;
continue;
}
else return validrets; // else we return the number of valid read
}
else if(format[pos + 1] == '%') {
if(__scanf_peek(in) != '%') return validrets;
else __scanf_in( in );
pos++;
pos += (format[pos] == '%');
continue;
}
/* Perform a conversion */
else {
struct scanf_format opt;
int spec = parse_fmt(format, &pos, &opt);
if(spec == 0)
@ -353,10 +342,10 @@ int __scanf(
bool use_unsigned = (f == 'o' || f == 'x' || f == 'X');
long long int temp;
err = __strto_int(in, base, NULL, &temp, use_unsigned,
int err = __strto_int(in, base, NULL, &temp, use_unsigned,
opt.field_width);
if(err == EOF && validrets == 0) return EOF;
if (err != 0) return validrets;
if(err) return validrets;
if(!opt.skip)
__scanf_store_i(temp, opt.size, args);
validrets++;
@ -374,10 +363,9 @@ int __scanf(
// read a double from the current input stream
// and store in the corresponding arg as a char by reference
long double temp;
err = __strto_fp( in, NULL, NULL, &temp,
opt.field_width);
int err = __strto_fp(in, NULL, NULL, &temp, opt.field_width);
if(err == EOF && validrets == 0) return EOF;
if (err != 0) return validrets;
if(err) return validrets;
if(!opt.skip)
__scanf_store_d(temp, opt.size, args);
validrets++;
@ -386,15 +374,14 @@ int __scanf(
case 'p': {
long int temp;
int err = 0;
if(!opt.skip) {
void *p = (void *) va_arg( *args, void** ); // get the adress of the target pointer (void**)
err = __strto_int( in, 0, p, NULL, true,
opt.field_width);
void *p = va_arg(*args, void *);
err = __strto_int(in, 0, p, NULL, true, opt.field_width);
validrets += (err == 0);
}
else err = __strto_int( in, 0, &temp, NULL, true,
opt.field_width);
if (err == 0) validrets++;
else return validrets;
else err = __strto_int(in, 0, &temp, NULL, true, opt.field_width);
if(err) return validrets;
break;
}
@ -437,7 +424,6 @@ int __scanf(
}
}
}
}
__scanf_end( in );
return validrets;