OpenShot Library | OpenShotAudio 0.2.2
juce_ChildProcess.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
28
30{
31 return activeProcess != nullptr && activeProcess->isRunning();
32}
33
34int ChildProcess::readProcessOutput (void* dest, int numBytes)
35{
36 return activeProcess != nullptr ? activeProcess->read (dest, numBytes) : 0;
37}
38
40{
41 return activeProcess == nullptr || activeProcess->killProcess();
42}
43
45{
46 return activeProcess != nullptr ? activeProcess->getExitCode() : 0;
47}
48
49bool ChildProcess::waitForProcessToFinish (const int timeoutMs) const
50{
51 auto timeoutTime = Time::getMillisecondCounter() + (uint32) timeoutMs;
52
53 do
54 {
55 if (! isRunning())
56 return true;
57
58 Thread::sleep (2);
59 }
60 while (timeoutMs < 0 || Time::getMillisecondCounter() < timeoutTime);
61
62 return false;
63}
64
66{
67 MemoryOutputStream result;
68
69 for (;;)
70 {
71 char buffer[512];
72 auto num = readProcessOutput (buffer, sizeof (buffer));
73
74 if (num <= 0)
75 break;
76
77 result.write (buffer, (size_t) num);
78 }
79
80 return result.toString();
81}
82
83
84//==============================================================================
85//==============================================================================
86#if JUCE_UNIT_TESTS
87
88class ChildProcessTests : public UnitTest
89{
90public:
91 ChildProcessTests()
92 : UnitTest ("ChildProcess", UnitTestCategories::threads)
93 {}
94
95 void runTest() override
96 {
97 beginTest ("Child Processes");
98
99 #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX
100 ChildProcess p;
101
102 #if JUCE_WINDOWS
103 expect (p.start ("tasklist"));
104 #else
105 expect (p.start ("ls /"));
106 #endif
107
108 auto output = p.readAllProcessOutput();
109 expect (output.isNotEmpty());
110 #endif
111 }
112};
113
114static ChildProcessTests childProcessUnitTests;
115
116#endif
117
118} // namespace juce
uint32 getExitCode() const
If the process has finished, this returns its exit code.
String readAllProcessOutput()
Blocks until the process has finished, and then returns its complete output as a string.
int readProcessOutput(void *destBuffer, int numBytesToRead)
Attempts to read some output from the child process.
~ChildProcess()
Destructor.
bool isRunning() const
Returns true if the child process is alive.
bool waitForProcessToFinish(int timeoutMs) const
Blocks until the process is no longer running.
bool kill()
Attempts to kill the child process.
ChildProcess()
Creates a process object.
Writes data to an internal memory buffer, which grows as required.
String toString() const
Attempts to detect the encoding of the data and convert it to a string.
bool write(const void *, size_t) override
Writes a block of data to the stream.
The JUCE String class!
Definition: juce_String.h:43
static void JUCE_CALLTYPE sleep(int milliseconds)
Suspends the execution of the current thread until the specified timeout period has elapsed (note tha...
static uint32 getMillisecondCounter() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
Definition: juce_Time.cpp:226
This is a base class for classes that perform a unit test.
Definition: juce_UnitTest.h:74