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
|
|
65 |
[streamStack notEmpty and:[streamStack last atEnd]] whileTrue:[
|
|
66 |
self popInputStream
|
|
67 |
].
|
|
68 |
|
|
69 |
"Created: / 21-05-2010 / 14:30:20 / cg"
|
|
70 |
! !
|
|
71 |
|
|
72 |
!MultiReadStream methodsFor:'stream protocol'!
|
|
73 |
|
|
74 |
atEnd
|
|
75 |
self checkCurrentStreamAtEnd.
|
|
76 |
^ streamStack isEmpty
|
|
77 |
|
|
78 |
"Created: / 21-05-2010 / 14:31:48 / cg"
|
|
79 |
!
|
|
80 |
|
2450
|
81 |
contentsSpecies
|
|
82 |
streamStack isEmptyOrNil ifTrue:[^ super contentsSpecies].
|
|
83 |
^ streamStack first contentsSpecies
|
|
84 |
|
|
85 |
"Created: / 21-05-2010 / 14:44:26 / cg"
|
|
86 |
!
|
|
87 |
|
2449
|
88 |
next
|
|
89 |
self checkCurrentStreamAtEnd.
|
|
90 |
streamStack isEmpty ifTrue:[^ self pastEndRead].
|
|
91 |
^ streamStack last next
|
|
92 |
|
|
93 |
"Created: / 21-05-2010 / 14:31:23 / cg"
|
|
94 |
!
|
|
95 |
|
|
96 |
peek
|
|
97 |
self checkCurrentStreamAtEnd.
|
|
98 |
streamStack isEmpty ifTrue:[^ self pastEndRead].
|
|
99 |
^ streamStack last peek
|
|
100 |
|
|
101 |
"Created: / 21-05-2010 / 14:29:06 / cg"
|
2450
|
102 |
!
|
|
103 |
|
2463
|
104 |
position
|
|
105 |
"/ thats depatable !!
|
|
106 |
"/ the code below only makes sense, if we see the multiStream as some kind of
|
|
107 |
"/ macro-expanding c-define stream
|
|
108 |
^ streamStack first position
|
|
109 |
|
|
110 |
"Created: / 21-05-2010 / 14:52:40 / cg"
|
|
111 |
!
|
|
112 |
|
2450
|
113 |
position1Based
|
|
114 |
"/ thats depatable !!
|
|
115 |
"/ the code below only makes sense, if we see the multiStream as some kind of
|
|
116 |
"/ macro-expanding c-define stream
|
|
117 |
^ streamStack first position1Based
|
|
118 |
|
|
119 |
"Created: / 21-05-2010 / 14:52:40 / cg"
|
|
120 |
!
|
|
121 |
|
|
122 |
readStream
|
|
123 |
^ self
|
|
124 |
|
|
125 |
"Created: / 21-05-2010 / 14:51:05 / cg"
|
2449
|
126 |
! !
|
|
127 |
|
|
128 |
!MultiReadStream methodsFor:'stream stacking'!
|
|
129 |
|
|
130 |
popInputStream
|
|
131 |
streamStack removeLast
|
|
132 |
|
|
133 |
"Created: / 21-05-2010 / 14:25:30 / cg"
|
|
134 |
!
|
|
135 |
|
|
136 |
pushInputStream:aReadStream
|
|
137 |
streamStack isNil ifTrue:[
|
|
138 |
streamStack := OrderedCollection new.
|
|
139 |
].
|
|
140 |
streamStack add:aReadStream
|
|
141 |
|
|
142 |
"Created: / 21-05-2010 / 14:25:17 / cg"
|
|
143 |
! !
|
|
144 |
|
|
145 |
!MultiReadStream class methodsFor:'documentation'!
|
|
146 |
|
|
147 |
version_CVS
|
2463
|
148 |
^ '$Header: /cvs/stx/stx/libbasic2/MultiReadStream.st,v 1.3 2010-07-11 17:21:36 stefan Exp $'
|
2449
|
149 |
! !
|