author | Claus Gittinger <cg@exept.de> |
Fri, 29 Jul 2011 17:43:00 +0200 | |
changeset 13525 | e17ebc11ca73 |
parent 13400 | 8dcdfacd2f34 |
child 14084 | 341e275c4bba |
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 |
"/ setup for no-encoding; allows for a later switch to a real encoder, |
|
49 |
"/ whenever an encoding pragma is encountered later. |
|
50 |
decoder := CharacterEncoder nullEncoderInstance. |
|
51 |
||
52 |
aStream isPositionable ifTrue:[ |
|
53 |
encoding := CharacterEncoder guessEncodingOfStream:aStream. |
|
54 |
(encoding notNil |
|
55 |
and:[ encoding ~= #'iso8859-1' |
|
56 |
and:[ encoding ~= #'ascii' ]]) ifTrue:[ |
|
57 |
decoder := CharacterEncoder encoderFor:encoding. |
|
58 |
]. |
|
59 |
]. |
|
60 |
decodedStream := EncodedStream stream:aStream encoder:decoder. |
|
61 |
decodedStream skipEncodingChunk. |
|
62 |
^ decodedStream |
|
63 |
! ! |
|
64 |
||
8047 | 65 |
!EncodedStream methodsFor:'accessing'! |
66 |
||
11992 | 67 |
contentsSpecies |
68 |
||
69 |
"Not sure if this is ok" |
|
70 |
||
71 |
^ stream contentsSpecies. |
|
72 |
||
73 |
"/ ^UnicodeString |
|
74 |
||
75 |
"Created: / 14-06-2005 / 17:11:01 / janfrog" |
|
76 |
! |
|
77 |
||
8047 | 78 |
encoder |
79 |
^ encoder |
|
80 |
! |
|
81 |
||
82 |
encoder:something |
|
83 |
encoder := something. |
|
84 |
! |
|
85 |
||
12646 | 86 |
pathName |
87 |
"if our base stream hat a pathname, delegate..." |
|
88 |
||
89 |
stream isNil ifTrue:[ |
|
90 |
^ nil. |
|
91 |
]. |
|
92 |
^ stream pathName. |
|
93 |
! |
|
94 |
||
11766 | 95 |
readStream |
96 |
^ self |
|
97 |
! |
|
98 |
||
8047 | 99 |
stream |
100 |
^ stream |
|
101 |
! |
|
102 |
||
103 |
stream:something |
|
104 |
stream := something. |
|
105 |
! ! |
|
106 |
||
107 |
!EncodedStream methodsFor:'stream protocol'! |
|
108 |
||
8175 | 109 |
atEnd |
110 |
^ stream atEnd |
|
111 |
! |
|
112 |
||
8165 | 113 |
close |
114 |
stream close |
|
115 |
! |
|
116 |
||
13400 | 117 |
contents |
118 |
||
119 |
^String streamContents: |
|
120 |
[:s| |
|
121 |
[ stream atEnd ] whileFalse: |
|
122 |
[s nextPut: stream next] |
|
123 |
] |
|
124 |
||
125 |
"Created: / 25-02-2010 / 23:34:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
126 |
! |
|
127 |
||
8213 | 128 |
cr |
8459 | 129 |
self nextPutAll:(Character cr asString) |
8213 | 130 |
! |
131 |
||
11992 | 132 |
emphasis:anObject |
133 |
||
134 |
stream emphasis:anObject |
|
135 |
||
136 |
"Created: / 15-06-2005 / 11:16:33 / janfrog" |
|
137 |
! |
|
138 |
||
11946 | 139 |
isOpen |
12690 | 140 |
^ stream notNil and:[stream isOpen] |
11946 | 141 |
! |
142 |
||
11766 | 143 |
next |
11992 | 144 |
|
145 |
^encoder readNextCharacterFrom:stream |
|
146 |
||
147 |
"Created: / 14-06-2005 / 17:01:39 / janfrog" |
|
148 |
! |
|
11766 | 149 |
|
11992 | 150 |
next:charactersToRead |
151 |
||
152 |
^encoder readNext:charactersToRead charactersFrom:stream |
|
153 |
||
154 |
"Created: / 16-06-2005 / 11:43:43 / masca" |
|
11766 | 155 |
! |
156 |
||
8147 | 157 |
nextChunk |
8175 | 158 |
|chunk| |
159 |
||
160 |
chunk := stream nextChunk. |
|
161 |
chunk isNil ifTrue:[^ chunk]. |
|
162 |
^ encoder decodeString:chunk |
|
8147 | 163 |
! |
164 |
||
8213 | 165 |
nextChunkPut:chunk |
166 |
stream nextChunkPut:(encoder encodeString:chunk) |
|
167 |
! |
|
168 |
||
8047 | 169 |
nextPut:aCharacter |
170 |
self nextPutAll:(aCharacter asString). |
|
171 |
! |
|
172 |
||
173 |
nextPutAll:aCollection |
|
174 |
stream nextPutAll:(encoder encodeString:aCollection). |
|
8147 | 175 |
! |
176 |
||
8444 | 177 |
peek |
11992 | 178 |
|
179 |
^stream peek |
|
180 |
||
181 |
"Created: / 20-06-2005 / 10:13:03 / masca" |
|
182 |
"Modified: / 20-06-2005 / 13:06:14 / masca" |
|
8444 | 183 |
! |
184 |
||
8175 | 185 |
peekFor:aCharacter |
186 |
^ stream peekFor:aCharacter |
|
187 |
! |
|
188 |
||
8165 | 189 |
position |
190 |
^ stream position |
|
191 |
! |
|
192 |
||
8197 | 193 |
position0Based |
11947 | 194 |
"to be obsoleted - use position" |
195 |
||
8197 | 196 |
^ stream position0Based |
197 |
! |
|
198 |
||
199 |
position0Based:newPosition |
|
11947 | 200 |
"to be obsoleted - use position" |
201 |
||
8197 | 202 |
stream position0Based:newPosition |
203 |
! |
|
204 |
||
8175 | 205 |
position1Based |
11947 | 206 |
"to be obsoleted - use position" |
207 |
||
8175 | 208 |
^ stream position1Based |
209 |
! |
|
210 |
||
8147 | 211 |
position1Based:newPosition |
11947 | 212 |
"to be obsoleted - use position" |
213 |
||
8147 | 214 |
stream position1Based:newPosition |
8175 | 215 |
! |
216 |
||
8197 | 217 |
position:newPosition |
218 |
stream position:newPosition |
|
219 |
! |
|
220 |
||
13400 | 221 |
reset |
222 |
stream reset |
|
223 |
||
224 |
"Created: / 25-02-2010 / 23:37:14 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
225 |
! |
|
226 |
||
8213 | 227 |
setToEnd |
228 |
stream setToEnd |
|
229 |
! |
|
230 |
||
11992 | 231 |
skip: anInteger |
232 |
||
233 |
"/ Should skip on character basis, not on bytes. This works for XML reader |
|
234 |
^stream skip: anInteger |
|
235 |
||
236 |
"Created: / 20-06-2005 / 13:06:06 / masca" |
|
237 |
! |
|
238 |
||
8175 | 239 |
skipSeparators |
240 |
^ stream skipSeparators |
|
8047 | 241 |
! ! |
242 |
||
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
243 |
!EncodedStream methodsFor:'utilities'! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
244 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
245 |
skipEncodingChunk |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
246 |
|pos chunk tree| |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
247 |
|
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
248 |
stream isPositionable ifFalse:[ |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
249 |
^ self |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
250 |
]. |
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
251 |
|
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
252 |
pos := self position. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
253 |
chunk := self nextChunk. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
254 |
tree := (Parser parseExpression:chunk). |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
255 |
"/ if this is a valid chunk (i.e. not a comment or encoding-directive), |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
256 |
"/ then position back, so it will be processed as usual. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
257 |
tree notNil ifTrue:[ |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
258 |
self position:pos |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
259 |
] |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
260 |
|
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
261 |
"Modified: / 29-07-2011 / 17:42:11 / cg" |
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
262 |
! ! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
263 |
|
8047 | 264 |
!EncodedStream class methodsFor:'documentation'! |
265 |
||
13400 | 266 |
version_CVS |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
267 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.20 2011-07-29 15:43:00 cg Exp $' |
12610 | 268 |
! |
269 |
||
13400 | 270 |
version_SVN |
13525
e17ebc11ca73
changed: #skipEncodingChunk
Claus Gittinger <cg@exept.de>
parents:
13400
diff
changeset
|
271 |
^ '§ Id: EncodedStream.st 10643 2011-06-08 21:53:07Z vranyj1 §' |
8047 | 272 |
! ! |