blocxx
Main Page
Namespaces
Classes
Files
File List
File Members
src
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"
40
#include "
blocxx/BaseStreamBuffer.hpp
"
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
47
namespace
BLOCXX_NAMESPACE
48
{
49
51
BaseStreamBuffer::BaseStreamBuffer
(
EDirectionFlag
direction,
size_t
bufSize)
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
];
57
initGetBuffer
();
58
}
59
if
(direction ==
E_OUT
|| direction ==
E_IN_OUT
)
60
{
61
m_outputBuffer
=
new
char
[
m_bufSize
];
62
initPutBuffer
();
63
}
64
}
66
void
67
BaseStreamBuffer::initBuffers
()
68
{
69
initPutBuffer
();
70
initGetBuffer
();
71
}
73
void
74
BaseStreamBuffer::initPutBuffer
()
75
{
76
setp(
m_outputBuffer
,
m_outputBuffer
+
m_bufSize
);
77
}
79
void
80
BaseStreamBuffer::initGetBuffer
()
81
{
82
setg(
m_inputBuffer
,
m_inputBuffer
,
m_inputBuffer
);
83
}
85
BaseStreamBuffer::~BaseStreamBuffer
()
86
{
87
delete
[]
m_inputBuffer
;
88
delete
[]
m_outputBuffer
;
89
}
91
int
92
BaseStreamBuffer::sync
()
93
{
94
return
buffer_out
();
95
}
97
int
98
BaseStreamBuffer::buffer_out
()
99
{
100
int
cnt = pptr() - pbase();
101
int
retval =
buffer_to_device
(
m_outputBuffer
, cnt);
102
pbump(-cnt);
103
return
retval;
104
}
106
int
107
BaseStreamBuffer::overflow
(
int
c)
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
}
126
std::streamsize
127
BaseStreamBuffer::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
}
148
int
149
BaseStreamBuffer::underflow
()
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
}
165
int
166
BaseStreamBuffer::buffer_in
()
167
{
168
int
retval =
buffer_from_device
(
m_inputBuffer
,
169
m_bufSize
);
170
if
(retval <= 0)
171
{
172
setg(0,0,0);
173
return
-1;
174
}
175
else
176
{
177
setg(
m_inputBuffer
,
m_inputBuffer
,
m_inputBuffer
+ retval);
178
return
retval;
179
}
180
}
182
int
183
BaseStreamBuffer::buffer_to_device
(
const
char
* c,
int
n)
184
{
185
BLOCXX_ASSERT
(
"Not implemented, should overwrite"
== 0);
186
return
-1;
// make the compiler happy
187
}
189
int
190
BaseStreamBuffer::buffer_from_device
(
char
* c,
int
n)
191
{
192
BLOCXX_ASSERT
(
"Not implemented, should overwrite"
== 0);
193
return
-1;
// make the compiler happy
194
}
195
196
}
// end namespace BLOCXX_NAMESPACE
197
Generated by
1.8.2