• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kbzip2filter.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000-2005 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kbzip2filter.h"
00021 
00022 #include <config.h>
00023 
00024 #if defined( HAVE_BZIP2_SUPPORT )
00025 
00026 // we don't need that
00027 #define BZ_NO_STDIO
00028 extern "C" {
00029     #include <bzlib.h>
00030 }
00031 
00032 #ifdef NEED_BZ2_PREFIX
00033         #define bzDecompressInit(x,y,z) BZ2_bzDecompressInit(x,y,z)
00034         #define bzDecompressEnd(x) BZ2_bzDecompressEnd(x)
00035         #define bzCompressEnd(x)  BZ2_bzCompressEnd(x)
00036         #define bzDecompress(x) BZ2_bzDecompress(x)
00037         #define bzCompress(x,y) BZ2_bzCompress(x, y)
00038         #define bzCompressInit(x,y,z,a) BZ2_bzCompressInit(x, y, z, a);
00039 #endif
00040 
00041 #include <kdebug.h>
00042 
00043 #include <qiodevice.h>
00044 
00045 
00046 
00047 // For docu on this, see /usr/doc/bzip2-0.9.5d/bzip2-0.9.5d/manual_3.html
00048 
00049 class KBzip2Filter::Private
00050 {
00051 public:
00052     Private()
00053     {
00054         memset(&zStream, 0, sizeof(zStream));
00055         mode = 0;
00056     }
00057 
00058     bz_stream zStream;
00059     int mode;
00060 };
00061 
00062 KBzip2Filter::KBzip2Filter()
00063     :d(new Private)
00064 {
00065 }
00066 
00067 
00068 KBzip2Filter::~KBzip2Filter()
00069 {
00070     delete d;
00071 }
00072 
00073 void KBzip2Filter::init( int mode )
00074 {
00075     d->zStream.next_in = 0;
00076     d->zStream.avail_in = 0;
00077     if ( mode == QIODevice::ReadOnly )
00078     {
00079         (void)bzDecompressInit(&d->zStream, 0, 0);
00080         //kDebug(7118) << "bzDecompressInit returned " << result;
00081         // No idea what to do with result :)
00082     } else if ( mode == QIODevice::WriteOnly ) {
00083         (void)bzCompressInit(&d->zStream, 5, 0, 0);
00084         //kDebug(7118) << "bzDecompressInit returned " << result;
00085     } else
00086         kWarning(7118) << "Unsupported mode " << mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
00087     d->mode = mode;
00088 }
00089 
00090 int KBzip2Filter::mode() const
00091 {
00092     return d->mode;
00093 }
00094 
00095 void KBzip2Filter::terminate()
00096 {
00097     if ( d->mode == QIODevice::ReadOnly )
00098     {
00099         int result = bzDecompressEnd(&d->zStream);
00100         kDebug(7118) << "bzDecompressEnd returned " << result;
00101     } else if ( d->mode == QIODevice::WriteOnly )
00102     {
00103         int result = bzCompressEnd(&d->zStream);
00104         kDebug(7118) << "bzCompressEnd returned " << result;
00105     } else
00106         kWarning(7118) << "Unsupported mode " << d->mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
00107 }
00108 
00109 
00110 void KBzip2Filter::reset()
00111 {
00112     kDebug(7118) << "KBzip2Filter::reset";
00113     // bzip2 doesn't seem to have a reset call...
00114     terminate();
00115     init( d->mode );
00116 }
00117 
00118 void KBzip2Filter::setOutBuffer( char * data, uint maxlen )
00119 {
00120     d->zStream.avail_out = maxlen;
00121     d->zStream.next_out = data;
00122 }
00123 
00124 void KBzip2Filter::setInBuffer( const char *data, unsigned int size )
00125 {
00126     d->zStream.avail_in = size;
00127     d->zStream.next_in = const_cast<char *>(data);
00128 }
00129 
00130 int KBzip2Filter::inBufferAvailable() const
00131 {
00132     return d->zStream.avail_in;
00133 }
00134 
00135 int KBzip2Filter::outBufferAvailable() const
00136 {
00137     return d->zStream.avail_out;
00138 }
00139 
00140 KBzip2Filter::Result KBzip2Filter::uncompress()
00141 {
00142     //kDebug(7118) << "Calling bzDecompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
00143     int result = bzDecompress(&d->zStream);
00144     if ( result != BZ_OK )
00145     {
00146         kDebug(7118) << "bzDecompress returned " << result;
00147         kDebug(7118) << "KBzip2Filter::uncompress " << ( result == BZ_STREAM_END ? KFilterBase::End : KFilterBase::Error );
00148     }
00149 
00150     switch (result) {
00151         case BZ_OK:
00152                 return KFilterBase::Ok;
00153         case BZ_STREAM_END:
00154                 return KFilterBase::End;
00155         default:
00156                 return KFilterBase::Error;
00157     }
00158 }
00159 
00160 KBzip2Filter::Result KBzip2Filter::compress( bool finish )
00161 {
00162     //kDebug(7118) << "Calling bzCompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
00163     int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN );
00164 
00165     switch (result) {
00166         case BZ_OK:
00167         case BZ_FLUSH_OK:
00168         case BZ_RUN_OK:
00169         case BZ_FINISH_OK:
00170                 return KFilterBase::Ok;
00171                 break;
00172         case BZ_STREAM_END:
00173                 kDebug(7118) << "  bzCompress returned " << result;
00174                 return KFilterBase::End;
00175         break;
00176         default:
00177                 kDebug(7118) << "  bzCompress returned " << result;
00178                 return KFilterBase::Error;
00179                 break;
00180     }
00181 }
00182 
00183 #endif  /* HAVE_BZIP2_SUPPORT */

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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