ExternalStream.st
changeset 19219 4988f69224c3
parent 19213 b990f28738b2
child 19227 5e949760a4e8
child 19394 5bb715922976
--- a/ExternalStream.st	Sun Feb 14 00:16:20 2016 +0100
+++ b/ExternalStream.st	Sun Feb 14 00:23:18 2016 +0100
@@ -3840,12 +3840,80 @@
     ^ self primitiveFailed
 !
 
-nextLong
-    "Read four bytes (msb-first) and return the value as a 32-bit signed Integer.
-     The returned value may be a LargeInteger.
-     (msb-first for compatibility with other smalltalks)"
-
-    ^ self nextUnsignedLongMSB:true
+nextInt16MSB:msbFlag
+    "Read two bytes and return the value as a 16-bit signed Integer.
+     If msbFlag is true, value is read with most-significant byte first,
+     otherwise least-significant byte comes first.
+     A nil is returned if EOF is reached (also when EOF is hit after the first byte).
+     Works in both binary and text modes."
+
+    |error|
+%{
+    OBJ fp;
+
+    __INST(lastErrorNumber) = nil;
+    if ((__INST(handleType) == nil)
+     || (__INST(handleType) == @symbol(filePointer))
+     || (__INST(handleType) == @symbol(socketFilePointer))
+     || (__INST(handleType) == @symbol(socketHandle))
+     || (__INST(handleType) == @symbol(pipeFilePointer))) {
+	if (((fp = __INST(handle)) != nil)
+	    && (__INST(mode) != @symbol(writeonly))
+	) {
+	    FILEPOINTER f;
+	    int ret, _buffered;
+	    short value;
+	    union {
+		unsigned char buffer[2];
+		short shortVal;
+	    } u;
+
+	    f = __FILEVal(fp);
+	    _buffered = (__INST(buffered) == true);
+	    if (_buffered) {
+		__READING__(f)
+	    }
+	    __READBYTES__(ret, f, u.buffer, 2, _buffered, __INST(handleType));
+
+	    if (ret == 2) {
+		if (__isSmallInteger(__INST(position))) {
+		    INT np = __intVal(__INST(position)) + 2;
+		    OBJ t;
+
+		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
+		} else {
+		    __INST(position) = nil; /* i.e. do not know */
+		}
+		if (msbFlag == true) {
+#if defined(__MSBFIRST__)
+		    value = u.shortVal;
+#else
+		    value = ((u.buffer[0] & 0xFF) << 8) | (u.buffer[1] & 0xFF);
+#endif
+		} else {
+#if defined(__LSBFIRST__)
+		    value = u.shortVal;
+#else
+		    value = ((u.buffer[1] & 0xFF) << 8) | (u.buffer[0] & 0xFF);
+#endif
+		}
+		RETURN (__mkSmallInteger(value));
+	    }
+
+	    if (ret < 0) {
+		__INST(position) = nil; /* i.e. do not know */
+		error = __mkSmallInteger(__threadErrno);
+	    } else /* ret == 0 */ {
+		__INST(hitEOF) = true;
+	    }
+	}
+    }
+%}.
+    hitEOF ifTrue:[^ self pastEndRead].
+    handle isNil ifTrue:[^ self errorNotOpen].
+    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
+    lastErrorNumber := error.
+    ^ self readError:error.
 !
 
 nextInt32MSB:msbFlag
@@ -3938,8 +4006,8 @@
     ^ self readError:error.
 !
 
-nextInt16MSB:msbFlag
-    "Read two bytes and return the value as a 16-bit signed Integer.
+nextUnsignedInt16MSB:msbFlag
+    "Read two bytes and return the value as a 16-bit unsigned Integer.
      If msbFlag is true, value is read with most-significant byte first,
      otherwise least-significant byte comes first.
      A nil is returned if EOF is reached (also when EOF is hit after the first byte).
@@ -3960,10 +4028,10 @@
 	) {
 	    FILEPOINTER f;
 	    int ret, _buffered;
-	    short value;
+	    unsigned int value;
 	    union {
 		unsigned char buffer[2];
-		short shortVal;
+		unsigned short shortVal;
 	    } u;
 
 	    f = __FILEVal(fp);
@@ -3986,13 +4054,13 @@
 #if defined(__MSBFIRST__)
 		    value = u.shortVal;
 #else
-		    value = ((u.buffer[0] & 0xFF) << 8) | (u.buffer[1] & 0xFF);
+		    value = (u.buffer[0] << 8) | u.buffer[1];
 #endif
 		} else {
 #if defined(__LSBFIRST__)
 		    value = u.shortVal;
 #else
-		    value = ((u.buffer[1] & 0xFF) << 8) | (u.buffer[0] & 0xFF);
+		    value = (u.buffer[1] << 8) | u.buffer[0];
 #endif
 		}
 		RETURN (__mkSmallInteger(value));
