From: Christian P. <cp...@us...> - 2005-01-06 16:47:59
|
Update of /cvsroot/pclasses/pclasses2/src/IO In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11889/src/IO Added Files: ZLibIOFilter.cpp Log Message: Added ZLibIOFilter as promised to Stephan ;-) --- NEW FILE: ZLibIOFilter.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/ZLibIOFilter.h> namespace P { namespace IO { ZLibIOFilter::ZLibIOFilter() : _strmOut(0), _strmIn(0) { } ZLibIOFilter::~ZLibIOFilter() { if(_strmOut) delete _strmOut; if(_strmIn) delete _strmIn; } IOFilter* ZLibIOFilter::create() { return new ZLibIOFilter(); } void ZLibIOFilter::sync(bool close) { // sync output stream ... if(_strmOut) { if(close) { while(!_strmOut->finish()) flushAvailBytes(); flushAvailBytes(); _strmOut->reset(); } else { _strmOut->sync(); flushAvailBytes(); } } if(_strmIn) { if(close) _strmIn->reset(); } IOFilter::sync(close); } bool ZLibIOFilter::isSeekable() { // seeking is too complex (for now ;-) return false; } size_t ZLibIOFilter::read(char* buffer, size_t count) throw(IO::IOError) { if(!_strmIn) _strmIn = new P::IO::ZLibInputStream(); if(_strmIn->bytesAvail() < count) { // refill output buffer ... char tmp[1024]; size_t got = IOFilter::read(tmp, sizeof(tmp)); size_t consumed = _strmIn->inflate(tmp, got); if(consumed < sizeof(tmp)) { //@fixme: we need to buffer rememaining bytes we could not inflate } count = _strmIn->bytesAvail(); } // dequeue bytes available in output buffer ... memcpy(buffer, _strmIn->buffer(), count); _strmIn->dequeue(count); return count; } size_t ZLibIOFilter::write(const char* buffer, size_t count) throw(IO::IOError) { if(!_strmOut) _strmOut = new P::IO::ZLibOutputStream(9); size_t consumed = _strmOut->deflate(buffer, count); flushAvailBytes(); return consumed; } size_t ZLibIOFilter::peek(char* buffer, size_t count) throw(IO::IOError) { return 0; } offset_t ZLibIOFilter::seek(offset_t offset, IO::IODevice::SeekMode mode) throw(IO::IOError) { throw IO::IOError(0, "Could not seek on device", P_SOURCEINFO); return -1; } offset_t ZLibIOFilter::size() const throw(IO::IOError) { //@fixme!!! return IOFilter::size(); } void ZLibIOFilter::flushAvailBytes() { size_t avail = _strmOut->bytesAvail(); if(avail) _strmOut->dequeue(IOFilter::write(_strmOut->buffer(), avail)); } } } |