]> git.saurik.com Git - wxWidgets.git/blame - include/wx/unix/pipe.h
Initial work on virtual file system support for the WebKitGTK+ backend. It now suppor...
[wxWidgets.git] / include / wx / unix / pipe.h
CommitLineData
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
24class wxPipe
25{
26public:
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
8e6efd1f 49 return false;
e2478fde
VZ
50 }
51
8e6efd1f 52 return true;
e2478fde
VZ
53 }
54
b8353201
VZ
55 // switch the given end of the pipe to non-blocking IO
56 bool MakeNonBlocking(Direction which)
57 {
58 const int flags = fcntl(m_fds[which], F_GETFL, 0);
59 if ( flags == -1 )
60 return false;
61
62 return fcntl(m_fds[which], F_SETFL, flags | O_NONBLOCK) == 0;
63 }
64
e2478fde
VZ
65 // return TRUE if we were created successfully
66 bool IsOk() const { return m_fds[Read] != INVALID_FD; }
67
68 // return the descriptor for one of the pipe ends
7fa4dc1c 69 int operator[](Direction which) const { return m_fds[which]; }
e2478fde
VZ
70
71 // detach a descriptor, meaning that the pipe dtor won't close it, and
72 // return it
73 int Detach(Direction which)
74 {
e2478fde
VZ
75 int fd = m_fds[which];
76 m_fds[which] = INVALID_FD;
77
78 return fd;
79 }
80
81 // close the pipe descriptors
82 void Close()
83 {
84 for ( size_t n = 0; n < WXSIZEOF(m_fds); n++ )
85 {
86 if ( m_fds[n] != INVALID_FD )
ca7aeeb7 87 {
e2478fde 88 close(m_fds[n]);
ca7aeeb7
VZ
89 m_fds[n] = INVALID_FD;
90 }
e2478fde
VZ
91 }
92 }
93
94 // dtor closes the pipe descriptors
95 ~wxPipe() { Close(); }
96
97private:
e4edebc0 98 int m_fds[2];
e2478fde
VZ
99};
100
9a88f03a 101#if wxUSE_STREAMS && wxUSE_FILE
2887179b
VZ
102
103#include "wx/wfstream.h"
104
105// ----------------------------------------------------------------------------
106// wxPipeInputStream: stream for reading from a pipe
107// ----------------------------------------------------------------------------
108
109class wxPipeInputStream : public wxFileInputStream
110{
111public:
112 wxPipeInputStream(int fd) : wxFileInputStream(fd) { }
113
114 // return TRUE if the pipe is still opened
115 bool IsOpened() const { return !Eof(); }
116
117 // return TRUE if we have anything to read, don't block
118 virtual bool CanRead() const;
119};
120
3b816097
VZ
121// ----------------------------------------------------------------------------
122// wxPipeOutputStream: stream for writing to a pipe
123// ----------------------------------------------------------------------------
124
125class wxPipeOutputStream : public wxFileOutputStream
126{
127public:
128 wxPipeOutputStream(int fd) : wxFileOutputStream(fd) { }
129
130 // Override the base class version to ignore "pipe full" errors: this is
131 // not an error for this class.
132 size_t OnSysWrite(const void *buffer, size_t size);
133};
134
9a88f03a 135#endif // wxUSE_STREAMS && wxUSE_FILE
2887179b 136
e2478fde
VZ
137#endif // _WX_UNIX_PIPE_H_
138