libkmime
kmime_codecs.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __KMIME_CODECS__
00033 #define __KMIME_CODECS__
00034
00035 #include <qasciidict.h>
00036 #if defined(QT_THREAD_SUPPORT)
00037 # include <qmutex.h>
00038 #endif
00039
00040 #include <qcstring.h>
00041
00042 #include <kdebug.h>
00043 #include <kdepimmacros.h>
00044
00045 namespace KMime {
00046
00047
00048 class Encoder;
00049 class Decoder;
00050
00057 class KDE_EXPORT Codec {
00058 protected:
00059
00060 static QAsciiDict<Codec>* all;
00061 #if defined(QT_THREAD_SUPPORT)
00062 static QMutex* dictLock;
00063 #endif
00064
00065 Codec() {}
00066 private:
00067 static void fillDictionary();
00068
00069 public:
00070 static Codec * codecForName( const char * name );
00071 static Codec * codecForName( const QCString & name );
00072
00073 virtual int maxEncodedSizeFor( int insize, bool withCRLF=false ) const = 0;
00074 virtual int maxDecodedSizeFor( int insize, bool withCRLF=false ) const = 0;
00075
00076 virtual Encoder * makeEncoder( bool withCRLF=false ) const = 0;
00077 virtual Decoder * makeDecoder( bool withCRLF=false ) const = 0;
00078
00111 virtual bool encode( const char* & scursor, const char * const send,
00112 char* & dcursor, const char * const dend,
00113 bool withCRLF=false ) const;
00114
00147 virtual bool decode( const char* & scursor, const char * const send,
00148 char* & dcursor, const char * const dend,
00149 bool withCRLF=false ) const;
00150
00158 virtual QByteArray encode( const QByteArray & src, bool withCRLF=false ) const;
00159
00171 virtual QCString encodeToQCString( const QByteArray & src, bool withCRLF=false ) const;
00172
00180 virtual QByteArray decode( const QByteArray & src, bool withCRLF=false ) const;
00181
00185 virtual const char * name() const = 0;
00186
00187 virtual ~Codec() {}
00188
00189 };
00190
00268 class Decoder {
00269 protected:
00270 friend class Codec;
00276 Decoder( bool withCRLF=false )
00277 : mWithCRLF( withCRLF ) {}
00278 public:
00279 virtual ~Decoder() {}
00280
00284 virtual bool decode( const char* & scursor, const char * const send,
00285 char* & dcursor, const char * const dend ) = 0;
00290 virtual bool finish( char* & dcursor, const char * const dend ) = 0;
00291
00292 protected:
00293 const bool mWithCRLF;
00294 };
00295
00300 class Encoder {
00301 protected:
00302 friend class Codec;
00306 Encoder( bool withCRLF=false )
00307 : mOutputBufferCursor( 0 ), mWithCRLF( withCRLF ) {}
00308 public:
00309 virtual ~Encoder() {}
00310
00313 virtual bool encode( const char* & scursor, const char * const send,
00314 char* & dcursor, const char * const dend ) = 0;
00315
00319 virtual bool finish( char* & dcursor, const char * const dend ) = 0;
00320
00321 protected:
00323 enum { maxBufferedChars = 8 };
00324
00328 bool write( char ch, char* & dcursor, const char * const dend ) {
00329 if ( dcursor != dend ) {
00330
00331 *dcursor++ = ch;
00332 return true;
00333 } else {
00334
00335 kdFatal( mOutputBufferCursor >= maxBufferedChars )
00336 << "KMime::Encoder: internal buffer overflow!" << endl;
00337 mOutputBuffer[ mOutputBufferCursor++ ] = ch;
00338 return false;
00339 }
00340 }
00341
00346 bool flushOutputBuffer( char* & dcursor, const char * const dend );
00347
00350 bool writeCRLF( char* & dcursor, const char * const dend ) {
00351 if ( mWithCRLF )
00352 write( '\r', dcursor, dend );
00353 return write( '\n', dcursor, dend );
00354 }
00355
00356 private:
00359 char mOutputBuffer[ maxBufferedChars ];
00360 protected:
00361 uchar mOutputBufferCursor;
00362 const bool mWithCRLF;
00363 };
00364
00365 }
00366
00367 #endif // __KMIME_CODECS__
|