]> pd.if.org Git - pdclib/blob - functions/stdio/fread.c
Cleaned up the testing a bit.
[pdclib] / functions / stdio / fread.c
1 /* $Id$ */
2
3 /* fwrite( void *, size_t, size_t, FILE * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10 #include <_PDCLIB_glue.h>
11
12 #ifndef REGTEST
13
14 #include <stdbool.h>
15 #include <string.h>
16
17 size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
18 {
19     if ( _PDCLIB_prepread( stream ) == EOF )
20     {
21         return 0;
22     }
23     char * dest = (char *)ptr;
24     size_t nmemb_i;
25     for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
26     {
27         for ( size_t size_i = 0; size_i < size; ++size_i )
28         {
29             if ( stream->bufidx == stream->bufend )
30             {
31                 if ( _PDCLIB_fillbuffer( stream ) == EOF )
32                 {
33                     /* Could not read requested data */
34                     return nmemb_i;
35                 }
36             }
37             dest[ nmemb_i * size + size_i ] = stream->buffer[ stream->bufidx++ ];
38         }
39     }
40     return nmemb_i;
41 }
42
43 #endif
44
45 #ifdef TEST
46 #include <_PDCLIB_test.h>
47
48 int main( void )
49 {
50     FILE * fh;
51     remove( testfile );
52     TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL );
53     TESTCASE( fwrite( "SUCCESS testing fwrite()\n", 1, 25, fh ) == 25 );
54     TESTCASE( fclose( fh ) == 0 );
55     /* TODO: Add readback test. */
56     TESTCASE( remove( testfile ) == 0 );
57     return TEST_RESULTS;
58 }
59
60 #endif
61