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.

116 lines
3.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. ChildProcess::ChildProcess() {}
  24. ChildProcess::~ChildProcess() {}
  25. bool ChildProcess::isRunning() const
  26. {
  27. return activeProcess != nullptr && activeProcess->isRunning();
  28. }
  29. int ChildProcess::readProcessOutput (void* dest, int numBytes)
  30. {
  31. return activeProcess != nullptr ? activeProcess->read (dest, numBytes) : 0;
  32. }
  33. bool ChildProcess::kill()
  34. {
  35. return activeProcess == nullptr || activeProcess->killProcess();
  36. }
  37. uint32 ChildProcess::getExitCode() const
  38. {
  39. return activeProcess != nullptr ? activeProcess->getExitCode() : 0;
  40. }
  41. bool ChildProcess::waitForProcessToFinish (const int timeoutMs) const
  42. {
  43. const uint32 timeoutTime = Time::getMillisecondCounter() + (uint32) timeoutMs;
  44. do
  45. {
  46. if (! isRunning())
  47. return true;
  48. }
  49. while (timeoutMs < 0 || Time::getMillisecondCounter() < timeoutTime);
  50. return false;
  51. }
  52. String ChildProcess::readAllProcessOutput()
  53. {
  54. MemoryOutputStream result;
  55. for (;;)
  56. {
  57. char buffer [512];
  58. const int num = readProcessOutput (buffer, sizeof (buffer));
  59. if (num <= 0)
  60. break;
  61. result.write (buffer, (size_t) num);
  62. }
  63. return result.toString();
  64. }
  65. //==============================================================================
  66. #if JUCE_UNIT_TESTS
  67. class ChildProcessTests : public UnitTest
  68. {
  69. public:
  70. ChildProcessTests() : UnitTest ("ChildProcess") {}
  71. void runTest() override
  72. {
  73. beginTest ("Child Processes");
  74. #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX
  75. ChildProcess p;
  76. #if JUCE_WINDOWS
  77. expect (p.start ("tasklist"));
  78. #else
  79. expect (p.start ("ls /"));
  80. #endif
  81. //String output (p.readAllProcessOutput());
  82. //expect (output.isNotEmpty());
  83. #endif
  84. }
  85. };
  86. static ChildProcessTests childProcessUnitTests;
  87. #endif