comments & fixes
authorClaus Gittinger <cg@exept.de>
Sat, 11 Jan 1997 16:51:18 +0100
changeset 474 61cb199537b5
parent 473 cb46b0ba9934
child 475 830e57ccd443
comments & fixes
FilteringLineStream.st
FilteringStream.st
--- a/FilteringLineStream.st	Sat Jan 11 15:22:27 1997 +0100
+++ b/FilteringLineStream.st	Sat Jan 11 16:51:18 1997 +0100
@@ -13,10 +13,10 @@
 
 
 FilteringStream subclass:#FilteringLineStream
-	instanceVariableNames:'lineBuffer'
+	instanceVariableNames:'lineBuffer lineFilter'
 	classVariableNames:''
 	poolDictionaries:''
-	category:'Streams'
+	category:'Streams-Misc'
 !
 
 !FilteringLineStream class methodsFor:'documentation'!
@@ -64,62 +64,82 @@
 
 examples
 "
+  pushing the contents of a stream onto another stream
+  (here, the Transcript) without a need to read everyting into a
+  buffer or to reinvent the read-loop:
+                                                                [exBegin]
+    |in pusher|
+
+    in := 'Makefile' asFilename readStream.
+    pusher := FilteringLineStream readingFrom:in writingTo:Transcript.
+    pusher filterUpToEnd
+                                                                [exEnd]
+
+
   filter all comments (starting with '#') from a Makefile:
-                                                        [exBegin]
+                                                                [exBegin]
     |in filter|
 
     in := 'Makefile' asFilename readStream.
 
-    filter := FilteringLineStream basicNew.
-    filter inputStream:in.
+    filter := FilteringLineStream readingFrom:in.
     filter filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]].
 
     filter outputStream:Transcript.
 
     filter filterUpToEnd
-                                                        [exEnd]
+                                                                [exEnd]
 
 
   the inverse - remove all comments from the Makefile:
-                                                        [exBegin]
+                                                                [exBegin]
     |in filter|
 
     in := 'Makefile' asFilename readStream.
 
-    filter := FilteringLineStream basicNew.
-    filter inputStream:in.
+    filter := FilteringLineStream readingFrom:in.
     filter filter:[:line | (line startsWith:'#') ifTrue:[nil] ifFalse:[line]].
 
     filter outputStream:Transcript.
 
     filter filterUpToEnd
-                                                        [exEnd]
+                                                                [exEnd]
 
 
   feed a second filter from the first filters output;
   (the remains are all lines starting with '#' and ending with '-'):
-                                                        [exBegin]
+                                                                [exBegin]
     |in filter1 filter2|
 
     in := 'Makefile' asFilename readStream.
 
-    filter1 := FilteringLineStream basicNew.
-
-    filter1 inputStream:in.
+    filter1 := FilteringLineStream readingFrom:in.
     filter1 filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]].
 
-    filter2 := FilteringLineStream basicNew.
+    filter2 := FilteringLineStream readingFrom:filter1.
     filter2 filter:[:line | (line endsWith:'-') ifTrue:[line] ifFalse:[nil]].
 
-    filter1 outputStream:filter2.
     filter2 outputStream:Transcript.
 
-    filter1 filterUpToEnd
-                                                        [exEnd]
+    filter2 filterUpToEnd
+                                                                [exEnd]
 "
 
 ! !
 
+!FilteringLineStream methodsFor:'access - pull-reading'!
+
+filterUpToEnd
+    "pull input from inputStream up to the end,
+     push it filtered into the outputStream."
+
+    [inputStream atEnd] whileFalse:[
+        self nextPutLine:(inputStream nextLine)
+    ].
+
+    "Created: 11.1.1997 / 16:09:05 / cg"
+! !
+
 !FilteringLineStream methodsFor:'access - push-writing'!
 
 cr
@@ -127,17 +147,23 @@
 
     |output|
 
