-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathccliprocess.cpp
More file actions
82 lines (73 loc) · 2.43 KB
/
Copy pathccliprocess.cpp
File metadata and controls
82 lines (73 loc) · 2.43 KB
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
/**
* Copyright (C) 2021 Jo2003 ([email protected])
* This file is part of cd2netmd_gui
*
* cd2netmd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cd2netmd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
*/
#include <QRegExp>
#include "ccliprocess.h"
#include "defines.h"
CCliProcess::CCliProcess(QObject *parent)
:QProcess(parent)
{
QProcess::setProcessChannelMode(ProcessChannelMode::MergedChannels);
QProcess::setReadChannel(ProcessChannel::StandardOutput);
connect(this, &QProcess::readyReadStandardOutput, this, &CCliProcess::extractPercent);
}
//--------------------------------------------------------------------------
//! @brief run the process
//!
//! @param[in] program The program
//! @param[in] arguments The arguments
//! @param[in] mode The mode
//! @param[in] nativeArgs optional native arguments (only used on Windows)
//--------------------------------------------------------------------------
void CCliProcess::run(const QString &program, const QStringList &arguments,
QIODevice::OpenMode mode, const QString& nativeArgs)
{
mLog.clear();
QProcess::setProgram(program);
QStringList args = arguments;
#ifdef Q_OS_WIN
// empty native args will clear last set args
setNativeArguments(nativeArgs);
#else
Q_UNUSED(nativeArgs)
#endif // Q_OS_WIN
QProcess::setArguments(args);
QProcess::start(mode);
waitForStarted();
}
bool CCliProcess::busy() const
{
return state() != QProcess::NotRunning;
}
void CCliProcess::extractPercent()
{
mLog += QString::fromUtf8(readAllStandardOutput());
int pos;
if ((pos = mLog.lastIndexOf(QChar('%'))) > 0)
{
int startPos = mLog.lastIndexOf(QRegExp("[^0-9]+"), pos - 1);
if (startPos > -1)
{
bool ok;
int length = (pos - 1) - startPos;
int percent = mLog.mid(startPos + 1, length).toInt(&ok);
if (ok)
{
emit progress(percent);
}
}
}
}