author | Stefan Vogel <sv@exept.de> |
Wed, 11 Jul 2012 18:42:49 +0200 | |
changeset 14182 | c8bfb55485d1 |
parent 14095 | 73651b01bf12 |
child 14333 | 59c0d7e60297 |
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:'' |
|
18 |
category:'Collections-Text-Encodings' |
|
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 |
" |
|
35 |
! ! |
|
8047 | 36 |
|
37 |
!EncodedStream class methodsFor:'instance creation'! |
|
38 |
||
39 |
stream:streamArg encoder:encoder |
|
40 |
^ (self basicNew) stream:streamArg; encoder:encoder |
|
41 |
! ! |
|
42 |
||
12610 | 43 |
!EncodedStream class methodsFor:'utilities'! |
44 |
||
45 |
decodedStreamFor:aStream |
|
46 |
|encoding decoder decodedStream| |
|
47 |
||
48 |
aStream isPositionable ifTrue:[ |
|
49 |
encoding := CharacterEncoder guessEncodingOfStream:aStream. |
|
50 |
(encoding notNil |
|
51 |
and:[ encoding ~= #'iso8859-1' |
|
52 |
and:[ encoding ~= #'ascii' ]]) ifTrue:[ |
|
53 |
decoder := CharacterEncoder encoderFor:encoding. |
|
54 |
]. |
|
55 |
]. |
|
14095 | 56 |
decoder isNil ifTrue:[ |
57 |
"/ setup for no-encoding; allows for a later switch to a real encoder, |
|
58 |
"/ whenever an encoding pragma is encountered later. |
|
59 |
decoder := CharacterEncoder nullEncoderInstance. |
|
60 |
]. |
|
61 |
decodedStream := self stream:aStream encoder:decoder. |
|
12610 | 62 |
decodedStream skipEncodingChunk. |
63 |
^ decodedStream |
|
64 |
! ! |
|
65 |
||
8047 | 66 |
!EncodedStream methodsFor:'accessing'! |
67 |
||
11992 | 68 |
contentsSpecies |
69 |
||
70 |
"Not sure if this is ok" |
|
71 |
||
72 |
^ stream contentsSpecies. |
|
73 |
||
74 |
"/ ^UnicodeString |
|
75 |
||
76 |
"Created: / 14-06-2005 / 17:11:01 / janfrog" |
|
77 |
! |
|
78 |
||
8047 | 79 |
encoder |
80 |
^ encoder |
|
81 |
! |
|
82 |
||
83 |
encoder:something |
|
84 |
encoder := something. |
|
85 |
! |
|
86 |
||
12646 | 87 |
pathName |
88 |
"if our base stream hat a pathname, delegate..." |
|
89 |
||
90 |
stream isNil ifTrue:[ |
|
91 |
^ nil. |
|
92 |
]. |
|
93 |
^ stream pathName. |
|
94 |
! |
|
95 |
||
11766 | 96 |
readStream |
97 |
^ self |
|
98 |
! |
|
99 |
||
8047 | 100 |
stream |
101 |
^ stream |
|
102 |
! |
|
103 |
||
104 |
stream:something |
|
105 |
stream := something. |
|
106 |
! ! |
|
107 |
||
108 |
!EncodedStream methodsFor:'stream protocol'! |
|
109 |
||
8175 | 110 |
atEnd |
111 |
^ stream atEnd |
|
112 |
! |
|
113 |
||
8165 | 114 |
close |
115 |
stream close |
|
116 |
! |
|
117 |
||
13400 | 118 |
contents |
119 |
||
120 |
^String streamContents: |
|
121 |
[:s| |
|
122 |
[ stream atEnd ] whileFalse: |
|
123 |
[s nextPut: stream next] |
|
124 |
] |
|
125 |
||
126 |
"Created: / 25-02-2010 / 23:34:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
127 |
! |
|
128 |
||
8213 | 129 |
cr |
8459 | 130 |
self nextPutAll:(Character cr asString) |
8213 | 131 |
! |
132 |
||
11992 | 133 |
emphasis:anObject |
134 |
||
135 |
stream emphasis:anObject |
|
136 |
||
137 |
"Created: / 15-06-2005 / 11:16:33 / janfrog" |
|
138 |
! |
|
139 |
||
11946 | 140 |
isOpen |
12690 | 141 |
^ stream notNil and:[stream isOpen] |
11946 | 142 |
! |
143 |
||
11766 | 144 |
next |
11992 | 145 |
|
146 |
^encoder readNextCharacterFrom:stream |
|
147 |
||
148 |
"Created: / 14-06-2005 / 17:01:39 / janfrog" |
|
149 |
! |
|
11766 | 150 |
|
11992 | 151 |
next:charactersToRead |
152 |
||
153 |
^encoder readNext:charactersToRead charactersFrom:stream |
|
154 |
||
155 |
"Created: / 16-06-2005 / 11:43:43 / masca" |
|
11766 | 156 |
! |
157 |
||
8147 | 158 |
nextChunk |
8175 | 159 |
|chunk| |
160 |
||
161 |
chunk := stream nextChunk. |
|
162 |
chunk isNil ifTrue:[^ chunk]. |
|
163 |
^ encoder decodeString:chunk |
|
8147 | 164 |
! |
165 |
||
8213 | 166 |
nextChunkPut:chunk |
167 |
stream nextChunkPut:(encoder encodeString:chunk) |
|
168 |
! |
|
169 |
||
8047 | 170 |
nextPut:aCharacter |
171 |
self nextPutAll:(aCharacter asString). |
|
172 |
! |
|
173 |
||
174 |
nextPutAll:aCollection |
|
175 |
stream nextPutAll:(encoder encodeString:aCollection). |
|
8147 | 176 |
! |
177 |
||
8444 | 178 |
peek |
11992 | 179 |
|
180 |
^stream peek |
|
181 |
||
182 |
"Created: / 20-06-2005 / 10:13:03 / masca" |
|
183 |
"Modified: / 20-06-2005 / 13:06:14 / masca" |
|
8444 | 184 |
! |
185 |
||
8175 | 186 |
peekFor:aCharacter |
187 |
^ stream peekFor:aCharacter |
|
188 |
! |
|
189 |
||
8165 | 190 |
position |
191 |
^ stream position |
|
192 |
! |
|
193 |
||
8197 | 194 |
position0Based |
11947 | 195 |
"to be obsoleted - use position" |
196 |
||
8197 | 197 |
^ stream position0Based |
198 |
! |
|
199 |
||
200 |
position0Based:newPosition |
|
11947 | 201 |
"to be obsoleted - use position" |
202 |
||
8197 | 203 |
stream position0Based:newPosition |
204 |
! |
|
205 |
||
8175 | 206 |
position1Based |
11947 | 207 |
"to be obsoleted - use position" |
208 |
||
8175 | 209 |
^ stream position1Based |
210 |
! |
|
211 |
||
8147 | 212 |
position1Based:newPosition |
11947 | 213 |
"to be obsoleted - use position" |
214 |
||
8147 | 215 |
stream position1Based:newPosition |
8175 | 216 |
! |
217 |
||
8197 | 218 |
position:newPosition |
219 |
stream position:newPosition |
|
220 |
! |
|
221 |
||
13400 | 222 |
reset |
223 |
stream reset |
|
224 |
||
225 |
"Created: / 25-02-2010 / 23:37:14 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
226 |
! |
|
227 |
||
8213 | 228 |
setToEnd |
229 |
stream setToEnd |
|
230 |
! |
|
231 |
||
11992 | 232 |
skip: anInteger |
233 |
||
234 |
"/ Should skip on character basis, not on bytes. This works for XML reader |
|
235 |
^stream skip: anInteger |
|
236 |
||
237 |
"Created: / 20-06-2005 / 13:06:06 / masca" |
|
238 |
! |
|
239 |
||
8175 | 240 |
skipSeparators |
241 |
^ stream skipSeparators |
|
8047 | 242 |
! ! |
243 |
||
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
244 |
!EncodedStream methodsFor:'utilities'! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
245 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
246 |
skipEncodingChunk |
14091 | 247 |
|pos chunk token| |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
248 |
|
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
249 |
stream isPositionable ifFalse:[ |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
250 |
^ self |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
251 |
]. |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
252 |
|
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
253 |
pos := self position. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
254 |
chunk := self nextChunk. |
14084 | 255 |
[ |
256 |
"/ if this is a valid chunk (i.e. not a comment or encoding-directive), |
|
257 |
"/ then position back, so it will be processed as usual. |
|
14091 | 258 |
"/ We could parse here, but this is overkill, since we are only interested in the fact, |
259 |
"/ that there is anything else than a comment in the chunk. |
|
260 |
"/ result := (Parser for:chunk) |
|
261 |
"/ ignoreErrors:true; |
|
262 |
"/ ignoreWarnings:true; |
|
263 |
"/ parseMethodBody. |
|
264 |
||
265 |
token := (Scanner for:chunk) |
|
266 |
ignoreErrors:true; |
|
267 |
ignoreWarnings:true; |
|
268 |
nextToken. |
|
269 |
||
270 |
token ~= #EOF ifTrue:[ |
|
14084 | 271 |
self position:pos |
272 |
]. |
|
14086 | 273 |
] on:Parser parseWarningSignal do:[:ex| |
14084 | 274 |
"really ignore any error. |
275 |
Even setting ignorError will output diagnostics here |
|
276 |
during standalone startup when debugging" |
|
14086 | 277 |
ex proceedWith:#ignore. |
14084 | 278 |
]. |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
279 |
|
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
280 |
"Modified: / 29-07-2011 / 17:42:11 / cg" |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
281 |
! ! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
282 |
|
8047 | 283 |
!EncodedStream class methodsFor:'documentation'! |
284 |
||
14086 | 285 |
version |
14095 | 286 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.25 2012-04-05 10:01:32 stefan Exp $' |
14086 | 287 |
! |
288 |
||
13400 | 289 |
version_CVS |
14095 | 290 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.25 2012-04-05 10:01:32 stefan Exp $' |
12610 | 291 |
! |
292 |
||
13400 | 293 |
version_SVN |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
294 |
^ '§ Id: EncodedStream.st 10643 2011-06-08 21:53:07Z vranyj1 §' |
8047 | 295 |
! ! |