author | Claus Gittinger <cg@exept.de> |
Fri, 18 Dec 2009 15:40:48 +0100 | |
changeset 12628 | 200526f05728 |
parent 12610 | cfbb45980e28 |
child 12646 | ee2144f97338 |
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 |
||
11766 | 86 |
readStream |
87 |
^ self |
|
88 |
! |
|
89 |
||
8047 | 90 |
stream |
91 |
^ stream |
|
92 |
! |
|
93 |
||
94 |
stream:something |
|
95 |
stream := something. |
|
96 |
! ! |
|
97 |
||
98 |
!EncodedStream methodsFor:'stream protocol'! |
|
99 |
||
8175 | 100 |
atEnd |
101 |
^ stream atEnd |
|
102 |
! |
|
103 |
||
8165 | 104 |
close |
105 |
stream close |
|
106 |
! |
|
107 |
||
8213 | 108 |
cr |
8459 | 109 |
self nextPutAll:(Character cr asString) |
8213 | 110 |
! |
111 |
||
11992 | 112 |
emphasis:anObject |
113 |
||
114 |
stream emphasis:anObject |
|
115 |
||
116 |
"Created: / 15-06-2005 / 11:16:33 / janfrog" |
|
117 |
! |
|
118 |
||
11946 | 119 |
isOpen |
120 |
^ stream isOpen |
|
121 |
! |
|
122 |
||
11766 | 123 |
next |
11992 | 124 |
|
125 |
^encoder readNextCharacterFrom:stream |
|
126 |
||
127 |
"Created: / 14-06-2005 / 17:01:39 / janfrog" |
|
128 |
! |
|
11766 | 129 |
|
11992 | 130 |
next:charactersToRead |
131 |
||
132 |
^encoder readNext:charactersToRead charactersFrom:stream |
|
133 |
||
134 |
"Created: / 16-06-2005 / 11:43:43 / masca" |
|
11766 | 135 |
! |
136 |
||
8147 | 137 |
nextChunk |
8175 | 138 |
|chunk| |
139 |
||
140 |
chunk := stream nextChunk. |
|
141 |
chunk isNil ifTrue:[^ chunk]. |
|
142 |
^ encoder decodeString:chunk |
|
8147 | 143 |
! |
144 |
||
8213 | 145 |
nextChunkPut:chunk |
146 |
stream nextChunkPut:(encoder encodeString:chunk) |
|
147 |
! |
|
148 |
||
8047 | 149 |
nextPut:aCharacter |
150 |
self nextPutAll:(aCharacter asString). |
|
151 |
! |
|
152 |
||
153 |
nextPutAll:aCollection |
|
154 |
stream nextPutAll:(encoder encodeString:aCollection). |
|
8147 | 155 |
! |
156 |
||
8444 | 157 |
peek |
11992 | 158 |
|
159 |
^stream peek |
|
160 |
||
161 |
"Created: / 20-06-2005 / 10:13:03 / masca" |
|
162 |
"Modified: / 20-06-2005 / 13:06:14 / masca" |
|
8444 | 163 |
! |
164 |
||
8175 | 165 |
peekFor:aCharacter |
166 |
^ stream peekFor:aCharacter |
|
167 |
! |
|
168 |
||
8165 | 169 |
position |
170 |
^ stream position |
|
171 |
! |
|
172 |
||
8197 | 173 |
position0Based |
11947 | 174 |
"to be obsoleted - use position" |
175 |
||
8197 | 176 |
^ stream position0Based |
177 |
! |
|
178 |
||
179 |
position0Based:newPosition |
|
11947 | 180 |
"to be obsoleted - use position" |
181 |
||
8197 | 182 |
stream position0Based:newPosition |
183 |
! |
|
184 |
||
8175 | 185 |
position1Based |
11947 | 186 |
"to be obsoleted - use position" |
187 |
||
8175 | 188 |
^ stream position1Based |
189 |
! |
|
190 |
||
8147 | 191 |
position1Based:newPosition |
11947 | 192 |
"to be obsoleted - use position" |
193 |
||
8147 | 194 |
stream position1Based:newPosition |
8175 | 195 |
! |
196 |
||
8197 | 197 |
position:newPosition |
198 |
stream position:newPosition |
|
199 |
! |
|
200 |
||
8213 | 201 |
setToEnd |
202 |
stream setToEnd |
|
203 |
! |
|
204 |
||
11992 | 205 |
skip: anInteger |
206 |
||
207 |
"/ Should skip on character basis, not on bytes. This works for XML reader |
|
208 |
^stream skip: anInteger |
|
209 |
||
210 |
"Created: / 20-06-2005 / 13:06:06 / masca" |
|
211 |
! |
|
212 |
||
8175 | 213 |
skipSeparators |
214 |
^ stream skipSeparators |
|
8047 | 215 |
! ! |
216 |
||
8712
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
217 |
!EncodedStream methodsFor:'utilities'! |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
218 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
219 |
skipEncodingChunk |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
220 |
|pos chunk tree| |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
221 |
|
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
222 |
pos := self position. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
223 |
chunk := self nextChunk. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
224 |
tree := (Parser parseExpression:chunk). |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
225 |
"/ 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
|
226 |
"/ then position back, so it will be processed as usual. |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
227 |
tree notNil ifTrue:[ |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
228 |
self position:pos |
bcef3a0c0a1d
skipEncodingCHunk now in EncodedStream
Claus Gittinger <cg@exept.de>
parents:
8459
diff
changeset
|
229 |
] |
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 |
|
8047 | 232 |
!EncodedStream class methodsFor:'documentation'! |
233 |
||
234 |
version |
|
12610 | 235 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.16 2009-12-11 16:54:29 cg Exp $' |
236 |
! |
|
237 |
||
238 |
version_CVS |
|
239 |
^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.16 2009-12-11 16:54:29 cg Exp $' |
|
8047 | 240 |
! ! |