buffered_ostream.tpp

Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2008 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien_jorge@yahoo.fr
00024 */
00030 #include <cassert>
00031 
00032 /*----------------------------------------------------------------------------*/
00038 template< typename Stream >
00039 claw::buffered_ostream<Stream>::buffered_ostream
00040 ( stream_type& f, unsigned int buffer_size )
00041   : m_stream(f), m_begin(new char[buffer_size]), m_end(m_begin+buffer_size),
00042     m_current(m_begin)
00043 {
00044 
00045 } // buffered_ostream::buffered_ostream()
00046 
00047 /*----------------------------------------------------------------------------*/
00051 template< typename Stream >
00052 claw::buffered_ostream<Stream>::~buffered_ostream()
00053 {
00054   flush();
00055   delete[] m_begin;
00056 } // buffered_ostream::~buffered_ostream()
00057 
00058 /*----------------------------------------------------------------------------*/
00063 template< typename Stream >
00064 template<typename T>
00065 void claw::buffered_ostream<Stream>::write( T v )
00066 {
00067   write( reinterpret_cast<const char*>(&v), sizeof(v) );
00068 } // buffered_ostream::read_more()
00069 
00070 /*----------------------------------------------------------------------------*/
00076 template< typename Stream >
00077 void claw::buffered_ostream<Stream>::write(const char* p, unsigned int n )
00078 {
00079   while (n > 0)
00080     {
00081       unsigned int q = std::min( n, (unsigned int)(m_end - m_current) );
00082       const char* end = p+q;
00083 
00084       for ( ; p!=end ; ++p, ++m_current )
00085         *m_current = *p;
00086 
00087       n -= q;
00088 
00089       if (m_current == m_end)
00090         flush();
00091     }
00092 } // buffered_ostream::write()
00093 
00094 /*----------------------------------------------------------------------------*/
00098 template< typename Stream >
00099 void claw::buffered_ostream<Stream>::flush()
00100 {
00101   if (m_current != m_begin)
00102     {
00103       m_stream.write( m_begin, m_current - m_begin );
00104       m_current = m_begin;
00105     }
00106 } // buffered_ostream::flush()

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1