123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /*
- ==============================================================================
- This file is part of the JUCE library.
- Copyright (c) 2017 - ROLI Ltd.
- 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 : public UnitTest
- {
- public:
- ChildProcessTests()
- : UnitTest ("ChildProcess", UnitTestCategories::threads)
- {}
- void runTest() override
- {
- beginTest ("Child Processes");
- #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX
- 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
|