mirror of
https://git.planet-casio.com/Vhex-Kernel-Core/fxlibc.git
synced 2024-12-28 20:43:38 +01:00
stdio: deduplicate scanf cases and remove most gotos
This commit is contained in:
parent
b11c059c0f
commit
69eadb67d2
1 changed files with 65 additions and 192 deletions
|
@ -161,9 +161,9 @@ void __define_set_range( char c1, char c2, bool value )
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return true if the char is in the allowed set or false otherwise */
|
/* return true if the char is in the allowed set or false otherwise */
|
||||||
bool __is_allowed( const unsigned char c )
|
bool __is_allowed(int c)
|
||||||
{
|
{
|
||||||
return __asciiallowed[ c ];
|
return (c != EOF) && __asciiallowed[c];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -200,37 +200,34 @@ int __scanset(char const * __restrict__ format, int *pos )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// start of format analysis loop
|
while(1) {
|
||||||
loopset:
|
// we find a '-' so need to check if we are considering a range or the char '-' only
|
||||||
|
if (format[*pos]=='-') {
|
||||||
// we find a '-' so need to check if we are considering a range or the char '-' only
|
// the char '-' is included in the allowed set
|
||||||
if (format[*pos]=='-') {
|
if (format[*pos+1]==']') {
|
||||||
// the char '-' is included in the allowed set
|
__asciiallowed[ '-' ] = !__neg; // if set in very final position before']', this is the char '-' only
|
||||||
if (format[*pos+1]==']') {
|
(*pos)++;
|
||||||
__asciiallowed[ '-' ] = !__neg; // if set in very final position before']', this is the char '-' only
|
// we have now finished the reading of the set cause the following char is ']'
|
||||||
(*pos)++;
|
return 0;
|
||||||
// we have now finished the reading of the set cause the following char is ']'
|
}
|
||||||
return 0;
|
// the char '-' indicates a range of char to be included into the set
|
||||||
|
else {
|
||||||
|
(*pos)++;
|
||||||
|
__eor = format[*pos];
|
||||||
|
__define_set_range( __sor, __eor, !__neg );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// the char '-' indicates a range of char to be included into the set
|
// we find the char ']' so it means we reach the end of this set
|
||||||
|
else if (format[*pos]==']') return 0;
|
||||||
|
// if we reach the '\0' we have a syntax problem
|
||||||
|
else if (format[*pos]=='\0') return -1;
|
||||||
|
// we are considering one particular char and prepare for a potential range if we find the char '-' later on
|
||||||
else {
|
else {
|
||||||
|
__sor = format[*pos];
|
||||||
|
__asciiallowed[ __sor ] = !__neg;
|
||||||
(*pos)++;
|
(*pos)++;
|
||||||
__eor = format[*pos];
|
|
||||||
__define_set_range( __sor, __eor, !__neg );
|
|
||||||
goto loopset;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// we find the char ']' so it means we reach the end of this set
|
|
||||||
else if (format[*pos]==']') return 0;
|
|
||||||
// if we reach the '\0' we have a syntax problem
|
|
||||||
else if (format[*pos]=='\0') return -1;
|
|
||||||
// we are considering one particular char and prepare for a potential range if we find the char '-' later on
|
|
||||||
else {
|
|
||||||
__sor = format[*pos];
|
|
||||||
__asciiallowed[ __sor ] = !__neg;
|
|
||||||
(*pos)++;
|
|
||||||
goto loopset;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -255,6 +252,8 @@ int __scanf(
|
||||||
|
|
||||||
__scanf_start( in );
|
__scanf_start( in );
|
||||||
|
|
||||||
|
// TODO: No __scanf_end() in any of the "return validrets"!!
|
||||||
|
|
||||||
while( format[pos] != 0 ) {
|
while( format[pos] != 0 ) {
|
||||||
in->readmaxlength = -1;
|
in->readmaxlength = -1;
|
||||||
user_length = 0;
|
user_length = 0;
|
||||||
|
@ -278,83 +277,26 @@ int __scanf(
|
||||||
switch( format[pos] ) {
|
switch( format[pos] ) {
|
||||||
// we need to decrypt the corresponding scanf set of character
|
// we need to decrypt the corresponding scanf set of character
|
||||||
case '[': {
|
case '[': {
|
||||||
char temp;
|
|
||||||
err = __scanset( format, &pos );
|
err = __scanset( format, &pos );
|
||||||
if (err!=0) return validrets;
|
if (err!=0) return validrets;
|
||||||
int currentlength = 0;
|
int currentlength = 0;
|
||||||
// we need to assign the read char to the corresponding pointer
|
// we need to assign the read char to the corresponding pointer
|
||||||
if (!skip) {
|
char *c = skip ? NULL : va_arg(*args, char *);
|
||||||
char *c = (char *) va_arg( *args, char* );
|
for(unsigned int u=0; u<in->readmaxlength; u++) {
|
||||||
if (in->readmaxlength==INT_MAX) {
|
int temp = __scanf_peek(in);
|
||||||
for(;;) {
|
if(__is_allowed(temp)) {
|
||||||
temp = __scanf_peek( in );
|
__scanf_in(in);
|
||||||
if (temp==EOF) return EOF;
|
if(c) *c++ = temp;
|
||||||
else if (__is_allowed( temp )) {
|
currentlength++;
|
||||||
*c++ = __scanf_in( in );
|
|
||||||
currentlength++;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (currentlength>0) {
|
|
||||||
*c = '\0';
|
|
||||||
validrets++;
|
|
||||||
goto exitset;
|
|
||||||
}
|
|
||||||
else return validrets;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for(unsigned int u=0; u<in->readmaxlength; u++) {
|
|
||||||
temp = __scanf_peek( in );
|
|
||||||
if (temp==EOF) return EOF;
|
|
||||||
else if (__is_allowed( temp )) {
|
|
||||||
*c++ = __scanf_in( in );
|
|
||||||
currentlength++;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (currentlength>0) {
|
|
||||||
*c = '\0';
|
|
||||||
validrets++;
|
|
||||||
goto exitset;
|
|
||||||
}
|
|
||||||
else return validrets;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (currentlength>0) {
|
|
||||||
*c = '\0';
|
|
||||||
validrets++;
|
|
||||||
goto exitset;
|
|
||||||
}
|
|
||||||
else return validrets;
|
|
||||||
}
|
}
|
||||||
|
else if(temp==EOF && !currentlength && !validrets)
|
||||||
|
return EOF;
|
||||||
|
else break;
|
||||||
}
|
}
|
||||||
|
if(!currentlength)
|
||||||
else
|
return validrets;
|
||||||
{
|
*c = '\0';
|
||||||
if (in->readmaxlength==INT_MAX) {
|
validrets += !skip;
|
||||||
for(;;) {
|
|
||||||
temp = __scanf_peek( in );
|
|
||||||
if (temp==EOF) return EOF;
|
|
||||||
else if (__is_allowed( temp )) {
|
|
||||||
__scanf_in( in );
|
|
||||||
currentlength++;
|
|
||||||
}
|
|
||||||
else if (currentlength>0) goto exitset;
|
|
||||||
else return validrets;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for(unsigned int u=0; u<in->readmaxlength; u++) {
|
|
||||||
temp = __scanf_peek( in );
|
|
||||||
if (temp==EOF) return EOF;
|
|
||||||
else if (__is_allowed( temp )) __scanf_in( in );
|
|
||||||
else if (currentlength>0) goto exitset;
|
|
||||||
else return validrets;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exitset:
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -499,111 +441,42 @@ int __scanf(
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'c': {
|
case 'c': {
|
||||||
int temp;
|
if(in->readmaxlength == INT_MAX)
|
||||||
if (!skip) {
|
in->readmaxlength = 1;
|
||||||
char *c = (char *) va_arg( *args, char* );
|
char *c = skip ? NULL : va_arg(*args, char *);
|
||||||
if (in->readmaxlength==INT_MAX) {
|
|
||||||
temp = __scanf_peek( in );
|
|
||||||
if (temp==EOF) return EOF;
|
|
||||||
else *c = __scanf_in( in );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for( unsigned int u = 0; u < in->readmaxlength; u++ ) {
|
|
||||||
temp = __scanf_peek( in );
|
|
||||||
if (temp==EOF) return EOF;
|
|
||||||
else *c++ = __scanf_in( in );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (in->readmaxlength==INT_MAX) {
|
|
||||||
temp = __scanf_peek( in );
|
|
||||||
if (temp==EOF) return EOF;
|
|
||||||
else {
|
|
||||||
__scanf_in( in );
|
|
||||||
goto endcharskip;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for( unsigned int u = 0; u < in->readmaxlength; u++ ) {
|
|
||||||
temp = __scanf_peek( in );
|
|
||||||
if (temp==EOF) return EOF;
|
|
||||||
else __scanf_in( in );
|
|
||||||
}
|
|
||||||
goto endcharskip;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
validrets++;
|
for(unsigned int u = 0; u < in->readmaxlength; u++) {
|
||||||
|
int temp = __scanf_in(in);
|
||||||
endcharskip:
|
if(temp==EOF) return EOF;
|
||||||
|
else if(c) *c++ = temp;
|
||||||
|
}
|
||||||
|
validrets += !skip;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 's': {
|
case 's': {
|
||||||
char temp;
|
char temp;
|
||||||
int curstrlength = 0;
|
int curstrlength = 0;
|
||||||
__purge_space( in );
|
__purge_space(in);
|
||||||
if (!skip) {
|
|
||||||
char *c = (char *) va_arg( *args, char* );
|
char *c = skip ? NULL : va_arg(*args, char *);
|
||||||
if (in->readmaxlength==INT_MAX) {
|
for(unsigned int u = 0; u < in->readmaxlength; u++) {
|
||||||
loopstring:
|
temp = __scanf_peek(in);
|
||||||
temp = __scanf_peek( in );
|
if(temp==EOF && curstrlength==0) return validrets;
|
||||||
if (temp==EOF && curstrlength==0) return validrets;
|
if(isspace(temp) || ((temp==EOF && curstrlength!=0))) {
|
||||||
if (isspace(temp) || ( (temp==EOF && curstrlength!=0) )) {
|
if(c) {
|
||||||
*c = 0;
|
*c = 0;
|
||||||
goto loopstringend;
|
validrets++;
|
||||||
}
|
|
||||||
else {
|
|
||||||
*c++ = __scanf_in( in );
|
|
||||||
curstrlength++;
|
|
||||||
goto loopstring;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for( unsigned int u = 0; u < in->readmaxlength; u++ ) {
|
int temp = __scanf_in( in );
|
||||||
temp = __scanf_peek( in );
|
if(c)
|
||||||
if (temp==EOF && curstrlength==0) return validrets;
|
*c++ = temp;
|
||||||
if (isspace(temp) || ( (temp==EOF && curstrlength!=0) )) {
|
curstrlength++;
|
||||||
*c = 0;
|
|
||||||
goto loopstringend;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
*c++ = __scanf_in( in );
|
|
||||||
curstrlength++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
if (in->readmaxlength==INT_MAX) {
|
|
||||||
loopstringskip:
|
|
||||||
temp = __scanf_peek( in );
|
|
||||||
if (temp==EOF && curstrlength==0) return validrets;
|
|
||||||
if (isspace(temp) || ( (temp==EOF && curstrlength!=0) )) goto loopstringendskip;
|
|
||||||
else {
|
|
||||||
__scanf_in( in );
|
|
||||||
curstrlength++;
|
|
||||||
goto loopstringskip;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for( unsigned int u = 0; u < in->readmaxlength; u++ ) {
|
|
||||||
temp = __scanf_peek( in );
|
|
||||||
if (temp==EOF && curstrlength==0) return validrets;
|
|
||||||
if (isspace(temp) || ( (temp==EOF && curstrlength!=0) )) goto loopstringendskip;
|
|
||||||
else {
|
|
||||||
__scanf_in( in );
|
|
||||||
curstrlength++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loopstringend:
|
|
||||||
validrets++;
|
|
||||||
|
|
||||||
loopstringendskip:
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue