From: Christian P. <cp...@us...> - 2005-01-06 18:30:42
|
Update of /cvsroot/pclasses/pclasses2/src/IO In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6341/src/IO Modified Files: Makefile.am Added Files: BZip2.cpp Log Message: Added bzip2 support. Index: Makefile.am =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/IO/Makefile.am,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- Makefile.am 6 Jan 2005 16:53:44 -0000 1.4 +++ Makefile.am 6 Jan 2005 18:30:33 -0000 1.5 @@ -2,5 +2,5 @@ METASOURCES = AUTO lib_LTLIBRARIES = libpclasses_io.la -libpclasses_io_la_SOURCES = IOError.cpp IODevice.cpp IOStream.cpp IOFilter.cpp URL.cpp ZLib.cpp ZLibIOFilter.cpp -libpclasses_io_la_LIBADD = $(top_builddir)/src/libpclasses.la -lz +libpclasses_io_la_SOURCES = IOError.cpp IODevice.cpp IOStream.cpp IOFilter.cpp URL.cpp ZLib.cpp ZLibIOFilter.cpp BZip2.cpp +libpclasses_io_la_LIBADD = $(top_builddir)/src/libpclasses.la -lz -lbz2 --- NEW FILE: BZip2.cpp --- /*************************************************************************** * Copyright (C) 2004 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/IO/BZip2.h" #include <bzlib.h> namespace P { namespace IO { BZip2Error::BZip2Error(int err, const char* what, const SourceInfo& si) : RuntimeError(what, si), _err(err) { } BZip2Error::~BZip2Error() { } int BZip2Error::errorNo() const throw() { return _err; } std::string BZip2Error::msg() const throw() { return ""; } BZip2Stream::BZip2Stream(size_t bufferSize) { if(bufferSize < 1) bufferSize = 64; bz_stream* strm = new bz_stream; _buffer = new char[bufferSize]; _bufferSize = bufferSize; strm->bzalloc = 0; strm->bzfree = 0; strm->opaque = 0; strm->next_in = 0; strm->next_out = _buffer; strm->avail_in = 0; strm->avail_out = _bufferSize; _strm = (void*)strm; } BZip2Stream::~BZip2Stream() { delete ((bz_stream*)_strm); delete[] _buffer; } size_t BZip2Stream::bytesAvail() const throw() { return _bufferSize - ((bz_stream*)_strm)->avail_out; } const char* BZip2Stream::buffer() const throw() { return (const char*)((bz_stream*)_strm)->next_out - (_bufferSize - ((bz_stream*)_strm)->avail_out); } size_t BZip2Stream::dequeue(size_t count) throw(BZip2Error) { size_t avail = bytesAvail(); if(count > avail) count = avail; // move remaining bytes to top of buffer ... if(count < avail) memmove(_buffer, ((bz_stream*)_strm)->next_out - avail + count, avail - count); ((bz_stream*)_strm)->next_out -= count; ((bz_stream*)_strm)->avail_out += count; return count; } void BZip2Stream::put(const char* buffer, size_t count) throw(BZip2Error) { if(((bz_stream*)_strm)->avail_out < count) throw BZip2Error(BZ_OUTBUFF_FULL, "Could not put data into output buffer", P_SOURCEINFO); memcpy(((bz_stream*)_strm)->next_out, buffer, count); ((bz_stream*)_strm)->next_out += count; ((bz_stream*)_strm)->avail_out -= count; } BZip2OutputStream::BZip2OutputStream(int level, size_t bufferSize) throw(BZip2Error) : BZip2Stream(bufferSize) { int ret; if((ret = ::BZ2_bzCompressInit((bz_stream*)_strm, 9, 0, 30)) != BZ_OK) throw BZip2Error(ret, "Could not init bzlib compression", P_SOURCEINFO); } BZip2OutputStream::~BZip2OutputStream() throw() { ::BZ2_bzCompressEnd((bz_stream*)_strm); } size_t BZip2OutputStream::compress(const char* buffer, size_t count) throw(BZip2Error) { if(!count) return 0; ((bz_stream*)_strm)->next_in = (char*)buffer; ((bz_stream*)_strm)->avail_in = count; int ret = ::BZ2_bzCompress((bz_stream*)_strm, BZ_RUN); if(ret != BZ_OK) throw BZip2Error(ret, "Could not compress", P_SOURCEINFO); // return with number of bytes consumed ... size_t deflated = count - ((bz_stream*)_strm)->avail_in; return deflated; } void BZip2OutputStream::sync() throw(BZip2Error) { int ret = ::BZ2_bzCompress((bz_stream*)_strm, BZ_FLUSH); if(ret != BZ_OK) throw BZip2Error(ret, "Could not flush stream", P_SOURCEINFO); } bool BZip2OutputStream::finish() throw(BZip2Error) { int ret = ::BZ2_bzCompress((bz_stream*)_strm, BZ_FINISH); // not enough buffer was avail... should call finish() again if(ret == BZ_FINISH_OK) return false; if(ret != BZ_STREAM_END) throw BZip2Error(BZ_OUTBUFF_FULL, "Could not finish stream", P_SOURCEINFO); return true; } void BZip2OutputStream::reset() throw(BZip2Error) { ((bz_stream*)_strm)->next_out = _buffer; ((bz_stream*)_strm)->avail_out = _bufferSize; } BZip2InputStream::BZip2InputStream(size_t bufferSize) throw(BZip2Error) : BZip2Stream(bufferSize), _eof(false) { int ret; if((ret = ::BZ2_bzDecompressInit((bz_stream*)_strm, 0, 0)) != BZ_OK) throw BZip2Error(ret, "Could not init bzlib decompression", P_SOURCEINFO); } BZip2InputStream::~BZip2InputStream() throw() { ::BZ2_bzDecompressEnd((bz_stream*)_strm); } size_t BZip2InputStream::decompress(const char* buffer, size_t count) throw(BZip2Error) { if(!count) return 0; ((bz_stream*)_strm)->next_in = (char*)buffer; ((bz_stream*)_strm)->avail_in = count; int ret = ::BZ2_bzDecompress((bz_stream*)_strm); if(ret == BZ_STREAM_END) { // we do nothing, so this or next call to inflate() returns 0 _eof = true; } else if(ret != BZ_OK) throw BZip2Error(ret, "Could not decompress", P_SOURCEINFO); // return with number of bytes consumed ... size_t inflated = count - ((bz_stream*)_strm)->avail_in; return inflated; } bool BZip2InputStream::eof() const throw() { return _eof; } void BZip2InputStream::reset() throw(BZip2Error) { ((bz_stream*)_strm)->next_out = _buffer; ((bz_stream*)_strm)->avail_out = _bufferSize; _eof = false; } } // !namespace IO } // !namespace P |