Stream.st
changeset 62 e1b4369c61fb
parent 50 71f3b9444905
child 70 73055652dd21
--- a/Stream.st	Fri Feb 25 14:03:58 1994 +0100
+++ b/Stream.st	Fri Feb 25 14:05:47 1994 +0100
@@ -22,7 +22,7 @@
 COPYRIGHT (c) 1989 by Claus Gittinger
               All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.5 1994-02-05 12:24:58 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Stream.st,v 1.6 1994-02-25 13:05:37 claus Exp $
 '!
 
 !Stream class methodsFor:'instance creation'!
@@ -36,13 +36,13 @@
 !Stream methodsFor:'accessing'!
 
 contents
-    "return the contents of the stream
+    "return the entire contents of the stream
      - we do not know here how to do it, it must be redefined in subclass"
 
     ^ self subclassResponsibility
 ! !
 
-!Stream methodsFor:'accessing-reading'!
+!Stream methodsFor:'reading'!
 
 next
     "return the next element of the stream
@@ -52,55 +52,92 @@
 !
 
 next:count
-    "return the next count elements of the stream as a Collection"
+    "return the next count elements of the stream as a Collection."
 
-    |array|
+    |coll|
 
-    array := Array new:count.
-    1 to:count do: [:index |
-        array at:index put:(self next)
+    coll := self contentsSpecies new:count.
+    1 to:count do:[:index |
+        coll at:index put:(self next)
     ].
-    ^ array
-!
+    ^ coll
 
-nextPeek
-    "advance to next element and return the peeked element"
-
-    self next.
-    ^ self peek
+    "(ReadStream on:#(1 2 3 4 5)) next:3"
+    "(ReadStream on:'hello') next:3"
 !
 
 nextMatchFor:anObject
-    "read from the receiver, searching for the argument, anObject.
-     if the end is reached, return nil; otherwise return the argument, 
-     anObject. The next read operation will return the element after anObject."
+    "read an element from the receiver, return true if it was equal to
+     the argument, anObject"
+
+    ^ (self next = anObject)
+
+    "
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     s nextMatchFor:2
+    "
+    "
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     s nextMatchFor:2.
+     s nextMatchFor:2
+    "
+!
+
+skip:count 
+    "skip count objects, return the receiver"
+
+    "dont know how to unread ..."
+    count < 0 ifTrue:[
+        ^ self error:'stream is not positionable'
+    ].
+    count timesRepeat:self next
+
+    "
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     s skip:4.
+     s next
+    "
+!
+
+skipFor:anObject
+    "skip all objects up-to and including anObject; return the element after anObject."
+
+    (self skipThrough:anObject) notNil ifTrue:[
+        ^ self next
+    ].
+    ^ nil
+
+    "
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     s skipFor:4.
+    "
+!
+
+skipThrough:anObject
+    "skip all objects up-to and including anObject. 
+     The next read operation will return the element after anObject."
 
     |nextElement|
 
     [self atEnd] whileFalse:[
         nextElement := self next.
-        (nextElement == anObject) ifTrue: [ ^ nextElement ]
+        (nextElement = anObject) ifTrue: [ ^ self ]
     ].
     ^ nil
-!
 
-skipFor:anObject
-    "skip all objects up-to and including anObject; return the element after anObject."
-
-    (self nextMatchFor:anObject) notNil ifTrue:[
-        ^ self next
-    ].
-    ^ nil
-!
-
-skipThrough:anObject
-    "skip all objects up-to and including anObject. The next read operation
-     will return the element after anObject."
-
-    ^ self nextMatchFor:anObject
+    "
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8).
+     s skipThrough:4.
+     s next
+    "
 ! !
 
-!Stream methodsFor:'accessing-writing'!
+!Stream methodsFor:'writing'!
 
 nextPut:anObject
     "put the argument, anObject onto the receiver
@@ -114,6 +151,16 @@
 
     count timesRepeat:[self nextPut:anObject].
     ^ anObject
+
+    "
+     |s|
+
+     s := WriteStream on:#().
+     s nextPut:1.
+     s next:5 put:2.
+     s nextPut:3.
+     s contents
+    "
 !
 
 nextPutAll:aCollection
@@ -123,6 +170,25 @@
         self nextPut:element
     ].
     ^ aCollection
+
+    "
+     |s|
+
+     s := WriteStream on:#().
+     s nextPutAll:(1 to:5).
+     s nextPutAll:#('one' 'two' 'three').
+     s contents
+    "
+    "
+     |s|
+
+     s := WriteStream on:(String new).
+     s nextPutAll:($a to:$f).
+     s nextPutAll:'one '; 
+       nextPutAll:'two ';
+       nextPutAll:'three'.
+     s contents
+    "
 !
 
 nextPutAll:aCollection startingAt:first to:last
@@ -133,10 +199,18 @@
         self nextPut:element
     ].
     ^ aCollection
+
+    "
+     |s|
+
+     s := WriteStream on:#().
+     s nextPutAll:#('one' 'two' 'three' 'four' 'five') startingAt:2 to:4.
+     s contents
+    "
 !
 
 endEntry
-    "for compatibility with Transcript"
+    "ignored here - for compatibility with Transcript"
 
     ^ self
 !
@@ -152,16 +226,54 @@
 showCr:aString
     "put all elements of the argument, aString onto the receiver;
      and append a newline.
-     for compatibility with TextCollectors"
+     For compatibility with TextCollectors"
 
     self show:aString.
     self cr
+!
+
+cr
+    "append a carriage-return to the stream"
+
+    self nextPut:(Character cr)
+!
+
+tab
+    "append a tab-character to the stream"
+
+    self nextPut:(Character tab)
+!
+
+crTab
+    "append a carriage-return followed by a tab to the stream"
+
+    self nextPut:(Character cr).
+    self nextPut:(Character tab)
+!
+
+space
+    "append a space character to the receiver-stream"
+
+    self nextPut:(Character space)
+!
+
+spaces:count
+    "append count space-characters to the receiver-stream"
+
+    self next:count put:(Character space)
+!
+
+ff
+    "append a form-feed (new-pagee) to the receiver-stream"
+
+    self nextPut:(Character ff)
 ! !
 
 !Stream methodsFor:'closing'!
 
 close
-    "close the stream - nothing done here"
+    "close the stream - nothing done here.
+     Added for compatibility with external streams."
 
     ^ self
 ! !
@@ -178,12 +290,32 @@
 !Stream methodsFor:'enumerating'!
 
 do:aBlock
-    "evaluate the argument, aBlock for every element up to the end of the
-     stream"
+    "evaluate the argument, aBlock for all remaining elements,
+     up to the end of the stream"
 
     [self atEnd] whileFalse:[
         aBlock value:(self next)
     ]
+
+    "
+     |s|
+     s := ReadStream on:#(1 2 3 4 5 6 7 8 9).
+     s next.
+     s next.
+     s do:[:element | Transcript showCr:element]
+    "
+! !
+
+!Stream methodsFor:'private'!
+
+contentsSpecies
+    "this should return the class to be returned an instance
+     of in the contents method. It is redefinable in subclasses"
+
+    "return Array here - since the abstract Stream has no idea
+     of the underlying collection class"
+
+    ^ Array
 ! !
 
 !Stream methodsFor:'queries'!