]> pd.if.org Git - pdclib.old/blobdiff - functions/stdio/_PDCLIB_flushbuffer.c
PDCLIB-8: First phase of intergation of new I/O backend system (with minimal
[pdclib.old] / functions / stdio / _PDCLIB_flushbuffer.c
diff --git a/functions/stdio/_PDCLIB_flushbuffer.c b/functions/stdio/_PDCLIB_flushbuffer.c
new file mode 100644 (file)
index 0000000..ffbbb4f
--- /dev/null
@@ -0,0 +1,56 @@
+/* _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
+\r
+int _PDCLIB_flushbuffer( struct _PDCLIB_file_t * stream )\r
+{\r
+    if ( ! ( stream->status & _PDCLIB_FBIN ) )\r
+    {\r
+        /* TODO: Text stream conversion here */\r
+    }\r
+\r
+    size_t written = 0;\r
+\r
+\r
+    while(written != stream->bufidx) {\r
+        size_t justWrote;\r
+        size_t toWrite = stream->bufidx - written;\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
+            stream->status |=_PDCLIB_ERRORFLAG;\r
+            stream->bufidx -= written;\r
+            memmove( stream->buffer, stream->buffer + written, stream->bufidx );\r
+            return EOF;\r
+        }\r
+    }\r
+\r
+    stream->bufidx = 0;\r
+    return 0;\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