blocxx
BaseStreamBuffer.cpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
39#include "blocxx/BLOCXX_config.h"
41#include "blocxx/Exception.hpp"
42#include "blocxx/String.hpp"
43#include "blocxx/Assertion.hpp"
44#include <iostream> // for cerr
45#include <cstring> // for memcpy
46
47namespace BLOCXX_NAMESPACE
48{
49
52 : m_bufSize(bufSize), m_inputBuffer(NULL), m_outputBuffer(NULL)
53{
54 if (direction == E_IN || direction == E_IN_OUT)
55 {
56 m_inputBuffer = new char[m_bufSize];
58 }
59 if (direction == E_OUT || direction == E_IN_OUT)
60 {
61 m_outputBuffer = new char[m_bufSize];
63 }
64}
66void
73void
79void
91int
93{
94 return buffer_out();
95}
97int
99{
100 int cnt = pptr() - pbase();
101 int retval = buffer_to_device(m_outputBuffer, cnt);
102 pbump(-cnt);
103 return retval;
104}
106int
108{
109 if (buffer_out() < 0)
110 {
111 return EOF;
112 }
113 else
114 {
115 if (c != EOF)
116 {
117 return sputc(c);
118 }
119 else
120 {
121 return c;
122 }
123 }
124}
126std::streamsize
127BaseStreamBuffer::xsputn(const char* s, std::streamsize n)
128{
129 if (n < epptr() - pptr())
130 {
131 memcpy(pptr(), s, n * sizeof(char));
132 pbump(n);
133 return n;
134 }
135 else
136 {
137 for (std::streamsize i = 0; i < n; i++)
138 {
139 if (sputc(s[i]) == EOF)
140 {
141 return i;
142 }
143 }
144 return n;
145 }
146}
148int
150{
151 if (gptr() < egptr())
152 {
153 return static_cast<unsigned char>(*gptr()); // need a static_cast so a -1 doesn't turn into an EOF
154 }
155 if (buffer_in() < 0)
156 {
157 return EOF;
158 }
159 else
160 {
161 return static_cast<unsigned char>(*gptr()); // need a static_cast so a -1 doesn't turn into an EOF
162 }
163}
165int
167{
169 m_bufSize);
170 if (retval <= 0)
171 {
172 setg(0,0,0);
173 return -1;
174 }
175 else
176 {
178 return retval;
179 }
180}
182int
184{
185 BLOCXX_ASSERT("Not implemented, should overwrite" == 0);
186 return -1; // make the compiler happy
187}
189int
191{
192 BLOCXX_ASSERT("Not implemented, should overwrite" == 0);
193 return -1; // make the compiler happy
194}
195
196} // end namespace BLOCXX_NAMESPACE
197
#define BLOCXX_ASSERT(CON)
BLOCXX_ASSERT works similar to the assert() macro, but instead of calling abort(),...
Definition Assertion.hpp:57
std::streamsize xsputn(const char *s, std::streamsize n)
virtual int buffer_to_device(const char *c, int n)
Writes the buffer to the "device".
virtual int buffer_from_device(char *c, int n)
Fill the buffer from the "device".
BaseStreamBuffer(EDirectionFlag direction, size_t bufSize=BASE_BUF_SIZE)
Create a base stream buffer.
Taken from RFC 1321.