|
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 |
|
81 next |
|
82 self checkCurrentStreamAtEnd. |
|
83 streamStack isEmpty ifTrue:[^ self pastEndRead]. |
|
84 ^ streamStack last next |
|
85 |
|
86 "Created: / 21-05-2010 / 14:31:23 / cg" |
|
87 ! |
|
88 |
|
89 peek |
|
90 self checkCurrentStreamAtEnd. |
|
91 streamStack isEmpty ifTrue:[^ self pastEndRead]. |
|
92 ^ streamStack last peek |
|
93 |
|
94 "Created: / 21-05-2010 / 14:29:06 / cg" |
|
95 ! ! |
|
96 |
|
97 !MultiReadStream methodsFor:'stream stacking'! |
|
98 |
|
99 popInputStream |
|
100 streamStack removeLast |
|
101 |
|
102 "Created: / 21-05-2010 / 14:25:30 / cg" |
|
103 ! |
|
104 |
|
105 pushInputStream:aReadStream |
|
106 streamStack isNil ifTrue:[ |
|
107 streamStack := OrderedCollection new. |
|
108 ]. |
|
109 streamStack add:aReadStream |
|
110 |
|
111 "Created: / 21-05-2010 / 14:25:17 / cg" |
|
112 ! ! |
|
113 |
|
114 !MultiReadStream class methodsFor:'documentation'! |
|
115 |
|
116 version_CVS |
|
117 ^ '$Header: /cvs/stx/stx/libbasic2/MultiReadStream.st,v 1.1 2010-05-21 12:35:52 cg Exp $' |
|
118 ! ! |