added and documented wxProcess::Open()

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16152 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-07-12 18:15:49 +00:00
parent 9a26db9ed2
commit da00a8bb16
3 changed files with 98 additions and 13 deletions

View File

@@ -45,6 +45,8 @@ for explicit destruction.}
\func{}{wxProcess}{\param{wxEvtHandler *}{ parent = NULL}, \param{int}{ id = -1}} \func{}{wxProcess}{\param{wxEvtHandler *}{ parent = NULL}, \param{int}{ id = -1}}
\func{}{wxProcess}{\param{int }{flags}}
Constructs a process object. {\it id} is only used in the case you want to Constructs a process object. {\it id} is only used in the case you want to
use wxWindows events. It identifies this object, or another window that will use wxWindows events. It identifies this object, or another window that will
receive the event. receive the event.
@@ -53,12 +55,20 @@ If the {\it parent} parameter is different from NULL, it will receive
a wxEVT\_END\_PROCESS notification event (you should insert EVT\_END\_PROCESS a wxEVT\_END\_PROCESS notification event (you should insert EVT\_END\_PROCESS
macro in the event table of the parent to handle it) with the given {\it id}. macro in the event table of the parent to handle it) with the given {\it id}.
The second constructor creates an object without any associated parent (and
hence no id neither) but allows to specify the {\it flags} which can have the
value of {\tt wxPROCESS\_DEFAULT} or {\tt wxPROCESS\_REDIRECT}. Specifying the
former value has no particular effect while using the latter one is equivalent
to calling \helpref{Redirect}{wxprocessredirect}.
\wxheading{Parameters} \wxheading{Parameters}
\docparam{parent}{The event handler parent.} \docparam{parent}{The event handler parent.}
\docparam{id}{id of an event.} \docparam{id}{id of an event.}
\docparam{flags}{either {\tt wxPROCESS\_DEFAULT} or {\tt wxPROCESS\_REDIRECT}}
\membersection{wxProcess::\destruct{wxProcess}} \membersection{wxProcess::\destruct{wxProcess}}
\func{}{\destruct{wxProcess}}{\void} \func{}{\destruct{wxProcess}}{\void}
@@ -184,6 +194,33 @@ It raises a wxWindows event when it isn't overridden.
\docparam{status}{The exit code of the process.} \docparam{status}{The exit code of the process.}
\membersection{wxProcess::Open}\label{wxprocessopen}
\func{static wxProcess *}{Open}{\param{const wxString\& }{cmd}}
This static method replaces the standard {\tt popen()} function: it launches
the process specified by the {\it cmd} parameter and returns the wxProcess
object which can be used to retrieve the streams connected to the standard
input, output and error output of the child process.
If the process couldn't be launched, {\tt NULL} is returned. Note that in any
case the returned pointer should {\bf not} be deleted, rather the process
object will be destroyed automatically when the child process terminates. This
does mean that the child process should be told to quit before the main program
exits to avoid memory leaks.
\wxheading{Parameters}
\docparam{cmd}{The command to execute, including optional arguments.}
\wxheading{Return value}
A pointer to new wxProcess object or {\tt NULL} on error.
\wxheading{See also}
\helpref{wxExecute}{wxexecute}
\membersection{wxProcess::Redirect}\label{wxprocessredirect} \membersection{wxProcess::Redirect}\label{wxprocessredirect}
\func{void}{Redirect}{\void} \func{void}{Redirect}{\void}

View File

