]> pd.if.org Git - pdclib/blob - _PDCLIB_flushbuffer.c
d2cb60c1e9917d15711f0255c625ed9de214b610
[pdclib] / _PDCLIB_flushbuffer.c
1 /* $Id$ */
2
3 /* _PDCLIB_flushbuffer( struct _PDCLIB_file_t * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 /* This is a stub implementation of _PDCLIB_flushbuffer
10 */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #ifndef REGTEST
16 #include <_PDCLIB_glue.h>
17 #include <errno.h>
18 #include <windows.h>
19
20 int _PDCLIB_flushbuffer( struct _PDCLIB_file_t * stream )
21 {
22     if ( ! ( stream->status & _PDCLIB_FBIN ) )
23     {
24         /* TODO: Text stream conversion here */
25     }
26
27     DWORD written = 0;
28
29
30     while(written != stream->bufidx) {
31         DWORD justWrote;
32         DWORD toWrite = stream->bufidx - written;
33         BOOL res = WriteFile( stream->handle, stream->buffer + written, 
34                               toWrite, &justWrote, NULL);
35         written += justWrote;
36         stream->pos.offset += justWrote;
37
38         if(!res) {
39             stream->status |=_PDCLIB_ERRORFLAG;
40             stream->bufidx -= written;
41             memmove( stream->buffer, stream->buffer + written, stream->bufidx );
42             _PDCLIB_w32errno();
43             return EOF;
44         }
45     }
46
47     stream->bufidx = 0;
48     return 0;
49 }
50
51 #endif
52
53
54 #ifdef TEST
55 #include <_PDCLIB_test.h>
56
57 int main( void )
58 {
59     /* Testing covered by ftell.c */
60     return TEST_RESULTS;
61 }
62
63 #endif
64