libkdenetwork Library API Documentation

kmime_codecs.h

00001 /*  -*- c++ -*-
00002     kmime_codecs.h
00003 
00004     This file is part of KMime, the KDE internet mail/usenet news message library.
00005     Copyright (c) 2001-2002 Marc Mutz <mutz@kde.org>
00006 
00007     KMime is free software; you can redistribute it and/or modify it
00008     under the terms of the GNU General Public License, version 2, as
00009     published by the Free Software Foundation.
00010 
00011     KMime is distributed in the hope that it will be useful, but
00012     WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this library; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020     In addition, as a special exception, the copyright holders give
00021     permission to link the code of this library with any edition of
00022     the Qt library by Trolltech AS, Norway (or with modified versions
00023     of Qt that use the same license as Qt), and distribute linked
00024     combinations including the two.  You must obey the GNU General
00025     Public License in all respects for all of the code used other than
00026     Qt.  If you modify this file, you may extend this exception to
00027     your version of the file, but you are not obligated to do so.  If
00028     you do not wish to do so, delete this exception statement from
00029     your version.
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> // QByteArray
00041 
00042 #include <kdebug.h> // for kdFatal()
00043 
00044 namespace KMime {
00045 
00046 // forward declarations:
00047 class Encoder;
00048 class Decoder;
00049 
00056 class Codec {
00057 protected:
00058 
00059   static QAsciiDict<Codec> all;
00060 #if defined(QT_THREAD_SUPPORT)
00061   static QMutex dictLock;
00062 #endif
00063 
00064   Codec() {}
00065 private:
00066   static void fillDictionary();
00067   
00068 public:
00069   static Codec * codecForName( const char * name );
00070   static Codec * codecForName( const QCString & name );
00071 
00072   virtual int maxEncodedSizeFor( int insize, bool withCRLF=false ) const = 0;
00073   virtual int maxDecodedSizeFor( int insize, bool withCRLF=false ) const = 0;
00074   
00075   virtual Encoder * makeEncoder( bool withCRLF=false ) const = 0;
00076   virtual Decoder * makeDecoder( bool withCRLF=false ) const = 0;
00077 
00110   virtual bool encode( const char* & scursor, const char * const send,
00111                char* & dcursor, const char * const dend,
00112                bool withCRLF=false ) const;
00113   
00146   virtual bool decode( const char* & scursor, const char * const send,
00147                char* & dcursor, const char * const dend,
00148                bool withCRLF=false ) const;
00149 
00157   virtual QByteArray encode( const QByteArray & src, bool withCRLF=false ) const;
00158 
00170   virtual QCString encodeToQCString( const QByteArray & src, bool withCRLF=false ) const;
00171 
00179   virtual QByteArray decode( const QByteArray & src, bool withCRLF=false ) const;
00180 
00184   virtual const char * name() const = 0;
00185   
00186   virtual ~Codec() {}
00187   
00188 };
00189   
00267 class Decoder {
00268 protected:
00269   friend class Codec;
00275   Decoder( bool withCRLF=false )
00276     : mWithCRLF( withCRLF ) {}
00277 public:
00278   virtual ~Decoder() {}
00279 
00283   virtual bool decode( const char* & scursor, const char * const send,
00284                char* & dcursor, const char * const dend ) = 0;
00289   virtual bool finish( char* & dcursor, const char * const dend ) = 0;
00290 
00291 protected:
00292   const bool mWithCRLF;
00293 };
00294   
00299 class Encoder {
00300 protected:
00301   friend class Codec;
00305   Encoder( bool withCRLF=false )
00306     : mOutputBufferCursor( 0 ), mWithCRLF( withCRLF ) {}
00307 public:
00308   virtual ~Encoder() {}
00309 
00312   virtual bool encode( const char* & scursor, const char * const send,
00313                char* & dcursor, const char * const dend ) = 0;
00314 
00318   virtual bool finish( char* & dcursor, const char * const dend ) = 0;
00319 
00320 protected:
00322   enum { maxBufferedChars = 8 };
00323 
00327   bool write( char ch, char* & dcursor, const char * const dend ) {
00328     if ( dcursor != dend ) {
00329       // if there's space in the output stream, write there:
00330       *dcursor++ = ch;
00331       return true;
00332     } else {
00333       // else buffer the output:
00334       kdFatal( mOutputBufferCursor >= maxBufferedChars )
00335     << "KMime::Encoder: internal buffer overflow!" << endl;
00336       mOutputBuffer[ mOutputBufferCursor++ ] = ch;
00337       return false;
00338     }
00339   }
00340 
00345   bool flushOutputBuffer( char* & dcursor, const char * const dend );
00346 
00349   bool writeCRLF( char* & dcursor, const char * const dend ) {
00350     if ( mWithCRLF )
00351       write( '\r', dcursor, dend );
00352     return write( '\n', dcursor, dend );
00353   }
00354 
00355 private:
00358   char mOutputBuffer[ maxBufferedChars ];
00359 protected:
00360   uchar mOutputBufferCursor;
00361   const bool mWithCRLF;
00362 };
00363 
00364 } // namespace KMime
00365 
00366 #endif // __KMIME_CODECS__
KDE Logo
This file is part of the documentation for libkdenetwork Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Aug 23 18:18:10 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003