OpenShot Library | libopenshot-audio  0.2.0
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 
23 namespace juce
24 {
25 
28 
30 {
31  return activeProcess != nullptr && activeProcess->isRunning();
32 }
33 
34 int 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 
49 bool 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 #if JUCE_UNIT_TESTS
85 
86 class ChildProcessTests : public UnitTest
87 {
88 public:
89  ChildProcessTests() : UnitTest ("ChildProcess", "Threads") {}
90 
91  void runTest() override
92  {
93  beginTest ("Child Processes");
94 
95  #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX
96  ChildProcess p;
97 
98  #if JUCE_WINDOWS
99  expect (p.start ("tasklist"));
100  #else
101  expect (p.start ("ls /"));
102  #endif
103 
104  //String output (p.readAllProcessOutput());
105  //expect (output.isNotEmpty());
106  #endif
107  }
108 };
109 
110 static ChildProcessTests childProcessUnitTests;
111 
112 #endif
113 
114 } // namespace juce
int readProcessOutput(void *destBuffer, int numBytesToRead)
Attempts to read some output from the child process.
bool waitForProcessToFinish(int timeoutMs) const
Blocks until the process is no longer running.
String readAllProcessOutput()
Blocks until the process has finished, and then returns its complete output as a string.
uint32 getExitCode() const
If the process has finished, this returns its exit code.
The JUCE String class!
Definition: juce_String.h:42
This is a base class for classes that perform a unit test.
Definition: juce_UnitTest.h:73
bool isRunning() const
Returns true if the child process is alive.
bool write(const void *, size_t) override
Writes a block of data to the stream.
ChildProcess()
Creates a process object.
Launches and monitors a child process.
~ChildProcess()
Destructor.
static void JUCE_CALLTYPE sleep(int milliseconds)
Suspends the execution of the current thread until the specified timeout period has elapsed (note tha...
bool kill()
Attempts to kill the child process.
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 start(const String &command, int streamFlags=wantStdOut|wantStdErr)
Attempts to launch a child process command.
static uint32 getMillisecondCounter() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
Definition: juce_Time.cpp:226