• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

KMIME Library

kmime_codecs.h

Go to the documentation of this file.
00001 /*  -*- c++ -*-
00002 
00003     KMime, the KDE internet mail/usenet news message library.
00004     Copyright (c) 2001-2002 Marc Mutz <mutz@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00060 #ifndef __KMIME_CODECS__
00061 #define __KMIME_CODECS__
00062 
00063 #include <QtCore/QByteArray>
00064 
00065 #include <kdebug.h> // for kFatal()
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     //@cond PRIVATE
00087     static KAutoDeleteHash<QByteArray, Codec> *all;
00088     static void cleanupCodec();
00089     //@endcond
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     //@cond PRIVATE
00384     const bool mWithCRLF;
00385     //@endcond
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           // if there's space in the output stream, write there:
00455           *dcursor++ = ch;
00456           return true;
00457         } else {
00458           // else buffer the output:
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     //@cond PRIVATE
00499     char mOutputBuffer[ maxBufferedChars ];
00500     //@endcond
00501 
00502   protected:
00503     //@cond PRIVATE
00504     uchar mOutputBufferCursor;
00505     const bool mWithCRLF;
00506     //@endcond
00507 };
00508 
00509 } // namespace KMime
00510 
00511 #endif // __KMIME_CODECS__

KMIME Library

Skip menu "KMIME Library"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.6
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal