]> pd.if.org Git - pdclib.old/blob - functions/stdio/fwrite.c
PDCLIB-13: fwrite did not reset it's offset after doing a buffer flush.
[pdclib.old] / functions / stdio / fwrite.c
1 /* $Id$ */
2
3 /* fwrite( const 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
11 #ifndef REGTEST
12
13 #include <_PDCLIB_glue.h>
14
15 #include <stdbool.h>
16 #include <string.h>
17
18 //TODO OS(2012-08-01): Ascertain purpose of lineend & potentially remove
19
20 size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
21 {
22     if ( _PDCLIB_prepwrite( stream ) == EOF )
23     {
24         return 0;
25     }
26     _PDCLIB_size_t offset = 0;
27     //bool lineend = false;
28     size_t nmemb_i;
29     for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
30     {
31         for ( size_t size_i = 0; size_i < size; ++size_i )
32         {
33             if ( ( stream->buffer[ stream->bufidx++ ] = ((char*)ptr)[ nmemb_i * size + size_i ] ) == '\n' )
34             {
35                 /* Remember last newline, in case we have to do a partial line-buffered flush */
36                 offset = stream->bufidx;
37                 //lineend = true;
38             }
39             if ( stream->bufidx == stream->bufsize )
40             {
41                 if ( _PDCLIB_flushbuffer( stream ) == EOF )
42                 {
43                     /* Returning number of objects completely buffered */
44                     return nmemb_i;
45                 }
46                 //lineend = false;
47                 /*
48                  * The entire buffer has been flushed; this means we have to
49                  * reset our newline position as we have already written
50                  * that part of the stream.
51                  */
52                 offset = 0;
53             }
54         }
55     }
56     /* Fully-buffered streams are OK. Non-buffered streams must be flushed,
57        line-buffered streams only if there's a newline in the buffer.
58     */
59     switch ( stream->status & ( _IONBF | _IOLBF ) )
60     {
61         case _IONBF:
62             if ( _PDCLIB_flushbuffer( stream ) == EOF )
63             {
64                 /* We are in a pinch here. We have an error, which requires a
65                    return value < nmemb. On the other hand, all objects have
66                    been written to buffer, which means all the caller had to
67                    do was removing the error cause, and re-flush the stream...
68                    Catch 22. We'll return a value one short, to indicate the
69                    error, and can't really do anything about the inconsistency.
70                 */
71                 return nmemb_i - 1;
72             }
73             break;
74         case _IOLBF:
75             {
76             size_t bufidx = stream->bufidx;
77             stream->bufidx = offset;
78             if ( _PDCLIB_flushbuffer( stream ) == EOF )
79             {
80                 /* See comment above. */
81                 stream->bufidx = bufidx;
82                 return nmemb_i - 1;
83             }
84             stream->bufidx = bufidx - offset;
85             memmove( stream->buffer, stream->buffer + offset, stream->bufidx );
86             }
87     }
88     return nmemb_i;
89 }
90
91 #endif
92
93 #ifdef TEST
94 #include <_PDCLIB_test.h>
95
96 int main( void )
97 {
98     /* Testing covered by fread(). */
99     return TEST_RESULTS;
100 }
101
102 #endif
103