00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00020 #ifndef flc_transcoding_H
00021 #define flc_transcoding_H
00022
00023 #include <falcon/setup.h>
00024 #include <falcon/types.h>
00025 #include <falcon/string.h>
00026 #include <falcon/stream.h>
00027 #include <falcon/falcondata.h>
00028
00029 namespace Falcon {
00030
00031 typedef struct tag_cp_iso_uint_table
00032 {
00033 uint16 unicode;
00034 uint16 local;
00035 } CP_ISO_UINT_TABLE;
00036
00075 class FALCON_DYN_CLASS Transcoder: public Stream
00076 {
00077 protected:
00078 Stream *m_stream;
00079 bool m_parseStatus;
00080 bool m_streamOwner;
00081
00082 Transcoder( Stream *s, bool bOwn );
00083 Transcoder( const Transcoder &other );
00084
00085 public:
00086
00087 virtual ~Transcoder();
00088
00089 virtual t_status status() const { return m_stream->status(); }
00090
00091 virtual bool isTranscoder() const { return true; }
00092
00096 virtual const String encoding() const = 0;
00097
00098
00102 Stream *underlying() const { return m_stream; }
00103
00124 bool encoderStatus() const { return m_parseStatus; }
00125
00138 void setUnderlying( Stream *s, bool owner=false );
00139
00140 virtual bool writeString( const String &source, uint32 begin=0, uint32 end = csh::npos );
00141 virtual bool readString( String &source, uint32 size );
00142
00143 protected:
00144 virtual int64 seek( int64 pos, e_whence w );
00145
00146 public:
00147
00148 virtual bool close();
00149 virtual int64 tell();
00150 virtual bool truncate( int64 pos=-1 );
00151
00152 virtual int32 read( void *buffer, int32 size ) { return m_stream->read( buffer, size ); }
00153 virtual int32 write( const void *buffer, int32 size ) { return m_stream->write( buffer, size ); }
00154 virtual bool errorDescription( ::Falcon::String &description ) const {
00155 return m_stream->errorDescription( description );
00156 }
00157
00158 virtual int32 readAvailable( int32 msecs_timeout, const Sys::SystemData *sysData = 0 ) {
00159 return m_stream->readAvailable( msecs_timeout, sysData );
00160 }
00161
00162 virtual int32 writeAvailable( int32 msecs_timeout, const Sys::SystemData *sysData ) {
00163 return m_stream->writeAvailable( msecs_timeout, sysData );
00164 }
00165
00166 virtual int64 lastError() const
00167 {
00168 return m_stream->lastError();
00169 }
00170
00171 virtual bool flush();
00172
00174 void detach() { m_stream = 0; m_streamOwner = false; }
00175 };
00176
00191 class FALCON_DYN_CLASS TranscoderEOL: public Transcoder
00192 {
00193 public:
00194 TranscoderEOL( Stream *s, bool bOwn=false ):
00195 Transcoder( s, bOwn )
00196 {}
00197 TranscoderEOL( const TranscoderEOL &other );
00198
00199 virtual bool get( uint32 &chr );
00200 virtual bool put( uint32 chr );
00201 virtual const String encoding() const { return "EOL"; }
00202 virtual FalconData *clone() const;
00203 };
00204
00216 class FALCON_DYN_CLASS TranscoderByte: public Transcoder
00217 {
00218 byte m_substitute;
00219
00220 public:
00221 TranscoderByte( Stream *s, bool bOwn=false ):
00222 Transcoder( s, bOwn ),
00223 m_substitute( (byte) '?' )
00224 {}
00225
00226 TranscoderByte( const TranscoderByte &other );
00227
00233 void substituteChar( char chr ) { m_substitute = (byte) chr; }
00234 char substituteChar() const { return m_substitute; }
00235
00236 virtual bool get( uint32 &chr );
00237 virtual bool put( uint32 chr );
00238 virtual const String encoding() const { return "byte"; }
00239 virtual FalconData *clone() const;
00240 };
00241
00243 class TranscoderUTF8: public Transcoder
00244 {
00245 public:
00246 TranscoderUTF8( Stream *s, bool bOwn=false ):
00247 Transcoder( s, bOwn )
00248 {}
00249
00250 TranscoderUTF8( const TranscoderUTF8 &other );
00251
00252 virtual bool get( uint32 &chr );
00253 virtual bool put( uint32 chr );
00254 virtual const String encoding() const { return "utf-8"; }
00255 virtual FalconData *clone() const;
00256 };
00257
00259 class TranscoderUTF16: public Transcoder
00260 {
00261 public:
00263 typedef enum {
00264 e_detect,
00265 e_le,
00266 e_be
00267 } t_endianity;
00268
00269 private:
00270 t_endianity m_defEndian;
00271 t_endianity m_streamEndian;
00272 t_endianity m_hostEndian;
00273
00274 bool m_bFirstIn;
00275 bool m_bFirstOut;
00276
00277 protected:
00278 bool m_bom;
00279
00280 public:
00281
00307 TranscoderUTF16( Stream *s, bool bOwn=false, t_endianity endianity = e_detect );
00308 TranscoderUTF16( const TranscoderUTF16 &other );
00309
00310 virtual bool get( uint32 &chr );
00311 virtual bool put( uint32 chr );
00312 virtual const String encoding() const { return "utf-16"; }
00313 t_endianity endianity() const { return m_streamEndian; }
00314 virtual FalconData *clone() const;
00315 };
00316
00329 FALCON_DYN_SYM Transcoder *TranscoderFactory( const String &encoding, Stream *stream=0, bool own = false );
00330
00344 FALCON_DYN_SYM bool TranscodeString( const String &source, const String &encoding, String &target );
00345
00346
00360 FALCON_DYN_SYM bool TranscodeFromString( const String &source, const String &encoding, String &target );
00361
00374 FALCON_DYN_SYM bool GetSystemEncoding( String &encoding );
00375
00376 }
00377
00378 #endif
00379
00380