EncodedStream.st
changeset 15049 3c4d74d39396
parent 14333 59c0d7e60297
child 15345 8f04bcf567e6
child 18045 c0c600e0d3b3
equal deleted inserted replaced
15048:ccc1e3a06733 15049:3c4d74d39396
    13 
    13 
    14 PeekableStream subclass:#EncodedStream
    14 PeekableStream subclass:#EncodedStream
    15 	instanceVariableNames:'encoder stream'
    15 	instanceVariableNames:'encoder stream'
    16 	classVariableNames:''
    16 	classVariableNames:''
    17 	poolDictionaries:''
    17 	poolDictionaries:''
    18 	category:'Collections-Text-Encodings'
    18 	category:'Streams-Misc'
    19 !
    19 !
    20 
    20 
    21 !EncodedStream class methodsFor:'documentation'!
    21 !EncodedStream class methodsFor:'documentation'!
    22 
    22 
    23 copyright
    23 copyright
    30  inclusion of the above copyright notice.   This software may not
    30  inclusion of the above copyright notice.   This software may not
    31  be provided or otherwise made available to, or used by, any
    31  be provided or otherwise made available to, or used by, any
    32  other person.  No title to or ownership of the software is
    32  other person.  No title to or ownership of the software is
    33  hereby transferred.
    33  hereby transferred.
    34 "
    34 "
       
    35 !
       
    36 
       
    37 documentation
       
    38 "
       
    39         a stream which transparently decodes from an external decoding,
       
    40         looking for '---- Encoding: xxx' near the beginning of the file.
       
    41 
       
    42         especially targeted towards reading ST/X source files.
       
    43 "
    35 ! !
    44 ! !
    36 
    45 
    37 !EncodedStream class methodsFor:'instance creation'!
    46 !EncodedStream class methodsFor:'instance creation'!
    38 
    47 
    39 stream:streamArg encoder:encoder
    48 stream:streamArg encoder:encoder
    41 ! !
    50 ! !
    42 
    51 
    43 !EncodedStream class methodsFor:'utilities'!
    52 !EncodedStream class methodsFor:'utilities'!
    44 
    53 
    45 decodedStreamFor:aStream
    54 decodedStreamFor:aStream
    46     |encoding decoder decodedStream|
    55     |encodingSymbol decoder decodedStream|
    47 
    56 
    48     aStream isPositionable ifTrue:[
    57     aStream isPositionable ifTrue:[
    49         encoding := CharacterEncoder guessEncodingOfStream:aStream.
    58         encodingSymbol := CharacterEncoder guessEncodingOfStream:aStream.
    50         (encoding notNil
    59         decoder := self encoderFor:encodingSymbol.
    51         and:[ encoding ~= #'iso8859-1' 
    60         decoder isNil ifTrue:[
    52         and:[ encoding ~= #'ascii' ]]) ifTrue:[
    61             "/ ascii or iso8895-1
    53             decoder := CharacterEncoder encoderFor:encoding.
    62             ^ aStream
    54         ].
    63         ].
    55     ].
    64     ] ifFalse:[
    56     decoder isNil ifTrue:[
    65         "/ setup for no-encoding; 
    57         "/ setup for no-encoding; allows for a later switch to a real encoder,
    66         "/ switch to a real encoder later,
    58         "/ whenever an encoding pragma is encountered later.
    67         "/ whenever an encoding pragma is encountered later.
    59         decoder := CharacterEncoder nullEncoderInstance.
    68         decoder := CharacterEncoder nullEncoderInstance.
    60     ].
    69     ].
    61     decodedStream := self stream:aStream encoder:decoder.
    70     decodedStream := self stream:aStream encoder:decoder.
    62     decodedStream skipEncodingChunk.
    71     decodedStream skipEncodingChunk.
    63     ^ decodedStream
    72     ^ decodedStream
       
    73 !
       
    74 
       
    75 encoderFor:anEncodingSymbol
       
    76     (anEncodingSymbol isNil
       
    77     or:[ anEncodingSymbol = #'iso8859-1' 
       
    78     or:[ anEncodingSymbol = #'ascii' ]]) ifTrue:[
       
    79         ^ nil
       
    80     ].
       
    81     ^ CharacterEncoder encoderFor:anEncodingSymbol.
    64 ! !
    82 ! !
    65 
    83 
    66 !EncodedStream methodsFor:'accessing'!
    84 !EncodedStream methodsFor:'accessing'!
    67 
    85 
    68 contentsSpecies
    86 contentsSpecies
   154 
   172 
   155     "Created: / 16-06-2005 / 11:43:43 / masca"
   173     "Created: / 16-06-2005 / 11:43:43 / masca"
   156 !
   174 !
   157 
   175 
   158 nextChunk
   176 nextChunk
   159     |chunk|
   177     "as a side effect, check for an encoding chunk"
       
   178 
       
   179     |prevEncoder chunk enc|
   160 
   180 
   161     chunk := stream nextChunk.
   181     chunk := stream nextChunk.
   162     chunk isNil ifTrue:[^ chunk].
   182     chunk isNil ifTrue:[^ chunk].
   163     ^ encoder decodeString:chunk
   183 
       
   184     prevEncoder := encoder.
       
   185     stream isPositionable ifFalse:[
       
   186         "/ not already checked
       
   187         encoder isNullEncoder ifTrue:[
       
   188             "/ check if we need lazy setup of the encoder
       
   189             "/ (used with non-positionable streams)
       
   190             (chunk includesString:'---- Encoding:') ifTrue:[
       
   191                 enc := self class encoderFor:(CharacterEncoder guessEncodingOfBuffer:chunk).
       
   192                 encoder := enc ? encoder.
       
   193             ]
       
   194         ].
       
   195     ].
       
   196 
       
   197     ^ prevEncoder decodeString:chunk
   164 !
   198 !
   165 
   199 
   166 nextChunkPut:chunk
   200 nextChunkPut:chunk
   167     stream nextChunkPut:(encoder encodeString:chunk)
   201     stream nextChunkPut:(encoder encodeString:chunk)
   168 !
   202 !
   245     "Created: / 20-06-2005 / 13:06:06 / masca"
   279     "Created: / 20-06-2005 / 13:06:06 / masca"
   246 !
   280 !
   247 
   281 
   248 skipSeparators
   282 skipSeparators
   249     ^ stream skipSeparators
   283     ^ stream skipSeparators
       
   284 ! !
       
   285 
       
   286 !EncodedStream methodsFor:'testing'!
       
   287 
       
   288 isEncodedStream
       
   289     ^ true
   250 ! !
   290 ! !
   251 
   291 
   252 !EncodedStream methodsFor:'utilities'!
   292 !EncodedStream methodsFor:'utilities'!
   253 
   293 
   254 skipEncodingChunk
   294 skipEncodingChunk
   289 ! !
   329 ! !
   290 
   330 
   291 !EncodedStream class methodsFor:'documentation'!
   331 !EncodedStream class methodsFor:'documentation'!
   292 
   332 
   293 version
   333 version
   294     ^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.26 2012-08-31 18:01:03 cg Exp $'
   334     ^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.27 2013-04-03 16:10:25 cg Exp $'
   295 !
   335 !
   296 
   336 
   297 version_CVS
   337 version_CVS
   298     ^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.26 2012-08-31 18:01:03 cg Exp $'
   338     ^ '$Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.27 2013-04-03 16:10:25 cg Exp $'
   299 !
   339 !
   300 
   340 
   301 version_SVN
   341 version_SVN
   302     ^ '§ Id: EncodedStream.st 10643 2011-06-08 21:53:07Z vranyj1  §'
   342     ^ '§ Id: EncodedStream.st 10643 2011-06-08 21:53:07Z vranyj1  §'
   303 ! !
   343 ! !
       
   344