prep for 0-based stream position
authorClaus Gittinger <cg@exept.de>
Tue, 25 Feb 2003 11:17:54 +0100
changeset 7053 13e04c48e23c
parent 7052 207cc9c62982
child 7054 685d359f9847
prep for 0-based stream position
ExternalStream.st
FileStream.st
FixedPoint.st
PositionableStream.st
--- a/ExternalStream.st	Tue Feb 25 11:15:21 2003 +0100
+++ b/ExternalStream.st	Tue Feb 25 11:17:54 2003 +0100
@@ -3060,20 +3060,6 @@
     ^ self subclassResponsibility
 !
 
-position
-    "return the position
-     - this must be redefined in subclass"
-
-    ^ self subclassResponsibility
-!
-
-position:anInteger
-    "set the position
-     - this must be redefined in subclass"
-
-    ^ self subclassResponsibility
-!
-
 reset
     "set the read position to the beginning of the collection"
 
@@ -4047,6 +4033,16 @@
     self argumentMustBeInteger
 ! !
 
+!ExternalStream methodsFor:'positioning'!
+
+position0Based
+    self subclassResponsibility
+!
+
+position0Based:index0Based
+    self subclassResponsibility
+! !
+
 !ExternalStream methodsFor:'private'!
 
 clearEOF
@@ -5790,7 +5786,7 @@
 !ExternalStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.248 2003-02-25 09:05:53 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ExternalStream.st,v 1.249 2003-02-25 10:16:46 cg Exp $'
 ! !
 
 ExternalStream initialize!
--- a/FileStream.st	Tue Feb 25 11:15:21 2003 +0100
+++ b/FileStream.st	Tue Feb 25 11:17:54 2003 +0100
@@ -652,9 +652,8 @@
 
 !FileStream methodsFor:'positioning'!
 
-position
-    "return the read/write position in the file -
-     notice, in smalltalk indices start at 1 so begin of file is 1"
+position0Based
+    "return the read/write position in the file"
 
 %{
 
@@ -662,46 +661,43 @@
     long currentPosition;
 
     if (__INST(filePointer) != nil) {
-	do {
-	    f = __FILEVal(__INST(filePointer));
+        do {
+            f = __FILEVal(__INST(filePointer));
 #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);                            
-		long offs = 0L;
+            } else {                                                   
+                OBJ rA = __INST(readAhead);                            
+                long offs = 0L;
 
-		if (rA != nil) {                                       
-		    __INST(readAhead) = nil;                           
-		    offs = -1L;
-		}                                                      
+                if (rA != nil) {                                       
+                    __INST(readAhead) = nil;                           
+                    offs = -1L;
+                }                                                      
 # 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
-	    if (__INST(buffered) == true) {
-		currentPosition = ftell(f);
-	    } else {
-		currentPosition = lseek(fileno(f), 0L, SEEK_CUR);
-	    }
+            if (__INST(buffered) == true) {
+                currentPosition = ftell(f);
+            } else {
+                currentPosition = lseek(fileno(f), 0L, SEEK_CUR);
+            }
 #endif
-	} while ((currentPosition < 0) && (__threadErrno == EINTR));
+        } while ((currentPosition < 0) && (__threadErrno == EINTR));
 
-	if (currentPosition >= 0) {
-	    /*
-	     * notice: Smalltalk index starts at ZeroPosition
-	     */
-	    RETURN ( __MKSMALLINT(currentPosition + __intVal( @global(PositionableStream:ZeroPosition) )) );
-	}
-	__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
+        if (currentPosition >= 0) {
+            RETURN ( __MKSMALLINT(currentPosition) );
+        }
+        __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
     }
 %}.
     lastErrorNumber notNil ifTrue:[^ self ioError].
@@ -709,7 +705,7 @@
     ^ self primitiveFailed
 !
 
-position:newPos
+position0Based:newPos
     "set the read/write position in the file"
 
     |rslt|
