author | Claus Gittinger <cg@exept.de> |
Thu, 04 Apr 2013 09:54:04 +0200 | |
changeset 15060 | 5c96544ded63 |
parent 15049 | 3c4d74d39396 |
child 15345 | 8f04bcf567e6 |
child 18045 | c0c600e0d3b3 |
permissions | -rw-r--r-- |
8148 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
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 |
" |
|
8047 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
8447
9bdea3431846
EncodedStream inherts from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
8444
diff
changeset
|
14 |
PeekableStream subclass:#EncodedStream |
8047 | 15 |
instanceVariableNames:'encoder stream' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
15049 | 18 |
category:'Streams-Misc' |
8047 | 19 |
! |
20 |
||
8148 | 21 |
!EncodedStream class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
15049 | 35 |
! |
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
a stream which transparently decodes from an external decoding, |
|
40 |
looking for '---- Encoding: xxx' near the beginning of the file. |
|
41 |
||
42 |
especially targeted towards reading ST/X source files. |
|
43 |
" |
|
8148 | 44 |
! ! |
8047 | 45 |
|
46 |
!EncodedStream class methodsFor:'instance creation'! |
|
47 |
||
48 |
stream:streamArg encoder:encoder |
|
49 |
^ (self basicNew) stream:streamArg; encoder:encoder |
|
50 |
! ! |
|
51 |
||
12610 | 52 |
!EncodedStream class methodsFor:'utilities'! |
53 |
||
54 |
decodedStreamFor:aStream |
|
15049 | 55 |
|encodingSymbol decoder decodedStream| |
12610 | 56 |
|
57 |
aStream isPositionable ifTrue:[ |
|
15049 | 58 |
encodingSymbol := CharacterEncoder guessEncodingOfStream:aStream. |
59 |
decoder := self encoderFor:encodingSymbol. |
|
60 |
decoder isNil ifTrue:[ |
|
61 |
"/ ascii or iso8895-1 |
|
62 |
^ aStream |
|
12610 | 63 |
]. |
15049 | 64 |
] ifFalse:[ |
65 |
"/ setup for no-encoding; |
|
66 |
"/ switch to a real encoder later, |
|
14095 | 67 |
"/ whenever an encoding pragma is encountered later. |
68 |
decoder := CharacterEncoder nullEncoderInstance. |
|
69 |
]. |
|
70 |
decodedStream := self stream:aStream encoder:decoder. |
|
12610 | 71 |
decodedStream skipEncodingChunk. |
72 |
^ decodedStream |
|
15049 | 73 |
! |
74 |
||
75 |
encoderFor:anEncodingSymbol |
|
76 |
(anEncodingSymbol isNil |
|
77 |
or:[ anEncodingSymbol = #'iso8859-1' |
|
78 |
or:[ anEncodingSymbol = #'ascii' ]]) ifTrue:[ |
|
79 |
^ nil |
|
80 |
]. |
|
81 |
^ CharacterEncoder encoderFor:anEncodingSymbol. |
|
12610 | 82 |
! ! |
83 |
||
8047 | 84 |
!EncodedStream methodsFor:'accessing'! |
85 |
||
11992 | 86 |
contentsSpecies |
87 |
||
88 |
"Not sure if this is ok" |
|
89 |
||
90 |
^ stream contentsSpecies. |
|
91 |
||
92 |
"/ ^UnicodeString |
|
93 |
||
94 |
"Created: / 14-06-2005 / 17:11:01 / janfrog" |
|
95 |
! |
|
96 |
||
8047 | 97 |
encoder |
98 |
^ encoder |
|
99 |
! |
|
100 |
||
101 |
encoder:something |
|
102 |
encoder := something. |
|
103 |
! |
|
104 |
||
12646 | 105 |
pathName |
106 |
"if our base stream hat a pathname, delegate..." |
|
107 |
||
108 |
stream isNil ifTrue:[ |
|
109 |
^ nil. |
|
110 |
]. |
|
111 |
^ stream pathName. |
|
112 |
! |
|
113 |
||
11766 | 114 |
readStream |
115 |
^ self |
|
116 |
! |
|
117 |
||
8047 | 118 |
stream |
119 |
^ stream |
|
120 |
! |
|
121 |
||
122 |
stream:something |
|
123 |
stream := something. |
|
124 |
! ! |
|
125 |
||
126 |
!EncodedStream methodsFor:'stream protocol'! |
|
127 |
||
8175 | 128 |
atEnd |
129 |
^ stream atEnd |
|
130 |
! |
|
131 |
||
8165 | 132 |
close |
133 |
stream close |
|
134 |
! |
|
135 |
||
13400 | 136 |
contents |
137 |
||
138 |
^String streamContents: |
|
139 |
[:s| |
|
140 |
[ stream atEnd ] whileFalse: |
|
141 |
[s nextPut: stream next] |
|
142 |
] |
|
143 |
||
144 |
"Created: / 25-02-2010 / 23:34:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
145 |
! |
|
146 |
||
8213 | 147 |
cr |
8459 | 148 |
self nextPutAll:(Character cr asString) |
8213 | 149 |
! |
150 |
||
11992 | 151 |
emphasis:anObject |
152 |
||
153 |
stream emphasis:anObject |
|
154 |
||
155 |
"Created: / 15-06-2005 / 11:16:33 / janfrog" |
|
156 |
! |
|
157 |
||
11946 | 158 |
isOpen |
12690 | 159 |
^ stream notNil and:[stream isOpen] |
11946 | 160 |
! |
161 |
||
11766 | 162 |
next |
11992 | 163 |
|
164 |
^encoder readNextCharacterFrom:stream |
|
165 |
||
166 |
"Created: / 14-06-2005 / 17:01:39 / janfrog" |
|
167 |
! |
|
11766 | 168 |
|
11992 | 169 |
next:charactersToRead |
170 |
||
171 |
^encoder readNext:charactersToRead charactersFrom:stream |
|
172 |
||
173 |
"Created: / 16-06-2005 / 11:43:43 / masca" |
|
11766 | 174 |
! |
175 |
||
8147 | 176 |
nextChunk |
15049 | 177 |
"as a side effect, check for an encoding chunk" |
178 |
||
179 |
|prevEncoder chunk enc| |
|
8175 | 180 |
|
181 |
chunk := stream nextChunk. |
|
182 |
chunk isNil ifTrue:[^ chunk]. |
|
15049 | 183 |
|
184 |
prevEncoder := encoder. |
|
185 |
stream isPositionable ifFalse:[ |
|
186 |
"/ not already checked |
|
187 |
encoder isNullEncoder ifTrue:[ |
|
188 |
"/ check if we need lazy setup of the encoder |
|
189 |
"/ (used with non-positionable streams) |
|
190 |
(chunk includesString:'---- Encoding:') ifTrue:[ |
|
191 |
enc := self class encoderFor:(CharacterEncoder guessEncodingOfBuffer:chunk). |
|
192 |
encoder := enc ? encoder. |
|
193 |
] |
|
194 |
]. |
|
195 |
]. |
|
196 |
||
197 |
^ prevEncoder decodeString:chunk |
|
8147 | 198 |
! |
199 |
||
8213 | 200 |
nextChunkPut:chunk |
201 |
stream nextChunkPut:(encoder encodeString:chunk) |
|
202 |
! |
|
203 |
||
8047 | 204 |
nextPut:aCharacter |
205 |
self nextPutAll:(aCharacter asString). |
|
206 |
! |
|
207 |
||
208 |
nextPutAll:aCollection |
|
209 |
stream nextPutAll:(encoder encodeString:aCollection). |
|
8147 | 210 |
! |
211 |
||
8444 | 212 |
peek |
11992 | 213 |
|
214 |
^stream peek |
|
215 |
||
216 |
"Created: / 20-06-2005 / 10:13:03 / masca" |
|
217 |
"Modified: / 20-06-2005 / 13:06:14 / masca" |
|
8444 | 218 |
! |
219 |
||
8175 | 220 |
peekFor:aCharacter |
221 |
^ stream peekFor:aCharacter |
|
222 |
! |
|
223 |
||
8165 | 224 |
position |
225 |
^ stream position |
|
226 |
! |
|
227 |
||
8197 | 228 |
position0Based |
11947 | 229 |
"to be obsoleted - use position" |
230 |
||
8197 | 231 |
^ stream position0Based |
232 |
! |
|
233 |
||
234 |
position0Based:newPosition |
|
11947 | 235 |
"to be obsoleted - use position" |
236 |
||
8197 | 237 |
stream position0Based:newPosition |
238 |
! |
|
239 |
||
8175 | 240 |
position1Based |
11947 | 241 |
"to be obsoleted - use position" |
242 |
||
8175 | 243 |
^ stream position1Based |
244 |
! |
|
245 |
||
8147 | 246 |
position1Based:newPosition |
11947 | 247 |
"to be obsoleted - use position" |
248 |
||
8147 | 249 |
stream position1Based:newPosition |
8175 | 250 |
! |
251 |
||
8197 | 252 |
position:newPosition |
253 |
stream position:newPosition |
|
254 |
! |
|
255 |
||
13400 | 256 |
reset |
257 |
stream reset |
|
258 |
||
259 |
"Created: / 25-02-2010 / 23:37:14 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
260 |
! |
|
261 |
||
8213 | 262 |
setToEnd |
263 |
stream setToEnd |
|
264 |
! |
|
265 |
||
14333 | 266 |
size |
267 |
"not correct, but probably better than 0" |
|
268 |
||
269 |
^ stream size |
|
270 |
||
271 |
"Created: / 31-08-2012 / 16:52:40 / cg" |
|
272 |
! |
|
273 |
||
11992 | 274 |
skip: anInteger |
275 |
||
276 |
"/ Should skip on character basis, not on bytes. This works for XML reader |
|
277 |
^stream skip: anInteger |
|
278 |
||
279 |
"Created: / 20-06-2005 / 13:06:06 / masca" |
|
280 |
! |
|
281 |
||
8175 | 282 |
skipSeparators |
283 |
^ stream skipSeparators |
|
8047 | 284 |
! ! |
285 |
||
15049 | 286 |
!EncodedStream methodsFor:'testing'! |
287 |
||
288 |
isEncodedStream |
|
289 |
^ true |
|
290 |
! ! |
|
291 |
||
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
292 |
!EncodedStream methodsFor:'utilities'! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
293 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
294 |
skipEncodingChunk |
14091 | 295 |
|pos chunk token| |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
296 |
|
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
297 |
stream isPositionable ifFalse:[ |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
298 |
^ self |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
299 |
]. |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
300 |
|
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
301 |
pos := self position. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
302 |
chunk := self nextChunk. |
14084 | 303 |
[ |
304 |
"/ if this is a valid chunk (i.e. not a comment or encoding-directive), |
|
305 |
"/ then position back, so it will be processed as usual. |
|
14091 | 306 |
"/ We could parse here, but this is overkill, since we are only interested in the fact, |
307 |
"/ that there is anything else than a comment in the chunk. |
|
308 |
"/ result := (Parser for:chunk) |
|
309 |
"/ ignoreErrors:true; |
|
310 |
"/ ignoreWarnings:true; |
|
311 |
"/ parseMethodBody. |
|
312 |
||
313 |
token := (Scanner for:chunk) |
|
314 |
ignoreErrors:true; |
|
315 |
ignoreWarnings:true; |
|
316 |
nextToken. |
|
317 |
||
318 |
token ~= #EOF ifTrue:[ |
|
14084 | 319 |
self position:pos |
320 |
]. |
|
14086 | 321 |
] on:Parser parseWarningSignal do:[:ex| |
14084 | 322 |
"really ignore any error. |
323 |
Even setting ignorError will output diagnostics here |
|
324 |
during standalone startup when debugging" |
|
14086 | 325 |
ex proceedWith:#ignore. |
14084 | 326 |
]. |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
327 |
|
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
328 |
"Modified: / 29-07-2011 / 17:42:11 / cg" |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
329 |
! ! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
330 |
|
8047 | 331 |
!EncodedStream class methodsFor:'documentation'! |
332 |
||
14086 | 333 |
version |
15049 | 334 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.27 2013-04-03 16:10:25 cg Exp $' |
14086 | 335 |
! |
336 |
||
13400 | 337 |
version_CVS |
15049 | 338 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.27 2013-04-03 16:10:25 cg Exp $' |
12610 | 339 |
! |
340 |
||
13400 | 341 |
version_SVN |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
342 |
^ '§ Id: EncodedStream.st 10643 2011-06-08 21:53:07Z vranyj1 §' |
8047 | 343 |
! ! |
15049 | 344 |