@@ -4105,82 +4173,6 @@
     ^ self readError:error.
 !
 
-nextUnsignedInt16MSB:msbFlag
-    "Read two bytes and return the value as a 16-bit unsigned Integer.
-     If msbFlag is true, value is read with most-significant byte first,
-     otherwise least-significant byte comes first.
-     A nil is returned if EOF is reached (also when EOF is hit after the first byte).
-     Works in both binary and text modes."
-
-    |error|
-%{
-    OBJ fp;
-
-    __INST(lastErrorNumber) = nil;
-    if ((__INST(handleType) == nil)
-     || (__INST(handleType) == @symbol(filePointer))
-     || (__INST(handleType) == @symbol(socketFilePointer))
-     || (__INST(handleType) == @symbol(socketHandle))
-     || (__INST(handleType) == @symbol(pipeFilePointer))) {
-	if (((fp = __INST(handle)) != nil)
-	    && (__INST(mode) != @symbol(writeonly))
-	) {
-	    FILEPOINTER f;
-	    int ret, _buffered;
-	    unsigned int value;
-	    union {
-		unsigned char buffer[2];
-		unsigned short shortVal;
-	    } u;
-
-	    f = __FILEVal(fp);
-	    _buffered = (__INST(buffered) == true);
-	    if (_buffered) {
-		__READING__(f)
-	    }
-	    __READBYTES__(ret, f, u.buffer, 2, _buffered, __INST(handleType));
-
-	    if (ret == 2) {
-		if (__isSmallInteger(__INST(position))) {
-		    INT np = __intVal(__INST(position)) + 2;
-		    OBJ t;
-
-		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
-		} else {
-		    __INST(position) = nil; /* i.e. do not know */
-		}
-		if (msbFlag == true) {
-#if defined(__MSBFIRST__)
-		    value = u.shortVal;
-#else
-		    value = (u.buffer[0] << 8) | u.buffer[1];
-#endif
-		} else {
-#if defined(__LSBFIRST__)
-		    value = u.shortVal;
-#else
-		    value = (u.buffer[1] << 8) | u.buffer[0];
-#endif
-		}
-		RETURN (__mkSmallInteger(value));
-	    }
-
-	    if (ret < 0) {
-		__INST(position) = nil; /* i.e. do not know */
-		error = __mkSmallInteger(__threadErrno);
-	    } else /* ret == 0 */ {
-		__INST(hitEOF) = true;
-	    }
-	}
-    }
-%}.
-    hitEOF ifTrue:[^ self pastEndRead].
-    handle isNil ifTrue:[^ self errorNotOpen].
-    (mode == #writeonly) ifTrue:[^ self errorWriteOnly].
-    lastErrorNumber := error.
-    ^ self readError:error.
-!
-
 nextWord
     <resource: #obsolete>
     "in text-mode:
@@ -4260,6 +4252,89 @@
     self writeError:error.
 !
 
+nextPutInt16:anIntegerOrCharacter MSB:msbFlag
+    "Write the argument, anIntegerOrCharacter as a short (two bytes). If msbFlag is
+     true, data is written most-significant byte first; otherwise least
+     first.
+     Works in both binary and text modes."
+
+    |error|
+%{
+    int num;
+    union {
+	char bytes[2];
+	short shortVal;
+    } u;
+    OBJ fp;
+
+    __INST(lastErrorNumber) = nil;
+    if ((__INST(handleType) == nil)
+     || (__INST(handleType) == @symbol(filePointer))
+     || (__INST(handleType) == @symbol(socketFilePointer))
+     || (__INST(handleType) == @symbol(socketHandle))
+     || (__INST(handleType) == @symbol(pipeFilePointer))) {
+	if (((fp = __INST(handle)) != nil)
+	 && (__INST(mode) != @symbol(readonly))
+	) {
+	    FILEPOINTER f = __FILEVal(fp);
+	    int _buffered = (__INST(buffered) == true);
+	    int cnt;
+
+	    if (__isSmallInteger(anIntegerOrCharacter)) {
+		num = __intVal(anIntegerOrCharacter);
+	    } else if (__isCharacter(anIntegerOrCharacter)) {
+		num = __smallIntegerVal(__characterVal(anIntegerOrCharacter));
+	    } else
+		goto out;
+
+	    if (msbFlag == true) {
+#if defined(__MSBFIRST__)
+		u.shortVal = num;
+#else
+		u.bytes[0] = (num >> 8) & 0xFF;
+		u.bytes[1] = num & 0xFF;
+#endif
+	    } else {
+#if defined(__LSBFIRST__)
+		u.shortVal = num;
+#else
+		u.bytes[1] = (num >> 8) & 0xFF;
+		u.bytes[0] = num & 0xFF;
+#endif
+	    }
+
+	    if (_buffered) {
+		__WRITING__(f)
+	    }
+	    __WRITEBYTES__(cnt, f, u.bytes, 2, _buffered, __INST(handleType));
+
+	    if (cnt == 2) {
+		if (__isSmallInteger(__INST(position))) {
+		    INT np = __intVal(__INST(position)) + 2;
+		    OBJ t;
+
+		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
+		} else {
+		    __INST(position) = nil; /* i.e. do not know */
+		}
+		RETURN ( self );
+	    }
+	    __INST(position) = nil; /* i.e. do not know */
+	    error = __mkSmallInteger(__threadErrno);
+	}
+    }
+out:;
+%}.
+    error notNil ifTrue:[
+	lastErrorNumber := error.
+	self writeError:error.
+	^ self
+    ].
+    handle isNil ifTrue:[self errorNotOpen. ^ self].
+    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
+    self argumentMustBeInteger
+!
+
 nextPutInt32:aNumber MSB:msbFlag
     "Write the argument, aNumber as a long (four bytes). If msbFlag is
      true, data is written most-significant byte first; otherwise least
@@ -4363,89 +4438,6 @@
 	^ super nextPutLong:aNumber MSB:msbFlag
     ].
     self argumentMustBeInteger
