]> git.saurik.com Git - wxWidgets.git/commitdiff
added and documented wxProcess::Open()
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 12 Jul 2002 18:15:49 +0000 (18:15 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 12 Jul 2002 18:15:49 +0000 (18:15 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16152 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/latex/wx/process.tex
include/wx/process.h
src/common/process.cpp

index f8475c9e6314d0e4fee28a41e247638a8769e4af..98e90695dc56a04c3f490ecf62634ad7bd3c8d5e 100644 (file)
@@ -45,6 +45,8 @@ for explicit destruction.}
 
 \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
 use wxWindows events. It identifies this object, or another window that will
 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
 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}
 
 \docparam{parent}{The event handler parent.}
 
 \docparam{id}{id of an event.}
 
+\docparam{flags}{either {\tt wxPROCESS\_DEFAULT} or {\tt wxPROCESS\_REDIRECT}}
+
 \membersection{wxProcess::\destruct{wxProcess}}
 
 \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.}
 
+\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}
 
 \func{void}{Redirect}{\void}
index 91c71032ebd3b171e7ceb3f40af4b9917ffeaf05..67ac9c258fbb889f3c4a2cabbca04e3a24f411d0 100644 (file)
 
 #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()
 // function will be called when the process terminates.
 class WXDLLEXPORT wxProcess : public wxEvtHandler
 {
 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)
-        { Init(parent, id, FALSE); }
-    wxProcess(wxEvtHandler *parent, bool redirect)
-        { Init(parent, -1, redirect); }
+        { Init(parent, id, wxPROCESS_DEFAULT); }
+
+    wxProcess(int flags) { Init(NULL, -1, flags); }
 
     virtual ~wxProcess();
 
@@ -62,23 +89,30 @@ public:
     void CloseOutput() { delete m_outputStream; m_outputStream = NULL; }
 
     // 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);
 #endif // wxUSE_STREAMS
 
-    // 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);
+    // for backwards compatibility only, don't use
+#if WXWIN_COMPATIBILITY_2_2
+    wxProcess(wxEvtHandler *parent, bool redirect)
+        { Init(parent, -1, redirect ? wxPROCESS_REDIRECT : wxPROCESS_DEFAULT); }
+#endif // WXWIN_COMPATIBILITY_2_2
 
 protected:
-    void Init(wxEvtHandler *parent, int id, bool redirect);
+    void Init(wxEvtHandler *parent, int id, int flags);
 
     int m_id;
 
 #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,
                    *m_errorStream;
     wxOutputStream *m_outputStream;
index 05e24ffb206d4804c2f0b6639d2f4ecacb5b4d86..d98c7178ef5ddd45ebf73151f6964e7e39fad240 100644 (file)
@@ -47,13 +47,13 @@ IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
 // wxProcess creation
 // ----------------------------------------------------------------------------
 
-void wxProcess::Init(wxEvtHandler *parent, int id, bool redirect)
+void wxProcess::Init(wxEvtHandler *parent, int id, int flags)
 {
     if ( parent )
         SetNextHandler(parent);
 
     m_id         = id;
-    m_redirect   = redirect;
+    m_redirect   = (flags & wxPROCESS_REDIRECT) != 0;
 
 #if wxUSE_STREAMS
     m_inputStream  = NULL;
@@ -62,6 +62,20 @@ void wxProcess::Init(wxEvtHandler *parent, int id, bool redirect)
 #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
 // ----------------------------------------------------------------------------