FileStream.st
changeset 15620 264cf9877dda
parent 15573 36facee1072e
child 16109 0e7d694bfc7a
child 18084 ab5b38bd8f81
--- a/FileStream.st	Sat Aug 10 13:31:57 2013 +0200
+++ b/FileStream.st	Sat Aug 10 13:32:53 2013 +0200
@@ -876,19 +876,19 @@
      Therefore, this method is reimplemented here (from ExternalStream)"
 
     outStream isExternalStream ifTrue:[
-	pos := self position0Based.
-	n := self size - pos.
-	nWritten := OperatingSystem
-	    copyFromFd:(self fileDescriptor)
-	    toFd:(outStream fileDescriptor)
-	    startIndex:pos
-	    count:n.
-	nWritten = n ifTrue:[
-	    ^ self
-	].
-	nWritten > 0 ifTrue:[
-	    self position0Based:pos+nWritten.
-	].
+        pos := self position.
+        n := self size - pos.
+        nWritten := OperatingSystem
+            copyFromFd:(self fileDescriptor)
+            toFd:(outStream fileDescriptor)
+            startIndex:pos
+            count:n.
+        nWritten = n ifTrue:[
+            ^ self
+        ].
+        nWritten > 0 ifTrue:[
+            self position:pos+nWritten.
+        ].
     ].
     ^ super copyToEndInto:outStream bufferSize:bufferSize.
 
@@ -904,7 +904,7 @@
 
 !FileStream methodsFor:'positioning'!
 
-position0Based
+position
     "return the read/write position in the file"
 
 %{
@@ -913,54 +913,54 @@
     off_t currentPosition;
 
     if (__INST(handle) != nil) {
-	do {
-	    f = __FILEVal(__INST(handle));
+        do {
+            f = __FILEVal(__INST(handle));
 #ifdef WIN32
-	    __threadErrno = 0;
-	    if (__INST(buffered) == true) {
+            __threadErrno = 0;
+            if (__INST(buffered) == true) {
 # if 0
-		currentPosition = STX_C_CALL1( "ftell", ftell, f);
+                currentPosition = STX_C_CALL1( "ftell", ftell, f);
 # else
-		currentPosition = ftell(f);
+                currentPosition = ftell(f);
 # endif
-	    } else {
-		OBJ rA = __INST(readAhead);
-		off_t offs = 0;
+            } else {
+                OBJ rA = __INST(readAhead);
+                off_t offs = 0;
 
-		if (rA != nil) {
-		    __INST(readAhead) = nil;
-		    offs = -1;
-		}
+                if (rA != nil) {
+                    __INST(readAhead) = nil;
+                    offs = -1;
+                }
 # if 0
-		currentPosition = STX_C_CALL3( "lseek", lseek, fileno(f), offs, SEEK_CUR);
+                currentPosition = STX_C_CALL3( "lseek", lseek, fileno(f), offs, SEEK_CUR);
 # else
-		currentPosition = lseek(fileno(f), offs, SEEK_CUR);
+                currentPosition = lseek(fileno(f), offs, SEEK_CUR);
 # endif
-	    }
+            }
 #else /* !WIN32 */
-	    if (__INST(buffered) == true) {
+            if (__INST(buffered) == true) {
 #ifdef _LFS_LARGEFILE
-		currentPosition = ftello(f);
+                currentPosition = ftello(f);
 #else
-		currentPosition = ftell(f);
+                currentPosition = ftell(f);
 #endif /* ! _LFS_LARGEFILE */
-	    } else {
-		currentPosition = lseek(fileno(f), (off_t)0, SEEK_CUR);
-	    }
+            } else {
+                currentPosition = lseek(fileno(f), (off_t)0, SEEK_CUR);
+            }
 #endif /* !WIN32 */
-	} while ((currentPosition < 0) && (__threadErrno == EINTR));
+        } while ((currentPosition < 0) && (__threadErrno == EINTR));
 
-	if (currentPosition >= 0) {
-	    OBJ rslt;
+        if (currentPosition >= 0) {
+            OBJ rslt;
 
-	    if (sizeof(currentPosition) == 8) {
-		rslt = __MKINT64 (&currentPosition);
-	    } else {
-		rslt = __MKINT(currentPosition);
-	    }
-	    RETURN ( rslt );
-	}
-	__INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
+            if (sizeof(currentPosition) == 8) {
+                rslt = __MKINT64 (&currentPosition);
+            } else {
+                rslt = __MKINT(currentPosition);
+            }
+            RETURN ( rslt );
+        }
+        __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
     }
 %}.
     lastErrorNumber notNil ifTrue:[^ self ioError].
