author | Claus Gittinger <cg@exept.de> |
Mon, 03 Jun 2013 12:39:29 +0200 | |
changeset 15345 | 8f04bcf567e6 |
parent 15049 | 3c4d74d39396 |
child 15477 | 32c7805d7a6e |
child 18066 | 89d51443ba6f |
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 |
||
11946 | 163 |
isOpen |
12690 | 164 |
^ stream notNil and:[stream isOpen] |
11946 | 165 |
! |
166 |
||
11766 | 167 |
next |
11992 | 168 |
|
169 |
^encoder readNextCharacterFrom:stream |
|
170 |
||
171 |
"Created: / 14-06-2005 / 17:01:39 / janfrog" |
|
172 |
! |
|
11766 | 173 |
|
11992 | 174 |
next:charactersToRead |
175 |
||
176 |
^encoder readNext:charactersToRead charactersFrom:stream |
|
177 |
||
178 |
"Created: / 16-06-2005 / 11:43:43 / masca" |
|
11766 | 179 |
! |
180 |
||
8147 | 181 |
nextChunk |
15049 | 182 |
"as a side effect, check for an encoding chunk" |
183 |
||
184 |
|prevEncoder chunk enc| |
|
8175 | 185 |
|
186 |
chunk := stream nextChunk. |
|
187 |
chunk isNil ifTrue:[^ chunk]. |
|
15049 | 188 |
|
189 |
prevEncoder := encoder. |
|
190 |
stream isPositionable ifFalse:[ |
|
191 |
"/ not already checked |
|
192 |
encoder isNullEncoder ifTrue:[ |
|
193 |
"/ check if we need lazy setup of the encoder |
|
194 |
"/ (used with non-positionable streams) |
|
195 |
(chunk includesString:'---- Encoding:') ifTrue:[ |
|
196 |
enc := self class encoderFor:(CharacterEncoder guessEncodingOfBuffer:chunk). |
|
197 |
encoder := enc ? encoder. |
|
198 |
] |
|
199 |
]. |
|
200 |
]. |
|
201 |
||
202 |
^ prevEncoder decodeString:chunk |
|
8147 | 203 |
! |
204 |
||
8213 | 205 |
nextChunkPut:chunk |
206 |
stream nextChunkPut:(encoder encodeString:chunk) |
|
207 |
! |
|
208 |
||
8047 | 209 |
nextPut:aCharacter |
210 |
self nextPutAll:(aCharacter asString). |
|
211 |
! |
|
212 |
||
213 |
nextPutAll:aCollection |
|
214 |
stream nextPutAll:(encoder encodeString:aCollection). |
|
8147 | 215 |
! |
216 |
||
8444 | 217 |
peek |
11992 | 218 |
|
219 |
^stream peek |
|
220 |
||
221 |
"Created: / 20-06-2005 / 10:13:03 / masca" |
|
222 |
"Modified: / 20-06-2005 / 13:06:14 / masca" |
|
8444 | 223 |
! |
224 |
||
8175 | 225 |
peekFor:aCharacter |
226 |
^ stream peekFor:aCharacter |
|
227 |
! |
|
228 |
||
8165 | 229 |
position |
230 |
^ stream position |
|
231 |
! |
|
232 |
||
8197 | 233 |
position0Based |
11947 | 234 |
"to be obsoleted - use position" |
235 |
||
8197 | 236 |
^ stream position0Based |
237 |
! |
|
238 |
||
239 |
position0Based:newPosition |
|
11947 | 240 |
"to be obsoleted - use position" |
241 |
||
8197 | 242 |
stream position0Based:newPosition |
243 |
! |
|
244 |
||
8175 | 245 |
position1Based |
11947 | 246 |
"to be obsoleted - use position" |
247 |
||
8175 | 248 |
^ stream position1Based |
249 |
! |
|
250 |
||
8147 | 251 |
position1Based:newPosition |
11947 | 252 |
"to be obsoleted - use position" |
253 |
||
8147 | 254 |
stream position1Based:newPosition |
8175 | 255 |
! |
256 |
||
8197 | 257 |
position:newPosition |
258 |
stream position:newPosition |
|
259 |
! |
|
260 |
||
13400 | 261 |
reset |
262 |
stream reset |
|
263 |
||
264 |
"Created: / 25-02-2010 / 23:37:14 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
265 |
! |
|
266 |
||
8213 | 267 |
setToEnd |
268 |
stream setToEnd |
|
269 |
! |
|
270 |
||
14333 | 271 |
size |
272 |
"not correct, but probably better than 0" |
|
273 |
||
274 |
^ stream size |
|
275 |
||
276 |
"Created: / 31-08-2012 / 16:52:40 / cg" |
|
277 |
! |
|
278 |
||
11992 | 279 |
skip: anInteger |
280 |
||
281 |
"/ Should skip on character basis, not on bytes. This works for XML reader |
|
282 |
^stream skip: anInteger |
|
283 |
||
284 |
"Created: / 20-06-2005 / 13:06:06 / masca" |
|
285 |
! |
|
286 |
||
8175 | 287 |
skipSeparators |
288 |
^ stream skipSeparators |
|
8047 | 289 |
! ! |
290 |
||
15049 | 291 |
!EncodedStream methodsFor:'testing'! |
292 |
||
293 |
isEncodedStream |
|
294 |
^ true |
|
295 |
! ! |
|
296 |
||
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
297 |
!EncodedStream methodsFor:'utilities'! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
298 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
299 |
skipEncodingChunk |
14091 | 300 |
|pos chunk token| |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
301 |
|
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
302 |
stream isPositionable ifFalse:[ |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
303 |
^ self |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
304 |
]. |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
305 |
|
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
306 |
pos := self position. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
307 |
chunk := self nextChunk. |
14084 | 308 |
[ |
309 |
"/ if this is a valid chunk (i.e. not a comment or encoding-directive), |
|
310 |
"/ then position back, so it will be processed as usual. |
|
14091 | 311 |
"/ We could parse here, but this is overkill, since we are only interested in the fact, |
312 |
"/ that there is anything else than a comment in the chunk. |
|
313 |
"/ result := (Parser for:chunk) |
|
314 |
"/ ignoreErrors:true; |
|
315 |
"/ ignoreWarnings:true; |
|
316 |
"/ parseMethodBody. |
|
317 |
||
318 |
token := (Scanner for:chunk) |
|
319 |
ignoreErrors:true; |
|
320 |
ignoreWarnings:true; |
|
321 |
nextToken. |
|
322 |
||
323 |
token ~= #EOF ifTrue:[ |
|
14084 | 324 |
self position:pos |
325 |
]. |
|
14086 | 326 |
] on:Parser parseWarningSignal do:[:ex| |
14084 | 327 |
"really ignore any error. |
328 |
Even setting ignorError will output diagnostics here |
|
329 |
during standalone startup when debugging" |
|
14086 | 330 |
ex proceedWith:#ignore. |
14084 | 331 |
]. |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
332 |
|
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
333 |
"Modified: / 29-07-2011 / 17:42:11 / cg" |
8712
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 |
|
8047 | 336 |
!EncodedStream class methodsFor:'documentation'! |
337 |
||
14086 | 338 |
version |
15345 | 339 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.28 2013-06-03 10:39:29 cg Exp $' |
14086 | 340 |
! |
341 |
||
13400 | 342 |
version_CVS |
15345 | 343 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.28 2013-06-03 10:39:29 cg Exp $' |
12610 | 344 |
! |
345 |
||
13400 | 346 |
version_SVN |
15345 | 347 |
^ '$ Id: EncodedStream.st 10643 2011-06-08 21:53:07Z vranyj1 $' |
8047 | 348 |
! ! |
15049 | 349 |