Claw  1.7.3
buffered_ostream.tpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #include <cassert>
31 
32 /*----------------------------------------------------------------------------*/
38 template< typename Stream >
40 ( stream_type& f, unsigned int buffer_size )
41  : m_stream(f), m_begin(new char[buffer_size]), m_end(m_begin+buffer_size),
42  m_current(m_begin)
43 {
44 
45 } // buffered_ostream::buffered_ostream()
46 
47 /*----------------------------------------------------------------------------*/
51 template< typename Stream >
53 {
54  flush();
55  delete[] m_begin;
56 } // buffered_ostream::~buffered_ostream()
57 
58 /*----------------------------------------------------------------------------*/
63 template< typename Stream >
64 template<typename T>
66 {
67  write( reinterpret_cast<const char*>(&v), sizeof(v) );
68 } // buffered_ostream::read_more()
69 
70 /*----------------------------------------------------------------------------*/
76 template< typename Stream >
77 void claw::buffered_ostream<Stream>::write(const char* p, unsigned int n )
78 {
79  while (n > 0)
80  {
81  unsigned int q = std::min( n, (unsigned int)(m_end - m_current) );
82  const char* end = p+q;
83 
84  for ( ; p!=end ; ++p, ++m_current )
85  *m_current = *p;
86 
87  n -= q;
88 
89  if (m_current == m_end)
90  flush();
91  }
92 } // buffered_ostream::write()
93 
94 /*----------------------------------------------------------------------------*/
98 template< typename Stream >
100 {
101  if (m_current != m_begin)
102  {
103  m_stream.write( m_begin, m_current - m_begin );
104  m_current = m_begin;
105  }
106 } // buffered_ostream::flush()