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