Claw  1.7.3
buffered_ostream.tpp
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 */
25 /**
26  * \file buffered_ostream.tpp
27  * \brief Implementation of the claw::buffered_ostream class.
28  * \author Julien Jorge
29  */
30 #include <cassert>
31 
32 /*----------------------------------------------------------------------------*/
33 /**
34  * \brief Constructor.
35  * \param f The file associated to the stream.
36  * \param buffer_size The size of the buffer.
37  */
38 template< typename Stream >
39 claw::buffered_ostream<Stream>::buffered_ostream
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 /*----------------------------------------------------------------------------*/
48 /**
49  * \brief Destructor.
50  */
51 template< typename Stream >
52 claw::buffered_ostream<Stream>::~buffered_ostream()
53 {
54  flush();
55  delete[] m_begin;
56 } // buffered_ostream::~buffered_ostream()
57 
58 /*----------------------------------------------------------------------------*/
59 /**
60  * \brief Write somethnig in the buffer.
61  * \param v The value to write.
62  */
63 template< typename Stream >
64 template<typename T>
65 void claw::buffered_ostream<Stream>::write( T v )
66 {
67  write( reinterpret_cast<const char*>(&v), sizeof(v) );
68 } // buffered_ostream::read_more()
69 
70 /*----------------------------------------------------------------------------*/
71 /**
72  * \brief Write a range of data in the buffer.
73  * \param p The begining of the range to write.
74  * \param n The length of the buffer pointed by p.
75  */
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 /*----------------------------------------------------------------------------*/
95 /**
96  * \brief Write the data from the buffer in the stream.
97  */
98 template< typename Stream >
99 void claw::buffered_ostream<Stream>::flush()
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()