KMIME Library
kmime_codecs.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00060 #ifndef __KMIME_CODECS__
00061 #define __KMIME_CODECS__
00062
00063 #include <QtCore/QByteArray>
00064
00065 #include <kdebug.h>
00066
00067 #include "kmime_export.h"
00068
00069 namespace KMime {
00070
00071 template <class Key, class T> class KAutoDeleteHash;
00072
00073 class Encoder;
00074 class Decoder;
00075
00083 class KMIME_EXPORT Codec
00084 {
00085 protected:
00086
00087 static KAutoDeleteHash<QByteArray, Codec> *all;
00088 static void cleanupCodec();
00089
00093 Codec() {}
00094
00095 public:
00101 static Codec *codecForName( const char *name );
00102
00108 static Codec *codecForName( const QByteArray &name );
00109
00118 virtual int maxEncodedSizeFor( int insize, bool withCRLF=false ) const = 0;
00119
00128 virtual int maxDecodedSizeFor( int insize, bool withCRLF=false ) const = 0;
00129
00137 virtual Encoder *makeEncoder( bool withCRLF=false ) const = 0;
00138
00146 virtual Decoder *makeDecoder( bool withCRLF=false ) const = 0;
00147
00182 virtual bool encode( const char* &scursor, const char * const send,
00183 char* &dcursor, const char * const dend,
00184 bool withCRLF=false ) const;
00185
00220 virtual bool decode( const char* &scursor, const char * const send,
00221 char* &dcursor, const char * const dend,
00222 bool withCRLF=false ) const;
00223
00234 virtual QByteArray encode( const QByteArray &src, bool withCRLF=false ) const;
00235
00246 virtual QByteArray decode( const QByteArray &src, bool withCRLF=false ) const;
00247
00251 virtual const char *name() const = 0;
00252
00256 virtual ~Codec() {}
00257
00258 private:
00262 static void fillDictionary();
00263 };
00264
00341 class Decoder
00342 {
00343 protected:
00344 friend class Codec;
00351 Decoder( bool withCRLF=false )
00352 : mWithCRLF( withCRLF ) {}
00353
00354 public:
00358 virtual ~Decoder() {}
00359
00369 virtual bool decode( const char* &scursor, const char * const send,
00370 char* &dcursor, const char * const dend ) = 0;
00371
00380 virtual bool finish( char* &dcursor, const char * const dend ) = 0;
00381
00382 protected:
00383
00384 const bool mWithCRLF;
00385
00386 };
00387
00394 class Encoder
00395 {
00396 protected:
00397 friend class Codec;
00403 explicit Encoder( bool withCRLF=false )
00404 : mOutputBufferCursor( 0 ), mWithCRLF( withCRLF ) {}
00405
00406 public:
00410 virtual ~Encoder() {}
00411
00421 virtual bool encode( const char* &scursor, const char * const send,
00422 char* &dcursor, const char * const dend ) = 0;
00423
00431 virtual bool finish( char* &dcursor, const char * const dend ) = 0;
00432
00433 protected:
00437 enum {
00438 maxBufferedChars = 8
00439 };
00440
00451 bool write( char ch, char* &dcursor, const char * const dend )
00452 {
00453 if ( dcursor != dend ) {
00454
00455 *dcursor++ = ch;
00456 return true;
00457 } else {
00458
00459 kFatal( mOutputBufferCursor >= maxBufferedChars )
00460 << "KMime::Encoder: internal buffer overflow!";
00461 mOutputBuffer[ mOutputBufferCursor++ ] = ch;
00462 return false;
00463 }
00464 }
00465
00476 bool flushOutputBuffer( char* &dcursor, const char * const dend );
00477
00485 bool writeCRLF( char* &dcursor, const char * const dend )
00486 {
00487 if ( mWithCRLF ) {
00488 write( '\r', dcursor, dend );
00489 }
00490 return write( '\n', dcursor, dend );
00491 }
00492
00493 private:
00498
00499 char mOutputBuffer[ maxBufferedChars ];
00500
00501
00502 protected:
00503
00504 uchar mOutputBufferCursor;
00505 const bool mWithCRLF;
00506
00507 };
00508
00509 }
00510
00511 #endif // __KMIME_CODECS__