forked from vnotex/vnote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessutils.cpp
158 lines (132 loc) · 4.3 KB
/
processutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "processutils.h"
#include <QProcess>
#include <QScopedPointer>
#include <QDebug>
#include "utils.h"
using namespace vnotex;
ProcessUtils::State ProcessUtils::start(const QString &p_program,
const QStringList &p_args,
const QByteArray &p_stdIn,
int &p_exitCodeOnSuccess,
QByteArray &p_stdOut,
QByteArray &p_stdErr)
{
QScopedPointer<QProcess> proc(new QProcess());
proc->start(p_program, p_args);
return handleProcess(proc.data(), p_stdIn, p_exitCodeOnSuccess, p_stdOut, p_stdErr);
}
ProcessUtils::State ProcessUtils::handleProcess(QProcess *p_process,
const QByteArray &p_stdIn,
int &p_exitCodeOnSuccess,
QByteArray &p_stdOut,
QByteArray &p_stdErr)
{
if (!p_process->waitForStarted()) {
return State::FailedToStart;
}
if (!p_stdIn.isEmpty()) {
if (p_process->write(p_stdIn) == -1) {
p_process->closeWriteChannel();
qWarning() << "failed to write to stdin of QProcess" << p_process->errorString();
return State::FailedToWrite;
} else {
p_process->closeWriteChannel();
}
}
p_process->waitForFinished();
State state = State::Succeeded;
if (p_process->exitStatus() == QProcess::CrashExit) {
state = State::Crashed;
} else {
p_exitCodeOnSuccess = p_process->exitCode();
}
p_stdOut = p_process->readAllStandardOutput();
p_stdErr = p_process->readAllStandardError();
return state;
}
QStringList ProcessUtils::parseCombinedArgString(const QString &p_args)
{
QStringList args;
QString tmp;
int quoteCount = 0;
bool inQuote = false;
// Handle quoting.
// Tokens can be surrounded by double quotes "hello world".
// Three consecutive double quotes represent the quote character itself.
for (int i = 0; i < p_args.size(); ++i) {
if (p_args.at(i) == QLatin1Char('"')) {
++quoteCount;
if (quoteCount == 3) {
// Third consecutive quote.
quoteCount = 0;
tmp += p_args.at(i);
}
continue;
}
if (quoteCount) {
if (quoteCount == 1) {
inQuote = !inQuote;
}
quoteCount = 0;
}
if (!inQuote && p_args.at(i).isSpace()) {
if (!tmp.isEmpty()) {
args += tmp;
tmp.clear();
}
} else {
tmp += p_args.at(i);
}
}
if (!tmp.isEmpty()) {
args += tmp;
}
return args;
}
QString ProcessUtils::combineArgString(const QStringList &p_args)
{
QString argStr;
for (const auto &arg : p_args) {
QString tmp(arg);
tmp.replace("\"", "\"\"\"");
if (tmp.contains(' ')) {
tmp = '"' + tmp + '"';
}
if (argStr.isEmpty()) {
argStr = tmp;
} else {
argStr = argStr + ' ' + tmp;
}
}
return argStr;
}
ProcessUtils::State ProcessUtils::start(const QString &p_program,
const QStringList &p_args,
const std::function<void(const QString &)> &p_logger,
const bool &p_askedToStop)
{
QProcess proc;
proc.start(p_program, p_args);
if (!proc.waitForStarted()) {
return State::FailedToStart;
}
while (proc.state() != QProcess::NotRunning) {
Utils::sleepWait(100);
auto outBa = proc.readAllStandardOutput();
auto errBa = proc.readAllStandardError();
QString msg;
if (!outBa.isEmpty()) {
msg += QString::fromLocal8Bit(outBa);
}
if (!errBa.isEmpty()) {
msg += QString::fromLocal8Bit(errBa);
}
if (!msg.isEmpty()) {
p_logger(msg);
}
if (p_askedToStop) {
break;
}
}
return proc.exitStatus() == QProcess::NormalExit ? State::Succeeded : State::Crashed;
}