From 0b5f84a52cf54e88b99a44efc6f5f2c46e9b294d Mon Sep 17 00:00:00 2001 From: solar Date: Fri, 25 Sep 2009 05:52:28 +0000 Subject: [PATCH] Started debugging scanf() functions. --- functions/_PDCLIB/scan.c | 20 ++++++++++++--- functions/stdio/fscanf.c | 20 ++++++++++++++- functions/stdio/vfscanf.c | 54 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 87 insertions(+), 7 deletions(-) diff --git a/functions/_PDCLIB/scan.c b/functions/_PDCLIB/scan.c index 2f59fa5..b601261 100644 --- a/functions/_PDCLIB/scan.c +++ b/functions/_PDCLIB/scan.c @@ -209,7 +209,15 @@ const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status ) *(c++) = rc; value_parsed = true; } - return value_parsed ? spec : NULL; + if ( value_parsed ) + { + ++status->n; + return ++spec; + } + else + { + return NULL; + } } case 's': { @@ -239,7 +247,8 @@ const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status ) if ( value_parsed ) { *c = '\0'; - return spec; + ++status->n; + return ++spec; } else { @@ -370,7 +379,7 @@ const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status ) puts( "UNSUPPORTED SCANF FLAG COMBINATION" ); return NULL; } - return spec; + return ++spec; } /* TODO: Floats. */ return NULL; @@ -379,10 +388,13 @@ const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status ) #ifdef TEST #include <_PDCLIB_test.h> +#include + + int main( void ) { - TESTCASE( NO_TESTDRIVER ); + /* Testing covered by fscanf.c */ return TEST_RESULTS; } diff --git a/functions/stdio/fscanf.c b/functions/stdio/fscanf.c index bc5bebf..6ea4866 100644 --- a/functions/stdio/fscanf.c +++ b/functions/stdio/fscanf.c @@ -26,9 +26,27 @@ int fscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format #ifdef TEST #include <_PDCLIB_test.h> +#include +#include + int main( void ) { - TESTCASE( NO_TESTDRIVER ); + char teststring1[] = " 1 23\045\0\067 "; + char buffer[15]; + FILE * fh; + TESTCASE( ( fh = fopen( "testfile", "w+" ) ) != NULL ); + TESTCASE( fwrite( teststring1, 15, 1, fh ) == 1 ); + rewind( fh ); + /* */ + TESTCASE( memset( buffer, CHAR_MAX, 15 ) == buffer ); \ + TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 ); \ + TESTCASE( fscanf( fh, "%14c", buffer ) == 1 ); \ + TESTCASE( memcmp( buffer, teststring1 + 0, 14 ) == 0 ); \ + TESTCASE( buffer[ 14 ] == CHAR_MAX ); \ + TESTCASE( memset( buffer, CHAR_MAX, 15 ) == buffer ); \ + TESTCASE( sscanf( teststring1 + 14, "%14c", buffer ) ); \ + TESTCASE( memcmp( buffer, teststring1 + 0, 14 ) == 0 ); \ + TESTCASE( buffer[ 14 ] == CHAR_MAX ); return TEST_RESULTS; } diff --git a/functions/stdio/vfscanf.c b/functions/stdio/vfscanf.c index bb1ebca..eda4226 100644 --- a/functions/stdio/vfscanf.c +++ b/functions/stdio/vfscanf.c @@ -8,13 +8,63 @@ #include #include +#include #ifndef REGTEST int vfscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, va_list arg ) { - /* TODO: Implement using vsscanf() reading from file buffer */ - return 0; + struct _PDCLIB_status_t status; + status.base = 0; + status.flags = 0; + status.n = 0; + status.i = 0; + status.this = 0; + status.s = NULL; + status.width = 0; + status.prec = 0; + status.stream = stream; + va_copy( status.arg, arg ); + while ( *format != '\0' ) + { + const char * rc; + if ( ( *format != '%' ) || ( ( rc = _PDCLIB_scan( format, &status ) ) == format ) ) + { + /* No conversion specifier, match verbatim */ + if ( isspace( *format ) ) + { + /* Whitespace char in format string: Skip all whitespaces */ + /* No whitespaces in input do not result in matching error */ + while ( isspace( *status.s ) ) + { + ++status.s; + ++status.i; + } + } + else + { + /* Non-whitespace char in format string: Match verbatim */ + if ( *status.s != *format ) + { + /* Matching error */ + return status.n; + } + else + { + ++status.s; + ++status.i; + } + } + ++format; + } + else + { + /* Continue parsing after conversion specifier */ + format = rc; + } + } + va_end( status.arg ); + return status.n; } #endif -- 2.40.0