-    lineBuffer isNil ifTrue:[lineBuffer := ''].
+    lineFilter isNil ifTrue:[
+        lineBuffer notNil ifTrue:[
+            outputStream nextPutAll:lineBuffer
+        ].
+        outputStream cr
+    ] ifFalse:[
+        lineBuffer isNil ifTrue:[lineBuffer := ''].
 
-    output := filter value:lineBuffer.
-    output notNil ifTrue:[
-        outputStream nextPutAll:output.
-        outputStream cr.
+        output := lineFilter value:lineBuffer.
+        output notNil ifTrue:[
+            outputStream nextPutAll:output.
+            outputStream cr.
+        ]
     ].
     lineBuffer := ''.
 
-    "Created: 2.7.1996 / 20:55:53 / cg"
-    "Modified: 2.7.1996 / 21:03:07 / cg"
+    "Modified: 11.1.1997 / 16:32:40 / cg"
 !
 
 nextPut:something
@@ -157,15 +183,35 @@
 nextPutAll:something
     "collect a line and push it when a cr arrives"
 
-    lineBuffer isNil ifTrue:[lineBuffer := ''].
-    lineBuffer := lineBuffer , something.
+    lineBuffer isNil ifTrue:[
+        lineBuffer := something
+    ] ifFalse:[
+        lineBuffer := lineBuffer , something.
+    ]
+
+    "Modified: 11.1.1997 / 16:31:28 / cg"
+! !
+
+!FilteringLineStream methodsFor:'accessing'!
 
-    "Created: 2.7.1996 / 20:54:45 / cg"
-    "Modified: 2.7.1996 / 21:03:30 / cg"
+filter
+    "return the filter"
+
+    ^ lineFilter
+
+    "Created: 11.1.1997 / 16:13:15 / cg"
+!
+
+filter:aBlock
+    "set the filter"
+
+    lineFilter := aBlock
+
+    "Created: 11.1.1997 / 16:13:25 / cg"
 ! !
 
 !FilteringLineStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/FilteringLineStream.st,v 1.3 1997-01-11 14:22:27 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/FilteringLineStream.st,v 1.4 1997-01-11 15:51:18 cg Exp $'
 ! !
--- a/FilteringStream.st	Sat Jan 11 15:22:27 1997 +0100
+++ b/FilteringStream.st	Sat Jan 11 16:51:18 1997 +0100
@@ -12,10 +12,10 @@
 
 
 Stream subclass:#FilteringStream
-	instanceVariableNames:'inputStream outputStream filter unbound'
+	instanceVariableNames:'inputStream outputStream filter'
 	classVariableNames:''
 	poolDictionaries:''
-	category:'Streams'
+	category:'Streams-Misc'
 !
 
 !FilteringStream class methodsFor:'documentation'!
@@ -62,14 +62,26 @@
 
 examples
 "
+  pushing the contents of a stream onto another stream
+  (here, the Transcript) without a need to read everyting into a
+  buffer or to reinvent the read-loop:
+  (notice, a FilteringLineStream does this with less overhead)
+                                                                [exBegin]
+    |in pusher|
+
+    in := 'Makefile' asFilename readStream.
+    pusher := FilteringStream readingFrom:in writingTo:Transcript.
+    pusher filterUpToEnd
+                                                                [exEnd]
+
+
   filter random numbers:
                                                                 [exBegin]
     |in filter|
 
     in := Random new.
 
-    filter := FilteringLineStream basicNew.
-    filter inputStream:in.
+    filter := FilteringLineStream readingFrom:in.
     filter filter:[:num | ((num >= 0.5) and:[num <= 0.6]) ifTrue:[num] ifFalse:[nil]].
 
     20 timesRepeat:[
@@ -117,18 +129,63 @@
 
 ! !
 
+!FilteringStream class methodsFor:'instance creation'!
+
+new
+    "create and return a new filteringStream.
+     The resulting stream must be connected to some other stream,
+     before being used"
+
+    ^ self basicNew initialize.
+
+    "Created: 11.1.1997 / 15:31:30 / cg"
+    "Modified: 11.1.1997 / 15:33:13 / cg"
+!
+
+readingFrom:aReadStream
+    "create and return a new filteringStream, which reads from
+     another stream"
+
+    ^ self basicNew initialize inputStream:aReadStream.
+
+    "Created: 11.1.1997 / 15:32:15 / cg"
+!
+
+readingFrom:aReadStream writingTo:aWriteStream
+    "create and return a new filteringStream, which reads from
+     aReadStream and writes to aWriteStream."
+
+    |newStream|
+
+    newStream := self basicNew initialize.
+    newStream inputStream:aReadStream.
+    newStream outputStream:aWriteStream.
+    ^ newStream
+
+    "Created: 11.1.1997 / 15:32:28 / cg"
+!
+
+writingTo:aWriteStream
+    "create and return a new filteringStream, which writes to
+     another stream"
+
+    ^ self basicNew initialize outputStream:aWriteStream.
+
+    "Created: 11.1.1997 / 15:32:36 / cg"
+! !
+
 !FilteringStream methodsFor:'access - pull-reading'!
 
 filterUpToEnd
     "pull input from inputStream up to the end,
-     push it filtered into the outputStream"
+     push it filtered into the outputStream."
 
     [inputStream atEnd] whileFalse:[
         self nextPut:(inputStream next)
     ].
 
-    "Modified: 2.7.1996 / 21:02:49 / cg"
     "Created: 2.7.1996 / 21:06:42 / cg"
+    "Modified: 11.1.1997 / 16:08:35 / cg"
 !
 
 next
@@ -140,6 +197,10 @@
     [inputStream atEnd] whileFalse:[
         "/ get an element
         input := inputStream next.
+        filter isNil ifTrue:[
+            ^ input
+        ].
+
         "/ filter it - this may return nil, to eat it
         output := filter value:input.
         output notNil ifTrue:[
@@ -150,7 +211,7 @@
     ^ nil
 
     "Created: 2.7.1996 / 21:09:58 / cg"
-    "Modified: 10.1.1997 / 20:23:36 / cg"
+    "Modified: 11.1.1997 / 16:12:22 / cg"
 ! !
 
 !FilteringStream methodsFor:'access - push-writing'!
@@ -161,13 +222,16 @@
     |output|
 
     "/ filter it
-    output := filter value:something.
-    output notNil ifTrue:[
-        outputStream nextPut:output
+    filter isNil ifTrue:[
+        outputStream nextPut:something
+    ] ifFalse:[
+        output := filter value:something.
+        output notNil ifTrue:[
+            outputStream nextPut:output
+        ]
     ]
 
-    "Created: 2.7.1996 / 21:07:58 / cg"
-    "Modified: 10.1.1997 / 20:23:56 / cg"
+    "Modified: 11.1.1997 / 16:12:52 / cg"
 ! !
 
 !FilteringStream methodsFor:'accessing'!
@@ -224,24 +288,18 @@
 
     "Modified: 2.7.1996 / 21:03:52 / cg"
     "Created: 2.7.1996 / 21:06:42 / cg"
-!
+! !
 
-unbound
-    "return the unbound flag"
-
-    ^ unbound
+!FilteringStream methodsFor:'misc'!
 
-    "Created: 2.7.1996 / 21:25:40 / cg"
-    "Modified: 10.1.1997 / 20:21:53 / cg"
-!
+close
+    "when I am closed, close my input - if any"
 
-unbound:something
-    "set the unbound flag"
+    inputStream notNil ifTrue:[
+        inputStream close
+    ]
 
-    unbound := something.
-
-    "Created: 2.7.1996 / 21:25:40 / cg"
-    "Modified: 10.1.1997 / 20:21:59 / cg"
+    "Created: 11.1.1997 / 15:27:17 / cg"
 ! !
 
 !FilteringStream methodsFor:'queries'!
@@ -249,15 +307,22 @@
 atEnd
     "return true, if the receiver stream is at the end"
 
-    unbound ifTrue:[^ false].
     ^ inputStream atEnd
 
-    "Created: 2.7.1996 / 21:19:51 / cg"
-    "Modified: 10.1.1997 / 20:22:20 / cg"
+    "Modified: 11.1.1997 / 16:26:10 / cg"
+!
+
+contentsSpecies
+    "return the kind of collection I should return when asked
+     for multiple elements."
+
+    ^ inputStream contentsSpecies
+
+    "Created: 11.1.1997 / 16:23:22 / cg"
 ! !
 
 !FilteringStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.2 1997-01-10 20:51:24 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/FilteringStream.st,v 1.3 1997-01-11 15:50:52 cg Exp $'
 ! !