author | Claus Gittinger <cg@exept.de> |
Wed, 22 Feb 2017 11:15:28 +0100 | |
changeset 21521 | 3766ddd131dc |
parent 21472 | f48f79c4ce5c |
child 21522 | c4fe9f268c0b |
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 |
||
17331 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
8447
9bdea3431846
EncodedStream inherts from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
8444
diff
changeset
|
16 |
PeekableStream subclass:#EncodedStream |
15618 | 17 |
instanceVariableNames:'encoder stream' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
15963 | 20 |
category:'Collections-Text-Encodings' |
8047 | 21 |
! |
22 |
||
8148 | 23 |
!EncodedStream class methodsFor:'documentation'! |
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
15049 | 37 |
! |
38 |
||
39 |
documentation |
|
40 |
" |
|
15664 | 41 |
a stream which transparently decodes from an external decoding, |
42 |
looking for '{ Encoding: xxx' near the beginning of the file. |
|
15049 | 43 |
|
15664 | 44 |
especially targeted towards reading ST/X source files. |
15049 | 45 |
" |
8148 | 46 |
! ! |
8047 | 47 |
|
48 |
!EncodedStream class methodsFor:'instance creation'! |
|
49 |
||
50 |
stream:streamArg encoder:encoder |
|
51 |
^ (self basicNew) stream:streamArg; encoder:encoder |
|
16394 | 52 |
|
53 |
" |
|
54 |
|s| |
|
55 |
s := EncodedStream stream:Transcript encoder:(CharacterEncoder encoderToEncodeFrom:#utf8 into:#unicode). |
|
21521 | 56 |
s nextPutAll:('öäü' utf8Encoded) |
16394 | 57 |
" |
18763 | 58 |
! |
59 |
||
60 |
stream:streamArg encoding:encodingSymbol |
|
61 |
^ self stream:streamArg encoder:(CharacterEncoder encoderFor:encodingSymbol) |
|
62 |
||
63 |
" |
|
64 |
|baseStream s| |
|
65 |
baseStream := '' readWriteStream. |
|
66 |
s := EncodedStream stream:baseStream encoding:#utf8. |
|
21521 | 67 |
s nextPutAll:'öäü'. |
18763 | 68 |
baseStream reset; contents. |
69 |
" |
|
8047 | 70 |
! ! |
71 |
||
17650 | 72 |
!EncodedStream class methodsFor:'Compatibility-VW5.4'! |
73 |
||
74 |
on: aStream encodedBy: aStreamEncoder |
|
75 |
||
76 |
^self basicNew on: aStream encodedBy: aStreamEncoder |
|
77 |
! ! |
|
78 |
||
12610 | 79 |
!EncodedStream class methodsFor:'utilities'! |
80 |
||
81 |
decodedStreamFor:aStream |
|
15345 | 82 |
"given a positionable stream, guess its encoding (by reading the |
83 |
first few lines, looking for a string with an encoding hint, |
|
84 |
and return an appropriate encoded string, which does the decoding |
|
85 |
on the fly. Used mostly to read UTF8 files (source code)" |
|
86 |
||
18763 | 87 |
|encodingSymbol decodedStream| |
12610 | 88 |
|
15664 | 89 |
aStream inputStream isPositionable ifTrue:[ |
90 |
encodingSymbol := CharacterEncoder guessEncodingOfStream:aStream inputStream. |
|
18763 | 91 |
decodedStream := self stream:aStream encoding:encodingSymbol. |
15963 | 92 |
"JV@2012-03-27: NO, DO NOT CHANGE POSITION!! Caller might be interested |
93 |
in all data!!!!!!" |
|
94 |
"/decodedStream skipEncodingChunk. |
|
15049 | 95 |
] ifFalse:[ |
96 |
"/ setup for no-encoding; |
|
97 |
"/ switch to a real encoder later, |
|
15664 | 98 |
"/ whenever an encoding pragma is encountered later by #nextChunk. |
99 |
decodedStream := self stream:aStream encoder:CharacterEncoder nullEncoderInstance. |
|
14095 | 100 |
]. |
12610 | 101 |
^ decodedStream |
15963 | 102 |
|
103 |
"Modified: / 23-08-2013 / 17:30:58 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
15049 | 104 |
! |
105 |
||
106 |
encoderFor:anEncodingSymbol |
|
107 |
(anEncodingSymbol isNil |
|
108 |
or:[ anEncodingSymbol = #'iso8859-1' |
|
109 |
or:[ anEncodingSymbol = #'ascii' ]]) ifTrue:[ |
|
110 |
^ nil |
|
111 |
]. |
|
112 |
^ CharacterEncoder encoderFor:anEncodingSymbol. |
|
12610 | 113 |
! ! |
114 |
||
17650 | 115 |
!EncodedStream methodsFor:'Compatibility-VW5.4'! |
116 |
||
117 |
on:aStream encodedBy:aStreamEncoder |
|
118 |
"Initialize the receiver on aStream with aStreamEncoder." |
|
119 |
||
120 |
"/ skipSize := PositionRecord new. |
|
121 |
"/ aStreamEncoder skipRecord: skipSize. |
|
122 |
"/ binary := false. |
|
123 |
"/ lineEndConvention == nil |
|
124 |
"/ ifTrue: |
|
125 |
"/ [aStream needsFileLineEndConversion |
|
126 |
"/ ifTrue: [self lineEndConvention: IOAccessor defaultLineEndConvention] |
|
127 |
"/ ifFalse: [self lineEndConvention: LineEndTransparent]]. |
|
128 |
self stream: aStream. |
|
129 |
self encoder: aStreamEncoder |
|
130 |
! ! |
|
131 |
||
8047 | 132 |
!EncodedStream methodsFor:'accessing'! |
133 |
||
11992 | 134 |
contentsSpecies |
135 |
||
136 |
"Not sure if this is ok" |
|
137 |
||
138 |
^ stream contentsSpecies. |
|
139 |
||
140 |
"/ ^UnicodeString |
|
141 |
||
142 |
"Created: / 14-06-2005 / 17:11:01 / janfrog" |
|
143 |
! |
|
144 |
||
8047 | 145 |
encoder |
146 |
^ encoder |
|
147 |
! |
|
148 |
||
149 |
encoder:something |
|
150 |
encoder := something. |
|
151 |
! |
|
152 |
||
15593 | 153 |
inputStream |
154 |
^ stream inputStream |
|
155 |
! |
|
156 |
||
157 |
lineNumber |
|
158 |
^ stream lineNumber |
|
159 |
! |
|
160 |
||
12646 | 161 |
pathName |
20477 | 162 |
"if our base stream has a pathname, delegate..." |
12646 | 163 |
|
164 |
stream isNil ifTrue:[ |
|
165 |
^ nil. |
|
166 |
]. |
|
167 |
^ stream pathName. |
|
168 |
! |
|
169 |
||
11766 | 170 |
readStream |
21472 | 171 |
"read from self" |
172 |
||
11766 | 173 |
^ self |
21472 | 174 |
|
175 |
"Modified (comment): / 16-02-2017 / 15:59:52 / stefan" |
|
11766 | 176 |
! |
177 |
||
8047 | 178 |
stream |
179 |
^ stream |
|
180 |
! |
|
181 |
||
182 |
stream:something |
|
183 |
stream := something. |
|
184 |
! ! |
|
185 |
||
21472 | 186 |
!EncodedStream methodsFor:'chunk input/output'! |
15963 | 187 |
|
21472 | 188 |
nextChunk |
189 |
"as a side effect, check for an encoding chunk" |
|
190 |
||
191 |
|prevEncoder chunk| |
|
15963 | 192 |
|
21472 | 193 |
chunk := stream nextChunk. |
194 |
chunk isEmptyOrNil ifTrue:[ |
|
195 |
^ chunk. |
|
15963 | 196 |
]. |
197 |
||
21472 | 198 |
prevEncoder := encoder. |
199 |
(prevEncoder isNullEncoder and:[stream isPositionable not]) ifTrue:[ |
|
200 |
"/ not already checked |
|
201 |
"/ check if we need lazy setup of the encoder |
|
202 |
"/ (used with non-positionable streams) |
|
203 |
(chunk includesString:'{ Encoding:') ifTrue:[ |
|
204 |
|enc| |
|
15963 | 205 |
|
21472 | 206 |
enc := self class encoderFor:(CharacterEncoder guessEncodingOfBuffer:chunk). |
207 |
enc notNil ifTrue:[ |
|
208 |
prevEncoder := encoder := enc. |
|
209 |
]. |
|
15963 | 210 |
] |
211 |
]. |
|
21472 | 212 |
^ prevEncoder decodeString:chunk |
15963 | 213 |
|
21472 | 214 |
"Modified: / 16-02-2017 / 14:54:57 / stefan" |
15963 | 215 |
! ! |
216 |
||
8047 | 217 |
!EncodedStream methodsFor:'stream protocol'! |
218 |
||
8175 | 219 |
atEnd |
220 |
^ stream atEnd |
|
221 |
! |
|
222 |
||
8165 | 223 |
close |
224 |
stream close |
|
225 |
! |
|
226 |
||
21521 | 227 |
collection |
228 |
"return the underlying container; nil, if there is none (eg. external streams)" |
|
229 |
||
230 |
^ stream collection |
|
231 |
||
232 |
"Created: / 22-02-2017 / 11:15:00 / cg" |
|
233 |
! |
|
234 |
||
13400 | 235 |
contents |
236 |
||
18157 | 237 |
^String streamContents: [:s| |
238 |
[ self atEnd ] whileFalse:[ |
|
239 |
|ch| |
|
240 |
ch := self next. |
|
241 |
"/ decoder may decide to return nil from #next, even though the |
|
242 |
"/ underlying stream was not at the end before. This is probably a bug... |
|
243 |
ch notNil ifTrue:[ |
|
244 |
s nextPut: ch |
|
245 |
] |
|
13400 | 246 |
] |
18157 | 247 |
] |
13400 | 248 |
|
249 |
"Created: / 25-02-2010 / 23:34:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
250 |
! |
|
251 |
||
8213 | 252 |
cr |
8459 | 253 |
self nextPutAll:(Character cr asString) |
8213 | 254 |
! |
255 |
||
11992 | 256 |
emphasis:anObject |
257 |
||
258 |
stream emphasis:anObject |
|
259 |
||
260 |
"Created: / 15-06-2005 / 11:16:33 / janfrog" |
|
261 |
! |
|
262 |
||
15477 | 263 |
flush |
264 |
stream flush |
|
265 |
! |
|
266 |
||
20363 | 267 |
isEmpty |
268 |
^ stream isEmpty |
|
269 |
! |
|
270 |
||
11946 | 271 |
isOpen |
12690 | 272 |
^ stream notNil and:[stream isOpen] |
11946 | 273 |
! |
274 |
||
11766 | 275 |
next |
11992 | 276 |
|
277 |
^encoder readNextCharacterFrom:stream |
|
278 |
||
279 |
"Created: / 14-06-2005 / 17:01:39 / janfrog" |
|
280 |
! |
|
11766 | 281 |
|
11992 | 282 |
next:charactersToRead |
283 |
||
284 |
^encoder readNext:charactersToRead charactersFrom:stream |
|
285 |
||
286 |
"Created: / 16-06-2005 / 11:43:43 / masca" |
|
11766 | 287 |
! |
288 |
||
21472 | 289 |
nextPut:aCharacter |
290 |
encoder encodeCharacter:aCharacter on:stream. |
|
8175 | 291 |
|
21472 | 292 |
"Modified: / 16-02-2017 / 16:22:23 / stefan" |
8047 | 293 |
! |
294 |
||
295 |
nextPutAll:aCollection |
|
17668 | 296 |
encoder encodeString:aCollection on:stream |
8147 | 297 |
! |
298 |
||
17331 | 299 |
nextPutAll:aCollection startingAt:start to:stop |
17668 | 300 |
encoder encodeString:(aCollection copyFrom:start to:stop) on:stream. |
17331 | 301 |
! |
302 |
||
8444 | 303 |
peek |
11992 | 304 |
|
305 |
^stream peek |
|
306 |
||
307 |
"Created: / 20-06-2005 / 10:13:03 / masca" |
|
308 |
"Modified: / 20-06-2005 / 13:06:14 / masca" |
|
8444 | 309 |
! |
310 |
||
8175 | 311 |
peekFor:aCharacter |
312 |
^ stream peekFor:aCharacter |
|
313 |
! |
|
314 |
||
8165 | 315 |
position |
316 |
^ stream position |
|
317 |
! |
|
318 |
||
8197 | 319 |
position0Based |
15618 | 320 |
<resource: #obsolete> |
11947 | 321 |
"to be obsoleted - use position" |
322 |
||
15618 | 323 |
^ stream position |
8197 | 324 |
! |
325 |
||
326 |
position0Based:newPosition |
|
15618 | 327 |
<resource: #obsolete> |
11947 | 328 |
"to be obsoleted - use position" |
329 |
||
15618 | 330 |
stream position:newPosition |
8197 | 331 |
! |
332 |
||
8175 | 333 |
position1Based |
15618 | 334 |
<resource: #obsolete> |
11947 | 335 |
"to be obsoleted - use position" |
336 |
||
15618 | 337 |
^ stream position + 1 |
8175 | 338 |
! |
339 |
||
8147 | 340 |
position1Based:newPosition |
15618 | 341 |
<resource: #obsolete> |
11947 | 342 |
"to be obsoleted - use position" |
343 |
||
15618 | 344 |
stream position:newPosition-1 |
8175 | 345 |
! |
346 |
||
8197 | 347 |
position:newPosition |
348 |
stream position:newPosition |
|
349 |
! |
|
350 |
||
13400 | 351 |
reset |
352 |
stream reset |
|
353 |
||
354 |
"Created: / 25-02-2010 / 23:37:14 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
355 |
! |
|
356 |
||
8213 | 357 |
setToEnd |
358 |
stream setToEnd |
|
359 |
! |
|
360 |
||
14333 | 361 |
size |
20363 | 362 |
"not always correct, but probably better than 0. |
363 |
Better use #isEmpty." |
|
14333 | 364 |
|
15585 | 365 |
"/ is that better? |
366 |
"/ self error:'size of input is unknown (due to decoding)' |
|
14333 | 367 |
^ stream size |
368 |
||
369 |
"Created: / 31-08-2012 / 16:52:40 / cg" |
|
370 |
! |
|
371 |
||
11992 | 372 |
skip: anInteger |
373 |
||
374 |
"/ Should skip on character basis, not on bytes. This works for XML reader |
|
375 |
^stream skip: anInteger |
|
376 |
||
377 |
"Created: / 20-06-2005 / 13:06:06 / masca" |
|
378 |
! |
|
379 |
||
8175 | 380 |
skipSeparators |
381 |
^ stream skipSeparators |
|
15477 | 382 |
! |
383 |
||
384 |
sync |
|
385 |
stream sync |
|
386 |
! |
|
387 |
||
388 |
syncData |
|
389 |
stream syncData |
|
8047 | 390 |
! ! |
391 |
||
15049 | 392 |
!EncodedStream methodsFor:'testing'! |
393 |
||
394 |
isEncodedStream |
|
395 |
^ true |
|
15963 | 396 |
|
397 |
"Created: / 04-02-2014 / 20:27:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
15592 | 398 |
! |
399 |
||
16258
1582c656cdb0
Added EncodedStream>>isPositionable
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
15963
diff
changeset
|
400 |
isPositionable |
1582c656cdb0
Added EncodedStream>>isPositionable
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
15963
diff
changeset
|
401 |
^ stream isPositionable |
1582c656cdb0
Added EncodedStream>>isPositionable
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
15963
diff
changeset
|
402 |
|
1582c656cdb0
Added EncodedStream>>isPositionable
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
15963
diff
changeset
|
403 |
"Created: / 14-03-2014 / 16:18:57 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
1582c656cdb0
Added EncodedStream>>isPositionable
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
15963
diff
changeset
|
404 |
! |
1582c656cdb0
Added EncodedStream>>isPositionable
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
15963
diff
changeset
|
405 |
|
15592 | 406 |
isReadable |
407 |
^ stream isReadable |
|
408 |
! |
|
409 |
||
410 |
isWritable |
|
411 |
^ stream isWritable |
|
15049 | 412 |
! ! |
413 |
||
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
414 |
!EncodedStream methodsFor:'utilities'! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
415 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
416 |
skipEncodingChunk |
14091 | 417 |
|pos chunk token| |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
418 |
|
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
419 |
stream isPositionable ifFalse:[ |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
420 |
^ self |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
421 |
]. |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
422 |
|
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
423 |
pos := self position. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
424 |
chunk := self nextChunk. |
14084 | 425 |
[ |
426 |
"/ if this is a valid chunk (i.e. not a comment or encoding-directive), |
|
427 |
"/ then position back, so it will be processed as usual. |
|
14091 | 428 |
"/ We could parse here, but this is overkill, since we are only interested in the fact, |
429 |
"/ that there is anything else than a comment in the chunk. |
|
430 |
"/ result := (Parser for:chunk) |
|
431 |
"/ ignoreErrors:true; |
|
432 |
"/ ignoreWarnings:true; |
|
433 |
"/ parseMethodBody. |
|
434 |
||
435 |
token := (Scanner for:chunk) |
|
436 |
ignoreErrors:true; |
|
437 |
ignoreWarnings:true; |
|
438 |
nextToken. |
|
439 |
||
440 |
token ~= #EOF ifTrue:[ |
|
14084 | 441 |
self position:pos |
442 |
]. |
|
14086 | 443 |
] on:Parser parseWarningSignal do:[:ex| |
14084 | 444 |
"really ignore any error. |
21472 | 445 |
Even setting ignoreError will output diagnostics here |
14084 | 446 |
during standalone startup when debugging" |
14086 | 447 |
ex proceedWith:#ignore. |
14084 | 448 |
]. |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
449 |
|
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
450 |
"Modified: / 29-07-2011 / 17:42:11 / cg" |
21472 | 451 |
"Modified (format): / 16-02-2017 / 16:01:09 / stefan" |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
452 |
! ! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
453 |
|
8047 | 454 |
!EncodedStream class methodsFor:'documentation'! |
455 |
||
14086 | 456 |
version |
18763 | 457 |
^ '$Header$' |
14086 | 458 |
! |
459 |
||
13400 | 460 |
version_CVS |
18763 | 461 |
^ '$Header$' |
12610 | 462 |
! |
463 |
||
13400 | 464 |
version_SVN |
15345 | 465 |
^ '$ Id: EncodedStream.st 10643 2011-06-08 21:53:07Z vranyj1 $' |
8047 | 466 |
! ! |
15049 | 467 |