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