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