-!
-
-nextPutInt16:anIntegerOrCharacter MSB:msbFlag
-    "Write the argument, anIntegerOrCharacter as a short (two bytes). If msbFlag is
-     true, data is written most-significant byte first; otherwise least
-     first.
-     Works in both binary and text modes."
-
-    |error|
-%{
-    int num;
-    union {
-	char bytes[2];
-	short shortVal;
-    } u;
-    OBJ fp;
-
-    __INST(lastErrorNumber) = nil;
-    if ((__INST(handleType) == nil)
-     || (__INST(handleType) == @symbol(filePointer))
-     || (__INST(handleType) == @symbol(socketFilePointer))
-     || (__INST(handleType) == @symbol(socketHandle))
-     || (__INST(handleType) == @symbol(pipeFilePointer))) {
-	if (((fp = __INST(handle)) != nil)
-	 && (__INST(mode) != @symbol(readonly))
-	) {
-	    FILEPOINTER f = __FILEVal(fp);
-	    int _buffered = (__INST(buffered) == true);
-	    int cnt;
-
-	    if (__isSmallInteger(anIntegerOrCharacter)) {
-		num = __intVal(anIntegerOrCharacter);
-	    } else if (__isCharacter(anIntegerOrCharacter)) {
-		num = __smallIntegerVal(__characterVal(anIntegerOrCharacter));
-	    } else
-		goto out;
-
-	    if (msbFlag == true) {
-#if defined(__MSBFIRST__)
-		u.shortVal = num;
-#else
-		u.bytes[0] = (num >> 8) & 0xFF;
-		u.bytes[1] = num & 0xFF;
-#endif
-	    } else {
-#if defined(__LSBFIRST__)
-		u.shortVal = num;
-#else
-		u.bytes[1] = (num >> 8) & 0xFF;
-		u.bytes[0] = num & 0xFF;
-#endif
-	    }
-
-	    if (_buffered) {
-		__WRITING__(f)
-	    }
-	    __WRITEBYTES__(cnt, f, u.bytes, 2, _buffered, __INST(handleType));
-
-	    if (cnt == 2) {
-		if (__isSmallInteger(__INST(position))) {
-		    INT np = __intVal(__INST(position)) + 2;
-		    OBJ t;
-
-		    t = __MKINT(np); __INST(position) = t; __STORE(self, t);
-		} else {
-		    __INST(position) = nil; /* i.e. do not know */
-		}
-		RETURN ( self );
-	    }
-	    __INST(position) = nil; /* i.e. do not know */
-	    error = __mkSmallInteger(__threadErrno);
-	}
-    }
-out:;
-%}.
-    error notNil ifTrue:[
-	lastErrorNumber := error.
-	self writeError:error.
-	^ self
-    ].
-    handle isNil ifTrue:[self errorNotOpen. ^ self].
-    (mode == #readonly) ifTrue:[self errorReadOnly. ^ self].
-    self argumentMustBeInteger
 ! !
 
 !ExternalStream methodsFor:'positioning'!