cisst-saw
Toggle main menu visibility
Loading...
Searching...
No Matches
tmp
cisst-saw
cisst
cisstCommon
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
14
This software is provided "as is" under an open source license, with
15
no warranty. The complete license can be found in license.txt and
16
http://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
30
#include <
cisstCommon/cmnPortability.h
>
31
#include <iostream>
32
64
65
template
<
class
_element,
class
_trait = std::
char
_traits<_element> >
66
class
cmnCallbackStreambuf
:
public
std::basic_streambuf<_element, _trait>
67
{
68
public
:
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
80
cmnCallbackStreambuf
(
CallbackType
func) :
Idx
(0),
Callback
(func) { }
81
82
protected
:
83
84
ElementType
Buffer
[256];
85
unsigned
int
Idx
;
86
CallbackType
Callback
;
87
89
virtual
void
PrintLine
() {
Buffer
[
Idx
] = 0;
Callback
(
Buffer
,
Idx
);
Idx
= 0; }
90
93
virtual
int_type
overflow
(
int_type
c)
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::cmnCallbackStreambuf
cmnCallbackStreambuf(CallbackType func)
Definition
cmnCallbackStreambuf.h:80
cmnCallbackStreambuf< char >::Idx
unsigned int Idx
Definition
cmnCallbackStreambuf.h:85
cmnCallbackStreambuf::ElementType
_element ElementType
Definition
cmnCallbackStreambuf.h:71
cmnCallbackStreambuf::PrintLine
virtual void PrintLine()
Definition
cmnCallbackStreambuf.h:89
cmnCallbackStreambuf::CallbackType
void(*) CallbackType(const ElementType *, int len)
Definition
cmnCallbackStreambuf.h:73
cmnCallbackStreambuf::sync
virtual int sync()
Definition
cmnCallbackStreambuf.h:137
cmnCallbackStreambuf< char >::Callback
CallbackType Callback
Definition
cmnCallbackStreambuf.h:86
cmnCallbackStreambuf::overflow
virtual int_type overflow(int_type c)
Definition
cmnCallbackStreambuf.h:93
cmnCallbackStreambuf::xsputn
virtual std::streamsize xsputn(const ElementType *s, std::streamsize n)
Definition
cmnCallbackStreambuf.h:113
cmnCallbackStreambuf< char >::Buffer
ElementType Buffer[256]
Definition
cmnCallbackStreambuf.h:84
cmnCallbackStreambuf::int_type
std::basic_streambuf< _element, _trait >::int_type int_type
Definition
cmnCallbackStreambuf.h:69
cmnPortability.h
Portability across compilers and operating systems tools.
Generated by
1.18.0