• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE Support
  • Sitemap
  • Contact Us
 

strigi/src/streams

gzipcompressstream.cpp

Go to the documentation of this file.
00001 /* This file is part of Strigi Desktop Search
00002  *
00003  * Copyright (C) 2006 Ben van Klinken <bvklinken@gmail.com>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 #include "gzipcompressstream.h"
00021 #include <zlib.h>
00022 #include <iostream>
00023 
00024 using namespace Strigi;
00025 using namespace std;
00026 
00027 GZipCompressInputStream::GZipCompressInputStream(InputStream* input, int level) {
00028     // initialize values that signal state
00029     m_status = Ok;
00030     zstream = 0;
00031     if ( level < 0 || level > 9 ) {
00032         level = Z_DEFAULT_COMPRESSION;
00033     }
00034 
00035     this->input = input;
00036 
00037     // initialize the z_stream
00038     zstream = (z_stream_s*)malloc(sizeof(z_stream_s));
00039     zstream->zalloc = Z_NULL;
00040     zstream->zfree = Z_NULL;
00041     zstream->opaque = Z_NULL;
00042     zstream->avail_in = 0;
00043 
00044     // initialize for writing gzip streams
00045     int r = deflateInit(zstream, level);
00046     if (r != Z_OK) {
00047         m_error = "Error initializing GZipCompressInputStream.";
00048         dealloc();
00049         m_status = Error;
00050         return;
00051     }
00052 
00053     // signal that we need to read into the buffer
00054     zstream->avail_out = 1;
00055 }
00056 GZipCompressInputStream::~GZipCompressInputStream() {
00057     dealloc();
00058 }
00059 void
00060 GZipCompressInputStream::dealloc() {
00061     if (zstream) {
00062         deflateEnd(zstream);
00063         free(zstream);
00064         zstream = 0;
00065     }
00066 }
00067 void
00068 GZipCompressInputStream::readFromStream() {
00069     // read data from the input stream
00070     const char* inStart;
00071     int32_t nread;
00072     nread = input->read(inStart, 1, 0);
00073     if (nread < -1) {
00074         m_status = Error;
00075         m_error = input->error();
00076     } else if (nread < 1) {
00077         zstream->avail_in = 0; //bail...
00078     } else {
00079         zstream->next_in = (Bytef*)inStart;
00080         zstream->avail_in = nread;
00081     }
00082 }
00083 int32_t
00084 GZipCompressInputStream::fillBuffer(char* start, int32_t space) {
00085     cerr << "GZCI " << this << " " << zstream << endl;
00086     if (zstream == 0) return -1;
00087 
00088     // make sure we can write into the buffer
00089     zstream->avail_out = space;
00090     zstream->next_out = (Bytef*)start;
00091 
00092     int r;
00093     // make sure there is data to decompress
00094     if (zstream->avail_in == 0) {
00095         readFromStream();
00096         if (m_status == Error) {
00097             cerr << "error " << endl;
00098             // no data was read
00099             return -1;
00100         } else if (zstream->avail_in == 0) {
00101             r = deflate(zstream, Z_FINISH);
00102             int32_t nwritten = space - zstream->avail_out;
00103             cerr << "GZCI end " << this << " " << nwritten << " " << m_status
00104 << endl;
00105             if (r != Z_OK) {
00106                 cerr << "GZCI streamend " << r << endl;
00107                 dealloc();
00108                 if (r != Z_STREAM_END) {
00109                     fprintf(stderr, "deflate should report Z_STREAM_END\n");
00110                     return -1;
00111                 }
00112             }
00113             return nwritten;
00114         }
00115     }
00116     r = deflate(zstream, Z_NO_FLUSH);
00117     // inform the buffer of the number of bytes that was read
00118     int32_t nwritten = space - zstream->avail_out;
00119     switch (r) {
00120     case Z_NEED_DICT:
00121         m_error = "Z_NEED_DICT while inflating stream.";
00122         m_status = Error;
00123         break;
00124     case Z_DATA_ERROR:
00125         m_error = "Z_DATA_ERROR while inflating stream.";
00126         m_status = Error;
00127         break;
00128     case Z_MEM_ERROR:
00129         m_error = "Z_MEM_ERROR while inflating stream.";
00130         m_status = Error;
00131         break;
00132     }
00133     cerr << "GZCI more " << this << " " << nwritten << endl;
00134     return nwritten;
00135 }

strigi/src/streams

Skip menu "strigi/src/streams"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

KDE Support

Skip menu "KDE Support"
  • akonadi
  • Decibel
  • grantlee
  • kdewin
  • phonon
  •     Backend
  • polkit-qt
  • qca
  • qimageblitz
  • soprano
  • strigi
  •     searchclient
  •     streamanalyzer
  •     streams
Generated for KDE Support by doxygen 1.5.9-20090814
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