1 /* _PDCLIB_flushbuffer( struct _PDCLIB_file_t * )
\r
3 This file is part of the Public Domain C Library (PDCLib).
\r
4 Permission is granted to use, modify, and / or redistribute at will.
\r
11 #include <_PDCLIB_glue.h>
\r
12 #include <_PDCLIB_io.h>
\r
14 static int flushsubbuffer( FILE * stream, size_t length )
\r
19 while( written != length )
\r
22 size_t toWrite = length - written;
\r
23 bool res = stream->ops->write( stream->handle, stream->buffer + written,
\r
24 toWrite, &justWrote);
\r
25 written += justWrote;
\r
26 stream->pos.offset += justWrote;
\r
30 stream->status |=_PDCLIB_ERRORFLAG;
\r
36 stream->bufidx -= written;
\r
37 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
\r
42 #if defined(_PDCLIB_NEED_EOL_TRANSLATION)
\r
43 #undef _PDCLIB_NEED_EOL_TRANSLATION
\r
44 #define _PDCLIB_NEED_EOL_TRANSLATION 1
\r
46 #define _PDCLIB_NEED_EOL_TRANSLATION 0
\r
49 int _PDCLIB_flushbuffer( FILE * stream )
\r
51 // if a text stream, and this platform needs EOL translation, well...
\r
52 if ( ! ( stream->status & _PDCLIB_FBIN ) && _PDCLIB_NEED_EOL_TRANSLATION )
\r
55 for ( pos = 0; pos < stream->bufidx; pos++ )
\r
57 if (stream->buffer[pos] == '\n' ) {
\r
58 if ( stream->bufidx == stream->bufend ) {
\r
59 // buffer is full. Need to print out everything up till now
\r
60 if( flushsubbuffer( stream, pos ) )
\r
68 // we have spare space in buffer. Shift everything 1char and
\r
70 memmove( &stream->buffer[pos+1], &stream->buffer[pos], stream->bufidx - pos );
\r
71 stream->buffer[pos] = '\r';
\r
79 return flushsubbuffer( stream, stream->bufidx );
\r
86 #include <_PDCLIB_test.h>
\r
90 /* Testing covered by ftell.c */
\r
91 return TEST_RESULTS;
\r