juce_ChildProcess.cpp 3.3 KB

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