]> pd.if.org Git - pdclib/blobdiff - functions/stdio/_PDCLIB_flushbuffer.c
dos2unix
[pdclib] / functions / stdio / _PDCLIB_flushbuffer.c
index 30f132f0261a82faf8af181cad12136a43478578..e7cf47c3549cad4d09e3687ec448666c0d450623 100644 (file)
-/* _PDCLIB_flushbuffer( struct _PDCLIB_file_t * )\r
-\r
-   This file is part of the Public Domain C Library (PDCLib).\r
-   Permission is granted to use, modify, and / or redistribute at will.\r
-*/\r
-\r
-#include <stdio.h>\r
-#include <string.h>\r
-\r
-#ifndef REGTEST\r
-#include "_PDCLIB_glue.h"\r
-#include "_PDCLIB_io.h"\r
-\r
-\r
-static int flushsubbuffer( FILE * stream, size_t length )\r
-{\r
-    size_t justWrote;\r
-    size_t written = 0;\r
-    int rv = 0;\r
-\r
-#if 0\r
-    // Very useful for debugging buffering issues\r
-    char l = '<', r = '>';\r
-    stream->ops->write( stream->handle, &l,  1, &justWrote );\r
-#endif\r
-\r
-    while( written != length )\r
-    {\r
-        size_t toWrite = length - written;\r
-\r
-        bool res = stream->ops->write( stream->handle, stream->buffer + written,\r
-                              toWrite, &justWrote);\r
-        written += justWrote;\r
-        stream->pos.offset += justWrote;\r
-\r
-        if (!res)\r
-        {\r
-            stream->status |= _PDCLIB_ERRORFLAG;\r
-            rv = EOF;\r
-            break;\r
-        }\r
-    }\r
-\r
-#if 0\r
-    stream->ops->write( stream->handle, &r,  1, &justWrote );\r
-#endif\r
-\r
-    stream->bufidx   -= written;\r
-#ifdef _PDCLIB_NEED_EOL_TRANSLATION\r
-    stream->bufnlexp -= written;\r
-#endif\r
-    memmove( stream->buffer, stream->buffer + written, stream->bufidx );\r
-\r
-    return rv;\r
-}\r
-\r
-int _PDCLIB_flushbuffer( FILE * stream )\r
-{\r
-#ifdef _PDCLIB_NEED_EOL_TRANSLATION\r
-    // if a text stream, and this platform needs EOL translation, well...\r
-    if ( ! ( stream->status & _PDCLIB_FBIN ) )\r
-    {\r
-        // Special case: buffer is full and we start with a \n\r
-        if ( stream->bufnlexp == 0\r
-            && stream->bufidx == stream->bufend\r
-            && stream->buffer[0] == '\n' )\r
-        {\r
-            char cr = '\r';\r
-            size_t written = 0;\r
-            bool res = stream->ops->write( stream->handle, &cr, 1, &written );\r
-\r
-            if (!res) {\r
-                stream->status |= _PDCLIB_ERRORFLAG;\r
-                return EOF;\r
-            }\r
-\r
-        }\r
-\r
-        for ( ; stream->bufnlexp < stream->bufidx; stream->bufnlexp++ )\r
-        {\r
-            if (stream->buffer[stream->bufnlexp] == '\n' ) {\r
-                if ( stream->bufidx == stream->bufend ) {\r
-                    // buffer is full. Need to print out everything up till now\r
-                    if( flushsubbuffer( stream, stream->bufnlexp - 1 ) )\r
-                    {\r
-                        return EOF;\r
-                    }\r
-                }\r
-\r
-                // we have spare space in buffer. Shift everything 1char and\r
-                // insert \r\r
-                memmove( &stream->buffer[stream->bufnlexp + 1],\r
-                         &stream->buffer[stream->bufnlexp],\r
-                         stream->bufidx - stream->bufnlexp );\r
-                stream->buffer[stream->bufnlexp] = '\r';\r
-\r
-                stream->bufnlexp++;\r
-                stream->bufidx++;\r
-            }\r
-        }\r
-    }\r
-#endif\r
-    return flushsubbuffer( stream, stream->bufidx );\r
-}\r
-\r
-#endif\r
-\r
-\r
-#ifdef TEST\r
-#include "_PDCLIB_test.h"\r
-\r
-int main( void )\r
-{\r
-    /* Testing covered by ftell.c */\r
-    return TEST_RESULTS;\r
-}\r
-\r
-#endif\r
-\r
+/* _PDCLIB_flushbuffer( struct _PDCLIB_file_t * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+#include <string.h>
+
+#ifndef REGTEST
+#include "_PDCLIB_glue.h"
+#include "_PDCLIB_io.h"
+
+
+static int flushsubbuffer( FILE * stream, size_t length )
+{
+    size_t justWrote;
+    size_t written = 0;
+    int rv = 0;
+
+#if 0
+    // Very useful for debugging buffering issues
+    char l = '<', r = '>';
+    stream->ops->write( stream->handle, &l,  1, &justWrote );
+#endif
+
+    while( written != length )
+    {
+        size_t toWrite = length - written;
+
+        bool res = stream->ops->write( stream->handle, stream->buffer + written,
+                              toWrite, &justWrote);
+        written += justWrote;
+        stream->pos.offset += justWrote;
+
+        if (!res)
+        {
+            stream->status |= _PDCLIB_ERRORFLAG;
+            rv = EOF;
+            break;
+        }
+    }
+
+#if 0
+    stream->ops->write( stream->handle, &r,  1, &justWrote );
+#endif
+
+    stream->bufidx   -= written;
+#ifdef _PDCLIB_NEED_EOL_TRANSLATION
+    stream->bufnlexp -= written;
+#endif
+    memmove( stream->buffer, stream->buffer + written, stream->bufidx );
+
+    return rv;
+}
+
+int _PDCLIB_flushbuffer( FILE * stream )
+{
+#ifdef _PDCLIB_NEED_EOL_TRANSLATION
+    // if a text stream, and this platform needs EOL translation, well...
+    if ( ! ( stream->status & _PDCLIB_FBIN ) )
+    {
+        // Special case: buffer is full and we start with a \n
+        if ( stream->bufnlexp == 0
+            && stream->bufidx == stream->bufend
+            && stream->buffer[0] == '\n' )
+        {
+            char cr = '\r';
+            size_t written = 0;
+            bool res = stream->ops->write( stream->handle, &cr, 1, &written );
+
+            if (!res) {
+                stream->status |= _PDCLIB_ERRORFLAG;
+                return EOF;
+            }
+
+        }
+
+        for ( ; stream->bufnlexp < stream->bufidx; stream->bufnlexp++ )
+        {
+            if (stream->buffer[stream->bufnlexp] == '\n' ) {
+                if ( stream->bufidx == stream->bufend ) {
+                    // buffer is full. Need to print out everything up till now
+                    if( flushsubbuffer( stream, stream->bufnlexp - 1 ) )
+                    {
+                        return EOF;
+                    }
+                }
+
+                // we have spare space in buffer. Shift everything 1char and
+                // insert \r
+                memmove( &stream->buffer[stream->bufnlexp + 1],
+                         &stream->buffer[stream->bufnlexp],
+                         stream->bufidx - stream->bufnlexp );
+                stream->buffer[stream->bufnlexp] = '\r';
+
+                stream->bufnlexp++;
+                stream->bufidx++;
+            }
+        }
+    }
+#endif
+    return flushsubbuffer( stream, stream->bufidx );
+}
+
+#endif
+
+
+#ifdef TEST
+#include "_PDCLIB_test.h"
+
+int main( void )
+{
+    /* Testing covered by ftell.c */
+    return TEST_RESULTS;
+}
+
+#endif
+