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