@@ -968,7 +968,7 @@
     ^ self primitiveFailed
 !
 
-position0Based:newPos
+position:newPos
     "set the read/write position in the file"
 
     |rslt|
@@ -979,95 +979,95 @@
     OBJ fp;
 
     if ((__INST(canPosition) != false) || (newPos == __mkSmallInteger(0))) {
-	if ((fp = __INST(handle)) != nil) {
+        if ((fp = __INST(handle)) != nil) {
 
 #if defined(_LFS_LARGE_FILE) && !defined(WIN32)
 # define FSEEK fseeko
-	    off_t nP;
+            off_t nP;
 #else
 #define FSEEK fseek
-	    long nP;
+            long nP;
 #endif
 
-	    if (__isSmallInteger(newPos)) {
-		nP = __intVal(newPos);
-		if (nP < 0) {
-		    __INST(lastErrorNumber) = __mkSmallInteger(EINVAL);
-		    goto getOutOfHere;
-		}
-	    } else {
-		nP = __signedLongIntVal(newPos);
-		if (nP < 0) {
-		    __INST(lastErrorNumber) = __mkSmallInteger(EINVAL);
-		    goto getOutOfHere;
-		}
-		if (nP == 0) {
-		    if (sizeof(nP) == 8) {
-			if (__signedLong64IntVal(newPos, &nP) == 0 || nP < 0) {
-			    __INST(lastErrorNumber) = __mkSmallInteger(EINVAL);
-			    goto getOutOfHere;
-			}
-		    } else {
-			__INST(lastErrorNumber) = __mkSmallInteger(EINVAL);
-			goto getOutOfHere;
-		    }
-		}
-	    }
+            if (__isSmallInteger(newPos)) {
+                nP = __intVal(newPos);
+                if (nP < 0) {
+                    __INST(lastErrorNumber) = __mkSmallInteger(EINVAL);
+                    goto getOutOfHere;
+                }
+            } else {
+                nP = __signedLongIntVal(newPos);
+                if (nP < 0) {
+                    __INST(lastErrorNumber) = __mkSmallInteger(EINVAL);
+                    goto getOutOfHere;
+                }
+                if (nP == 0) {
+                    if (sizeof(nP) == 8) {
+                        if (__signedLong64IntVal(newPos, &nP) == 0 || nP < 0) {
+                            __INST(lastErrorNumber) = __mkSmallInteger(EINVAL);
+                            goto getOutOfHere;
+                        }
+                    } else {
+                        __INST(lastErrorNumber) = __mkSmallInteger(EINVAL);
+                        goto getOutOfHere;
+                    }
+                }
+            }
 
-	    f = __FILEVal(fp);
+            f = __FILEVal(fp);
 
-	    do {
+            do {
 #if defined(DO_WRAP_CALL_FSEEK)
-		__threadErrno = 0;
-		if (__INST(buffered) == true) {
-		    ret = STX_C_CALL3( "fseek", fseek, f, nP, SEEK_SET);
-		} else {
-		    __INST(readAhead) = nil;
-		    ret = STX_C_CALL3( "lseek", lseek, fileno(f), nP, SEEK_SET);
-		}
+                __threadErrno = 0;
+                if (__INST(buffered) == true) {
+                    ret = STX_C_CALL3( "fseek", fseek, f, nP, SEEK_SET);
+                } else {
+                    __INST(readAhead) = nil;
+                    ret = STX_C_CALL3( "lseek", lseek, fileno(f), nP, SEEK_SET);
+                }
 #else
-		if (__INST(buffered) == true) {
-		    ret = FSEEK(f, nP, SEEK_SET);
-		} else {
-		    ret = lseek(fileno(f), nP, SEEK_SET);
-		}
-		__threadErrno = errno;
+                if (__INST(buffered) == true) {
+                    ret = FSEEK(f, nP, SEEK_SET);
+                } else {
+                    ret = lseek(fileno(f), nP, SEEK_SET);
+                }
+                __threadErrno = errno;
 #endif
-	    } while ((ret < 0) && (__threadErrno == EINTR));
-	    if (ret >= 0) {
-		__INST(position) = newPos; __STORE(self, newPos);
-		/*
-		 * just to make certain ...
-		 */
-		__INST(hitEOF) = false;
-		RETURN ( self );
-	    }
-	    __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
-	}
+            } while ((ret < 0) && (__threadErrno == EINTR));
+            if (ret >= 0) {
+                __INST(position) = newPos; __STORE(self, newPos);
+                /*
+                 * just to make certain ...
+                 */
+                __INST(hitEOF) = false;
+                RETURN ( self );
+            }
+            __INST(lastErrorNumber) = __mkSmallInteger(__threadErrno);
+        }
     }
     getOutOfHere: ;
 #undef FSEEK
 %}.
     canPosition == false ifTrue:[
-	"/ position by rewinding & re-reading everything up-to
-	"/ that point.
-	^ self slowPosition0Based:newPos
+        "/ position by rewinding & re-reading everything up-to
+        "/ that point.
+        ^ self slowPosition:newPos
     ].
     lastErrorNumber notNil ifTrue:[
-	(OperatingSystem errorSymbolForNumber:lastErrorNumber) == #EINVAL ifTrue:[
-	    "/ invalid position
-	    ^ self positionError
-	].
-	"/ assume I/O error
-	^ self ioError
+        (OperatingSystem errorSymbolForNumber:lastErrorNumber) == #EINVAL ifTrue:[
+            "/ invalid position
+            ^ self positionError
+        ].
+        "/ assume I/O error
+        ^ self ioError
     ].
     handle isNil ifTrue:[^ self errorNotOpen].
 
     rslt := self positionFile:handle position:newPos.
     rslt >= 0 ifTrue:[
-	position := newPos.
+        position := newPos.
     ] ifFalse:[
-	hitEOF := true.
+        hitEOF := true.
     ]
 !
 
