• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • 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-compression.h>
00023 
00024 #if 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 #if 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 <QDebug>
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     : isInitialized(false)
00054     {
00055         memset(&zStream, 0, sizeof(zStream));
00056         mode = 0;
00057     }
00058 
00059     bz_stream zStream;
00060     int mode;
00061     bool isInitialized;
00062 };
00063 
00064 KBzip2Filter::KBzip2Filter()
00065     :d(new Private)
00066 {
00067 }
00068 
00069 
00070 KBzip2Filter::~KBzip2Filter()
00071 {
00072     delete d;
00073 }
00074 
00075 void KBzip2Filter::init( int mode )
00076 {
00077     if (d->isInitialized) {
00078         terminate();
00079     }
00080 
00081     d->zStream.next_in = 0;
00082     d->zStream.avail_in = 0;
00083     if ( mode == QIODevice::ReadOnly )
00084     {
00085         (void)bzDecompressInit(&d->zStream, 0, 0);
00086         //qDebug() << "bzDecompressInit returned " << result;
00087         // TODO: test result and return false on error
00088     } else if ( mode == QIODevice::WriteOnly ) {
00089         (void)bzCompressInit(&d->zStream, 5, 0, 0);
00090         //qDebug() << "bzDecompressInit returned " << result;
00091         // TODO: test result and return false on error
00092     } else {
00093         qWarning() << "Unsupported mode " << mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
00094         // TODO return false
00095     }
00096     d->mode = mode;
00097     d->isInitialized = true;
00098 }
00099 
00100 int KBzip2Filter::mode() const
00101 {
00102     return d->mode;
00103 }
00104 
00105 void KBzip2Filter::terminate()
00106 {
00107     if (d->mode == QIODevice::ReadOnly) {
00108         int result = bzDecompressEnd(&d->zStream);
00109         // TODO: test result and return false on error
00110         //qDebug() << "bzDecompressEnd returned " << result;
00111     } else if (d->mode == QIODevice::WriteOnly) {
00112         int result = bzCompressEnd(&d->zStream);
00113         // TODO: test result and return false on error
00114         //qDebug() << "bzCompressEnd returned " << result;
00115     } else {
00116         qWarning() << "Unsupported mode " << d->mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
00117         // TODO return false
00118     }
00119     d->isInitialized = false;
00120 }
00121 
00122 
00123 void KBzip2Filter::reset()
00124 {
00125     // bzip2 doesn't seem to have a reset call...
00126     terminate();
00127     init( d->mode );
00128 }
00129 
00130 void KBzip2Filter::setOutBuffer( char * data, uint maxlen )
00131 {
00132     d->zStream.avail_out = maxlen;
00133     d->zStream.next_out = data;
00134 }
00135 
00136 void KBzip2Filter::setInBuffer( const char *data, unsigned int size )
00137 {
00138     d->zStream.avail_in = size;
00139     d->zStream.next_in = const_cast<char *>(data);
00140 }
00141 
00142 int KBzip2Filter::inBufferAvailable() const
00143 {
00144     return d->zStream.avail_in;
00145 }
00146 
00147 int KBzip2Filter::outBufferAvailable() const
00148 {
00149     return d->zStream.avail_out;
00150 }
00151 
00152 KBzip2Filter::Result KBzip2Filter::uncompress()
00153 {
00154     //qDebug() << "Calling bzDecompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
00155     int result = bzDecompress(&d->zStream);
00156     if ( result != BZ_OK )
00157     {
00158         qDebug() << "bzDecompress returned" << result;
00159         qDebug() << "KBzip2Filter::uncompress" << ( result == BZ_STREAM_END ? KFilterBase::End : KFilterBase::Error );
00160     }
00161 
00162     switch (result) {
00163         case BZ_OK:
00164                 return KFilterBase::Ok;
00165         case BZ_STREAM_END:
00166                 return KFilterBase::End;
00167         default:
00168                 return KFilterBase::Error;
00169     }
00170 }
00171 
00172 KBzip2Filter::Result KBzip2Filter::compress( bool finish )
00173 {
00174     //qDebug() << "Calling bzCompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
00175     int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN );
00176 
00177     switch (result) {
00178         case BZ_OK:
00179         case BZ_FLUSH_OK:
00180         case BZ_RUN_OK:
00181         case BZ_FINISH_OK:
00182                 return KFilterBase::Ok;
00183                 break;
00184         case BZ_STREAM_END:
00185                 //qDebug() << "  bzCompress returned " << result;
00186                 return KFilterBase::End;
00187         break;
00188         default:
00189                 //qDebug() << "  bzCompress returned " << result;
00190                 return KFilterBase::Error;
00191                 break;
00192     }
00193 }
00194 
00195 #endif  /* HAVE_BZIP2_SUPPORT */

KDECore

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

kdelibs

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