/* Use local copy of CODE_BASE for speed. */
#define LOCAL_CODEREL_TO_ABS( a ) ((cell_t *) (((cell_t) a) + CodeBase))
+/* Truncate the unsigned double cell integer LO/HI to an uint64_t. */
+static uint64_t UdToUint64( ucell_t Lo, ucell_t Hi )
+{
+ return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
+ ? (((uint64_t)Lo) | (((uint64_t)Hi) >> (sizeof(ucell_t) * 8)))
+ : Lo );
+}
+
+/* Return TRUE if the unsigned double cell integer LO/HI is not greater
+ * then the greatest uint64_t.
+ */
+static int UdIsUint64( ucell_t Lo, ucell_t Hi )
+{
+ return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
+ ? TRUE
+ : Hi == 0 );
+}
+
static const char *pfSelectFileModeCreate( int fam );
static const char *pfSelectFileModeOpen( int fam );
FileStream *File = (FileStream *) TOS;
ucell_t SizeHi = (ucell_t) M_POP;
ucell_t SizeLo = (ucell_t) M_POP;
- TOS = sdResizeFile( File, SizeLo, SizeHi );
+ TOS = ( UdIsUint64( SizeLo, SizeHi )
+ ? sdResizeFile( File, UdToUint64( SizeLo, SizeHi ))
+ : THROW_RESIZE_FILE );
}
endcase;
return -1;
}
-ThrowCode sdResizeFile( FileStream * File, ucell_t SizeLo, ucell_t SizeHi )
+ThrowCode sdResizeFile( FileStream * File, uint64_t NewSize )
{
UNIMPLEMENTED("sdResizeFile");
- TOUCH(File);
- TOUCH(SizeLo);
- TOUCH(SizeHi);
+ TOUCH(NewSize);
return THROW_RESIZE_FILE;
}
cell_t sdSeekFile( FileStream * Stream, off_t Position, int32_t Mode );
cell_t sdRenameFile( const char *OldName, const char *NewName );
cell_t sdDeleteFile( const char *FileName );
- ThrowCode sdResizeFile( FileStream *, ucell_t SizeLo, ucell_t SizeHi );
+ ThrowCode sdResizeFile( FileStream *, uint64_t Size);
off_t sdTellFile( FileStream * Stream );
cell_t sdCloseFile( FileStream * Stream );
cell_t sdInputChar( FileStream *stream );
#define PF_SEEK_CUR (SEEK_CUR)
#define PF_SEEK_END (SEEK_END)
- ThrowCode sdResizeFile( FileStream *, ucell_t SizeLo, ucell_t SizeHi );
+ ThrowCode sdResizeFile( FileStream *, uint64_t Size);
/*
** printf() is only used for debugging purposes.
CreateDicEntryC( ID_FILE_REPOSITION, "REPOSITION-FILE", 0 );
CreateDicEntryC( ID_FILE_FLUSH, "FLUSH-FILE", 0 );
CreateDicEntryC( ID_FILE_RENAME, "(RENAME-FILE)", 0 );
- CreateDicEntryC( ID_FILE_RESIZE, "RESIZE-FILE", 0 );
+ CreateDicEntryC( ID_FILE_RESIZE, "(RESIZE-FILE)", 0 );
CreateDicEntryC( ID_FILE_RO, "R/O", 0 );
CreateDicEntryC( ID_FILE_RW, "R/W", 0 );
CreateDicEntryC( ID_FILE_WO, "W/O", 0 );
return Error;
}
-/* Return non-FALSE if the double-cell unsigned number LO/HI
- * is greater then LONG_MAX.
- */
-static bool_t IsGreaterThanLongMax( ucell_t Lo, ucell_t Hi )
-{
- return (Hi != 0) || (Lo > LONG_MAX);
-}
-
-ThrowCode sdResizeFile( FileStream *File, ucell_t SizeLo, ucell_t SizeHi )
+ThrowCode sdResizeFile( FileStream *File, uint64_t Size )
{
bool_t Error = TRUE;
- if( !IsGreaterThanLongMax( SizeLo, SizeHi ) )
+ if( Size <= LONG_MAX )
{
- long Newsize = (long) SizeLo;
+ long Newsize = (long) Size;
if( fseek( File, 0, SEEK_END ) == 0 )
{
long Oldsize = ftell( File );
THEN
;
+\ A limit used to perform a sanity check on the size argument for
+\ RESIZE-FILE.
+2variable RESIZE-FILE-LIMIT
+10000000 0 resize-file-limit 2! \ 10MB is somewhat arbitrarily chosen
+
+: RESIZE-FILE ( ud fileid -- ior )
+ -rot 2dup resize-file-limit 2@ d> ( fileid ud big? )
+ IF
+ ." Argument (" 0 d.r ." ) is larger then RESIZE-FILE-LIMIT." cr
+ ." (You can increase RESIZE-FILE-LIMIT with 2!)" cr
+ abort
+ ELSE
+ rot (resize-file)
+ THEN
+;
+
: ( ( "comment<rparen>" -- )
source-id
CASE
rot = -rot = and
;
+: D< ( d1 d2 -- flag )
+ d- nip 0<
+;
+
+: D> ( d1 d2 -- flag )
+ 2swap d<
+;
+
\ define some useful constants ------------------------------
1 0= constant FALSE
0 0= constant TRUE