cisst-saw
Loading...
Searching...
No Matches
cmnCallbackStreambuf.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3
4/*
5
6 Author(s): Peter Kazanzides
7 Created on: 2006-01-13
8
9 (C) Copyright 2006-2007 Johns Hopkins University (JHU), All Rights
10 Reserved.
11
12--- begin cisst license - do not edit ---
13
14This software is provided "as is" under an open source license, with
15no warranty. The complete license can be found in license.txt and
16http://www.cisst.org/cisst/license.txt.
17
18--- end cisst license ---
19
20*/
21
25#pragma once
26
27#ifndef _cmnCallbackStreambuf_h
28#define _cmnCallbackStreambuf_h
29
31#include <iostream>
32
64
65template<class _element, class _trait = std::char_traits<_element> >
66class cmnCallbackStreambuf : public std::basic_streambuf<_element, _trait>
67{
68public:
69 typedef typename std::basic_streambuf<_element, _trait>::int_type int_type;
71 typedef _element ElementType;
73 typedef void (*CallbackType)(const ElementType *, int len);
74
81
82protected:
83
85 unsigned int Idx;
87
89 virtual void PrintLine() { Buffer[Idx] = 0; Callback(Buffer, Idx); Idx = 0; }
90
94 {
95 // follow the basic_streambuf standard
96 if (_trait::eq_int_type(_trait::eof(), c))
97 return (_trait::not_eof(c));
98
99 // This code assumes that the buffer can never be full
100 // on entry because full buffers are always printed.
101 Buffer[Idx++] = c;
102 if (c == '\n')
103 PrintLine(); // We got a newline, print the buffer
104 else if (Idx >= sizeof(Buffer)-1)
105 PrintLine(); // Buffer is full, print it
106
107 // follow the basic_streambuf standard
108 return _trait::not_eof(c);
109 }
110
113 virtual std::streamsize xsputn(const ElementType* s, std::streamsize n)
114 {
115 std::streamsize todo = n;
116 while (todo > 0) {
117 std::streamsize tmp = static_cast<std::streamsize>(sizeof(Buffer)-Idx-1);
118 std::streamsize limit = (todo < tmp) ? todo : tmp;
119 for (std::streamsize i=0; i < limit; i++) {
120 Buffer[Idx++] = *s;
121 if (*s == '\n')
122 PrintLine();
123 s++;
124 }
125 todo -= limit;
126 // If buffer is full, print it
127 if (Idx >= sizeof(Buffer)-1)
128 PrintLine();
129 }
130 // If buffer is full, print it
131 if (Idx >= sizeof(Buffer)-1)
132 PrintLine();
133 return n;
134 }
135
137 virtual int sync()
138 {
139 if (Idx > 0)
140 PrintLine();
141 return 0;
142 }
143};
144
145#endif // _cmnCallbackStreambuf_h
cmnCallbackStreambuf(CallbackType func)
Definition cmnCallbackStreambuf.h:80
unsigned int Idx
Definition cmnCallbackStreambuf.h:85
_element ElementType
Definition cmnCallbackStreambuf.h:71
virtual void PrintLine()
Definition cmnCallbackStreambuf.h:89
void(*) CallbackType(const ElementType *, int len)
Definition cmnCallbackStreambuf.h:73
virtual int sync()
Definition cmnCallbackStreambuf.h:137
CallbackType Callback
Definition cmnCallbackStreambuf.h:86
virtual int_type overflow(int_type c)
Definition cmnCallbackStreambuf.h:93
virtual std::streamsize xsputn(const ElementType *s, std::streamsize n)
Definition cmnCallbackStreambuf.h:113
ElementType Buffer[256]
Definition cmnCallbackStreambuf.h:84
std::basic_streambuf< _element, _trait >::int_type int_type
Definition cmnCallbackStreambuf.h:69
Portability across compilers and operating systems tools.