Debian Codegraph @

CodeLite/cl_process.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : cl_process.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25 #include "cl_process.h"
26 #include <wx/log.h>
27 #include <wx/txtstrm.h>
28 #include <wx/sstream.h>
29 #include "procutils.h"
30
31 clProcess::clProcess(int id, const wxString& cmdLine, bool redirect)
32 : wxProcess(NULL, id)
33 , m_pid(-1)
34 , m_uid(id)
35 , m_cmd(cmdLine)
36 , m_redirect(redirect)
37 {
38 }
39
40 clProcess::~clProcess() {}
41
42 long clProcess::GetPid() { return m_pid; }
43
44 void clProcess::SetPid(long pid) { m_pid = pid; }
45
46 void clProcess::Terminate(wxSignal signalNo)
47 {
48 wxKillError rc;
49 wxKill(GetPid(), signalNo, &rc, wxKILL_CHILDREN);
50
51 // Sleep for 150 ms to allow the process to be killed and
52 // the main frame to handle the event or else we can get
53 // memory leak
54 wxMilliSleep(150);
55 }
56
57 long clProcess::Start(bool hide)
58 {
59 if(m_redirect) {
60 Redirect();
61 }
62
63 long flags = wxEXEC_ASYNC | wxEXEC_MAKE_GROUP_LEADER;
64 if(!hide) {
65 flags |= wxEXEC_NOHIDE;
66 }
67
68 m_pid = wxExecute(m_cmd, flags, this);
69 return m_pid;
70 }
71
72 bool clProcess::HasInput(wxString& input, wxString& errors)
73 {
74 if(m_redirect == false) {
75 wxASSERT_MSG(false, wxT("Process is not redirected"));
76 return false;
77 }
78
79 bool hasInput = false;
80 while(IsInputAvailable()) {
81 wxTextInputStream tis(*GetInputStream());
82 wxChar ch = tis.GetChar();
83 input << ch;
84 hasInput = true;
85 if(ch == wxT('\n'))
86 break;
87 }
88
89 while(IsErrorAvailable()) {
90 wxTextInputStream tis(*GetErrorStream());
91 wxChar ch = tis.GetChar();
92 errors << ch;
93 hasInput = true;
94 if(ch == wxT('\n'))
95 break;
96 }
97 return hasInput;
98 }
99
100 bool clProcess::ReadAll(wxString& input, wxString& errors)
101 {
102 if(m_redirect == false) {
103 wxASSERT_MSG(false, wxT("Process is not redirected"));
104 return false;
105 }
106
107 bool hasInput = false;
108 while(IsInputAvailable()) {
109 wxTextInputStream tis(*GetInputStream());
110 input << tis.GetChar();
111 hasInput = true;
112 }
113
114 while(IsErrorAvailable()) {
115 wxTextInputStream tis(*GetErrorStream());
116 errors << tis.GetChar();
117 hasInput = true;
118 }
119 return hasInput;
120 }
121
122 bool clProcess::Write(const wxString& text)
123 {
124 if(m_redirect == false) {
125 wxASSERT_MSG(false, wxT("Process is not redirected"));
126 return false;
127 }
128 wxTextOutputStream tos(*GetOutputStream());
129 tos.WriteString(text);
130 return true;
131 }