]> pd.if.org Git - pdclib.old/blob - functions/stdio/fwrite.c
* Change the style of inclusion of the internal/ headers. Modern preprocessors
[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         }
49     }
50     /* Fully-buffered streams are OK. Non-buffered streams must be flushed,
51        line-buffered streams only if there's a newline in the buffer.
52     */
53     switch ( stream->status & ( _IONBF | _IOLBF ) )
54     {
55         case _IONBF:
56             if ( _PDCLIB_flushbuffer( stream ) == EOF )
57             {
58                 /* We are in a pinch here. We have an error, which requires a
59                    return value < nmemb. On the other hand, all objects have
60                    been written to buffer, which means all the caller had to
61                    do was removing the error cause, and re-flush the stream...
62                    Catch 22. We'll return a value one short, to indicate the
63                    error, and can't really do anything about the inconsistency.
64                 */
65                 return nmemb_i - 1;
66             }
67             break;
68         case _IOLBF:
69             {
70             size_t bufidx = stream->bufidx;
71             stream->bufidx = offset;
72             if ( _PDCLIB_flushbuffer( stream ) == EOF )
73             {
74                 /* See comment above. */
75                 stream->bufidx = bufidx;
76                 return nmemb_i - 1;
77             }
78             stream->bufidx = bufidx - offset;
79             memmove( stream->buffer, stream->buffer + offset, stream->bufidx );
80             }
81     }
82     return nmemb_i;
83 }
84
85 #endif
86
87 #ifdef TEST
88 #include <_PDCLIB_test.h>
89
90 int main( void )
91 {
92     /* Testing covered by fread(). */
93     return TEST_RESULTS;
94 }
95
96 #endif
97