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