Loading...
Searching...
No Matches
juce_ChildProcess.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
ChildProcess::ChildProcess() {}
ChildProcess::~ChildProcess() {}
bool ChildProcess::isRunning() const
{
return activeProcess != nullptr && activeProcess->isRunning();
}
int ChildProcess::readProcessOutput (void* dest, int numBytes)
{
return activeProcess != nullptr ? activeProcess->read (dest, numBytes) : 0;
}
bool ChildProcess::kill()
{
return activeProcess == nullptr || activeProcess->killProcess();
}
uint32 ChildProcess::getExitCode() const
{
return activeProcess != nullptr ? activeProcess->getExitCode() : 0;
}
bool ChildProcess::waitForProcessToFinish (const int timeoutMs) const
{
auto timeoutTime = Time::getMillisecondCounter() + (uint32) timeoutMs;
do
{
if (! isRunning())
return true;
Thread::sleep (2);
}
while (timeoutMs < 0 || Time::getMillisecondCounter() < timeoutTime);
return false;
}
String ChildProcess::readAllProcessOutput()
{
MemoryOutputStream result;
for (;;)
{
char buffer[512];
auto num = readProcessOutput (buffer, sizeof (buffer));
if (num <= 0)
break;
result.write (buffer, (size_t) num);
}
return result.toString();
}
//==============================================================================
//==============================================================================
#if JUCE_UNIT_TESTS
class ChildProcessTests final : public UnitTest
{
public:
ChildProcessTests()
: UnitTest ("ChildProcess", UnitTestCategories::threads)
{}
void runTest() override
{
beginTest ("Child Processes");
#if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX || JUCE_BSD
ChildProcess p;
#if JUCE_WINDOWS
expect (p.start ("tasklist"));
#else
expect (p.start ("ls /"));
#endif
auto output = p.readAllProcessOutput();
expect (output.isNotEmpty());
#endif
}
};
static ChildProcessTests childProcessUnitTests;
#endif
} // namespace juce
juce::ChildProcess::getExitCode
uint32 getExitCode() const
Definition juce_ChildProcess.cpp:44
juce::ChildProcess::readAllProcessOutput
String readAllProcessOutput()
Definition juce_ChildProcess.cpp:65
juce::ChildProcess::readProcessOutput
int readProcessOutput(void *destBuffer, int numBytesToRead)
Definition juce_ChildProcess.cpp:34
juce::ChildProcess::~ChildProcess
~ChildProcess()
Definition juce_ChildProcess.cpp:27
juce::ChildProcess::isRunning
bool isRunning() const
Definition juce_ChildProcess.cpp:29
juce::ChildProcess::waitForProcessToFinish
bool waitForProcessToFinish(int timeoutMs) const
Definition juce_ChildProcess.cpp:49
juce::ChildProcess::kill
bool kill()
Definition juce_ChildProcess.cpp:39
juce::ChildProcess::ChildProcess
ChildProcess()
Definition juce_ChildProcess.cpp:26
juce::MemoryOutputStream
Definition juce_MemoryOutputStream.h:36
juce::MemoryOutputStream::toString
String toString() const
Definition juce_MemoryOutputStream.cpp:195
juce::MemoryOutputStream::write
bool write(const void *, size_t) override
Definition juce_MemoryOutputStream.cpp:102
juce::String
Definition juce_String.h:53
juce::Thread::sleep
static void JUCE_CALLTYPE sleep(int milliseconds)
juce::Time::getMillisecondCounter
static uint32 getMillisecondCounter() noexcept
Definition juce_Time.cpp:241
juce::UnitTest
Definition juce_UnitTest.h:70