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