From 336369a5b94ae6588fb891606d37053f9fb8c2d6 Mon Sep 17 00:00:00 2001 From: "phil@softsynth.com" Date: Sun, 1 Aug 2010 21:41:34 +0000 Subject: [PATCH] Fix FILE-POSITION REPOSITION-FILE and FILE-SIZE. Now use double precision offsets. --- csrc/pf_inner.c | 46 +++++++++++++++++++++++++++++++++------------- csrc/pf_io.h | 8 ++++---- releases.txt | 4 ++++ 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/csrc/pf_inner.c b/csrc/pf_inner.c index ab1e38d..59ddfff 100644 --- a/csrc/pf_inner.c +++ b/csrc/pf_inner.c @@ -278,7 +278,7 @@ int pfCatch( ExecToken XT ) char *CharPtr; cell_t *CellPtr; FileStream *FileID; - uint8_t *CodeBase = CODE_BASE; + uint8_t *CodeBase = (uint8_t *) CODE_BASE; ThrowCode ExceptionReturnCode = 0; /* FIXME @@ -1018,11 +1018,18 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); case ID_FILE_SIZE: /* ( fid -- ud ior ) */ /* Determine file size by seeking to end and returning position. */ FileID = (FileStream *) TOS; - Scratch = sdTellFile( FileID ); - sdSeekFile( FileID, 0, PF_SEEK_END ); - M_PUSH( sdTellFile( FileID )); - sdSeekFile( FileID, Scratch, PF_SEEK_SET ); - TOS = (Scratch < 0) ? -4 : 0 ; /* !!! err num */ + { + off_t endposition, offsetHi; + off_t original = sdTellFile( FileID ); + sdSeekFile( FileID, 0, PF_SEEK_END ); + endposition = sdTellFile( FileID ); + M_PUSH(endposition); + // Just use a 0 if they are the same size. + offsetHi = (sizeof(off_t) > sizeof(cell_t)) ? (endposition >> (8*sizeof(cell_t))) : 0 ; + M_PUSH(offsetHi); + sdSeekFile( FileID, original, PF_SEEK_SET ); + TOS = (original < 0) ? -4 : 0 ; /* !!! err num */ + } endcase; case ID_FILE_WRITE: /* ( addr len fid -- ior ) */ @@ -1033,15 +1040,28 @@ DBUG(("XX ah,m,l = 0x%8x,%8x,%8x - qh,l = 0x%8x,%8x\n", ah,am,al, qh,ql )); TOS = (Temp != Scratch) ? -3 : 0; endcase; - case ID_FILE_REPOSITION: /* ( pos fid -- ior ) */ - FileID = (FileStream *) TOS; - Scratch = M_POP; - TOS = sdSeekFile( FileID, Scratch, PF_SEEK_SET ); + case ID_FILE_REPOSITION: /* ( ud fid -- ior ) */ + { + FileID = (FileStream *) TOS; + off_t offset = M_POP; + // Avoid compiler warnings on Mac. + offset = (sizeof(off_t) > sizeof(cell_t)) ? (offset << 8*sizeof(cell_t)) : 0 ; + offset += M_POP; + TOS = sdSeekFile( FileID, offset, PF_SEEK_SET ); + } endcase; - case ID_FILE_POSITION: /* ( pos fid -- ior ) */ - M_PUSH( sdTellFile( (FileStream *) TOS )); - TOS = 0; + case ID_FILE_POSITION: /* ( fid -- ud ior ) */ + { + off_t offsetHi; + FileID = (FileStream *) TOS; + off_t position = sdTellFile( FileID ); + M_PUSH(position); + // Just use a 0 if they are the same size. + offsetHi = (sizeof(off_t) > sizeof(cell_t)) ? (position >> (8*sizeof(cell_t))) : 0 ; + M_PUSH(offsetHi); + TOS = (position < 0) ? -4 : 0 ; /* !!! err num */ + } endcase; case ID_FILE_RO: /* ( -- fam ) */ diff --git a/csrc/pf_io.h b/csrc/pf_io.h index c31cdc4..5ee78f4 100644 --- a/csrc/pf_io.h +++ b/csrc/pf_io.h @@ -84,8 +84,8 @@ void ioTerm( void ); cell_t sdFlushFile( FileStream * Stream ); cell_t sdReadFile( void *ptr, cell_t Size, int32_t nItems, FileStream * Stream ); cell_t sdWriteFile( void *ptr, cell_t Size, int32_t nItems, FileStream * Stream ); - cell_t sdSeekFile( FileStream * Stream, cell_t Position, int32_t Mode ); - cell_t sdTellFile( FileStream * Stream ); + cell_t sdSeekFile( FileStream * Stream, off_t Position, int32_t Mode ); + off_t sdTellFile( FileStream * Stream ); cell_t sdCloseFile( FileStream * Stream ); cell_t sdInputChar( FileStream *stream ); @@ -118,8 +118,8 @@ void ioTerm( void ); #define sdFlushFile fflush #define sdReadFile fread #define sdWriteFile fwrite - #define sdSeekFile fseek - #define sdTellFile ftell + #define sdSeekFile fseeko + #define sdTellFile ftello #define sdCloseFile fclose #define sdInputChar fgetc diff --git a/releases.txt b/releases.txt index 2e5e4e1..62f3502 100644 --- a/releases.txt +++ b/releases.txt @@ -1,6 +1,10 @@ Release History for pForth - a Portable ANS-like Forth written in ANSI 'C' Documentation for pForth at http://www.softsynth.com/pforth/ + +V?? + - Fixed REPOSITION-FILE FILE-SIZE and FILE-POSITION. + They used to use single precision offset. Now use double as specified. V26 5/20/2010 - 64-bit support for M* UM/MOD etc by Aleksej Saushev. Thanks Aleksej! -- 2.20.1