-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtee_stdio_filebuf.hh
44 lines (40 loc) · 1.72 KB
/
tee_stdio_filebuf.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* tee_stdio_filebuf.hh - a stdio_filebuf that behaves as tee(1)
*
* Copyright (C) Javier Lopez-Gomez, 2016
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <ext/stdio_filebuf.h>
template <typename _CharT, typename _Traits = std::char_traits<_CharT>>
class tee_stdio_filebuf : public __gnu_cxx::stdio_filebuf<_CharT, _Traits> {
protected:
typedef __gnu_cxx::stdio_filebuf<_CharT, _Traits> filebuf_type;
std::ostream _out;
typename filebuf_type::int_type underflow() {
auto ret = filebuf_type::underflow();
_out.write(filebuf_type::gptr(), filebuf_type::egptr() - filebuf_type::gptr());
return ret;
}
public:
/**
* @param _fd An open file descriptor.
* @param _mode Same meaning as in a standard filebuf.
* @param _obuf The std::basic_filebuf to write if a read from this
* filebuf is attempted
*
* This constructor associates a file stream buffer with an open
* POSIX file descriptor; a read from this filebuf will cause the read
* buffer to be written to _obuf. The file descriptor will be
* automatically closed when the stdio_filebuf is closed/destroyed.
*/
tee_stdio_filebuf(int _fd, std::ios_base::openmode _mode, std::basic_filebuf<_CharT, _Traits> &_obuf)
: filebuf_type(_fd, _mode), _out(&_obuf) {}
};