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