3 /* fwrite( const void *, size_t, size_t, FILE * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
12 #include <_PDCLIB_io.h>
13 #include <_PDCLIB_glue.h>
18 //TODO OS(2012-08-01): Ascertain purpose of lineend & potentially remove
20 size_t fwrite_unlocked( const void * _PDCLIB_restrict ptr,
21 size_t size, size_t nmemb,
22 FILE * _PDCLIB_restrict stream )
24 if ( _PDCLIB_prepwrite( stream ) == EOF )
28 _PDCLIB_size_t offset = 0;
29 //bool lineend = false;
31 for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
33 for ( size_t size_i = 0; size_i < size; ++size_i )
35 if ( ( stream->buffer[ stream->bufidx++ ] = ((char*)ptr)[ nmemb_i * size + size_i ] ) == '\n' )
37 /* Remember last newline, in case we have to do a partial line-buffered flush */
38 offset = stream->bufidx;
41 if ( stream->bufidx == stream->bufsize )
43 if ( _PDCLIB_flushbuffer( stream ) == EOF )
45 /* Returning number of objects completely buffered */
50 * The entire buffer has been flushed; this means we have to
51 * reset our newline position as we have already written
52 * that part of the stream.
58 /* Fully-buffered streams are OK. Non-buffered streams must be flushed,
59 line-buffered streams only if there's a newline in the buffer.
61 switch ( stream->status & ( _IONBF | _IOLBF ) )
64 if ( _PDCLIB_flushbuffer( stream ) == EOF )
66 /* We are in a pinch here. We have an error, which requires a
67 return value < nmemb. On the other hand, all objects have
68 been written to buffer, which means all the caller had to
69 do was removing the error cause, and re-flush the stream...
70 Catch 22. We'll return a value one short, to indicate the
71 error, and can't really do anything about the inconsistency.
78 size_t bufidx = stream->bufidx;
79 stream->bufidx = offset;
80 if ( _PDCLIB_flushbuffer( stream ) == EOF )
82 /* See comment above. */
83 stream->bufidx = bufidx;
86 stream->bufidx = bufidx - offset;
87 memmove( stream->buffer, stream->buffer + offset, stream->bufidx );
93 size_t fwrite( const void * _PDCLIB_restrict ptr,
94 size_t size, size_t nmemb,
95 FILE * _PDCLIB_restrict stream )
98 size_t r = fwrite_unlocked( ptr, size, nmemb, stream );
99 funlockfile( stream );
106 #include <_PDCLIB_test.h>
110 /* Testing covered by fread(). */