| 1 | // |
| 2 | // PipeStream.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Processes |
| 6 | // Module: PipeStream |
| 7 | // |
| 8 | // Definition of the PipeStream class. |
| 9 | // |
| 10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Foundation_PipeStream_INCLUDED |
| 18 | #define Foundation_PipeStream_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/Pipe.h" |
| 23 | #include "Poco/BufferedStreamBuf.h" |
| 24 | #include <istream> |
| 25 | #include <ostream> |
| 26 | |
| 27 | |
| 28 | namespace Poco { |
| 29 | |
| 30 | |
| 31 | class Foundation_API PipeStreamBuf: public BufferedStreamBuf |
| 32 | /// This is the streambuf class used for reading from and writing to a Pipe. |
| 33 | { |
| 34 | public: |
| 35 | typedef BufferedStreamBuf::openmode openmode; |
| 36 | |
| 37 | PipeStreamBuf(const Pipe& pipe, openmode mode); |
| 38 | /// Creates a PipeStreamBuf with the given Pipe. |
| 39 | |
| 40 | ~PipeStreamBuf(); |
| 41 | /// Destroys the PipeStreamBuf. |
| 42 | |
| 43 | void close(); |
| 44 | /// Closes the pipe. |
| 45 | |
| 46 | protected: |
| 47 | int readFromDevice(char* buffer, std::streamsize length); |
| 48 | int writeToDevice(const char* buffer, std::streamsize length); |
| 49 | |
| 50 | private: |
| 51 | enum |
| 52 | { |
| 53 | STREAM_BUFFER_SIZE = 1024 |
| 54 | }; |
| 55 | |
| 56 | Pipe _pipe; |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | class Foundation_API PipeIOS: public virtual std::ios |
| 61 | /// The base class for PipeInputStream and |
| 62 | /// PipeOutputStream. |
| 63 | /// |
| 64 | /// This class is needed to ensure the correct initialization |
| 65 | /// order of the stream buffer and base classes. |
| 66 | { |
| 67 | public: |
| 68 | PipeIOS(const Pipe& pipe, openmode mode); |
| 69 | /// Creates the PipeIOS with the given Pipe. |
| 70 | |
| 71 | ~PipeIOS(); |
| 72 | /// Destroys the PipeIOS. |
| 73 | /// |
| 74 | /// Flushes the buffer, but does not close the pipe. |
| 75 | |
| 76 | PipeStreamBuf* rdbuf(); |
| 77 | /// Returns a pointer to the internal PipeStreamBuf. |
| 78 | |
| 79 | void close(); |
| 80 | /// Flushes the stream and closes the pipe. |
| 81 | |
| 82 | protected: |
| 83 | PipeStreamBuf _buf; |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | class Foundation_API PipeOutputStream: public PipeIOS, public std::ostream |
| 88 | /// An output stream for writing to a Pipe. |
| 89 | { |
| 90 | public: |
| 91 | PipeOutputStream(const Pipe& pipe); |
| 92 | /// Creates the PipeOutputStream with the given Pipe. |
| 93 | |
| 94 | ~PipeOutputStream(); |
| 95 | /// Destroys the PipeOutputStream. |
| 96 | /// |
| 97 | /// Flushes the buffer, but does not close the pipe. |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | class Foundation_API PipeInputStream: public PipeIOS, public std::istream |
| 102 | /// An input stream for reading from a Pipe. |
| 103 | /// |
| 104 | /// Using formatted input from a PipeInputStream |
| 105 | /// is not recommended, due to the read-ahead behavior of |
| 106 | /// istream with formatted reads. |
| 107 | { |
| 108 | public: |
| 109 | PipeInputStream(const Pipe& pipe); |
| 110 | /// Creates the PipeInputStream with the given Pipe. |
| 111 | |
| 112 | ~PipeInputStream(); |
| 113 | /// Destroys the PipeInputStream. |
| 114 | }; |
| 115 | |
| 116 | |
| 117 | } // namespace Poco |
| 118 | |
| 119 | |
| 120 | #endif // Foundation_PipeStream_INCLUDED |
| 121 | |