reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
DataStream.cxx
Go to the documentation of this file.
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#include "rutil/ResipAssert.h"
7#include "rutil/Data.hxx"
8
9// Remove warning about 'this' use in initiator list - pointer is only stored
10#if defined(WIN32) && !defined(__GNUC__)
11#pragma warning( disable : 4355 ) // using this in base member initializer list
12#endif
13
14using namespace resip;
15
17 : mStr(str)
18{
19#ifdef RESIP_USE_STL_STREAMS
20 char* gbuf = const_cast<char*>(mStr.mBuf);
21 setg(gbuf, gbuf, gbuf+mStr.size());
22 // expose the excess capacity as the put buffer
23 setp(gbuf+mStr.mSize, gbuf+mStr.mCapacity);
24#endif
25}
26
29
30#ifndef RESIP_USE_STL_STREAMS
31uint64_t DataBuffer::tellpbuf(void)
32{
33 return mStr.size();
34}
35
36size_t DataBuffer::readbuf(char *buf, size_t count)
37{
38 if (count <= 0)
39 {
40 return 0;
41 }
42
43 if (!buf)
44 {
45 resip_assert(0);
46 return 0;
47 }
48
49 size_t cursize = mStr.size();
50
51 size_t toread = (cursize < count) ? (cursize) : (count);
52
53 memcpy(buf,mStr.begin(),toread);
54
55 //wow, not efficient. Just added this function for repro, need to revisit. @TODO.
56 mStr = mStr.substr(toread);
57
58 return toread;
59}
60
61size_t DataBuffer::writebuf(const char *str, size_t count)
62{
63 if( count <= 0 )
64 {
65 return 0;
66 }
67
68 mStr.append(str,count);
69 return count;
70}
71size_t DataBuffer::putbuf(char ch)
72{
73 mStr += ch;
74
75 return 1;
76}
77#else
78int
80{
81 Data::size_type len = (Data::size_type)(pptr() - pbase());
82 if (len > 0)
83 {
84 size_t pos = gptr() - eback(); // remember the get position
85 mStr.mSize += len;
86 char* gbuf = const_cast<char*>(mStr.data());
87 // reset the get buffer
88 setg(gbuf, gbuf+pos, gbuf+mStr.size());
89 // reset the put buffer
90 setp(gbuf + mStr.mSize, gbuf + mStr.mCapacity);
91 }
92 return 0;
93}
94
95int
97{
98 // sync, but reallocate
99 Data::size_type len = (Data::size_type)(pptr() - pbase());
100 // .kw. test below is always true. Checked with David Butcher
101 // and he says this is safe but perhaps performs unrequired
102 // re-allocs.
103 if (len >= 0)
104 {
105 size_t pos = gptr() - eback(); // remember the get position
106
107 // update the length
108 mStr.mSize += len;
109
110 // resize the underlying Data and reset the input buffer
111 mStr.resize(((mStr.mCapacity+16)*3)/2, true);
112
113 char* gbuf = const_cast<char*>(mStr.mBuf);
114 // reset the get buffer
115 setg(gbuf, gbuf+pos, gbuf+mStr.mSize);
116 // reset the put buffer
117 setp(gbuf + mStr.mSize, gbuf + mStr.mCapacity);
118 }
119 if (c != -1)
120 {
121 mStr.mBuf[mStr.mSize] = c;
122 pbump(1);
123 return c;
124 }
125 return 0;
126}
127#endif
128
130 : DataBuffer(str),
131 DecodeStream(this)
132{
133}
134
137
139 : DataBuffer(str),
140 EncodeStream(this)
141{
142 // don't call this with a read-only buffer!
143 resip_assert(str.mShareEnum != Data::Share);
144}
145
147{
148 flush();
149}
150
151void
153{
154 flush();
155 mStr.clear();
156#ifdef RESIP_USE_STL_STREAMS
157 // reset the underlying buffer state
158 char* gbuf = const_cast<char*>(mStr.mBuf);
159 setg(gbuf, gbuf, gbuf+mStr.size());
160 setp(gbuf+mStr.mSize, gbuf+mStr.mCapacity);
161#endif
162}
163
165 : DataBuffer(str),
167 std::iostream(this)
168#else
169 ResipFastOStream(this)
170#endif
171{
172 // don't call this with a read-only buffer!
173 resip_assert(str.mShareEnum != Data::Share);
174}
175
177{
178 flush();
179}
180
181/* ====================================================================
182 * The Vovida Software License, Version 1.0
183 *
184 * Copyright 2000-2005
185 *
186 * Redistribution and use in source and binary forms, with or without
187 * modification, are permitted provided that the following conditions
188 * are met:
189 *
190 * 1. Redistributions of source code must retain the above copyright
191 * notice, this list of conditions and the following disclaimer.
192 *
193 * 2. Redistributions in binary form must reproduce the above copyright
194 * notice, this list of conditions and the following disclaimer in
195 * the documentation and/or other materials provided with the
196 * distribution.
197 *
198 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
199 * and "Vovida Open Communication Application Library (VOCAL)" must
200 * not be used to endorse or promote products derived from this
201 * software without prior written permission. For written
202 * permission, please contact vocal@vovida.org.
203 *
204 * 4. Products derived from this software may not be called "VOCAL", nor
205 * may "VOCAL" appear in their name, without prior written
206 * permission of Vovida Networks, Inc.
207 *
208 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
209 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
210 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
211 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
212 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
213 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
214 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
216 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
217 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
218 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
219 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
220 * DAMAGE.
221 *
222 * ====================================================================
223 *
224 * This software consists of voluntary contributions made by Vovida
225 * Networks, Inc. and many individuals on behalf of Vovida Networks,
226 * Inc. For more information on Vovida Networks, Inc., please see
227 * <http://www.vovida.org/>.
228 *
229 */
#define resip_assert(x)
This file needs to work for both C and C++ code.
Implementation of std::streambuf used to back the DataStream, iDataStream, and oDataStream.
DataBuffer(Data &str)
virtual int sync()
virtual ~DataBuffer()
virtual int overflow(int c=-1)
~DataStream()
Calls flush on itself to force the update to the Data reference passed into the constructor.
DataStream(Data &str)
Constructs a DataStream with a Data to operate on.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
@ Share
The Data instance will use the buffer in a read-only mode.
Definition Data.hxx:266
Data & append(const char *str, size_type len)
Appends the specified number of bytes to the end of this Data.
Definition Data.cxx:1058
Data substr(size_type first, size_type count=Data::npos) const
Copies a portion of this Data into a new Data.
Definition Data.cxx:1932
size_t size_type
Definition Data.hxx:94
size_type size() const noexcept
Returns the number of bytes in this Data.
Definition Data.hxx:599
const char * begin() const noexcept
Returns a pointer to the beginning of the buffer used by the Data.
Definition Data.hxx:642
Data & clear()
Clears the contents of this Data.
Definition Data.hxx:800
const char * data() const noexcept
Returns a pointer to the contents of this Data.
Definition Data.hxx:608
std::ostream replacement.
iDataStream(Data &str)
Constructs a iDataStream with a Data to operate on.
void reset()
Clears the underlying Data reference.
oDataStream(Data &str)
Constructs a oDataStream with a Data to operate on.
~oDataStream()
Calls flush on itself to force the update to the Data reference passed into the constructor.
dcm! – add UnusedChecking(_enum) below;
unsigned long long uint64_t
Definition Stun.hxx:39
#define EncodeStream
#define DecodeStream
#define RESIP_USE_STL_STREAMS