]> pd.if.org Git - pdclib/commitdiff
Started debugging scanf() functions.
authorsolar <unknown>
Fri, 25 Sep 2009 05:52:28 +0000 (05:52 +0000)
committersolar <unknown>
Fri, 25 Sep 2009 05:52:28 +0000 (05:52 +0000)
functions/_PDCLIB/scan.c
functions/stdio/fscanf.c
functions/stdio/vfscanf.c

index 2f59fa59cc405d49979da8123b7196caaf4de38f..b6012611b707b290158d4c3585de5a271149790b 100644 (file)
@@ -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 <limits.h>
+
 
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    /* Testing covered by fscanf.c */
     return TEST_RESULTS;
 }
 
index bc5bebfe523e4192278a6667e814a87c8f37c54f..6ea4866452bebfa70c0b7c8aa13768257edbfbc5 100644 (file)
@@ -26,9 +26,27 @@ int fscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format
 #ifdef TEST
 #include <_PDCLIB_test.h>
 
+#include <limits.h>
+#include <string.h>
+
 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;
 }
 
index bb1ebcaaa450a946a9527385fb9f199a60b0a312..eda4226988d9c2b6222f624a613563331790accd 100644 (file)
@@ -8,13 +8,63 @@
 
 #include <stdio.h>
 #include <stdarg.h>
+#include <ctype.h>
 
 #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