|
1 " |
|
2 COPYRIGHT (c) 1989-93 by Claus Gittinger |
|
3 All Rights Reserved |
|
4 |
|
5 This software is furnished under a license and may be used |
|
6 only in accordance with the terms of that license and with the |
|
7 inclusion of the above copyright notice. This software may not |
|
8 be provided or otherwise made available to, or used by, any |
|
9 other person. No title to or ownership of the software is |
|
10 hereby transferred. |
|
11 " |
|
12 |
|
13 PositionableStream subclass:#WriteStream |
|
14 instanceVariableNames:'writeLimit' |
|
15 classVariableNames:'' |
|
16 poolDictionaries:'' |
|
17 category:'Streams' |
|
18 ! |
|
19 |
|
20 WriteStream comment:' |
|
21 |
|
22 COPYRIGHT (c) 1989-93 by Claus Gittinger |
|
23 All Rights Reserved |
|
24 |
|
25 Streams for writing into - this implementation currently DOES change the identity if the |
|
26 streamed-upon collection if it cannot grow. Thus its slightly incompatible to ST-80 since |
|
27 aStream contents does not always return the original collection. This will change soon. |
|
28 |
|
29 %W% %E% |
|
30 '! |
|
31 |
|
32 !WriteStream class methodsFor:'instance creation'! |
|
33 |
|
34 on:aCollection from:start to:last |
|
35 "create and return a new stream for writing onto aCollection, where |
|
36 writing is limited to the elements in the range start to last." |
|
37 |
|
38 |newStream| |
|
39 newStream := super on:aCollection from:start to:last. |
|
40 newStream writeLimit:last. |
|
41 ^ newStream |
|
42 ! ! |
|
43 |
|
44 !WriteStream methodsFor:'private'! |
|
45 |
|
46 on:aCollection |
|
47 "create and return a new stream for writing onto aCollection" |
|
48 |
|
49 super on:aCollection. |
|
50 writeLimit := readLimit |
|
51 ! ! |
|
52 |
|
53 !WriteStream methodsFor:'accessing'! |
|
54 |
|
55 contents |
|
56 "return the current contents (a collection) of the stream" |
|
57 |
|
58 collection size == (position - 1) ifFalse:[ |
|
59 collection isFixedSize ifTrue:[ |
|
60 collection := collection copyFrom:1 to:(position - 1) |
|
61 ] ifFalse:[ |
|
62 collection grow:(position - 1) |
|
63 ] |
|
64 ]. |
|
65 ^ collection |
|
66 ! |
|
67 |
|
68 writeLimit:aNumber |
|
69 "set the writeLimit, thats the position after which writing is |
|
70 prohibited." |
|
71 |
|
72 (aNumber < collection size) ifTrue:[writeLimit := aNumber] |
|
73 ! ! |
|
74 |
|
75 !WriteStream methodsFor:'reading/writing'! |
|
76 |
|
77 next |
|
78 "catch read access to write stream" |
|
79 |
|
80 ^ self error:'WriteStreams cannot read' |
|
81 ! |
|
82 |
|
83 peek |
|
84 "catch read access to write stream" |
|
85 |
|
86 ^ self error:'WriteStreams cannot read' |
|
87 ! |
|
88 |
|
89 nextPut:anObject |
|
90 "append the argument, anObject to the stream" |
|
91 |
|
92 (position > collection size) ifTrue:[self growCollection]. |
|
93 collection at:position put:anObject. |
|
94 (position > readLimit) ifTrue:[readLimit := position]. |
|
95 position := position + 1. |
|
96 ^anObject |
|
97 ! |
|
98 |
|
99 nextPutAll:aCollection |
|
100 "append all elements of the argument, aCollection to the stream" |
|
101 |
|
102 |nMore final| |
|
103 |
|
104 nMore := aCollection size. |
|
105 final := position + nMore - 1. |
|
106 (final > collection size) ifTrue:[ |
|
107 self growCollection:final |
|
108 ]. |
|
109 collection replaceFrom:position |
|
110 to:final |
|
111 with:aCollection |
|
112 startingAt:1. |
|
113 |
|
114 position := position + nMore. |
|
115 (position > readLimit) ifTrue:[readLimit := position - 1]. |
|
116 ^ aCollection |
|
117 ! |
|
118 |
|
119 cr |
|
120 "append a carriage-return to the stream" |
|
121 |
|
122 self nextPut:(Character cr) |
|
123 ! |
|
124 |
|
125 tab |
|
126 "append a tab-character to the stream" |
|
127 |
|
128 self nextPut:(Character tab) |
|
129 ! |
|
130 |
|
131 crTab |
|
132 "append a carriage-return followed by a tab to the stream" |
|
133 |
|
134 self nextPut:(Character cr). |
|
135 self nextPut:(Character tab) |
|
136 ! |
|
137 |
|
138 space |
|
139 "append a space character to the receiver-stream" |
|
140 |
|
141 self nextPut:(Character space) |
|
142 ! |
|
143 |
|
144 spaces:count |
|
145 "append count space-characters to the receiver-stream" |
|
146 |
|
147 1 to:count do:[:dummy | |
|
148 self nextPut:(Character space) |
|
149 ] |
|
150 ! |
|
151 |
|
152 ff |
|
153 "append a form-feed (new-pagee) to the receiver-stream" |
|
154 |
|
155 self nextPut:(Character ff) |
|
156 ! ! |
|
157 |
|
158 !WriteStream methodsFor:'private'! |
|
159 |
|
160 growCollection |
|
161 |oldSize newSize newColl| |
|
162 |
|
163 oldSize := collection size. |
|
164 (oldSize == 0) ifTrue:[ |
|
165 newSize := 10 |
|
166 ] ifFalse:[ |
|
167 newSize := oldSize * 2 |
|
168 ]. |
|
169 collection isFixedSize ifTrue:[ |
|
170 newColl := collection species new:newSize. |
|
171 newColl replaceFrom:1 to:oldSize with:collection startingAt:1. |
|
172 collection := newColl |
|
173 ] ifFalse:[ |
|
174 collection grow:newSize. |
|
175 ]. |
|
176 writeLimit := newSize |
|
177 ! |
|
178 |
|
179 growCollection:minNewSize |
|
180 |oldSize newSize newColl| |
|
181 |
|
182 oldSize := collection size. |
|
183 (oldSize == 0) ifTrue:[ |
|
184 newSize := minNewSize |
|
185 ] ifFalse:[ |
|
186 newSize := oldSize * 2. |
|
187 [newSize < minNewSize] whileTrue:[ |
|
188 newSize := newSize * 2 |
|
189 ] |
|
190 ]. |
|
191 collection isFixedSize ifTrue:[ |
|
192 newColl := collection species new:newSize. |
|
193 newColl replaceFrom:1 to:oldSize with:collection startingAt:1. |
|
194 collection := newColl |
|
195 ] ifFalse:[ |
|
196 collection grow:newSize |
|
197 ]. |
|
198 writeLimit := newSize |
|
199 ! ! |