@@ -722,65 +718,60 @@
 
     if ((__INST(canPosition) != false) 
      || (newPos == __MKSMALLINT(1))) {
-	if ((fp = __INST(filePointer)) != nil) {
-	    if (__isSmallInteger(newPos)) {
-		f = __FILEVal(fp);
-		nP = (long)__intVal(newPos);
+        if ((fp = __INST(filePointer)) != nil) {
+            if (__isSmallInteger(newPos)) {
+                f = __FILEVal(fp);
+                nP = (long)__intVal(newPos);
 
-		/*
-		 * notice: Smalltalk index starts at ZeroPosition
-		 */
-		nP = nP - __intVal( @global(PositionableStream:ZeroPosition));
-
-		do {
+                do {
 #ifdef WIN32
-		    __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);
-		    }
+                    if (__INST(buffered) == true) {
+                        ret = fseek(f, nP, SEEK_SET);
+                    } else {
+                        ret = lseek(fileno(f), nP, SEEK_SET);
+                    }
 #endif
-		} while ((ret < 0) && (__threadErrno == EINTR));
-		if (ret >= 0) {
-		    __INST(position) = newPos;
-		    /*
-		     * just to make certain ...
-		     */
-		    __INST(hitEOF) = false;
-		    RETURN ( self );
-		}
-		__INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
-	    }
-	}
+                } while ((ret < 0) && (__threadErrno == EINTR));
+                if (ret >= 0) {
+                    __INST(position) = newPos;
+                    /*
+                     * just to make certain ...
+                     */
+                    __INST(hitEOF) = false;
+                    RETURN ( self );
+                }
+                __INST(lastErrorNumber) = __MKSMALLINT(__threadErrno);
+            }
+        }
     }
 %}.
     canPosition == false ifTrue:[
-	"/ position by rewinding & re-reading everything up-to
-	"/ that point.
-	^ self slowPosition:newPos
+        "/ position by rewinding & re-reading everything up-to
+        "/ that point.
+        ^ self slowPosition0Based: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
     ].
     filePointer isNil ifTrue:[^ self errorNotOpen].
-    rslt := self positionFile:filePointer position:newPos.
+    rslt := self positionFile:filePointer position0Based:newPos.
     rslt >= 0 ifTrue:[
-	position := newPos.
+        position := newPos + ZeroPosition.
     ] ifFalse:[
-	hitEOF := true.
+        hitEOF := true.
     ]
 !
 
@@ -822,34 +813,47 @@
     ^ 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."
-
-    |buffer amount|
-
-    self isReadable ifFalse:[
-	"/ sorry
-	^ self positionError
-    ].
-
-    buffer := ByteArray new:8*1024.
+         This should really be done transparently by the stdio library."
 
-    (position isNil "/ i.e. unknown
-    or:[newPos < position]) ifTrue:[
-	self reset.
-    ].
-    [position < newPos] whileTrue:[
-	amount := (buffer size) min:(newPos-position).
-	(self nextBytes:amount into:buffer startingAt:1) ~~ amount ifTrue:[
-	    ^ self positionError
-	].
-    ].
-    "/ ('FileStream [info]: slow position - please convert ''' , pathName printString , ''' to streamLF format') infoPrintCR.
+    ^ self slowPosition0Based:(newPos - ZeroPosition)
 ! !
 
 !FileStream methodsFor:'printing & storing'!
@@ -1359,7 +1363,7 @@
 !FileStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.89 2003-02-25 09:05:53 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/FileStream.st,v 1.90 2003-02-25 10:16:57 cg Exp $'
 ! !
 
 FileStream initialize!
--- a/FixedPoint.st	Tue Feb 25 11:15:21 2003 +0100
+++ b/FixedPoint.st	Tue Feb 25 11:17:54 2003 +0100
@@ -222,7 +222,7 @@
             fractionStream nextPut:char
         ].
 
-        scale := fractionStream positionStartingAt0.
+        scale := fractionStream position0Based.
         fractionStream reset.
         fractionPart := Number readFrom:fractionStream.
     ].
@@ -233,7 +233,7 @@
 
     "
      FixedPoint readFrom:'1.00' 
-     FixedPoint readFrom:'123.456'
+     FixedPoint readFrom:'123.456' 
      FixedPoint readFrom:'-123.456' 
      FixedPoint readFrom:'123' 
      FixedPoint readFrom:'-123' 
@@ -271,7 +271,6 @@
     ^ $s
 ! !
 
-
 !FixedPoint methodsFor:'accessing'!
 
 scale
@@ -957,7 +956,6 @@
     "Modified: 12.4.1997 / 11:22:02 / cg"
 ! !
 
-
 !FixedPoint methodsFor:'testing'!
 
 isFixedPoint
@@ -972,5 +970,5 @@
 !FixedPoint class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.25 2002-11-26 11:41:07 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/FixedPoint.st,v 1.26 2003-02-25 10:17:54 cg Exp $'
 ! !
--- a/PositionableStream.st	Tue Feb 25 11:15:21 2003 +0100
+++ b/PositionableStream.st	Tue Feb 25 11:17:54 2003 +0100
@@ -857,14 +857,13 @@
 position1Based
     "return the read position 1-based"
 
-    ^ position - ZeroPosition + 1
+    ^ self position0Based + 1
 !
 
 position1Based:index1Based
     "set the read (or write) position"
 
-    ((index1Based > (readLimit+1)) or:[index1Based < 1]) ifTrue: [^ self positionError:index1Based].
-    position := index1Based - 1 + ZeroPosition
+    self position0Based:(index1Based - 1)
 
     "
      |s|
@@ -1194,7 +1193,7 @@
 !PositionableStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.121 2003-02-25 10:14:58 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.122 2003-02-25 10:16:22 cg Exp $'
 ! !
 
 PositionableStream initialize!