• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • compression
kbzip2filter.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000-2005 David Faure <faure@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kbzip2filter.h"
21 
22 #include <config-compression.h>
23 
24 #if HAVE_BZIP2_SUPPORT
25 
26 // we don't need that
27 #define BZ_NO_STDIO
28 extern "C" {
29  #include <bzlib.h>
30 }
31 
32 #if NEED_BZ2_PREFIX
33  #define bzDecompressInit(x,y,z) BZ2_bzDecompressInit(x,y,z)
34  #define bzDecompressEnd(x) BZ2_bzDecompressEnd(x)
35  #define bzCompressEnd(x) BZ2_bzCompressEnd(x)
36  #define bzDecompress(x) BZ2_bzDecompress(x)
37  #define bzCompress(x,y) BZ2_bzCompress(x, y)
38  #define bzCompressInit(x,y,z,a) BZ2_bzCompressInit(x, y, z, a);
39 #endif
40 
41 #include <QDebug>
42 
43 #include <qiodevice.h>
44 
45 
46 
47 // For docu on this, see /usr/doc/bzip2-0.9.5d/bzip2-0.9.5d/manual_3.html
48 
49 class KBzip2Filter::Private
50 {
51 public:
52  Private()
53  : isInitialized(false)
54  {
55  memset(&zStream, 0, sizeof(zStream));
56  mode = 0;
57  }
58 
59  bz_stream zStream;
60  int mode;
61  bool isInitialized;
62 };
63 
64 KBzip2Filter::KBzip2Filter()
65  :d(new Private)
66 {
67 }
68 
69 
70 KBzip2Filter::~KBzip2Filter()
71 {
72  delete d;
73 }
74 
75 void KBzip2Filter::init( int mode )
76 {
77  if (d->isInitialized) {
78  terminate();
79  }
80 
81  d->zStream.next_in = 0;
82  d->zStream.avail_in = 0;
83  if ( mode == QIODevice::ReadOnly )
84  {
85  (void)bzDecompressInit(&d->zStream, 0, 0);
86  //qDebug() << "bzDecompressInit returned " << result;
87  // TODO: test result and return false on error
88  } else if ( mode == QIODevice::WriteOnly ) {
89  (void)bzCompressInit(&d->zStream, 5, 0, 0);
90  //qDebug() << "bzDecompressInit returned " << result;
91  // TODO: test result and return false on error
92  } else {
93  qWarning() << "Unsupported mode " << mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
94  // TODO return false
95  }
96  d->mode = mode;
97  d->isInitialized = true;
98 }
99 
100 int KBzip2Filter::mode() const
101 {
102  return d->mode;
103 }
104 
105 void KBzip2Filter::terminate()
106 {
107  if (d->mode == QIODevice::ReadOnly) {
108  int result = bzDecompressEnd(&d->zStream);
109  // TODO: test result and return false on error
110  //qDebug() << "bzDecompressEnd returned " << result;
111  } else if (d->mode == QIODevice::WriteOnly) {
112  int result = bzCompressEnd(&d->zStream);
113  // TODO: test result and return false on error
114  //qDebug() << "bzCompressEnd returned " << result;
115  } else {
116  qWarning() << "Unsupported mode " << d->mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
117  // TODO return false
118  }
119  d->isInitialized = false;
120 }
121 
122 
123 void KBzip2Filter::reset()
124 {
125  // bzip2 doesn't seem to have a reset call...
126  terminate();
127  init( d->mode );
128 }
129 
130 void KBzip2Filter::setOutBuffer( char * data, uint maxlen )
131 {
132  d->zStream.avail_out = maxlen;
133  d->zStream.next_out = data;
134 }
135 
136 void KBzip2Filter::setInBuffer( const char *data, unsigned int size )
137 {
138  d->zStream.avail_in = size;
139  d->zStream.next_in = const_cast<char *>(data);
140 }
141 
142 int KBzip2Filter::inBufferAvailable() const
143 {
144  return d->zStream.avail_in;
145 }
146 
147 int KBzip2Filter::outBufferAvailable() const
148 {
149  return d->zStream.avail_out;
150 }
151 
152 KBzip2Filter::Result KBzip2Filter::uncompress()
153 {
154  //qDebug() << "Calling bzDecompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
155  int result = bzDecompress(&d->zStream);
156  if ( result != BZ_OK )
157  {
158  qDebug() << "bzDecompress returned" << result;
159  qDebug() << "KBzip2Filter::uncompress" << ( result == BZ_STREAM_END ? KFilterBase::End : KFilterBase::Error );
160  }
161 
162  switch (result) {
163  case BZ_OK:
164  return KFilterBase::Ok;
165  case BZ_STREAM_END:
166  return KFilterBase::End;
167  default:
168  return KFilterBase::Error;
169  }
170 }
171 
172 KBzip2Filter::Result KBzip2Filter::compress( bool finish )
173 {
174  //qDebug() << "Calling bzCompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
175  int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN );
176 
177  switch (result) {
178  case BZ_OK:
179  case BZ_FLUSH_OK:
180  case BZ_RUN_OK:
181  case BZ_FINISH_OK:
182  return KFilterBase::Ok;
183  break;
184  case BZ_STREAM_END:
185  //qDebug() << " bzCompress returned " << result;
186  return KFilterBase::End;
187  break;
188  default:
189  //qDebug() << " bzCompress returned " << result;
190  return KFilterBase::Error;
191  break;
192  }
193 }
194 
195 #endif /* HAVE_BZIP2_SUPPORT */
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Fri Dec 7 2012 15:57:17 by doxygen 1.8.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

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-4.8.5 API Reference

Skip menu "kdelibs-4.8.5 API Reference"
  • 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
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal