Commit | Line | Data |
---|---|---|
e2478fde VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/unix/pipe.h | |
3 | // Purpose: wxPipe class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 24.06.2003 (extracted from src/unix/utilsunx.cpp) | |
7 | // RCS-ID: $Id$ | |
77ffb593 | 8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org> |
65571936 | 9 | // Licence: wxWindows licence |
e2478fde VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_UNIX_PIPE_H_ | |
13 | #define _WX_UNIX_PIPE_H_ | |
14 | ||
15 | #include <unistd.h> | |
16 | ||
af848193 VS |
17 | #include "wx/log.h" |
18 | #include "wx/intl.h" | |
19 | ||
e2478fde VZ |
20 | // ---------------------------------------------------------------------------- |
21 | // wxPipe: this class encapsulates pipe() system call | |
22 | // ---------------------------------------------------------------------------- | |
23 | ||
24 | class wxPipe | |
25 | { | |
26 | public: | |
27 | // the symbolic names for the pipe ends | |
28 | enum Direction | |
29 | { | |
30 | Read, | |
e4edebc0 | 31 | Write |
e2478fde VZ |
32 | }; |
33 | ||
34 | enum | |
35 | { | |
36 | INVALID_FD = -1 | |
37 | }; | |
38 | ||
39 | // default ctor doesn't do anything | |
40 | wxPipe() { m_fds[Read] = m_fds[Write] = INVALID_FD; } | |
41 | ||
42 | // create the pipe, return TRUE if ok, FALSE on error | |
43 | bool Create() | |
44 | { | |
45 | if ( pipe(m_fds) == -1 ) | |
46 | { | |
47 | wxLogSysError(_("Pipe creation failed")); | |
48 | ||
49 | return FALSE; | |
50 | } | |
51 | ||
52 | return TRUE; | |
53 | } | |
54 | ||
55 | // return TRUE if we were created successfully | |
56 | bool IsOk() const { return m_fds[Read] != INVALID_FD; } | |
57 | ||
58 | // return the descriptor for one of the pipe ends | |
7fa4dc1c | 59 | int operator[](Direction which) const { return m_fds[which]; } |
e2478fde VZ |
60 | |
61 | // detach a descriptor, meaning that the pipe dtor won't close it, and | |
62 | // return it | |
63 | int Detach(Direction which) | |
64 | { | |
e2478fde VZ |
65 | int fd = m_fds[which]; |
66 | m_fds[which] = INVALID_FD; | |
67 | ||
68 | return fd; | |
69 | } | |
70 | ||
71 | // close the pipe descriptors | |
72 | void Close() | |
73 | { | |
74 | for ( size_t n = 0; n < WXSIZEOF(m_fds); n++ ) | |
75 | { | |
76 | if ( m_fds[n] != INVALID_FD ) | |
77 | close(m_fds[n]); | |
78 | } | |
79 | } | |
80 | ||
81 | // dtor closes the pipe descriptors | |
82 | ~wxPipe() { Close(); } | |
83 | ||
84 | private: | |
e4edebc0 | 85 | int m_fds[2]; |
e2478fde VZ |
86 | }; |
87 | ||
2887179b VZ |
88 | #if wxUSE_STREAMS |
89 | ||
90 | #include "wx/wfstream.h" | |
91 | ||
92 | // ---------------------------------------------------------------------------- | |
93 | // wxPipeInputStream: stream for reading from a pipe | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | class wxPipeInputStream : public wxFileInputStream | |
97 | { | |
98 | public: | |
99 | wxPipeInputStream(int fd) : wxFileInputStream(fd) { } | |
100 | ||
101 | // return TRUE if the pipe is still opened | |
102 | bool IsOpened() const { return !Eof(); } | |
103 | ||
104 | // return TRUE if we have anything to read, don't block | |
105 | virtual bool CanRead() const; | |
106 | }; | |
107 | ||
108 | #endif // wxUSE_STREAMS | |
109 | ||
e2478fde VZ |
110 | #endif // _WX_UNIX_PIPE_H_ |
111 |