cstream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef TNT_CSTREAM
29 #define TNT_CSTREAM
30 
31 #include <iostream>
32 #include <vector>
33 
35 
36 namespace tnt
37 {
38 
39 class cstreambuf : public std::streambuf
40 {
41  typedef std::vector<char*> _chunks_type;
42  unsigned _chunksize;
43  _chunks_type _chunks;
44 
45  public:
46  typedef _chunks_type::size_type size_type;
47 
48  explicit cstreambuf(unsigned chunksize = 32768)
49  : _chunksize(chunksize)
50  { }
51 
52  ~cstreambuf();
53 
54  size_type chunkcount() const
55  { return _chunks.size(); }
56 
57  size_type size() const
58  { return _chunks.size() == 0 ? 0
59  : (_chunks.size() - 1) * _chunksize + pptr() - _chunks.back(); }
60 
61  size_type chunksize(size_type n) const
62  {
63  return _chunks.size() == 0 ? 0
64  : n + 1 < _chunks.size() ? _chunksize
65  : n + 1 == _chunks.size() ? static_cast<size_type>(pptr() - _chunks.back())
66  : 0;
67  }
68 
69  const char* chunk(size_type n) const
70  { return _chunks[n]; }
71 
72  void rollback(size_type n);
73 
74  void makeEmpty();
75 
76  private:
77  std::streambuf::int_type overflow(std::streambuf::int_type ch);
78  std::streambuf::int_type underflow();
79  int sync();
80 };
81 
82 class ocstream : public std::ostream
83 {
84  cstreambuf _streambuf;
85 
86  public:
87  typedef cstreambuf::size_type size_type;
88 
89  explicit ocstream(unsigned chunksize = 32768)
90  : std::ostream(0),
91  _streambuf(chunksize)
92  {
93  init(&_streambuf);
94  }
95 
96  size_type chunkcount() const
97  { return _streambuf.chunkcount(); }
98 
99  const char* chunk(size_type n) const
100  { return _streambuf.chunk(n); }
101 
102  size_type chunksize(size_type n) const
103  { return _streambuf.chunksize(n); }
104 
105  size_type size() const
106  { return _streambuf.size(); }
107 
108  void rollback(size_type n)
109  { _streambuf.rollback(n); }
110 
111  void makeEmpty()
112  { _streambuf.makeEmpty(); }
113 
114  std::string str() const;
115 
116  void output(std::ostream& out) const;
117 };
118 
119 }
120 
121 #endif // TNT_CSTREAM