reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
resipfaststreams.hxx
Go to the documentation of this file.
1#if !defined(RESIP_RESIPFASTSTREAMS_HXX)
2#define RESIP_RESIPFASTSTREAMS_HXX
9#define RESIP_USE_STL_STREAMS
10
11#ifdef HAVE_CONFIG_H
12#include "config.h"
13#endif
14
15#include <cinttypes>
16
17#include <iostream> //for std::endl, std::cerr, etc.
18#include <stdio.h> //for snprintf
19
20#include "rutil/ResipAssert.h"
21#include "rutil/compat.hxx"
22
23#include <memory>
24
25namespace resip
26{
27
29{
30 public:
32 {}
33 virtual ~ ResipStreamBuf(void)
34 {}
35 virtual size_t writebuf(const char *s, size_t count) = 0;
36 virtual size_t readbuf(char *buf, size_t count) = 0;
37 virtual size_t putbuf(char ch) = 0;
38 virtual void flushbuf(void)=0;
39 virtual uint64_t tellpbuf(void)=0;
40};
41
43{
44 public:
45 ResipBasicIOStream(void):good_(false),eof_(true)
46 {}
49
50 bool good(void) const
51 {
52 return good_;
53 }
54 bool eof(void) const
55 {
56 return eof_;
57 }
58 void clear(void) const
59 {}
60
61 protected:
62 bool good_;
63 bool eof_;
64};
65
69{
70 public:
72 {
73 good_ = true;
74 }
75 virtual ~ResipFastOStream(void)
76 {}
77
78 virtual uint64_t tellp(void)
79 {
80 if (rdbuf())
81 {
82 return rdbuf()->tellpbuf();
83 }
84 return 0;
85 }
86
87 ResipStreamBuf * rdbuf(void) const
88 {
89 return buf_;
90 }
91
93 {
94 buf_ = buf;
95 }
96
98 {
99 if (rdbuf())
100 {
101 rdbuf()->flushbuf();
102 }
103 return *this;
104 }
105
106 ResipFastOStream &write(const char *s, size_t count)
107 {
108 if (rdbuf())
109 {
110 rdbuf()->writebuf(s,count);
111 }
112 return *this;
113 }
114
116 {
117 if (rdbuf())
118 {
119 rdbuf()->putbuf(ch);
120 }
121 return *this;
122 }
123
125 {
126 //int i = (b == true) ? (1):(0);
127 *this<<(static_cast<int32_t>(b));
128 return *this;
129 }
130
132 {
133 *this<<(static_cast<int32_t>(i));
134 return *this;
135 }
136
138 {
139 *this<<(static_cast<uint32_t>(ui));
140 return *this;
141 }
142
144 {
145 if (!buf_)
146 {
147 return *this;
148 }
149 char buf[33];
150 snprintf(buf,33,"%" PRId32,l);
151 size_t count = strlen(buf);
152 if (buf_->writebuf(buf,count) < count)
153 {
154 good_ = false;
155 }
156
157 return *this;
158 }
159
161 {
162 if (!buf_)
163 {
164 return *this;
165 }
166 char buf[33];
167 snprintf(buf,33,"%" PRIu32,ul);
168 size_t count = strlen(buf);
169 if (buf_->writebuf(buf,count) < count)
170 {
171 good_ = false;
172 }
173
174 return *this;
175 }
176
178 {
179 if (!buf_)
180 {
181 return *this;
182 }
183
184 char buf[66];
185 snprintf(buf,66,"%" PRId64,i64);
186
187 size_t count = strlen(buf);
188 if (buf_->writebuf(buf,count) < count)
189 {
190 good_ = false;
191 }
192
193 return *this;
194 }
195
197 {
198 if (!buf_)
199 {
200 return *this;
201 }
202
203 char buf[66];
204 snprintf(buf,66,"%" PRIu64,ui64);
205
206 size_t count = strlen(buf);
207 if (buf_->writebuf(buf,count) < count)
208 {
209 good_ = false;
210 }
211
212 return *this;
213 }
214
216 {
217 *this<< (static_cast<double>(f));
218
219 return *this;
220 }
221
223 {
224 if (!buf_)
225 {
226 return *this;
227 }
228
229 char buf[_CVTBUFSIZE];
230 GCVT(d,6,buf,_CVTBUFSIZE);//6 significant digits is the default for %f
231 size_t count = strlen(buf);
232#ifndef WIN32
233 //not using the non-standard microsoft conversion functions
234 //remove any trailing zeros. Note that resipfastreams does not support STL stream width or precision
235 //modifiers
236 size_t idx=0;
237 for (; count > 1; count--)
238 {
239 idx = count-1;
240 if (buf[idx] != '0' && buf[idx] != '.')
241 {
242 break;
243 }
244 }
245#endif
246 if (buf_->writebuf(buf,count) < count)
247 {
248 good_ = false;
249 }
250
251 return *this;
252 }
253
255 {
256 if (!buf_)
257 {
258 return *this;
259 }
260
261 char buf[32];
262 SNPRINTF_1(buf,32,_TRUNCATE,"%p",vp);
263 size_t count = strlen(buf);
264 if (buf_->writebuf(buf,count) < count)
265 {
266 good_ = false;
267 }
268
269 return *this;
270 }
271#ifdef WIN32
272 ResipFastOStream& operator<<(std::ostream& (__cdecl *_Pfn)(std::ostream&))
273#else
274 ResipFastOStream& operator<<(std::ostream& (*_Pfn)(std::ostream &))
275#endif
276 {
277 if (!buf_)
278 {
279 return *this;
280 }
281
282 if (_Pfn == static_cast<std::ostream& (*)(std::ostream&)>(std::endl))
283 {
284 if (buf_->writebuf("\n",1) < 1)
285 {
286 good_ = false;
287 }
288 }
289 else
290 {
291 resip_assert(0);
292 }
293 return *this;
294 }
295
296 private:
297 ResipStreamBuf *buf_;
298};
299
300
302{
303 ostr.write(str,strlen(str));
304
305 return ostr;
306}
307
309{
310 ostr.put(ch);
311
312 return ostr;
313}
314
316{
317 ostr.put((char)ch);
318
319 return ostr;
320}
321
322inline resip::ResipFastOStream& operator<<(resip::ResipFastOStream& ostr, const unsigned char *str)
323{
324 ostr.write((const char *)str,strlen((const char *)str));
325
326 return ostr;
327}
328
330{
331 ostr.put((char)ch);
332
333 return ostr;
334}
335
336inline resip::ResipFastOStream& operator<<(resip::ResipFastOStream& ostr, const signed char *str)
337{
338 ostr.write((const char *)str,strlen((const char *)str));
339
340 return ostr;
341}
342
343inline resip::ResipFastOStream & operator<<(resip::ResipFastOStream &ostr, const std::string &str)
344{
345 ostr.write(str.c_str(),str.size());
346
347 return ostr;
348}
349
350template <typename T>
351ResipFastOStream& operator<<(ResipFastOStream& os, const std::shared_ptr<T>& ptr)
352{
353 os << ptr.get();
354 return os;
355}
356
360{
361 public:
363 {}
364 virtual ~ResipFastIStream(void)
365 {}
366
367 ResipStreamBuf * rdbuf(void) const
368 {
369 return buf_;
370 }
371
372 ResipFastIStream &read(char *s, size_t count)
373 {
374 if (rdbuf())
375 {
376 rdbuf()->readbuf(s,count);
377 }
378 return *this;
379 }
380
381 private:
382 ResipStreamBuf *buf_;
383};
384
388{
389 public:
390 typedef enum BufType
391 {
394 stdCout
396
398 :type_(type)
399 {}
400
402 {}
403
404 virtual size_t writebuf(const char *s, size_t count)
405 {
406 switch (type_)
407 {
408 case stdCerr:
409 {
410 std::cerr << s;
411 break;
412 }
413 case stdCout:
414 {
415 std::cout << s;
416 break;
417 }
418 default:
419 break;
420 }
421 return count;
422 }
423 virtual size_t readbuf(char *buf, size_t count)
424 {
425 return 0;
426 }
427 virtual size_t putbuf(char ch)
428 {
429 return writebuf(&ch,1);
430 }
431 virtual void flushbuf(void)
432 {}
433 virtual uint64_t tellpbuf(void)
434 {
435 return 0;
436 }
437
438 private:
439 BufType type_;
440};
441
445{
446 public:
448 :ResipFastOStream(0),buf_(type)
449 {
450 rdbuf(&buf_);
451 }
452
454 {}
455
456 private:
457 ResipStdBuf buf_;
458};
459
460#ifdef RESIP_USE_STL_STREAMS
461#define EncodeStream std::ostream
462#define DecodeStream std::istream
463#define resipCerr std::cerr
464#define resipCout std::cout
465#else
466#define EncodeStream resip::ResipFastOStream
467#define DecodeStream resip::ResipFastIStream
468extern ResipStdCOStream resipFastCerr;
469extern ResipStdCOStream resipFastCout;
470#define resipCerr resip::resipFastCerr
471#define resipCout resip::resipFastCout
472#endif
473extern ResipStdCOStream resipFastNull;
474
475} //namespace resip
476
477#endif //RESIP_RESIPFASTSTREAMS_HXX
478
479/* ====================================================================
480 * The Vovida Software License, Version 1.0
481 *
482 * Copyright (c) 2005. All rights reserved.
483 *
484 * Redistribution and use in source and binary forms, with or without
485 * modification, are permitted provided that the following conditions
486 * are met:
487 *
488 * 1. Redistributions of source code must retain the above copyright
489 * notice, this list of conditions and the following disclaimer.
490 *
491 * 2. Redistributions in binary form must reproduce the above copyright
492 * notice, this list of conditions and the following disclaimer in
493 * the documentation and/or other materials provided with the
494 * distribution.
495 *
496 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
497 * and "Vovida Open Communication Application Library (VOCAL)" must
498 * not be used to endorse or promote products derived from this
499 * software without prior written permission. For written
500 * permission, please contact vocal@vovida.org.
501 *
502 * 4. Products derived from this software may not be called "VOCAL", nor
503 * may "VOCAL" appear in their name, without prior written
504 * permission of Vovida Networks, Inc.
505 *
506 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
507 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
508 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
509 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
510 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
511 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
512 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
513 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
514 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
515 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
516 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
517 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
518 * DAMAGE.
519 *
520 * ====================================================================
521 *
522 * This software consists of voluntary contributions made by Vovida
523 * Networks, Inc. and many individuals on behalf of Vovida Networks,
524 * Inc. For more information on Vovida Networks, Inc., please see
525 * <http://www.vovida.org/>.
526 *
527 */
528
529
530
531
#define resip_assert(x)
This file needs to work for both C and C++ code.
std::istream replacement
ResipFastIStream & read(char *s, size_t count)
ResipFastIStream(ResipStreamBuf *buf)
ResipStreamBuf * rdbuf(void) const
std::ostream replacement.
ResipFastOStream & operator<<(int16_t i)
ResipStreamBuf * rdbuf(void) const
ResipFastOStream & operator<<(bool b)
ResipFastOStream & operator<<(int32_t l)
ResipFastOStream & operator<<(float f)
ResipFastOStream & flush(void)
ResipFastOStream(ResipStreamBuf *buf)
ResipFastOStream & write(const char *s, size_t count)
virtual uint64_t tellp(void)
ResipFastOStream & operator<<(uint16_t ui)
void rdbuf(ResipStreamBuf *buf)
ResipFastOStream & operator<<(uint32_t ul)
ResipFastOStream & operator<<(int64_t i64)
ResipFastOStream & operator<<(std::ostream &(*_Pfn)(std::ostream &))
ResipFastOStream & operator<<(const void *vp)
ResipFastOStream & operator<<(double d)
ResipFastOStream & put(char ch)
ResipFastOStream & operator<<(uint64_t ui64)
Used to replace std::cerr, std::cout, etc.
virtual size_t readbuf(char *buf, size_t count)
virtual size_t writebuf(const char *s, size_t count)
virtual void flushbuf(void)
virtual uint64_t tellpbuf(void)
virtual size_t putbuf(char ch)
A direct replacement for std::cout, std::cerr, etc.
ResipStdCOStream(ResipStdBuf::BufType type)
virtual void flushbuf(void)=0
virtual size_t putbuf(char ch)=0
virtual uint64_t tellpbuf(void)=0
virtual size_t writebuf(const char *s, size_t count)=0
virtual size_t readbuf(char *buf, size_t count)=0
This file is used to handle compatibility fixes/tweaks so reSIProcate can function on multiple platfo...
#define _TRUNCATE
Definition compat.hxx:358
#define SNPRINTF_1(buffer, sizeofBuffer, count, format, var1)
Definition compat.hxx:359
#define GCVT(f, sigdigits, buffer, bufferlen)
Definition compat.hxx:364
#define _CVTBUFSIZE
Definition compat.hxx:365
#define PRId32
Definition inttypes.h:72
#define PRIu32
Definition inttypes.h:120
#define PRIu64
Definition inttypes.h:133
#define PRId64
Definition inttypes.h:79
dcm! – add UnusedChecking(_enum) below;
unsigned short uint16_t
Definition Stun.hxx:30
unsigned long long uint64_t
Definition Stun.hxx:39
unsigned int uint32_t
Definition Stun.hxx:34
ResipStdCOStream resipFastNull
EncodeStream & operator<<(EncodeStream &strm, const BaseException &e)
signed short int16_t
Definition stdint.h:81
signed __int64 int64_t
Definition stdint.h:94
signed int int32_t
Definition stdint.h:82