///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2020 LoRd_MuldeR <MuldeR2@GMX.de>
//
// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
+// it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version, but always including the *additional*
-// restrictions defined in the "License.txt" file.
+// (at your option) any later version; always including the non-optional
+// LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
//Internal
#include "Global.h"
-#include "JobObject.h"
//MUtils
#include <MUtils/Global.h>
#include <MUtils/OSSupport.h>
+#include <MUtils/JobObject.h>
//Qt
#include <QProcess>
#include <QMutex>
#include <QMutexLocker>
-#include <QLibrary>
#include <QProcessEnvironment>
#include <QDir>
#include <QElapsedTimer>
/*
* Static Objects
*/
-QScopedPointer<JobObject> AbstractTool::s_jobObjectInstance;
-QScopedPointer<QElapsedTimer> AbstractTool::s_startProcessTimer;
+QScopedPointer<MUtils::JobObject> AbstractTool::s_jobObjectInstance;
+QScopedPointer<QElapsedTimer> AbstractTool::s_startProcessTimer;
/*
* Synchronization
if(s_referenceCounter++ == 0)
{
- s_jobObjectInstance.reset(new JobObject());
+ s_jobObjectInstance.reset(new MUtils::JobObject());
s_startProcessTimer.reset(new QElapsedTimer());
if(!MUtils::OS::setup_timer_resolution())
{
/*
* Initialize and launch process object
*/
-bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
+bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args, const QString &workingDir)
{
QMutexLocker lock(&s_startProcessMutex);
}
emit messageLogged(commandline2string(program, args) + "\n");
- MUtils::init_process(process, QFileInfo(program).absolutePath());
+ MUtils::init_process(process, workingDir.isEmpty() ? QFileInfo(program).absolutePath() : workingDir);
process.start(program, args);
}
/*
+* Wait for process to terminate while processing its output
+*/
+AbstractTool::result_t AbstractTool::awaitProcess(QProcess &process, QAtomicInt &abortFlag, int *const exitCode)
+{
+ return awaitProcess(process, abortFlag, [](const QString &text) { return false; }, exitCode);
+}
+
+/*
+* Wait for process to terminate while processing its output
+*/
+AbstractTool::result_t AbstractTool::awaitProcess(QProcess &process, QAtomicInt &abortFlag, std::function<bool(const QString &text)> &&handler, int *const exitCode)
+{
+ bool bTimeout = false;
+ bool bAborted = false;
+
+ QString lastText;
+
+ while (process.state() != QProcess::NotRunning)
+ {
+ if (CHECK_FLAG(abortFlag))
+ {
+ process.kill();
+ bAborted = true;
+ emit messageLogged("\nABORTED BY USER !!!");
+ break;
+ }
+
+ process.waitForReadyRead(m_processTimeoutInterval);
+ if (!process.bytesAvailable() && process.state() == QProcess::Running)
+ {
+ process.kill();
+ qWarning("Tool process timed out <-- killing!");
+ emit messageLogged("\nPROCESS TIMEOUT !!!");
+ bTimeout = true;
+ break;
+ }
+
+ while (process.bytesAvailable() > 0)
+ {
+ QByteArray line = process.readLine();
+ if (line.size() > 0)
+ {
+ line.replace('\r', char(0x20)).replace('\b', char(0x20)).replace('\t', char(0x20));
+ const QString text = QString::fromUtf8(line.constData()).simplified();
+ if (!text.isEmpty())
+ {
+ if (!handler(text))
+ {
+ if (text.compare(lastText, Qt::CaseInsensitive) != 0)
+ {
+ emit messageLogged(lastText = text);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ process.waitForFinished();
+ if (process.state() != QProcess::NotRunning)
+ {
+ process.kill();
+ process.waitForFinished(-1);
+ }
+
+ if (exitCode)
+ {
+ *exitCode = process.exitCode();
+ }
+
+ emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
+ if (!(bAborted || bTimeout)) emit statusUpdated(100);
+
+ if (bAborted || bTimeout || (process.exitCode() != EXIT_SUCCESS))
+ {
+ return bAborted ? RESULT_ABORTED : (bTimeout ? RESULT_TIMEOUT : RESULT_FAILURE);
+ }
+
+ return RESULT_SUCCESS;
+}
+
+/*
* Convert program arguments to single string
*/
QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)