]> pd.if.org Git - pdclib/blobdiff - functions/_PDCLIB/scan.c
Handling end-of-string and unspecified width.
[pdclib] / functions / _PDCLIB / scan.c
index c9826109cd56e9fe6af0fa0cd3e5b86564e4a74c..f41079bd78ee98908e98642e14011ff3ac21718a 100644 (file)
@@ -47,7 +47,7 @@ static int GET( struct _PDCLIB_status_t * status )
     }
     else
     {
-        return *((status->s)++);
+        return ( *status->s == '\0' ) ? EOF : *((status->s)++);
     }
 }
 
@@ -106,7 +106,12 @@ const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status )
        strtol() will return zero. In both cases, endptr will point to the
        rest of the conversion specifier - just what we need.
     */
+    char const * prev_spec = spec;
     status->width = (int)strtol( spec, (char**)&spec, 10 );
+    if ( spec == prev_spec )
+    {
+        status->width = SIZE_MAX;
+    }
 
     /* Optional length modifier
        We step one character ahead in any case, and step back only if we find
@@ -209,7 +214,16 @@ 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
+            {
+                /* FIXME: Need two kinds of "no match" here: zero width, and input error */
+                return NULL;
+            }
         }
         case 's':
         {
@@ -239,7 +253,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
             {
@@ -366,8 +381,11 @@ const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status )
             /* ASSIGN( E_size | E_unsigned, unsigned size_t ); */
             ASSIGN( E_ptrdiff, ptrdiff_t );
             /* ASSIGN( E_ptrdiff | E_unsigned, unsigned ptrdiff_t ); */
+            default:
+                puts( "UNSUPPORTED SCANF FLAG COMBINATION" );
+                return NULL;
         }
-        return spec;
+        return ++spec;
     }
     /* TODO: Floats. */
     return NULL;
@@ -376,10 +394,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;
 }