2449
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
4090
|
3 |
"{ NameSpace: Smalltalk }"
|
|
4 |
|
2449
|
5 |
PeekableStream subclass:#MultiReadStream
|
|
6 |
instanceVariableNames:'streamStack'
|
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
|
9 |
category:'Streams-Misc'
|
|
10 |
!
|
|
11 |
|
|
12 |
!MultiReadStream class methodsFor:'documentation'!
|
|
13 |
|
|
14 |
documentation
|
|
15 |
"
|
4457
|
16 |
Stacked streams.
|
|
17 |
|
|
18 |
At any time, another stream can be stacked onto a stream stack.
|
|
19 |
Making this stream's contents to be returned before the rest.
|
|
20 |
To the stream reader, this looks like a single stream delivering the
|
3287
|
21 |
embedded stream contents sequentially.
|
2449
|
22 |
|
4457
|
23 |
Useful when reading files which include each other,
|
|
24 |
or to handle define-macro expansion in a c-parser.
|
2449
|
25 |
|
3272
|
26 |
[Author:]
|
|
27 |
Claus Gittinger
|
|
28 |
|
2449
|
29 |
[instance variables:]
|
|
30 |
"
|
|
31 |
|
|
32 |
"Created: / 21-05-2010 / 14:34:48 / cg"
|
|
33 |
!
|
|
34 |
|
|
35 |
examples
|
|
36 |
"
|
|
37 |
|s|
|
|
38 |
|
|
39 |
s := MultiReadStream on:('abcd' readStream).
|
|
40 |
self assert:(s peek == $a).
|
|
41 |
self assert:(s next == $a).
|
|
42 |
s pushInputStream:('1234' readStream).
|
|
43 |
self assert:(s next == $1).
|
|
44 |
self assert:(s next == $2).
|
|
45 |
s pushInputStream:('aa' readStream).
|
|
46 |
self assert:(s next == $a).
|
|
47 |
self assert:(s next == $a).
|
|
48 |
self assert:(s atEnd not).
|
|
49 |
self assert:(s next == $3).
|
|
50 |
self assert:(s next == $4).
|
|
51 |
self assert:(s next == $b).
|
|
52 |
self assert:(s next == $c).
|
|
53 |
self assert:(s next == $d).
|
|
54 |
self assert:(s atEnd).
|
|
55 |
self assert:(s peek == nil).
|
|
56 |
self assert:(s next == nil).
|
|
57 |
"
|
|
58 |
|
|
59 |
"Created: / 21-05-2010 / 14:27:53 / cg"
|
|
60 |
! !
|
|
61 |
|
|
62 |
!MultiReadStream class methodsFor:'instance creation'!
|
|
63 |
|
|
64 |
on:aReadStream
|
|
65 |
^ self basicNew pushInputStream:aReadStream
|
|
66 |
|
|
67 |
"Created: / 21-05-2010 / 14:26:03 / cg"
|
|
68 |
! !
|
|
69 |
|
|
70 |
!MultiReadStream methodsFor:'private'!
|
|
71 |
|
|
72 |
checkCurrentStreamAtEnd
|
2655
|
73 |
[streamStack size > 1 and:[streamStack last atEnd]] whileTrue:[
|
|
74 |
"keep the last stream, so that the #position kludge will work"
|
2449
|
75 |
self popInputStream
|
|
76 |
].
|
|
77 |
|
|
78 |
"Created: / 21-05-2010 / 14:30:20 / cg"
|
|
79 |
! !
|
|
80 |
|
|
81 |
!MultiReadStream methodsFor:'stream protocol'!
|
|
82 |
|
|
83 |
atEnd
|
|
84 |
self checkCurrentStreamAtEnd.
|
2655
|
85 |
^ streamStack isEmptyOrNil or:[streamStack last atEnd]
|
2449
|
86 |
|
|
87 |
"Created: / 21-05-2010 / 14:31:48 / cg"
|
|
88 |
!
|
|
89 |
|
2450
|
90 |
contentsSpecies
|
3090
|
91 |
"return a class of which instances will be returned, when
|
|
92 |
parts of the collection are asked for.
|
|
93 |
(see upTo-kind of methods in Stream)"
|
|
94 |
|
2450
|
95 |
streamStack isEmptyOrNil ifTrue:[^ super contentsSpecies].
|
|
96 |
^ streamStack first contentsSpecies
|
|
97 |
|
|
98 |
"Created: / 21-05-2010 / 14:44:26 / cg"
|
|
99 |
!
|
|
100 |
|
2449
|
101 |
next
|
|
102 |
self checkCurrentStreamAtEnd.
|
2655
|
103 |
streamStack isEmptyOrNil ifTrue:[^ self pastEndRead].
|
2449
|
104 |
^ streamStack last next
|
|
105 |
|
|
106 |
"Created: / 21-05-2010 / 14:31:23 / cg"
|
|
107 |
!
|
|
108 |
|
|
109 |
peek
|
|
110 |
self checkCurrentStreamAtEnd.
|
2655
|
111 |
streamStack isEmptyOrNil ifTrue:[^ self pastEndRead].
|
2449
|
112 |
^ streamStack last peek
|
|
113 |
|
|
114 |
"Created: / 21-05-2010 / 14:29:06 / cg"
|
2450
|
115 |
!
|
|
116 |
|
2463
|
117 |
position
|
4090
|
118 |
"/ that's debatable !!
|
2463
|
119 |
"/ the code below only makes sense, if we see the multiStream as some kind of
|
|
120 |
"/ macro-expanding c-define stream
|
2654
|
121 |
|
|
122 |
streamStack isEmptyOrNil ifTrue:[
|
2655
|
123 |
"we assume we are at the beginning"
|
|
124 |
^ 0
|
2654
|
125 |
].
|
2463
|
126 |
^ streamStack first position
|
|
127 |
|
|
128 |
"Created: / 21-05-2010 / 14:52:40 / cg"
|
|
129 |
!
|
|
130 |
|
2654
|
131 |
position0Based
|
3081
|
132 |
<resource: #obsolete>
|
|
133 |
^ self position
|
2654
|
134 |
|
|
135 |
"Created: / 21-05-2010 / 14:52:40 / cg"
|
|
136 |
!
|
|
137 |
|
2450
|
138 |
position1Based
|
3081
|
139 |
<resource: #obsolete>
|
|
140 |
^ self position + 1
|
2450
|
141 |
|
|
142 |
"Created: / 21-05-2010 / 14:52:40 / cg"
|
|
143 |
!
|
|
144 |
|
|
145 |
readStream
|
|
146 |
^ self
|
|
147 |
|
|
148 |
"Created: / 21-05-2010 / 14:51:05 / cg"
|
2449
|
149 |
! !
|
|
150 |
|
|
151 |
!MultiReadStream methodsFor:'stream stacking'!
|
|
152 |
|
|
153 |
popInputStream
|
|
154 |
streamStack removeLast
|
|
155 |
|
|
156 |
"Created: / 21-05-2010 / 14:25:30 / cg"
|
|
157 |
!
|
|
158 |
|
|
159 |
pushInputStream:aReadStream
|
|
160 |
streamStack isNil ifTrue:[
|
|
161 |
streamStack := OrderedCollection new.
|
|
162 |
].
|
|
163 |
streamStack add:aReadStream
|
|
164 |
|
|
165 |
"Created: / 21-05-2010 / 14:25:17 / cg"
|
|
166 |
! !
|
|
167 |
|
|
168 |
!MultiReadStream class methodsFor:'documentation'!
|
|
169 |
|
|
170 |
version_CVS
|
4090
|
171 |
^ '$Header$'
|
2449
|
172 |
! !
|
3081
|
173 |
|