author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Sat, 19 Jan 2013 01:30:00 +0000 | |
branch | jv |
changeset 18011 | deb0c3355881 |
parent 17966 | 8b5df02e171f |
parent 14333 | 59c0d7e60297 |
child 18039 | 09806667c605 |
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 |
||
14333 | 232 |
size |
233 |
"not correct, but probably better than 0" |
|
234 |
||
235 |
^ stream size |
|
236 |
||
237 |
"Created: / 31-08-2012 / 16:52:40 / cg" |
|
238 |
! |
|
239 |
||
11992 | 240 |
skip: anInteger |
241 |
||
242 |
"/ Should skip on character basis, not on bytes. This works for XML reader |
|
243 |
^stream skip: anInteger |
|
244 |
||
245 |
"Created: / 20-06-2005 / 13:06:06 / masca" |
|
246 |
! |
|
247 |
||
8175 | 248 |
skipSeparators |
249 |
^ stream skipSeparators |
|
8047 | 250 |
! ! |
251 |
||
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
252 |
!EncodedStream methodsFor:'utilities'! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
253 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
254 |
skipEncodingChunk |
14091 | 255 |
|pos chunk token| |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
256 |
|
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
257 |
stream isPositionable ifFalse:[ |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
258 |
^ self |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
259 |
]. |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
260 |
|
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
261 |
pos := self position. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
262 |
chunk := self nextChunk. |
14084 | 263 |
[ |
264 |
"/ if this is a valid chunk (i.e. not a comment or encoding-directive), |
|
265 |
"/ then position back, so it will be processed as usual. |
|
14091 | 266 |
"/ We could parse here, but this is overkill, since we are only interested in the fact, |
267 |
"/ that there is anything else than a comment in the chunk. |
|
268 |
"/ result := (Parser for:chunk) |
|
269 |
"/ ignoreErrors:true; |
|
270 |
"/ ignoreWarnings:true; |
|
271 |
"/ parseMethodBody. |
|
272 |
||
273 |
token := (Scanner for:chunk) |
|
274 |
ignoreErrors:true; |
|
275 |
ignoreWarnings:true; |
|
276 |
nextToken. |
|
277 |
||
278 |
token ~= #EOF ifTrue:[ |
|
14084 | 279 |
self position:pos |
280 |
]. |
|
14086 | 281 |
] on:Parser parseWarningSignal do:[:ex| |
14084 | 282 |
"really ignore any error. |
283 |
Even setting ignorError will output diagnostics here |
|
284 |
during standalone startup when debugging" |
|
14086 | 285 |
ex proceedWith:#ignore. |
14084 | 286 |
]. |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
287 |
|
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
288 |
"Modified: / 29-07-2011 / 17:42:11 / cg" |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
289 |
! ! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
290 |
|
8047 | 291 |
!EncodedStream class methodsFor:'documentation'! |
292 |
||
14086 | 293 |
version |
14333 | 294 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.26 2012-08-31 18:01:03 cg Exp $' |
14086 | 295 |
! |
296 |
||
13400 | 297 |
version_CVS |
14333 | 298 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.26 2012-08-31 18:01:03 cg Exp $' |
12610 | 299 |
! |
300 |
||
13400 | 301 |
version_SVN |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
302 |
^ '§ Id: EncodedStream.st 10643 2011-06-08 21:53:07Z vranyj1 §' |
8047 | 303 |
! ! |