|
Class: PeekableStream
Object
|
+--Stream
|
+--PeekableStream
|
+--ActorStream
|
+--CollectingReadStream
|
+--EncodedStream
|
+--FilteringStream
|
+--JSONReader
|
+--MultiReadStream
|
+--PositionableStream
|
+--ZipArchive::AbstractZipStream
- Package:
- stx:libbasic
- Category:
- Streams
- Version:
- rev:
1.68
date: 2018/06/11 12:34:12
- user: stefan
- file: PeekableStream.st directory: libbasic
- module: stx stc-classLibrary: libbasic
- Author:
- Claus Gittinger
abstract superclass for all Stream which support read-ahead
(i.e. peeking) of one element.
Concrete subclasses must implement a peek method.
[caveat:]
Basing capabilities like readability/writability/positionability/peekability on inheritance makes
the class hierarchy ugly and leads to strange and hard to teach redefinitions (aka. NonPositionableStream
below PositionableStream or ExternalReadStream under WriteStream)
Signal constants
-
currentFileInDirectoryQuerySignal
-
return the querySignal, which can be used to ask for the current directory
during a fileIn (that is the directory where the filed-in file resides),
and in a fileBrowsers doIt.
Using this, allows for the loaded code or doIts to ask for the fileBrowsers
current directory, by asking this querySignal (which is nice sometimes).
-
currentSourceContainerQuery
-
return the querySignal, which can be used to ask for the current source container filename
during a fileIn
Using this, allows for the loaded code to remember the classes file name.
initialization
-
initialize
-
setup the signal used to handle errors during fileIn
usage example(s):
queries
-
currentFileInDirectory
-
during a fileIn (if a script), the script can ask for the current directory
-
currentSourceContainer
-
during a fileIn (if a script), the script can ask for the current filename
testing
-
isAbstract
-
accessing
-
contents
-
(comment from inherited method)
return the entire contents of the stream.
For a readStream, that is the rest (i.e. upToEnd),
for a writeStream, that is the collected data. As we do not know here,
what we are, this is the responsibility of a subclass...
chunk input/output
-
nextChunk
-
return the next chunk, i.e. all characters up to the next
exclamation mark. Within the chunk, exclamation marks have to be doubled,
they are undoubled here.
Except for primitive code, in which doubling is not needed (allowed).
This exception was added to make it easier to edit primitive code with
external editors. However, this means, that other Smalltalks cannot always
read chunks containing primitive code
- but that doesn't really matter, since C-primitives are an ST/X feature anyway.
usage example(s):
match primitive only at beginning of line
(ExternalStream>>#nextChunk did this, although stc allows primitive to start anywhere)
|
usage example(s):
now map CR LF to LF (ExternalStream>>#nextChunk did this)
|
usage example(s):
map all CR in a CR only file to NL (ExternalStream>>#nextChunk did this)
|
fileIn
-
fileIn
-
file in from the receiver, i.e. read chunks and evaluate them -
return the value of the last chunk.
-
fileInBinary
-
file in from the receiver, i.e. read binary stored classes and/or objects.
Return the last object.
positioning
-
skipAny: skipCollection
-
skip all characters included in the argument-set.
returns the next peeked element or nil, if the end-of-stream was reached.
-
skipSeparators
-
skip all whitespace;
return the next peeked element or nil, if the end-of-stream was reached.
The stream's elements should be characters.
Notice: compare this method to skipSpaces
-
skipSeparatorsExceptCR
-
skip all whitespace except carriage return; returns the
next peeked element or nil, if the end-of-stream was reached.
The streams elements should be characters.
Notice: compare this method to skipSpaces and skipSeparators
-
skipSpaces
-
skip all spaces; returns the next peeked element or
nil, if the end-of-stream was reached.
The streams elements should be characters.
Notice: this one skips only spaces (i.e. no cr, tabs etc)
usually, skipSeparators is what you want.
private fileIn
-
basicFileInNotifying: someone passChunk: passChunk
-
central method to file in from the receiver, i.e. read chunks and evaluate them -
return the value of the last chunk.
Someone (which is usually some codeView) is notified of errors.
-
fileInNextChunkNotifying: someone
-
read next chunk, evaluate it and return the result;
someone (which is usually some codeView) is notified of errors.
Filein is done as follows:
read a chunk
if it started with an excla, evaluate it, and let the resulting object
fileIn more chunks.
This is a nice trick, since the methodsFor: expression evaluates to
a ClassCategoryReader which reads and compiles chunks for its class.
However, other than methodsFor expressions are possible - you can
(in theory) create readers for any syntax.
-
fileInNextChunkNotifying: someone passChunk: passChunk
-
read next chunk, evaluate it and return the result;
someone (which is usually some codeView) is notified of errors.
Filein is done as follows:
read a chunk
if it started with an excla, evaluate it, and let the resulting object
fileIn more chunks.
This is a nice trick, since the methodsFor: expression evaluates to
a ClassCategoryReader which reads and compiles chunks for its class.
However, other than methodsFor expressions are possible - you can
(in theory) create readers for any syntax.
-
fileInNextChunkNotifying: someone passChunk: passChunk silent: beSilent
-
read next chunk, evaluate it and return the result;
someone (which is usually some codeView) is notified of errors.
Filein is done as follows:
read a chunk
if it started with an excla, evaluate it, and let the resulting object
fileIn more chunks.
This is a nice trick, since the methodsFor: expression evaluates to
a ClassCategoryReader which reads and compiles chunks for its class.
However, other than methodsFor expressions are possible - you can
(in theory) create readers for any syntax.
The beSilent argument controls output to the transcript, if it's true or
false. If it's nil, output is controlled by the Smalltalk>>silenLoading setting.
-
fileInNotifying: notifiedLoader passChunk: passChunk
-
central method to file in from the receiver, i.e. read chunks and evaluate them -
return the value of the last chunk.
Someone (which is usually some codeView) is notified of errors.
-
fileInNotifying: notifiedLoader passChunk: passChunk inDirectory: aDirectory
-
central method to file in from the receiver, i.e. read chunks and evaluate them -
return the value of the last chunk.
Someone (which is usually some codeView) is notified of errors.
Add aDirectory to the search path for classes, while performing the fileIn.
-
fileInXMLNotifying: someone passChunk: passChunk
-
filein an XML source file (format as in campSmalltalk DTD)
reading
-
nextPeek
-
advance to next element and return the peeked element
-
nextPeekOrNil
-
advance to next element and return the peeked element
-
nextUpTo: anObject
-
read a collection of all objects up-to anObject and return these
elements, but excluding anObject.
The next read operation will return anObject.
If anObject is not encountered, all elements up to the end are read
and returned, and the stream is positioned at the end.
Compare this with #upTo: which positions behind anObject
-
peek
-
return the next element of the stream without advancing (i.e.
the following send of next will return this element again.)
- we do not know here how to do it, it must be redefined in subclass
** This method raises an error - it must be redefined in concrete classes **
-
peekFor: anObject
-
if the next-to-be-read object is equal to the argument, anObject, read it
and return true. Otherwise, leave the receiver unaffected and return false.
usage example(s):
#() readStream peekFor:nil
#() readStream signalAtEnd:true; peekFor:nil
#(nil) readStream peekFor:nil
'abc' readStream next; peekFor:$b.
'abc' readStream next; peekFor:$c.
|
-
peekOrNil
-
like #peek, this returns the next readAhead element, if available.
However, unlike #peek, this does not raise an atEnd-query signal - even
if handled. Instead, nil is returned immediately.
-
skipUntil: aBlock
-
skip all elements for which aBlock returns false.
Return true if more elements can be read, false if eof has been reached.
usage example(s):
#(1 2 3 4 5 6 7 8 9 10) readStream
skipUntil:[:el | el >= 5];
next
|
-
skipWhile: aBlock
-
skip all elements for which aBlock returns true. Return true if more elements can be read,
false if eof has been reached.
usage example(s):
#(1 2 3 4 5 6 7 8 9 10) readStream
skipWhile:[:el | el <= 5];
next
|
-
throughAnyForWhich: checkBlock
-
read & return a collection of all objects up-to and including
the elements for which checkBlock returns true.
(i.e. read until checkBlock returns false on an element)
If no such element is encountered, all elements up to the end are read
and returned.
-
upToAny: aCollectionOfObjects
-
read a collection of all objects up to an element that is contained in
aCollectionOfObjects and return these elements, but excluding the matching one.
The next read operation will return the element AFTER anObject.
If no such element is encountered, all elements up to the end are read
and returned.
Compare this with #throughAll: which also reads up to some object
and also positions behind it, but DOES include it in the returned
value.
-
upToBeforeAny: aCollectionOfObjects
-
read a collection of all objects up to an element that is contained in
aCollectionOfObjects and return these elements, but excluding the matching one.
The next read operation will return the matching element.
If no such element is encountered, all elements up to the end are read
and returned.
This returns the exact same as upToAny: would, but leaves the stream's position so that
the next read returns the matching delimiter instead of skipping it.
Caveat: this is the one which should have been called upTo: in the first place;
however, it seems now too late for a change.
-
upToElementForWhich: aBlock
-
read elements until aBlock returns true for an element.
Return the collected elements excluding that element.
Leave the stream positioned for the next read to return that element.
If no element matches, all elements up to the end are returned
usage example(s):
#(1 2 3 4 5 6 7 8 9 10) readStream
upToElementForWhich:[:el | el > 5]
|
-
upToMatching: aBlock
-
Return the next elements up to but not including the next element
for which aBlock returns true.
The next read will return that matching element.
If none matches, the remaining elements up to the end are returned.
usage example(s):
'hello world' readStream upToMatching:[:c | c isSeparator].
|
usage example(s):
'helloworld' readStream upToMatching:[:c | c isSeparator].
|
-
upToSeparator
-
Return the next elements up to but not including the next separator.
The next read will return the separator.
If no separator is encountered, the contents up to the end is returned.
The elements are supposed to understand #isSeparator
(i.e. the receiver is supposed to be a character-stream).
usage example(s):
'hello world' readStream upToSeparator
'helloworld' readStream upToSeparator
'helloworld' readStream upToSeparator
'' readStream upToSeparator
|s|
s := 'hello world' readStream.
s upToSeparator.
s upToEnd
|
reading-numbers
-
nextDecimalInteger
-
read the next integer in radix 10.
Does NOT skip initial whitespace.
The streams elements should be characters.
Be careful - this method returns 0 if not positioned on a digit initially
or if the end of the stream is encountered.
reading-strings
-
nextAlphaNumericWord
-
read the next word (i.e. up to non letter-or-digit).
Return a string containing those characters.
Any leading non-alphaNumeric chars are skipped.
National letters or digits in unicode are not treated as letters.
-
nextDelimited: terminator
-
return the contents of the receiver, up to the next terminator element.
Doubled terminators indicate an embedded terminator element.
For example: 'this '' was a quote'.
Start positioned before the initial terminator.
usage example(s):
('*foo bar baz* more foo' readStream nextDelimited:$*)
('*foo bar **baz***' readStream nextDelimited:$*)
|
-
nextMatching: matchBlock1 thenMatching: matchBlock2
-
read the next word. The first character must match matchBlock1,
remaining characters must match matchBlock2.
Return a string containing those characters.
Useful to read identifiers, where the first char is from a different
set than the remaining (letter vs. letterOrDigit)
-
nextSymbol
-
read the next selector-symbol (i.e. up to non letter-or-digit).
Return a string containing those characters.
Any leading non-alphaNumeric chars are skipped.
|