@@ -24,6 +24,16 @@
#include "wx/utils.h" // for wxSignal #include "wx/utils.h" // for wxSignal
// the wxProcess creation flags
enum
{
// no redirection
wxPROCESS_DEFAULT = 0,
// redirect the IO of the child process
wxPROCESS_REDIRECT = 1
};
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// A wxProcess object should be passed to wxExecute - than its OnTerminate() // A wxProcess object should be passed to wxExecute - than its OnTerminate()
// function will be called when the process terminates. // function will be called when the process terminates.
@@ -32,10 +42,27 @@
class WXDLLEXPORT wxProcess : public wxEvtHandler class WXDLLEXPORT wxProcess : public wxEvtHandler
{ {
public: public:
// kill the process with the given PID
static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM);
// test if the given process exists
static bool Exists(int pid);
// this function replaces the standard popen() one: it launches a process
// asynchronously and allows the caller to get the streams connected to its
// std{in|out|err}
//
// on error NULL is returned, in any case the process object will be
// deleted automatically when the process terminates and should *not* be
// deleted by the caller
static wxProcess *Open(const wxString& cmd);
// ctors
wxProcess(wxEvtHandler *parent = (wxEvtHandler *) NULL, int id = -1) wxProcess(wxEvtHandler *parent = (wxEvtHandler *) NULL, int id = -1)
{ Init(parent, id, FALSE); } { Init(parent, id, wxPROCESS_DEFAULT); }
wxProcess(wxEvtHandler *parent, bool redirect)
{ Init(parent, -1, redirect); } wxProcess(int flags) { Init(NULL, -1, flags); }
virtual ~wxProcess(); virtual ~wxProcess();
@@ -62,23 +89,30 @@ public:
void CloseOutput() { delete m_outputStream; m_outputStream = NULL; } void CloseOutput() { delete m_outputStream; m_outputStream = NULL; }
// implementation only (for wxExecute) // implementation only (for wxExecute)
void SetPipeStreams(wxInputStream *inStream, //
wxOutputStream *outStream, // NB: the streams passed here should correspond to the child process
// stdout, stdin and stderr and here the normal naming convention is
// used unlike elsewhere in this class
void SetPipeStreams(wxInputStream *outStream,
wxOutputStream *inStream,
wxInputStream *errStream); wxInputStream *errStream);
#endif // wxUSE_STREAMS #endif // wxUSE_STREAMS
// kill the process with the given PID // for backwards compatibility only, don't use
static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM); #if WXWIN_COMPATIBILITY_2_2
wxProcess(wxEvtHandler *parent, bool redirect)
// test if the given process exists { Init(parent, -1, redirect ? wxPROCESS_REDIRECT : wxPROCESS_DEFAULT); }
static bool Exists(int pid); #endif // WXWIN_COMPATIBILITY_2_2
protected: protected:
void Init(wxEvtHandler *parent, int id, bool redirect); void Init(wxEvtHandler *parent, int id, int flags);
int m_id; int m_id;
#if wxUSE_STREAMS #if wxUSE_STREAMS
// these streams are connected to stdout, stderr and stdin of the child
// process respectively (yes, m_inputStream corresponds to stdout -- very
// confusing but too late to change now)
wxInputStream *m_inputStream, wxInputStream *m_inputStream,
*m_errorStream; *m_errorStream;
wxOutputStream *m_outputStream; wxOutputStream *m_outputStream;

View File

@@ -47,13 +47,13 @@ IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
// wxProcess creation // wxProcess creation
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxProcess::Init(wxEvtHandler *parent, int id, bool redirect) void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
{ {
if ( parent ) if ( parent )
SetNextHandler(parent); SetNextHandler(parent);
m_id = id; m_id = id;
m_redirect = redirect; m_redirect = (flags & wxPROCESS_REDIRECT) != 0;
#if wxUSE_STREAMS #if wxUSE_STREAMS
m_inputStream = NULL; m_inputStream = NULL;
@@ -62,6 +62,20 @@ void wxProcess::Init(wxEvtHandler *parent, int id, bool redirect)
#endif // wxUSE_STREAMS #endif // wxUSE_STREAMS
} }
/* static */
wxProcess *wxProcess::Open(const wxString& cmd)
{
wxProcess *process = new wxProcess(wxPROCESS_REDIRECT);
if ( !wxExecute(cmd, wxEXEC_ASYNC, process) )
{
// couldn't launch the process
delete process;
return NULL;
}
return process;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxProcess termination // wxProcess termination
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------