Skip to content

Commit

Permalink
libcore|CommandLine: Starting a process and keeping a reference to it
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Nov 12, 2016
1 parent 7791594 commit 6a0e012
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
3 changes: 3 additions & 0 deletions doomsday/sdk/libcore/include/de/core/commandline.h
Expand Up @@ -24,6 +24,7 @@
#include <vector>
#include <map>

#include <QProcess>
#include <QStringList>

#include "../libcore.h"
Expand Down Expand Up @@ -227,6 +228,8 @@ class DENG2_PUBLIC CommandLine
*/
bool executeAndWait(String *output = 0) const;

QProcess *executeProcess() const;

static CommandLine &get();

private:
Expand Down
41 changes: 25 additions & 16 deletions doomsday/sdk/libcore/src/core/commandline.cpp
Expand Up @@ -440,34 +440,43 @@ bool CommandLine::execute() const
return false;
}

LOG_DEBUG("Started detached process %i using \"%s\"") << pid << at(0);
LOG_DEBUG("Started detached process %i \"%s\"") << pid << at(0);
return true;
}

bool CommandLine::executeAndWait(String *output) const
{
std::unique_ptr<QProcess> proc(executeProcess());
if (!proc)
{
return false;
}
bool result = proc->waitForFinished();
if (output)
{
*output = String::fromUtf8(Block(proc->readAll()));
}
return result;
}

QProcess *CommandLine::executeProcess() const
{
LOG_AS("CommandLine");

if (count() < 1) return false;
if (count() < 1) return nullptr;

QStringList args;
for (int i = 1; i < count(); ++i) args << at(i);

LOG_DEBUG("Starting process \"%s\"") << at(0);

if (output) output->clear();

QProcess proc;
proc.start(at(0), args);
if (!proc.waitForStarted()) return false;
bool result = proc.waitForFinished();

if (output)
auto *proc = new QProcess;
proc->start(at(0), args);
if (!proc->waitForStarted())
{
*output = String::fromUtf8(Block(proc.readAll()));
delete proc;
return nullptr;
}

return result;
LOG_DEBUG("Started process %i \"%s\"") << proc->pid() << at(0);
return proc;
}

CommandLine &CommandLine::get()
Expand All @@ -488,4 +497,4 @@ dint CommandLine::ArgWithParams::size() const
return params.size();
}

} // namespace de
} // namespace de

0 comments on commit 6a0e012

Please sign in to comment.