/[resiprocate]/main/rutil/resipfaststreams.hxx
ViewVC logotype

Contents of /main/rutil/resipfaststreams.hxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9493 - (show annotations) (download)
Sat Apr 7 10:56:50 2012 UTC (7 years, 9 months ago) by dpocock
File MIME type: text/plain
File size: 14570 byte(s)
Include config.h from even more places where it may be needed
1 #if !defined(RESIP_RESIPFASTSTREAMS_HXX)
2 #define RESIP_RESIPFASTSTREAMS_HXX
3 /*! \file resipfaststreams.hxx
4 \brief Replaces STL streams for general encoding purposes.
5
6 #define RESIP_USE_STL_STREAMS will use the STL for stream encoding (std::ostream). Undefining RESIP_USE_STL_STREAMS will
7 cause resip to use the alternative stream handling defined in this file for encoding objects.
8 */
9 #define RESIP_USE_STL_STREAMS
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <iostream> //for std::endl, std::cerr, etc.
16 #include <stdio.h> //for snprintf
17
18 #include <cassert>
19 #include "rutil/compat.hxx"
20
21 namespace resip
22 {
23
24 class ResipStreamBuf
25 {
26 public:
27 ResipStreamBuf(void)
28 {}
29 virtual ~ ResipStreamBuf(void)
30 {}
31 virtual size_t writebuf(const char *s, size_t count) = 0;
32 virtual size_t readbuf(char *buf, size_t count) = 0;
33 virtual size_t putbuf(char ch) = 0;
34 virtual void flushbuf(void)=0;
35 virtual UInt64 tellpbuf(void)=0;
36 };
37
38 class ResipBasicIOStream
39 {
40 public:
41 ResipBasicIOStream(void):good_(false),eof_(true)
42 {}
43 ~ResipBasicIOStream(void)
44 {}
45
46 bool good(void) const
47 {
48 return good_;
49 }
50 bool eof(void) const
51 {
52 return eof_;
53 }
54 void clear(void) const
55 {}
56
57 protected:
58 bool good_;
59 bool eof_;
60 };
61
62
63 #if (defined(WIN32) || defined(_WIN32_WCE))
64
65 #if (defined(_MSC_VER) && _MSC_VER >= 1400 )
66 #define SNPRINTF_1(buffer,sizeofBuffer,count,format,var1) _snprintf_s(buffer,sizeofBuffer,_TRUNCATE,format,var1)
67 #define LTOA(value,string,sizeofstring,radix) _ltoa_s(value,string,sizeofstring,radix)
68 #define ULTOA(value,string,sizeofstring,radix) _ultoa_s(value,string,sizeofstring,radix)
69 #define I64TOA(value,string,sizeofstring,radix) _i64toa_s(value,string,sizeofstring,radix)
70 #define UI64TOA(value,string,sizeofstring,radix) _ui64toa_s(value,string,sizeofstring,radix)
71 #define GCVT(val,num,buffer,buffersize) _gcvt_s(buffer,buffersize,val,num)
72 #else
73 #define _TRUNCATE -1
74 #define SNPRINTF_1(buffer,sizeofBuffer,count,format,var1) _snprintf(buffer,count,format,var1)
75 #define LTOA(value,string,sizeofstring,radix) _ltoa(value,string,radix)
76 #define ULTOA(value,string,sizeofstring,radix) _ultoa(value,string,radix)
77 #define I64TOA(value,string,sizeofstring,radix) _i64toa(value,string,radix)
78 #define UI64TOA(value,string,sizeofstring,radix) _ui64toa(value,string,radix)
79 #define GCVT(val,sigdigits,buffer,buffersize) _gcvt(val,sigdigits,buffer)
80 #endif
81
82 #else //non-windows
83 #define _TRUNCATE -1
84 #define SNPRINTF_1(buffer,sizeofBuffer,count,format,var1) snprintf(buffer,sizeofBuffer,format,var1)
85 #define LTOA(l,buffer,bufferlen,radix) SNPRINTF_1(buffer,bufferlen,bufferlen,"%li",l)
86 #define ULTOA(ul,buffer,bufferlen,radix) SNPRINTF_1(buffer,bufferlen,bufferlen,"%lu",ul)
87 #define I64TOA(value,string,sizeofstring,radix) SNPRINTF_1(string,sizeofstring,sizeofstring,"%ll",value)
88 #define UI64TOA(value,string,sizeofstring,radix) SNPRINTF_1(string,sizeofstring,sizeofstring,"%llu",value)
89 #define GCVT(f,sigdigits,buffer,bufferlen) SNPRINTF_1(buffer,bufferlen,bufferlen,"%f",f)
90 #define _CVTBUFSIZE 309+40
91 #endif
92
93 /** std::ostream replacement.
94 */
95 class ResipFastOStream : public ResipBasicIOStream
96 {
97 public:
98 ResipFastOStream(ResipStreamBuf *buf):buf_(buf)
99 {
100 good_ = true;
101 }
102 virtual ~ResipFastOStream(void)
103 {}
104
105 virtual UInt64 tellp(void)
106 {
107 if (rdbuf())
108 {
109 return rdbuf()->tellpbuf();
110 }
111 return 0;
112 }
113
114 ResipStreamBuf * rdbuf(void) const
115 {
116 return buf_;
117 }
118
119 void rdbuf(ResipStreamBuf *buf)
120 {
121 buf_ = buf;
122 }
123
124 ResipFastOStream & flush(void)
125 {
126 if (rdbuf())
127 {
128 rdbuf()->flushbuf();
129 }
130 return *this;
131 }
132
133 ResipFastOStream &write(const char *s, size_t count)
134 {
135 if (rdbuf())
136 {
137 rdbuf()->writebuf(s,count);
138 }
139 return *this;
140 }
141
142 ResipFastOStream &put(char ch)
143 {
144 if (rdbuf())
145 {
146 rdbuf()->putbuf(ch);
147 }
148 return *this;
149 }
150
151 ResipFastOStream& operator<<(bool b)
152 {
153 //int i = (b == true) ? (1):(0);
154 *this<<(static_cast<long>(b));
155 return *this;
156 }
157
158 ResipFastOStream& operator<<(short s)
159 {
160 *this<<(static_cast<long>(s));
161 return *this;
162 }
163
164 ResipFastOStream& operator<<(unsigned short us)
165 {
166 *this<<(static_cast<unsigned long>(us));
167 return *this;
168 }
169
170 ResipFastOStream& operator<<(int i)
171 {
172 *this<<(static_cast<long>(i));
173 return *this;
174 }
175
176 #ifdef _W64
177 //for size_t
178 ResipFastOStream& operator<<(_W64 unsigned int ui)
179 {
180 *this<<(static_cast<unsigned long>(ui));
181 return *this;
182 }
183 #else
184 ResipFastOStream& operator<<(unsigned int ui)
185 {
186 *this<<(static_cast<unsigned long>(ui));
187 return *this;
188 }
189 #endif
190
191 ResipFastOStream& operator<<(long l)
192 {
193 if (!buf_)
194 {
195 return *this;
196 }
197 char buf[33];
198 LTOA(l,buf,33,10);
199 size_t count = strlen(buf);
200 if (buf_->writebuf(buf,count) < count)
201 {
202 good_ = false;
203 }
204
205 return *this;
206 }
207
208 ResipFastOStream& operator<<(unsigned long ul)
209 {
210 if (!buf_)
211 {
212 return *this;
213 }
214 char buf[33];
215 ULTOA(ul,buf,33,10);
216 size_t count = strlen(buf);
217 if (buf_->writebuf(buf,count) < count)
218 {
219 good_ = false;
220 }
221
222 return *this;
223 }
224
225 #ifdef WIN32
226 ResipFastOStream& operator<<(__int64 i64)
227 {
228 if (!buf_)
229 {
230 return *this;
231 }
232 char buf[66];
233 I64TOA(i64,buf,66,10);
234 size_t count = strlen(buf);
235 if (buf_->writebuf(buf,count) < count)
236 {
237 good_ = false;
238 }
239
240 return *this;
241 }
242
243 ResipFastOStream& operator<<(unsigned __int64 ui64)
244 {
245 if (!buf_)
246 {
247 return *this;
248 }
249
250 char buf[66];
251 UI64TOA(ui64,buf,66,10);
252 size_t count = strlen(buf);
253 if (buf_->writebuf(buf,count) < count)
254 {
255 good_ = false;
256 }
257
258 return *this;
259 }
260 #else
261 ResipFastOStream& operator<<(UInt64 ui64)
262 {
263 if (!buf_)
264 {
265 return *this;
266 }
267
268 char buf[66];
269 UI64TOA(ui64,buf,66,10);
270
271 size_t count = strlen(buf);
272 if (buf_->writebuf(buf,count) < count)
273 {
274 good_ = false;
275 }
276
277 return *this;
278 }
279 #endif
280
281 ResipFastOStream& operator<<(float f)
282 {
283 *this<< (static_cast<double>(f));
284
285 return *this;
286 }
287
288 ResipFastOStream& operator<<(double d)
289 {
290 if (!buf_)
291 {
292 return *this;
293 }
294
295 char buf[_CVTBUFSIZE];
296 GCVT(d,6,buf,_CVTBUFSIZE);//6 significant digits is the default for %f
297 size_t count = strlen(buf);
298 #ifndef WIN32
299 //not using the non-standard microsoft conversion functions
300 //remove any trailing zeros. Note that resipfastreams does not support STL stream width or precision
301 //modifiers
302 size_t idx=0;
303 for (; count > 1; count--)
304 {
305 idx = count-1;
306 if (buf[idx] != '0' && buf[idx] != '.')
307 {
308 break;
309 }
310 }
311 #endif
312 if (buf_->writebuf(buf,count) < count)
313 {
314 good_ = false;
315 }
316
317 return *this;
318 }
319
320 ResipFastOStream& operator<<(const void *vp)
321 {
322 if (!buf_)
323 {
324 return *this;
325 }
326
327 char buf[32];
328 SNPRINTF_1(buf,32,_TRUNCATE,"%p",vp);
329 size_t count = strlen(buf);
330 if (buf_->writebuf(buf,count) < count)
331 {
332 good_ = false;
333 }
334
335 return *this;
336 }
337 #ifdef WIN32
338 ResipFastOStream& operator<<(std::ostream& (__cdecl *_Pfn)(std::ostream&))
339 #else
340 ResipFastOStream& operator<<(std::ostream& (*_Pfn)(std::ostream &))
341 #endif
342 {
343 if (!buf_)
344 {
345 return *this;
346 }
347
348 if (_Pfn == static_cast<std::ostream& (*)(std::ostream&)>(std::endl))
349 {
350 if (buf_->writebuf("\n",1) < 1)
351 {
352 good_ = false;
353 }
354 }
355 else
356 {
357 assert(0);
358 }
359 return *this;
360 }
361
362 private:
363 ResipStreamBuf *buf_;
364 };
365
366
367 inline resip::ResipFastOStream& operator<<(resip::ResipFastOStream& ostr, const char *str)
368 {
369 ostr.write(str,strlen(str));
370
371 return ostr;
372 }
373
374 inline resip::ResipFastOStream& operator<<(resip::ResipFastOStream& ostr, char ch)
375 {
376 ostr.put(ch);
377
378 return ostr;
379 }
380
381 inline resip::ResipFastOStream& operator<<(resip::ResipFastOStream& ostr, unsigned char ch)
382 {
383 ostr.put((char)ch);
384
385 return ostr;
386 }
387
388 inline resip::ResipFastOStream& operator<<(resip::ResipFastOStream& ostr, const unsigned char *str)
389 {
390 ostr.write((const char *)str,strlen((const char *)str));
391
392 return ostr;
393 }
394
395 inline resip::ResipFastOStream& operator<<(resip::ResipFastOStream& ostr, signed char ch)
396 {
397 ostr.put((char)ch);
398
399 return ostr;
400 }
401
402 inline resip::ResipFastOStream& operator<<(resip::ResipFastOStream& ostr, const signed char *str)
403 {
404 ostr.write((const char *)str,strlen((const char *)str));
405
406 return ostr;
407 }
408
409 inline resip::ResipFastOStream & operator<<(resip::ResipFastOStream &ostr, const std::string &str)
410 {
411 ostr.write(str.c_str(),str.size());
412
413 return ostr;
414 }
415
416 /** std::istream replacement
417 */
418 class ResipFastIStream : public ResipBasicIOStream
419 {
420 public:
421 ResipFastIStream(ResipStreamBuf *buf):buf_(buf)
422 {}
423 virtual ~ResipFastIStream(void)
424 {}
425
426 ResipStreamBuf * rdbuf(void) const
427 {
428 return buf_;
429 }
430
431 ResipFastIStream &read(char *s, size_t count)
432 {
433 if (rdbuf())
434 {
435 rdbuf()->readbuf(s,count);
436 }
437 return *this;
438 }
439
440 private:
441 ResipStreamBuf *buf_;
442 };
443
444 /** Used to replace std::cerr, std::cout, etc.
445 */
446 class ResipStdBuf : public ResipStreamBuf
447 {
448 public:
449 typedef enum BufType
450 {
451 null,
452 stdCerr,
453 stdCout
454 } BufType;
455
456 ResipStdBuf(BufType type)
457 :type_(type)
458 {}
459
460 ~ResipStdBuf(void)
461 {}
462
463 virtual size_t writebuf(const char *s, size_t count)
464 {
465 switch (type_)
466 {
467 case stdCerr:
468 {
469 std::cerr << s;
470 break;
471 }
472 case stdCout:
473 {
474 std::cout << s;
475 break;
476 }
477 default:
478 break;
479 }
480 return count;
481 }
482 virtual size_t readbuf(char *buf, size_t count)
483 {
484 return 0;
485 }
486 virtual size_t putbuf(char ch)
487 {
488 return writebuf(&ch,1);
489 }
490 virtual void flushbuf(void)
491 {}
492 virtual UInt64 tellpbuf(void)
493 {
494 return 0;
495 }
496
497 private:
498 BufType type_;
499 };
500
501 /** A direct replacement for std::cout, std::cerr, etc.
502 */
503 class ResipStdCOStream: public ResipFastOStream
504 {
505 public:
506 ResipStdCOStream(ResipStdBuf::BufType type)
507 :ResipFastOStream(0),buf_(type)
508 {
509 rdbuf(&buf_);
510 }
511
512 ~ResipStdCOStream(void)
513 {}
514
515 private:
516 ResipStdBuf buf_;
517 };
518
519 #ifdef RESIP_USE_STL_STREAMS
520 #define EncodeStream std::ostream
521 #define DecodeStream std::istream
522 #define resipCerr std::cerr
523 #define resipCout std::cout
524 #else
525 #define EncodeStream resip::ResipFastOStream
526 #define DecodeStream resip::ResipFastIStream
527 extern ResipStdCOStream resipFastCerr;
528 extern ResipStdCOStream resipFastCout;
529 #define resipCerr resip::resipFastCerr
530 #define resipCout resip::resipFastCout
531 #endif
532 extern ResipStdCOStream resipFastNull;
533
534 } //namespace resip
535
536 #endif //RESIP_RESIPFASTSTREAMS_HXX
537
538 /* ====================================================================
539 * The Vovida Software License, Version 1.0
540 *
541 * Copyright (c) 2005. All rights reserved.
542 *
543 * Redistribution and use in source and binary forms, with or without
544 * modification, are permitted provided that the following conditions
545 * are met:
546 *
547 * 1. Redistributions of source code must retain the above copyright
548 * notice, this list of conditions and the following disclaimer.
549 *
550 * 2. Redistributions in binary form must reproduce the above copyright
551 * notice, this list of conditions and the following disclaimer in
552 * the documentation and/or other materials provided with the
553 * distribution.
554 *
555 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
556 * and "Vovida Open Communication Application Library (VOCAL)" must
557 * not be used to endorse or promote products derived from this
558 * software without prior written permission. For written
559 * permission, please contact vocal@vovida.org.
560 *
561 * 4. Products derived from this software may not be called "VOCAL", nor
562 * may "VOCAL" appear in their name, without prior written
563 * permission of Vovida Networks, Inc.
564 *
565 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
566 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
567 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
568 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
569 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
570 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
571 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
572 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
573 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
574 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
575 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
576 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
577 * DAMAGE.
578 *
579 * ====================================================================
580 *
581 * This software consists of voluntary contributions made by Vovida
582 * Networks, Inc. and many individuals on behalf of Vovida Networks,
583 * Inc. For more information on Vovida Networks, Inc., please see
584 * <http://www.vovida.org/>.
585 *
586 */
587
588
589
590

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27