The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. ChildProcess::ChildProcess() {}
  18. ChildProcess::~ChildProcess() {}
  19. bool ChildProcess::isRunning() const
  20. {
  21. return activeProcess != nullptr && activeProcess->isRunning();
  22. }
  23. int ChildProcess::readProcessOutput (void* dest, int numBytes)
  24. {
  25. return activeProcess != nullptr ? activeProcess->read (dest, numBytes) : 0;
  26. }
  27. bool ChildProcess::kill()
  28. {
  29. return activeProcess == nullptr || activeProcess->killProcess();
  30. }
  31. uint32 ChildProcess::getExitCode() const
  32. {
  33. return activeProcess != nullptr ? activeProcess->getExitCode() : 0;
  34. }
  35. bool ChildProcess::waitForProcessToFinish (const int timeoutMs) const
  36. {
  37. const uint32 timeoutTime = Time::getMillisecondCounter() + (uint32) timeoutMs;
  38. do
  39. {
  40. if (! isRunning())
  41. return true;
  42. }
  43. while (timeoutMs < 0 || Time::getMillisecondCounter() < timeoutTime);
  44. return false;
  45. }
  46. String ChildProcess::readAllProcessOutput()
  47. {
  48. MemoryOutputStream result;
  49. for (;;)
  50. {
  51. char buffer [512];
  52. const int num = readProcessOutput (buffer, sizeof (buffer));
  53. if (num <= 0)
  54. break;
  55. result.write (buffer, (size_t) num);
  56. }
  57. return result.toString();
  58. }
  59. //==============================================================================
  60. #if JUCE_UNIT_TESTS
  61. class ChildProcessTests : public UnitTest
  62. {
  63. public:
  64. ChildProcessTests() : UnitTest ("ChildProcess") {}
  65. void runTest() override
  66. {
  67. beginTest ("Child Processes");
  68. #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX
  69. ChildProcess p;
  70. #if JUCE_WINDOWS
  71. expect (p.start ("tasklist"));
  72. #else
  73. expect (p.start ("ls /"));
  74. #endif
  75. //String output (p.readAllProcessOutput());
  76. //expect (output.isNotEmpty());
  77. #endif
  78. }
  79. };
  80. static ChildProcessTests childProcessUnitTests;
  81. #endif