author | Stefan Vogel <sv@exept.de> |
Tue, 27 Apr 2010 10:10:28 +0200 | |
changeset 12895 | 560050c70684 |
parent 12690 | 1dab7b405304 |
child 13400 | 8dcdfacd2f34 |
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 |
||
8213 | 117 |
cr |
8459 | 118 |
self nextPutAll:(Character cr asString) |
8213 | 119 |
! |
120 |
||
11992 | 121 |
emphasis:anObject |
122 |
||
123 |
stream emphasis:anObject |
|
124 |
||
125 |
"Created: / 15-06-2005 / 11:16:33 / janfrog" |
|
126 |
! |
|
127 |
||
11946 | 128 |
isOpen |
12690 | 129 |
^ stream notNil and:[stream isOpen] |
11946 | 130 |
! |
131 |
||
11766 | 132 |
next |
11992 | 133 |
|
134 |
^encoder readNextCharacterFrom:stream |
|
135 |
||
136 |
"Created: / 14-06-2005 / 17:01:39 / janfrog" |
|
137 |
! |
|
11766 | 138 |
|
11992 | 139 |
next:charactersToRead |
140 |
||
141 |
^encoder readNext:charactersToRead charactersFrom:stream |
|
142 |
||
143 |
"Created: / 16-06-2005 / 11:43:43 / masca" |
|
11766 | 144 |
! |
145 |
||
8147 | 146 |
nextChunk |
8175 | 147 |
|chunk| |
148 |
||
149 |
chunk := stream nextChunk. |
|
150 |
chunk isNil ifTrue:[^ chunk]. |
|
151 |
^ encoder decodeString:chunk |
|
8147 | 152 |
! |
153 |
||
8213 | 154 |
nextChunkPut:chunk |
155 |
stream nextChunkPut:(encoder encodeString:chunk) |
|
156 |
! |
|
157 |
||
8047 | 158 |
nextPut:aCharacter |
159 |
self nextPutAll:(aCharacter asString). |
|
160 |
! |
|
161 |
||
162 |
nextPutAll:aCollection |
|
163 |
stream nextPutAll:(encoder encodeString:aCollection). |
|
8147 | 164 |
! |
165 |
||
8444 | 166 |
peek |
11992 | 167 |
|
168 |
^stream peek |
|
169 |
||
170 |
"Created: / 20-06-2005 / 10:13:03 / masca" |
|
171 |
"Modified: / 20-06-2005 / 13:06:14 / masca" |
|
8444 | 172 |
! |
173 |
||
8175 | 174 |
peekFor:aCharacter |
175 |
^ stream peekFor:aCharacter |
|
176 |
! |
|
177 |
||
8165 | 178 |
position |
179 |
^ stream position |
|
180 |
! |
|
181 |
||
8197 | 182 |
position0Based |
11947 | 183 |
"to be obsoleted - use position" |
184 |
||
8197 | 185 |
^ stream position0Based |
186 |
! |
|
187 |
||
188 |
position0Based:newPosition |
|
11947 | 189 |
"to be obsoleted - use position" |
190 |
||
8197 | 191 |
stream position0Based:newPosition |
192 |
! |
|
193 |
||
8175 | 194 |
position1Based |
11947 | 195 |
"to be obsoleted - use position" |
196 |
||
8175 | 197 |
^ stream position1Based |
198 |
! |
|
199 |
||
8147 | 200 |
position1Based:newPosition |
11947 | 201 |
"to be obsoleted - use position" |
202 |
||
8147 | 203 |
stream position1Based:newPosition |
8175 | 204 |
! |
205 |
||
8197 | 206 |
position:newPosition |
207 |
stream position:newPosition |
|
208 |
! |
|
209 |
||
8213 | 210 |
setToEnd |
211 |
stream setToEnd |
|
212 |
! |
|
213 |
||
11992 | 214 |
skip: anInteger |
215 |
||
216 |
"/ Should skip on character basis, not on bytes. This works for XML reader |
|
217 |
^stream skip: anInteger |
|
218 |
||
219 |
"Created: / 20-06-2005 / 13:06:06 / masca" |
|
220 |
! |
|
221 |
||
8175 | 222 |
skipSeparators |
223 |
^ stream skipSeparators |
|
8047 | 224 |
! ! |
225 |
||
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
226 |
!EncodedStream methodsFor:'utilities'! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
227 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
228 |
skipEncodingChunk |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
229 |
|pos chunk tree| |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
230 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
231 |
pos := self position. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
232 |
chunk := self nextChunk. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
233 |
tree := (Parser parseExpression:chunk). |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
234 |
"/ 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
|
235 |
"/ then position back, so it will be processed as usual. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
236 |
tree notNil ifTrue:[ |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
237 |
self position:pos |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
238 |
] |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
239 |
! ! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
240 |
|
8047 | 241 |
!EncodedStream class methodsFor:'documentation'! |
242 |
||
243 |
version |
|
12690 | 244 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.18 2010-02-04 16:11:51 stefan Exp $' |
12610 | 245 |
! |
246 |
||
247 |
version_CVS |
|
12690 | 248 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.18 2010-02-04 16:11:51 stefan Exp $' |
8047 | 249 |
! ! |