blob: bbd98a6fbd7c3f3dbc174267a55a048967d66a16 (
plain) (
blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#pragma once
#include "input.h"
#include "output.h"
#include <util/system/pipe.h>
#include <util/generic/ptr.h>
#include <util/generic/string.h>
/**
* @addtogroup Streams_Pipes
* @{
*/
/**
* Base class for starting a process and communicating with it via pipes.
*/
class TPipeBase {
protected:
/**
* Starts a new process and opens a pipe.
*
* @param command Command line to start a process with.
* @param mode Data transfer mode for the pipe. Use
* "r" for reading and "w" for writing.
*/
TPipeBase(const TString& command, const char* mode);
virtual ~TPipeBase();
protected:
class TImpl;
THolder<TImpl> Impl_;
};
/**
* Input stream that binds to a standard output stream of a newly started process.
*
* Note that if the process ends with non-zero exit status, `Read` function will
* throw an exception.
*/
class TPipeInput: protected TPipeBase, public IInputStream {
public:
/**
* Starts a new process and opens a pipe.
*
* @param command Command line to start a process with.
*/
TPipeInput(const TString& command);
private:
size_t DoRead(void* buf, size_t len) override;
};
/**
* Output stream that binds to a standard input stream of a newly started process.
*
* Note that if the process ends with non-zero exit status, `Close` function will
* throw an exception.
*/
class TPipeOutput: protected TPipeBase, public IOutputStream {
public:
/**
* Starts a new process and opens a pipe.
*
* @param command Command line to start a process with.
*/
TPipeOutput(const TString& command);
/**
* Waits for the process to terminate and throws an exception if it ended
* with a non-zero exit status.
*/
void Close();
private:
void DoWrite(const void* buf, size_t len) override;
};
class TPipedBase {
protected:
TPipedBase(PIPEHANDLE fd);
virtual ~TPipedBase();
protected:
TPipeHandle Handle_;
};
/**
* Input stream that binds to a standard output stream of an existing process.
*/
class TPipedInput: public TPipedBase, public IInputStream {
public:
TPipedInput(PIPEHANDLE fd);
~TPipedInput() override;
private:
size_t DoRead(void* buf, size_t len) override;
};
/**
* Output stream that binds to a standard input stream of an existing process.
*/
class TPipedOutput: public TPipedBase, public IOutputStream {
public:
TPipedOutput(PIPEHANDLE fd);
~TPipedOutput() override;
private:
void DoWrite(const void* buf, size_t len) override;
};
/** @} */
|