@@ -1132,47 +1132,36 @@
     ^ self primitiveFailed
 !
 
-slowPosition0Based:newPos
-    "position the file by re-reading everything up-to newPos.
-     The effect is the same as that of #position:, but its much slower.
-     This is required to reposition nonPositionable streams, such
-     as tape-streams or variable-record-RMS files under VMS.
-     Caveat:
-	 This should really be done transparently by the stdio library."
-
-    |buffer amount pos0Based|
-
-    self isReadable ifFalse:[
-	"/ sorry
-	^ self positionError
-    ].
-
-    buffer := ByteArray new:8*1024.
-
-    (position isNil "/ i.e. unknown
-    or:[newPos < (pos0Based := self position0Based)]) ifTrue:[
-	self reset.
-	pos0Based := self position0Based.
-    ].
-    [pos0Based < newPos] whileTrue:[
-	amount := (buffer size) min:(newPos-pos0Based).
-	(self nextBytes:amount into:buffer startingAt:1) ~~ amount ifTrue:[
-	    ^ self positionError
-	].
-	pos0Based := self position0Based.
-    ].
-    "/ ('FileStream [info]: slow position - please convert ''' , pathName printString , ''' to streamLF format') infoPrintCR.
-!
-
 slowPosition:newPos
     "position the file by re-reading everything up-to newPos.
      The effect is the same as that of #position:, but its much slower.
      This is required to reposition nonPositionable streams, such
      as tape-streams or variable-record-RMS files under VMS.
      Caveat:
-	 This should really be done transparently by the stdio library."
+         This should really be done transparently by the stdio library."
+
+    |buffer amount pos0Based|
+
+    self isReadable ifFalse:[
+        "/ sorry
+        ^ self positionError
+    ].
+
+    buffer := ByteArray new:8*1024.
 
-    ^ self slowPosition0Based:newPos
+    (position isNil "/ i.e. unknown
+    or:[newPos < (pos0Based := self position)]) ifTrue:[
+        self reset.
+        pos0Based := self position.
+    ].
+    [pos0Based < newPos] whileTrue:[
+        amount := (buffer size) min:(newPos-pos0Based).
+        (self nextBytes:amount into:buffer startingAt:1) ~~ amount ifTrue:[
+            ^ self positionError
+        ].
+        pos0Based := self position.
+    ].
+    "/ ('FileStream [info]: slow position - please convert ''' , pathName printString , ''' to streamLF format') infoPrintCR.
 ! !
 
 !FileStream methodsFor:'printing & storing'!
@@ -1998,11 +1987,11 @@
 !FileStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.168 2013-07-29 08:11:37 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.169 2013-08-10 11:32:53 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.168 2013-07-29 08:11:37 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.169 2013-08-10 11:32:53 stefan Exp $'
 ! !