]> pd.if.org Git - pdclib/blobdiff - functions/stdio/fscanf.c
restrict keyword cleanup.
[pdclib] / functions / stdio / fscanf.c
index 2be1758375c1abb015c50de9360586c555da7b85..17b0dca4c9add00de4e45f667628696d137f3c5e 100644 (file)
@@ -1,85 +1,81 @@
-// ----------------------------------------------------------------------------
-// $Id$
-// ----------------------------------------------------------------------------
-// Public Domain C Library - http://pdclib.sourceforge.net
-// This code is Public Domain. Use, modify, and redistribute at will.
-// ----------------------------------------------------------------------------
+/* $Id$ */
 
-int fscanf( FILE * restrict stream, const char * restrict format, ... ) { /* TODO */ };
+/* fscanf( FILE *, const char *, ... )
 
-/* PDPC code - unreviewed
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+#include <stdarg.h>
+
+#ifndef REGTEST
+
+int fscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict format, ... )
 {
-    va_list arg;
-    int ret;
+    int rc;
+    va_list ap;
+    va_start( ap, format );
+    rc = vfscanf( stream, format, ap );
+    va_end( ap );
+    return rc;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+#include <string.h>
+
+char scanstring[] = "  1 23\00045\000\00067 ";
 
-    va_start(arg, format);
-    ret = vvscanf(format, arg, stream, NULL);
-    va_end(arg);
-    return (ret);
+void scantest( int testnr, FILE * fh, size_t position, char const * format, 
+               int expected_fscan_rc, char const * expected_fscan_output, size_t expected_fscan_length, 
+               int expected_sscan_rc, char const * expected_sscan_output, size_t expected_sscan_length )
+{
+    char buffer[15];
+    printf( "Test %d\n", testnr );
+    TESTCASE( memset( buffer, -1, 15 ) == buffer );
+    TESTCASE( fseek( fh, position, SEEK_SET ) == 0 );
+    TESTCASE( fscanf( fh, format, buffer ) == expected_fscan_rc );
+    TESTCASE( memcmp( buffer, expected_fscan_output, expected_fscan_length ) == 0 );
+    TESTCASE( memset( buffer, -1, 15 ) == buffer );
+    TESTCASE( sscanf( scanstring + position, format, buffer ) == expected_sscan_rc );
+    TESTCASE( memcmp( buffer, expected_sscan_output, expected_sscan_length ) == 0 );
 }
 
-static int vvscanf(const char *format, va_list arg, FILE *fp, const char *s)
+int main( void )
 {
-    int ch;
-    int fin = 0;
-    int cnt = 0;
-    char *cptr;
-    int *iptr;
-
-    inch();
-    while (!fin)
-    {
-        if (*format == '\0')
-        {
-            fin = 1;
-        }
-        else if (*format == '%')
-        {
-            format++;
-            if (*format == '%')
-            {
-                if (ch != '%') return (cnt);
-                inch();
-            }
-            else if (*format == 's')
-            {
-                cptr = va_arg(arg, char *);
-                *cptr++ = (char)ch;
-                inch();
-                while ((ch >= 0) && (!isspace(ch)))
-                {
-                    *cptr++ = (char)ch;
-                    inch();
-                }
-                *cptr = '\0';
-                if (ch < 0)
-                {
-                    fin = 1;
-                }
-            }
-            else if (*format == 'd')
-            {
-                iptr = va_arg(arg, int *);
-                if (!isdigit(ch)) return (cnt);
-                *iptr = ch - '0';
-                inch();
-                while ((ch >= 0) && (isdigit(ch)))
-                {
-                    *iptr = *iptr * 10 + (ch - '0');
-                    inch();
-                }
-                if (ch < 0)
-                {
-                    fin = 1;
-                }
-            }
-        }
-        else
-        {
-            if (ch != *format) return (cnt);
-            inch();
-        }
-    }
-    return (cnt);
+    FILE * fh;
+    TESTCASE( ( fh = fopen( "testfile", "w+" ) ) != NULL );
+    TESTCASE( fwrite( scanstring, 14, 1, fh ) == 1 );
+    rewind( fh );
+
+    /* %14c - full scan */
+    scantest( 1, fh, 0, "%14c",
+              1, "  1 23\00045\000\00067 \377", 15,
+              1, "  1 23\377", 7 );
+
+    /* %c - default to one, reading whitespace */
+    scantest( 2, fh, 0, "%c",
+              1, " \377", 2,
+              1, " \377", 2 );
+
+    /* %1c - reading zero byte */
+    scantest( 3, fh, 9, "%1c",
+              1, "\000\377", 2,
+              -1, "\377", 1 );
+
+    /* %0c - NOT reading EOF */
+    scantest( 4, fh, 13, "%0c",
+              0, "\377", 1,
+              0, "\377", 1 );
+              
+    TESTCASE( fclose( fh ) == 0 );
+    //TESTCASE( remove( "testfile" ) == 0 );
+
+    return TEST_RESULTS;
 }
-*/
+
+#endif