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