From: Christian P. <cp...@us...> - 2004-12-30 17:10:44
|
Update of /cvsroot/pclasses/pclasses2/src/IO In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30316/src/IO Added Files: IOStream.cpp Log Message: Added IOStream, IOStreamBuffer --- NEW FILE: IOStream.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/IOStream.h" namespace P { namespace IO { IOStreamBuffer::IOStreamBuffer(IODevice& dev, size_t outBufferSize, size_t inBufferSize) : _dev(dev) { _outBufferSize = outBufferSize; if(_outBufferSize > 0) { _outBuffer = new char[_outBufferSize]; setp(_outBuffer, _outBuffer + _outBufferSize); } else { _outBuffer = 0; } _inBufferSize = inBufferSize; if(_inBufferSize > 0) { _inBuffer = new char[_inBufferSize]; setg(_inBuffer, _inBuffer, _inBuffer + _inBufferSize); } else { _inBuffer = 0; } } IOStreamBuffer::~IOStreamBuffer() { if(_outBuffer) delete[] _outBuffer; if(_inBuffer) delete[] _inBuffer; } int IOStreamBuffer::overflow(int ch) { int_type ret = traits_type::eof(); if(traits_type::eq_int_type(ch, traits_type::eof())) { size_t count = pptr() - pbase(); // write out buffered chars ... if(count) { try { size_t written = _dev.write(pbase(), count); if(written == count) { setp(_outBuffer, _outBuffer + _outBufferSize); ret = traits_type::not_eof(ch); } } catch(...) { } } } else { // stream is unbuffered ? if(!pptr()) { char tmp[1]; tmp[0] = ch; try { size_t written = _dev.write(tmp, 1); if(written == 1) ret = traits_type::not_eof(ch); } catch(...) { } } else { // buffer full ? if(pptr() == epptr()) { ret = overflow(traits_type::eof()); if(ret == traits_type::eof()) return ret; } // put char into buffer traits_type::assign(*pptr(), traits_type::to_char_type(ch)); pbump(1); ret = traits_type::not_eof(ch); } } return ret; } int IOStreamBuffer::underflow() { //@fixme return traits_type::eof(); } int IOStreamBuffer::sync() { int ret = 0; if(overflow(traits_type::eof()) == traits_type::eof()) ret = -1; return ret; } IOStream::IOStream(IODevice& dev) : std::iostream(new IOStreamBuffer(dev)) { } IOStream::~IOStream() { delete rdbuf(); } IOStreamBuffer& IOStream::buffer() const { return *static_cast<IOStreamBuffer*>(rdbuf()); } } } |