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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 TQt library by Trolltech AS, Norway (or with modified versions 00023 of TQt that use the same license as TQt), 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 TQt. 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 <tqasciidict.h> 00036 #if defined(TQT_THREAD_SUPPORT) 00037 # include <tqmutex.h> 00038 #endif 00039 00040 #include <tqcstring.h> // TQByteArray 00041 00042 #include <kdebug.h> // for kdFatal() 00043 #include <kdepimmacros.h> 00044 00045 namespace KMime { 00046 00047 // forward declarations: 00048 class Encoder; 00049 class Decoder; 00050 00057 class KDE_EXPORT Codec { 00058 protected: 00059 00060 static TQAsciiDict<Codec>* all; 00061 #if defined(TQT_THREAD_SUPPORT) 00062 static TQMutex* 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 TQCString & 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 TQByteArray encode( const TQByteArray & src, bool withCRLF=false ) const; 00159 00171 virtual TQCString encodeToTQCString( const TQByteArray & src, bool withCRLF=false ) const; 00172 00180 virtual TQByteArray decode( const TQByteArray & 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 // if there's space in the output stream, write there: 00331 *dcursor++ = ch; 00332 return true; 00333 } else { 00334 // else buffer the output: 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 } // namespace KMime 00366 00367 #endif // __KMIME_CODECS__