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