]> pd.if.org Git - pdclib/blob - functions/stdio/_PDCLIB_flushbuffer.c
PDCLIB-8: First phase of intergation of new I/O backend system (with minimal
[pdclib] / functions / stdio / _PDCLIB_flushbuffer.c
1 /* _PDCLIB_flushbuffer( struct _PDCLIB_file_t * )\r
2 \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
5 */\r
6 \r
7 #include <stdio.h>\r
8 #include <string.h>\r
9 \r
10 #ifndef REGTEST\r
11 #include <_PDCLIB_glue.h>\r
12 \r
13 int _PDCLIB_flushbuffer( struct _PDCLIB_file_t * stream )\r
14 {\r
15     if ( ! ( stream->status & _PDCLIB_FBIN ) )\r
16     {\r
17         /* TODO: Text stream conversion here */\r
18     }\r
19 \r
20     size_t written = 0;\r
21 \r
22 \r
23     while(written != stream->bufidx) {\r
24         size_t justWrote;\r
25         size_t toWrite = stream->bufidx - written;\r
26         bool res = stream->ops->write( stream->handle, stream->buffer + written, \r
27                               toWrite, &justWrote);\r
28         written += justWrote;\r
29         stream->pos.offset += justWrote;\r
30 \r
31         if(!res) {\r
32             stream->status |=_PDCLIB_ERRORFLAG;\r
33             stream->bufidx -= written;\r
34             memmove( stream->buffer, stream->buffer + written, stream->bufidx );\r
35             return EOF;\r
36         }\r
37     }\r
38 \r
39     stream->bufidx = 0;\r
40     return 0;\r
41 }\r
42 \r
43 #endif\r
44 \r
45 \r
46 #ifdef TEST\r
47 #include <_PDCLIB_test.h>\r
48 \r
49 int main( void )\r
50 {\r
51     /* Testing covered by ftell.c */\r
52     return TEST_RESULTS;\r
53 }\r
54 \r
55 #endif\r
56 \r