eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'FilteringLineStream':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: FilteringLineStream


Inheritance:

   Object
   |
   +--Stream
      |
      +--PeekableStream
         |
         +--FilteringStream
            |
            +--FilteringLineStream

Package:
stx:libbasic2
Category:
Streams-Misc
Version:
rev: 1.9 date: 2014/11/08 10:20:41
user: cg
file: FilteringLineStream.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Claus Gittinger

Description:


A FilteringLineStream is much like a FilteringStream, but
processes its input line-wise.

A FilteringLineStream can be connected to some input
(from which characters are read via the ReadStream protocol),
and/or to some output (to which characters are written via
the WriteStream protocol.

The FilteringLineStream itself performs some filtering/processing
on the elements as they arrive, optionally suppressing filtering
(i.e. removing) some elements.


Related information:

    FilteringStream
    ReadStream
    WriteStream

Instance protocol:

access - pull-reading
o  filterUpToEnd
pull input from inputStream up to the end,
push it filtered into the outputStream.

access - push-writing
o  cr
finish a line, push it filtered into the outputStream

o  nextPut: something
collect a line and push it when a cr arrives

o  nextPutAll: something
collect a line and push it when a cr arrives

accessing
o  filter
return the filter

o  filter: aBlock
set the filter

o  isBinary

o  size
mhmh - please use isEmpty; anything else is unknown


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:
  |in pusher|

  in := 'Makefile' asFilename readStream.
  pusher := FilteringLineStream readingFrom:in writingTo:Transcript.
  pusher filterUpToEnd
collect a command's stdout and stderr, colorizing error output in red and normal output in green:
  |in out stdoutColorizer errColorizer|

  out := Transcript.
  in := OperatingSystem 
          executeCommand:'bc'
          inputFrom:
'4+5
bla bla foo
4+5
bar
' readStream
          outputTo:((FilteringLineStream writingTo:out) filter:[:line | line colorizeAllWith:Color green darkened])
          errorTo:((FilteringLineStream writingTo:out) filter:[:line | line colorizeAllWith:Color red]).
filter through an ActorStream:
  |lineNr in pusher actorPerLine|

  lineNr := 0.

  actorPerLine := ActorStream new.
  actorPerLine nextPutLineBlock:[:line | lineNr := lineNr + 1. Transcript show:lineNr; showCR:line].

  in := 'Makefile' asFilename readStream.
  pusher := FilteringLineStream readingFrom:in writingTo:actorPerLine.
  pusher filterUpToEnd
filter all comments (starting with '#') from a Makefile:
  |in filter|

  in := 'Makefile' asFilename readStream.

  filter := FilteringLineStream readingFrom:in.
  filter filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]].

  filter outputStream:Transcript.

  filter filterUpToEnd
the inverse - remove all comments from the Makefile:
  |in filter|

  in := 'Makefile' asFilename readStream.

  filter := FilteringLineStream readingFrom:in.
  filter filter:[:line | (line startsWith:'#') ifTrue:[nil] ifFalse:[line]].

  filter outputStream:Transcript.

  filter filterUpToEnd
feed a second filter from the first filters output; (the remains are all lines starting with '#' and ending with '-'):
  |in filter1 filter2|

  in := 'Makefile' asFilename readStream.

  filter1 := FilteringLineStream readingFrom:in.
  filter1 filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]].

  filter2 := FilteringLineStream readingFrom:filter1.
  filter2 filter:[:line | (line endsWith:'-') ifTrue:[line] ifFalse:[nil]].

  filter2 outputStream:Transcript.

  filter2 filterUpToEnd


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Thu, 02 Dec 2021 03:24:15 GMT