2023-02-08 11:04:11 +00:00
|
|
|
//
|
|
|
|
// PipeStream.h
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Processes
|
|
|
|
// Module: PipeStream
|
|
|
|
//
|
|
|
|
// Definition of the PipeStream class.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef Foundation_PipeStream_INCLUDED
|
|
|
|
#define Foundation_PipeStream_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
#include <istream>
|
|
|
|
#include <ostream>
|
2023-02-13 09:22:33 +00:00
|
|
|
#include "Poco/BufferedStreamBuf.h"
|
|
|
|
#include "Poco/Foundation.h"
|
|
|
|
#include "Poco/Pipe.h"
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
namespace Poco
|
|
|
|
{
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
class Foundation_API PipeStreamBuf : public BufferedStreamBuf
|
|
|
|
/// This is the streambuf class used for reading from and writing to a Pipe.
|
2023-02-08 11:04:11 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-02-13 09:22:33 +00:00
|
|
|
typedef BufferedStreamBuf::openmode openmode;
|
|
|
|
|
|
|
|
PipeStreamBuf(const Pipe & pipe, openmode mode);
|
|
|
|
/// Creates a PipeStreamBuf with the given Pipe.
|
|
|
|
|
|
|
|
~PipeStreamBuf();
|
|
|
|
/// Destroys the PipeStreamBuf.
|
|
|
|
|
|
|
|
void close();
|
|
|
|
/// Closes the pipe.
|
|
|
|
|
2023-02-08 11:04:11 +00:00
|
|
|
protected:
|
2023-02-13 09:22:33 +00:00
|
|
|
int readFromDevice(char * buffer, std::streamsize length);
|
|
|
|
int writeToDevice(const char * buffer, std::streamsize length);
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
private:
|
2023-02-13 09:22:33 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
STREAM_BUFFER_SIZE = 1024
|
|
|
|
};
|
2023-02-08 11:04:11 +00:00
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
Pipe _pipe;
|
2023-02-08 11:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
class Foundation_API PipeIOS : public virtual std::ios
|
|
|
|
/// The base class for PipeInputStream and
|
|
|
|
/// PipeOutputStream.
|
|
|
|
///
|
|
|
|
/// This class is needed to ensure the correct initialization
|
|
|
|
/// order of the stream buffer and base classes.
|
2023-02-08 11:04:11 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-02-13 09:22:33 +00:00
|
|
|
PipeIOS(const Pipe & pipe, openmode mode);
|
|
|
|
/// Creates the PipeIOS with the given Pipe.
|
|
|
|
|
|
|
|
~PipeIOS();
|
|
|
|
/// Destroys the PipeIOS.
|
|
|
|
///
|
|
|
|
/// Flushes the buffer, but does not close the pipe.
|
|
|
|
|
|
|
|
PipeStreamBuf * rdbuf();
|
|
|
|
/// Returns a pointer to the internal PipeStreamBuf.
|
|
|
|
|
|
|
|
void close();
|
|
|
|
/// Flushes the stream and closes the pipe.
|
2023-02-08 11:04:11 +00:00
|
|
|
|
|
|
|
protected:
|
2023-02-13 09:22:33 +00:00
|
|
|
PipeStreamBuf _buf;
|
2023-02-08 11:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
class Foundation_API PipeOutputStream : public PipeIOS, public std::ostream
|
|
|
|
/// An output stream for writing to a Pipe.
|
2023-02-08 11:04:11 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-02-13 09:22:33 +00:00
|
|
|
PipeOutputStream(const Pipe & pipe);
|
|
|
|
/// Creates the PipeOutputStream with the given Pipe.
|
2023-02-08 11:04:11 +00:00
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
~PipeOutputStream();
|
|
|
|
/// Destroys the PipeOutputStream.
|
|
|
|
///
|
|
|
|
/// Flushes the buffer, but does not close the pipe.
|
2023-02-08 11:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
class Foundation_API PipeInputStream : public PipeIOS, public std::istream
|
|
|
|
/// An input stream for reading from a Pipe.
|
|
|
|
///
|
|
|
|
/// Using formatted input from a PipeInputStream
|
|
|
|
/// is not recommended, due to the read-ahead behavior of
|
|
|
|
/// istream with formatted reads.
|
2023-02-08 11:04:11 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-02-13 09:22:33 +00:00
|
|
|
PipeInputStream(const Pipe & pipe);
|
|
|
|
/// Creates the PipeInputStream with the given Pipe.
|
2023-02-08 11:04:11 +00:00
|
|
|
|
2023-02-13 09:22:33 +00:00
|
|
|
~PipeInputStream();
|
|
|
|
/// Destroys the PipeInputStream.
|
2023-02-08 11:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|
|
|
|
|
|
|
|
|
|
|
|
#endif // Foundation_PipeStream_INCLUDED
|