| 1 | // |
|---|---|
| 2 | // TeeStream.cpp |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Streams |
| 6 | // Module: TeeStream |
| 7 | // |
| 8 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/TeeStream.h" |
| 16 | |
| 17 | |
| 18 | namespace Poco { |
| 19 | |
| 20 | |
| 21 | TeeStreamBuf::TeeStreamBuf(): |
| 22 | _pIstr(0) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | |
| 27 | TeeStreamBuf::TeeStreamBuf(std::istream& istr): |
| 28 | _pIstr(&istr) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | |
| 33 | TeeStreamBuf::TeeStreamBuf(std::ostream& ostr): |
| 34 | _pIstr(0) |
| 35 | { |
| 36 | _streams.push_back(&ostr); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | TeeStreamBuf::~TeeStreamBuf() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void TeeStreamBuf::addStream(std::ostream& ostr) |
| 46 | { |
| 47 | _streams.push_back(&ostr); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | int TeeStreamBuf::readFromDevice() |
| 52 | { |
| 53 | if (_pIstr) |
| 54 | { |
| 55 | int c = _pIstr->get(); |
| 56 | if (c != -1) writeToDevice((char) c); |
| 57 | return c; |
| 58 | } |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | int TeeStreamBuf::writeToDevice(char c) |
| 64 | { |
| 65 | for (StreamVec::iterator it = _streams.begin(); it != _streams.end(); ++it) |
| 66 | { |
| 67 | (*it)->put(c); |
| 68 | } |
| 69 | return charToInt(c); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | TeeIOS::TeeIOS() |
| 74 | { |
| 75 | poco_ios_init(&_buf); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | TeeIOS::TeeIOS(std::istream& istr): _buf(istr) |
| 80 | { |
| 81 | poco_ios_init(&_buf); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | TeeIOS::TeeIOS(std::ostream& ostr): _buf(ostr) |
| 86 | { |
| 87 | poco_ios_init(&_buf); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | TeeIOS::~TeeIOS() |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void TeeIOS::addStream(std::ostream& ostr) |
| 97 | { |
| 98 | _buf.addStream(ostr); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | TeeStreamBuf* TeeIOS::rdbuf() |
| 103 | { |
| 104 | return &_buf; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | TeeInputStream::TeeInputStream(std::istream& istr): TeeIOS(istr), std::istream(&_buf) |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | |
| 113 | TeeInputStream::~TeeInputStream() |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | |
| 118 | TeeOutputStream::TeeOutputStream(): std::ostream(&_buf) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | |
| 123 | TeeOutputStream::TeeOutputStream(std::ostream& ostr): TeeIOS(ostr), std::ostream(&_buf) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | |
| 128 | TeeOutputStream::~TeeOutputStream() |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | |
| 133 | } // namespace Poco |
| 134 |