15 #include "multiplexStreamBuf.h"
17 #if defined(WIN32_VC) || defined(WIN64_VC)
18 #define WINDOWS_LEAN_AND_MEAN
20 #undef WINDOWS_LEAN_AND_MEAN
28 #ifndef HAVE_STREAMSIZE
30 typedef int streamsize;
39 void MultiplexStreamBuf::Output::
42 switch (_output_type) {
44 assert(_out != (ostream *)NULL);
49 assert(_fout != (FILE *)NULL);
64 void MultiplexStreamBuf::Output::
65 write_string(
const string &str) {
66 switch (_output_type) {
68 assert(_out != (ostream *)NULL);
69 _out->write(str.data(), str.length());
74 assert(_fout != (FILE *)NULL);
75 fwrite(str.data(), str.length(), 1, _fout);
80 #if defined(WIN32_VC) || defined(WIN64_VC)
81 OutputDebugString(str.c_str());
93 MultiplexStreamBuf() {
94 #ifndef PHAVE_IOSTREAM
107 ~MultiplexStreamBuf() {
111 Outputs::iterator oi;
112 for (oi = _outputs.begin(); oi != _outputs.end(); ++oi) {
127 MultiplexStreamBuf::OutputType output_type,
128 ostream *out, FILE *fout,
bool owns_obj) {
136 o._buffer_type = buffer_type;
137 o._output_type = output_type;
140 o._owns_obj = owns_obj;
141 _outputs.push_back(o);
156 write_chars(
"", 0,
true);
165 int MultiplexStreamBuf::
171 streamsize n = pptr() - pbase();
174 write_chars(pbase(), n,
false);
181 write_chars(&c, 1,
false);
194 int MultiplexStreamBuf::
200 streamsize n = pptr() - pbase();
207 write_chars(pbase(), n,
false);
224 void MultiplexStreamBuf::
225 write_chars(
const char *start,
int length,
bool flush) {
226 size_t orig = _line_buffer.length();
229 latest = string(start, length);
236 line = _line_buffer + latest;
242 _line_buffer += latest;
243 size_t eol = _line_buffer.rfind(
'\n', orig);
244 if (eol != string::npos) {
245 line = _line_buffer.substr(0, eol + 1);
246 _line_buffer = _line_buffer.substr(eol + 1);
250 Outputs::iterator oi;
251 for (oi = _outputs.begin(); oi != _outputs.end(); ++oi) {
253 switch (out._buffer_type) {
256 if (!latest.empty()) {
257 out.write_string(latest);
265 out.write_string(line);
void flush()
Forces out all output that hasn't yet been written.
void add_output(BufferType buffer_type, OutputType output_type, ostream *out=(ostream *) NULL, FILE *fout=(FILE *) NULL, bool owns_obj=false)
Adds the indicated output destinition to the set of things that will be written to when characters ar...