StreamBuffer.h
1 /*
2  * Copyright (C) 2012 Marc Boris Duerner
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 
29 #ifndef Pt_StreamBuffer_h
30 #define Pt_StreamBuffer_h
31 
32 #include <Pt/Api.h>
33 #include <Pt/String.h>
34 #include <streambuf>
35 #include <cstddef>
36 
37 #if defined(_MSC_VER) && defined(_WIN32_WCE)
38  // alternatively compile with /FORCE:multiple
39  template class PT_EXPORT std::basic_streambuf<char>;
40 #endif
41 
42 #if defined(_MSC_VER)
43  template class PT_EXPORT std::basic_streambuf<Pt::Char>;
44 #endif
45 
46 namespace Pt {
47 
50 template <typename CharT, typename TraitsT = std::char_traits<CharT> >
51 class BasicStreamBuffer : public std::basic_streambuf<CharT, TraitsT>
52 {
53  public:
54  typedef CharT char_type;
55  typedef TraitsT traits_type;
56  typedef typename TraitsT::int_type int_type;
57  typedef typename TraitsT::pos_type pos_type;
58  typedef typename TraitsT::off_type off_type;
59 
60  public:
64  { }
65 
68  std::streamsize speekn(CharT* buffer, std::streamsize size)
69  {
70  if(size <= 0)
71  return 0;
72 
73  int_type next = 0;
74  if( ! this->gptr() || this->gptr() == this->egptr() )
75  {
76  next = this->underflow();
77 
78  if( traits_type::eof() == next)
79  return 0;
80  }
81 
82  std::size_t avail = this->gptr() ? this->egptr() - this->gptr() : 0;
83 
84  // unbuffered streambufs
85  if(avail == 0)
86  {
87  *buffer = traits_type::to_char_type(next);
88  return 1;
89  }
90 
91  std::size_t n = static_cast<std::size_t>(size);
92  if(avail < n)
93  n = avail;
94 
95  traits_type::copy(buffer, this->gptr(), n);
96  return size;
97  }
98 
101  std::streamsize out_avail()
102  {
103  if( this->pptr() )
104  return this->pptr() - this->pbase();
105 
106  return showfull();
107  }
108 
109  protected:
113  { }
114 
117  virtual std::streamsize showfull()
118  { return 0; }
119 };
120 
121 } // namespace Pt
122 
123 #endif
Buffer for input and output streams.
Definition: StreamBuffer.h:51
std::streamsize speekn(CharT *buffer, std::streamsize size)
Peek data in stream.
Definition: StreamBuffer.h:68
virtual std::streamsize showfull()
Returns the number of characters buffered for output.
Definition: StreamBuffer.h:117
BasicStreamBuffer()
Default Constructor.
Definition: StreamBuffer.h:112
std::streamsize out_avail()
Returns the number of characters buffered for output.
Definition: StreamBuffer.h:101
~BasicStreamBuffer()
Destructor.
Definition: StreamBuffer.h:63