LMMS
Loading...
Searching...
No Matches
juce_ChildProcess.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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{
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
85{
86 return activeProcess != nullptr ? activeProcess->getPID() : 0;
87}
88
89//==============================================================================
90//==============================================================================
91#if JUCE_UNIT_TESTS
92
93class ChildProcessTests : public UnitTest
94{
95public:
96 ChildProcessTests()
97 : UnitTest ("ChildProcess", UnitTestCategories::threads)
98 {}
99
100 void runTest() override
101 {
102 beginTest ("Child Processes");
103
104 #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX || JUCE_BSD
106
107 #if JUCE_WINDOWS
108 expect (p.start ("tasklist"));
109 #else
110 expect (p.start ("ls /"));
111 #endif
112
113 auto output = p.readAllProcessOutput();
114 expect (output.isNotEmpty());
115 #endif
116 }
117};
118
119static ChildProcessTests childProcessUnitTests;
120
121#endif
122
123} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
Definition ChildProcess.h:43
ChildProcess()
Definition ChildProcess.cpp:280
bool waitForProcessToFinish(int timeoutMs)
Definition ChildProcess.cpp:303
~ChildProcess()
Definition ChildProcess.cpp:281
bool isRunning() const
Definition ChildProcess.cpp:283
uint32 getPID() const noexcept
Definition ChildProcess.cpp:321
bool kill()
Definition ChildProcess.cpp:288
uint32 getExitCode() const
Definition juce_ChildProcess.cpp:44
String readAllProcessOutput()
Definition juce_ChildProcess.cpp:65
int readProcessOutput(void *destBuffer, int numBytesToRead)
Definition juce_ChildProcess.cpp:34
bool isRunning() const
Definition juce_ChildProcess.cpp:29
std::unique_ptr< ActiveProcess > activeProcess
Definition juce_ChildProcess.h:109
Definition juce_MemoryOutputStream.h:36
Definition juce_String.h:53
static void JUCE_CALLTYPE sleep(int milliseconds)
Definition juce_posix_SharedCode.h:44
static uint32 getMillisecondCounter() noexcept
Definition juce_Time.cpp:241
Definition juce_UnitTest.h:70
Definition juce_UnitTestCategories.h:27
Definition carla_juce.cpp:31
unsigned int uint32
Definition juce_MathsFunctions.h:45
uch * p
Definition crypt.c:594
int result
Definition process.c:1455
#define const
Definition zconf.h:137