# HG changeset patch # User Claus Gittinger # Date 1075982249 -3600 # Node ID 634e73a5cdd9bea0e6533b13157651453fccb6f7 # Parent 5c357f1ba0c9b5ce62504a8c835b9cb0fc6ebd72 preps for 64bit positions diff -r 5c357f1ba0c9 -r 634e73a5cdd9 FileStream.st --- a/FileStream.st Thu Feb 05 12:52:51 2004 +0100 +++ b/FileStream.st Thu Feb 05 12:57:29 2004 +0100 @@ -1,6 +1,6 @@ " COPYRIGHT (c) 1989 by Claus Gittinger - All Rights Reserved + All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the @@ -13,10 +13,10 @@ "{ Package: 'stx:libbasic' }" ExternalStream subclass:#FileStream - instanceVariableNames:'pathName canPosition removeOnClose' - classVariableNames:'UserInitiatedFileSaveQuerySignal' - poolDictionaries:'' - category:'Streams-External' + instanceVariableNames:'pathName canPosition removeOnClose' + classVariableNames:'UserInitiatedFileSaveQuerySignal' + poolDictionaries:'' + category:'Streams-External' ! !FileStream primitiveDefinitions! @@ -200,7 +200,7 @@ copyright " COPYRIGHT (c) 1989 by Claus Gittinger - All Rights Reserved + All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the @@ -231,14 +231,14 @@ I know, this is ugly, but what else could we do ? [instance variables:] - pathName the files path (if known) - canPosition positionable - read above comment + pathName the files path (if known) + canPosition positionable - read above comment [author:] - Claus Gittinger + Claus Gittinger [see also:] - Filename DirectoryStream PipeStream Socket + Filename DirectoryStream PipeStream Socket " ! @@ -253,11 +253,11 @@ For example, to create a file with fixed records and recordLength of 100, use: - |newFile| + |newFile| - newFile := FileStream new pathName:''. - newFile setMode:#writeonly. - newFile openWithMode:'w' attributes:#('rfm=fix' 'fsz=100'). + newFile := FileStream new pathName:''. + newFile setMode:#writeonly. + newFile openWithMode:'w' attributes:#('rfm=fix' 'fsz=100'). since all of the above is private protocol, and it is considered bad style to access these from user programs, we recommend subclassing FileStream as @@ -513,9 +513,9 @@ s := self readOnlyFileNamed:filename. text ifTrue:[ - s text + s text ] ifFalse:[ - s binary + s binary ]. ^ s ! @@ -544,11 +544,11 @@ s := self newFileNamed:filename. s notNil ifTrue:[ - textModeBoolean ifTrue:[ - s text - ] ifFalse:[ - s binary - ] + textModeBoolean ifTrue:[ + s text + ] ifFalse:[ + s binary + ] ]. ^ s ! ! @@ -666,7 +666,7 @@ super closeFile. removeOnClose == true ifTrue:[ - pathName asFilename remove. + pathName asFilename remove. ] "Created: / 13.8.1998 / 12:11:22 / cg" @@ -680,7 +680,7 @@ %{ HFILE f; - long currentPosition; + off_t currentPosition; if (__INST(filePointer) != nil) { do { @@ -717,7 +717,14 @@ } while ((currentPosition < 0) && (__threadErrno == EINTR)); if (currentPosition >= 0) { - RETURN ( __MKINT(currentPosition) ); + OBJ rslt; + + if (sizeof(currentPosition) == 8) { + rslt = __MKINT64 (¤tPosition); + } else { + rslt = __MKINT(currentPosition); + } + RETURN ( rslt ); } __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); } @@ -854,29 +861,29 @@ long ret; if (__INST(filePointer) != nil) { - f = __FILEVal(__INST(filePointer)); - __INST(position) = nil; /* i.e. unknown */ - do { + f = __FILEVal(__INST(filePointer)); + __INST(position) = nil; /* i.e. unknown */ + do { #ifdef WIN32 - __threadErrno = 0; - if (__INST(buffered) == true) { - ret = STX_C_CALL3( "fseek", fseek, f, 0L, SEEK_END); - } else { - __INST(readAhead) = nil; - ret = STX_C_CALL3( "lseek", lseek, fileno(f), 0L, SEEK_END); - } + __threadErrno = 0; + if (__INST(buffered) == true) { + ret = STX_C_CALL3( "fseek", fseek, f, 0L, SEEK_END); + } else { + __INST(readAhead) = nil; + ret = STX_C_CALL3( "lseek", lseek, fileno(f), 0L, SEEK_END); + } #else - if (__INST(buffered) == true) { - ret = fseek(f, 0L, SEEK_END); - } else { - ret = lseek(fileno(f), 0L, SEEK_END); - } + if (__INST(buffered) == true) { + ret = fseek(f, 0L, SEEK_END); + } else { + ret = lseek(fileno(f), 0L, SEEK_END); + } #endif - } while ((ret < 0) && (__threadErrno == EINTR)); - if (ret >= 0) { - RETURN ( self ); - } - __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); + } while ((ret < 0) && (__threadErrno == EINTR)); + if (ret >= 0) { + RETURN ( self ); + } + __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); } %}. lastErrorNumber notNil ifTrue:[^ self ioError]. @@ -947,8 +954,8 @@ aStream nextPutAll:'(FileStream oldFileNamed:'. aStream nextPutAll:pathName storeString. (self position ~~ ZeroPosition) ifTrue:[ - aStream nextPutAll:'; position:'. - self position storeOn:aStream + aStream nextPutAll:'; position:'. + self position storeOn:aStream ]. aStream nextPut:$) @@ -979,12 +986,12 @@ pathName isNil ifTrue:[^nil]. (mode == #readonly) ifTrue: [ - didWrite := false. - ^ self openWithMode:ReadMode + didWrite := false. + ^ self openWithMode:ReadMode ]. (mode == #writeonly) ifTrue: [ - didWrite := true. - ^ self openWithMode:WriteMode + didWrite := true. + ^ self openWithMode:WriteMode ]. ^ self openWithMode:ReadWriteMode ! @@ -1015,213 +1022,213 @@ retry: if (__isNonNilObject(pathName) && (__qClass(pathName)==String)) { #ifdef __VMS__ - do { - /* - * allow passing additional RMS arguments. - * stupid: DEC does not seem to offer an interface for passing a char **. - */ - __threadErrno = 0; + do { + /* + * allow passing additional RMS arguments. + * stupid: DEC does not seem to offer an interface for passing a char **. + */ + __threadErrno = 0; - { - if (__isArray(attributeSpec)) { - OBJ *ap = __ArrayInstPtr(attributeSpec)->a_element; - int numAttrib = 0; - int i; + { + if (__isArray(attributeSpec)) { + OBJ *ap = __ArrayInstPtr(attributeSpec)->a_element; + int numAttrib = 0; + int i; - numAttrib = __arraySize(attributeSpec); - for (i=0; i= 0) { - switch (statBuffer.st_fab_rfm) { - case FAB$C_UDF: /* undefined (also stream binary) */ - case FAB$C_VAR: /* variable length records */ - case FAB$C_VFC: /* variable fixed control */ - case FAB$C_STM: /* RMS-11 stream (valid only for sequen> */ - default: - __INST(canPosition) = false; - break; + if (fstat(fileno(f), &statBuffer) >= 0) { + switch (statBuffer.st_fab_rfm) { + case FAB$C_UDF: /* undefined (also stream binary) */ + case FAB$C_VAR: /* variable length records */ + case FAB$C_VFC: /* variable fixed control */ + case FAB$C_STM: /* RMS-11 stream (valid only for sequen> */ + default: + __INST(canPosition) = false; + break; - case FAB$C_FIX: /* fixed length records */ - case FAB$C_STMLF: /* LF stream (valid only for sequential> */ - case FAB$C_STMCR: /* CR stream (valid only for sequential> */ - __INST(canPosition) = true; - break; - } - } - } + case FAB$C_FIX: /* fixed length records */ + case FAB$C_STMLF: /* LF stream (valid only for sequential> */ + case FAB$C_STMCR: /* CR stream (valid only for sequential> */ + __INST(canPosition) = true; + break; + } + } + } # endif #else /* not VMS */ - __INST(canPosition) = true; + __INST(canPosition) = true; #endif /* poor VMS */ - if (@global(FileOpenTrace) == true) { - fprintf(stderr, "fopen %s [FileStream] -> %x\n", __stringVal(pathName), f); - } + if (@global(FileOpenTrace) == true) { + fprintf(stderr, "fopen %s [FileStream] -> %x\n", __stringVal(pathName), f); + } - filePointer = __MKOBJ((INT)f); - __INST(position) = @global(PositionableStream:ZeroPosition); - } + filePointer = __MKOBJ((INT)f); + __INST(position) = @global(PositionableStream:ZeroPosition); + } } %}. ^ filePointer @@ -1296,10 +1303,10 @@ filePointer := self openFile:pathName withMode:openmode attributes:attributeSpec. filePointer isNil ifTrue:[ - " - the open failed for some reason ... - " - ^ self openError + " + the open failed for some reason ... + " + ^ self openError ]. position := ZeroPosition. buffered := true. "default is buffered" @@ -1328,21 +1335,21 @@ |oldPos| filePointer notNil ifTrue:[ - "it was open, when snapped-out" - filePointer := nil. - Lobby unregister:self. - oldPos := position. - self open. - filePointer isNil ifTrue:[ - "/ this happens, if after a restart, - "/ the file is no longer present or accessable ..." + "it was open, when snapped-out" + filePointer := nil. + Lobby unregister:self. + oldPos := position. + self open. + filePointer isNil ifTrue:[ + "/ this happens, if after a restart, + "/ the file is no longer present or accessable ..." - (self class name , ' [warning]: could not reOpen file: ', pathName) errorPrintCR. - ] ifFalse:[ - oldPos notNil ifTrue:[ - self position:oldPos. - ] - ] + (self class name , ' [warning]: could not reOpen file: ', pathName) errorPrintCR. + ] ifFalse:[ + oldPos notNil ifTrue:[ + self position:oldPos. + ] + ] ] "Modified: 10.1.1997 / 17:50:51 / cg" @@ -1365,10 +1372,10 @@ int size; if (__INST(filePointer) != nil) { - f = __FILEVal(__INST(filePointer)); - if ((size = filesize(fileno(f))) >= 0) { - RETURN ( __MKSMALLINT(size) ); - } + f = __FILEVal(__INST(filePointer)); + if ((size = filesize(fileno(f))) >= 0) { + RETURN ( __MKSMALLINT(size) ); + } } #else struct stat buf; @@ -1376,26 +1383,26 @@ int fd; if (__INST(filePointer) != nil) { - f = __FILEVal(__INST(filePointer)); - fd = fileno(f); - do { - ret = fstat(fd, &buf); - } while ((ret < 0) && (__threadErrno == EINTR)); - if (ret >= 0) { - RETURN ( __MKSMALLINT( (int)(buf.st_size) ) ); - } - __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); + f = __FILEVal(__INST(filePointer)); + fd = fileno(f); + do { + ret = fstat(fd, &buf); + } while ((ret < 0) && (__threadErrno == EINTR)); + if (ret >= 0) { + RETURN ( __MKSMALLINT( (int)(buf.st_size) ) ); + } + __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno); } #endif %}. "could add a fall-back here: - oldPosition := self position. - self setToEnd. - sz := self position. - self position:oldPosition. - ^ sz + oldPosition := self position. + self setToEnd. + sz := self position. + self position:oldPosition. + ^ sz " lastErrorNumber notNil ifTrue:[^ self ioError]. filePointer isNil ifTrue:[^ self errorNotOpen]. @@ -1434,7 +1441,7 @@ !FileStream class methodsFor:'documentation'! version - ^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.99 2004-02-05 11:52:51 cg Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.100 2004-02-05 11:57:29 cg Exp $' ! ! FileStream initialize!