]> pd.if.org Git - pdclib/blobdiff - functions/stdio/fread.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / stdio / fread.c
index b77b5dd4724d700dafdc2a242793bb8f047a705e..bf3185c4cf5d66e7c6aee1d06e04d8f996a4d794 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /* fwrite( void *, size_t, size_t, FILE * )
 
    This file is part of the Public Domain C Library (PDCLib).
@@ -7,14 +5,18 @@
 */
 
 #include <stdio.h>
-#include <_PDCLIB_glue.h>
 
 #ifndef REGTEST
+#include "_PDCLIB_io.h"
 
 #include <stdbool.h>
 #include <string.h>
 
-size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
+size_t _PDCLIB_fread_unlocked( 
+    void * _PDCLIB_restrict ptr, 
+    size_t size, size_t nmemb, 
+    FILE * _PDCLIB_restrict stream 
+)
 {
     if ( _PDCLIB_prepread( stream ) == EOF )
     {
@@ -24,36 +26,60 @@ size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PD
     size_t nmemb_i;
     for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
     {
-        for ( size_t size_i = 0; size_i < size; ++size_i )
-        {
-            if ( stream->bufidx == stream->bufend )
-            {
-                if ( _PDCLIB_fillbuffer( stream ) == EOF )
-                {
-                    /* Could not read requested data */
-                    return nmemb_i;
-                }
-            }
-            dest[ nmemb_i * size + size_i ] = stream->buffer[ stream->bufidx++ ];
-        }
+        size_t numread = _PDCLIB_getchars( &dest[ nmemb_i * size ], size, EOF, 
+                                           stream );
+        if( numread != size )
+            break;
     }
     return nmemb_i;
 }
 
+size_t fread( void * _PDCLIB_restrict ptr, 
+              size_t size, size_t nmemb, 
+              FILE * _PDCLIB_restrict stream )
+{
+    _PDCLIB_flockfile( stream );
+    size_t r = _PDCLIB_fread_unlocked( ptr, size, nmemb, stream );
+    _PDCLIB_funlockfile( stream );
+    return r;
+}
+
 #endif
 
 #ifdef TEST
-#include <_PDCLIB_test.h>
+#include "_PDCLIB_test.h"
 
 int main( void )
 {
     FILE * fh;
-    remove( "testfile" );
-    TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL );
-    TESTCASE( fwrite( "SUCCESS testing fwrite()\n", 1, 25, fh ) == 25 );
+    char const * message = "Testing fwrite()...\n";
+    char buffer[21];
+    buffer[20] = 'x';
+    TESTCASE( ( fh = tmpfile() ) != NULL );
+    /* fwrite() / readback */
+    TESTCASE( fwrite( message, 1, 20, fh ) == 20 );
+    rewind( fh );
+    TESTCASE( fread( buffer, 1, 20, fh ) == 20 );
+    TESTCASE( memcmp( buffer, message, 20 ) == 0 );
+    TESTCASE( buffer[20] == 'x' );
+    /* same, different nmemb / size settings */
+    rewind( fh );
+    TESTCASE( memset( buffer, '\0', 20 ) == buffer );
+    TESTCASE( fwrite( message, 5, 4, fh ) == 4 );
+    rewind( fh );
+    TESTCASE( fread( buffer, 5, 4, fh ) == 4 );
+    TESTCASE( memcmp( buffer, message, 20 ) == 0 );
+    TESTCASE( buffer[20] == 'x' );
+    /* same... */
+    rewind( fh );
+    TESTCASE( memset( buffer, '\0', 20 ) == buffer );
+    TESTCASE( fwrite( message, 20, 1, fh ) == 1 );
+    rewind( fh );
+    TESTCASE( fread( buffer, 20, 1, fh ) == 1 );
+    TESTCASE( memcmp( buffer, message, 20 ) == 0 );
+    TESTCASE( buffer[20] == 'x' );
+    /* Done. */
     TESTCASE( fclose( fh ) == 0 );
-    /* TODO: Add readback test. */
-    TESTCASE( remove( "testfile" ) == 0 );
     return TEST_RESULTS;
 }