]> pd.if.org Git - pdclib/commitdiff
_PDCLIB_flushbuffer for win32. correct seeking behaviour.
authorOwen Shepherd <owen.shepherd@e43.eu>
Wed, 15 Aug 2012 13:08:07 +0000 (14:08 +0100)
committerOwen Shepherd <owen.shepherd@e43.eu>
Wed, 15 Aug 2012 13:08:07 +0000 (14:08 +0100)
functions/_PDCLIB/filemode.c
functions/stdio/fgetpos.c
functions/stdio/fopen.c
functions/stdio/ftell.c
platform/win32/functions/_PDCLIB/_PDCLIB_flushbuffer.c
platform/win32/functions/_PDCLIB/_PDCLIB_seek.c
platform/win32/internals/_PDCLIB_config.h

index ded5408542576274ad43a910a1e3b9d2776b4e6b..454810626d74a616dc47a75380487a2976560033 100644 (file)
@@ -15,6 +15,8 @@
 */
 unsigned int _PDCLIB_filemode( char const * const mode )
 {
+    if(!mode) return 0;
+
     unsigned rc = 0;
     switch ( mode[0] )
     {
index 248fb356a0f07d5fb6ef91c904b97de758212ee4..3e41c40fd365768b7e61bb5d8f56ce52b0fec1ac 100644 (file)
@@ -31,6 +31,7 @@ int main( void )
     TESTCASE( ( fh = tmpfile() ) != NULL );
     TESTCASE( fgetpos( fh, &pos1 ) == 0 );
     TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) );
+    TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
     TESTCASE( fgetpos( fh, &pos2 ) == 0 );
     TESTCASE( fsetpos( fh, &pos1 ) == 0 );
     TESTCASE( ftell( fh ) == 0 );
index 18b7b3ae4df8e2cf5646bb87fc88fc20bedc8eba..ff8e8e8a08862ea7679d54d3457d41531c3a7599 100644 (file)
@@ -20,6 +20,10 @@ FILE * fopen( const char * _PDCLIB_restrict filename,
               const char * _PDCLIB_restrict mode )
 {
     int imode = _PDCLIB_filemode( mode );
+    
+    if( imode == 0 || filename == NULL )
+        return NULL;
+
     _PDCLIB_fd_t fd = _PDCLIB_open( filename, imode );
     if(fd == _PDCLIB_NOHANDLE) {
         return NULL;
index 27e25ee9e7f209ff247ff5a03417ef538538da5d..e82ca48155e25d22e4fcacfb2f93858e3634f6c9 100644 (file)
@@ -38,7 +38,8 @@ long int ftell( struct _PDCLIB_file_t * stream )
         _PDCLIB_errno = _PDCLIB_ERANGE;
         return -1;
     }
-    return (long int)( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + stream->ungetidx ) );
+    long int res = ( stream->pos.offset - ( ( (int)stream->bufend - (int)stream->bufidx ) + stream->ungetidx ) );
+    return res;
 }
 
 #endif
index 82e36a0217b11190e02158a0af8d3338baf3dd3c..d2cb60c1e9917d15711f0255c625ed9de214b610 100644 (file)
@@ -33,6 +33,7 @@ int _PDCLIB_flushbuffer( struct _PDCLIB_file_t * stream )
         BOOL res = WriteFile( stream->handle, stream->buffer + written, 
                               toWrite, &justWrote, NULL);
         written += justWrote;
+        stream->pos.offset += justWrote;
 
         if(!res) {
             stream->status |=_PDCLIB_ERRORFLAG;
index c4c25f87b281829627a943d006101b6f51c5352d..3add6591c535962fc775a9a311759bcb0b64f8de 100644 (file)
 #include <errno.h>
 #ifndef REGTEST
 #include <_PDCLIB_glue.h>
+#include <windows.h>
 
+extern void _PDCLIB_w32errno( void );
 _PDCLIB_int64_t _PDCLIB_seek( struct _PDCLIB_file_t * stream, _PDCLIB_int64_t offset, int whence )
 {
-    errno = ENOTSUP;
-    return EOF;
+    LARGE_INTEGER liOffset;
+    liOffset.QuadPart = offset;
+    BOOL rv = SetFilePointerEx( stream->handle, liOffset, &liOffset, whence );
+    if(!rv) {
+        _PDCLIB_w32errno();
+        return EOF;
+    }
+    stream->pos.offset = liOffset.QuadPart;
+    return liOffset.QuadPart;
 }
 
 #endif
index 793065d6c6ec2656c155ca7f99667a62c485b69d..c828c7a3b1de50a4538346908a217ae86b85b2a4 100644 (file)
@@ -336,6 +336,8 @@ typedef void * _PDCLIB_fd_t;
 /* The values of SEEK_SET, SEEK_CUR and SEEK_END, used by fseek().
    Since at least one platform (POSIX) uses the same symbols for its own "seek"
    function, we use whatever the host defines (if it does define them).
+
+   Win32 note: Must match Win32 API values (FILE_BEGIN/FILE_CURRENT/FILE_END)
 */
 #define _PDCLIB_SEEK_SET 0
 #define _PDCLIB_SEEK_CUR 1