1 /* _PDCLIB_flushbuffer( struct _PDCLIB_file_t * )
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
11 #include "_PDCLIB_glue.h"
12 #include "_PDCLIB_io.h"
15 static int flushsubbuffer( FILE * stream, size_t length )
22 // Very useful for debugging buffering issues
23 char l = '<', r = '>';
24 stream->ops->write( stream->handle, &l, 1, &justWrote );
27 while( written != length )
29 size_t toWrite = length - written;
31 bool res = stream->ops->write( stream->handle, stream->buffer + written,
34 stream->pos.offset += justWrote;
38 stream->status |= _PDCLIB_ERRORFLAG;
45 stream->ops->write( stream->handle, &r, 1, &justWrote );
48 stream->bufidx -= written;
49 #ifdef _PDCLIB_NEED_EOL_TRANSLATION
50 stream->bufnlexp -= written;
52 memmove( stream->buffer, stream->buffer + written, stream->bufidx );
57 int _PDCLIB_flushbuffer( FILE * stream )
59 #ifdef _PDCLIB_NEED_EOL_TRANSLATION
60 // if a text stream, and this platform needs EOL translation, well...
61 if ( ! ( stream->status & _PDCLIB_FBIN ) )
63 // Special case: buffer is full and we start with a \n
64 if ( stream->bufnlexp == 0
65 && stream->bufidx == stream->bufend
66 && stream->buffer[0] == '\n' )
70 bool res = stream->ops->write( stream->handle, &cr, 1, &written );
73 stream->status |= _PDCLIB_ERRORFLAG;
79 for ( ; stream->bufnlexp < stream->bufidx; stream->bufnlexp++ )
81 if (stream->buffer[stream->bufnlexp] == '\n' ) {
82 if ( stream->bufidx == stream->bufend ) {
83 // buffer is full. Need to print out everything up till now
84 if( flushsubbuffer( stream, stream->bufnlexp - 1 ) )
90 // we have spare space in buffer. Shift everything 1char and
92 memmove( &stream->buffer[stream->bufnlexp + 1],
93 &stream->buffer[stream->bufnlexp],
94 stream->bufidx - stream->bufnlexp );
95 stream->buffer[stream->bufnlexp] = '\r';
103 return flushsubbuffer( stream, stream->bufidx );
110 #include "_PDCLIB_test.h"
114 /* Testing covered by ftell.c */