From 705262600b29cb849566f331aebd6ad132f39cad Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 4 Apr 2018 16:22:04 +0100 Subject: [PATCH] Quick cleanup of some posix code --- .../juce_core/native/juce_posix_SharedCode.h | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index 7d4b91acf6..1ca77c6b3b 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -1127,20 +1127,19 @@ class ChildProcess::ActiveProcess { public: ActiveProcess (const StringArray& arguments, int streamFlags) - : childPID (0), pipeHandle (0), readHandle (0) { - String exe (arguments[0].unquoted()); + auto exe = arguments[0].unquoted(); // Looks like you're trying to launch a non-existent exe or a folder (perhaps on OSX // you're trying to launch the .app folder rather than the actual binary inside it?) jassert (File::getCurrentWorkingDirectory().getChildFile (exe).existsAsFile() || ! exe.containsChar (File::getSeparatorChar())); - int pipeHandles[2] = { 0 }; + int pipeHandles[2] = {}; if (pipe (pipeHandles) == 0) { - const pid_t result = fork(); + auto result = fork(); if (result < 0) { @@ -1165,9 +1164,10 @@ public: close (pipeHandles[1]); Array argv; - for (int i = 0; i < arguments.size(); ++i) - if (arguments[i].isNotEmpty()) - argv.add (const_cast (arguments[i].toRawUTF8())); + + for (auto& arg : arguments) + if (arg.isNotEmpty()) + argv.add (const_cast (arg.toRawUTF8())); argv.add (nullptr); @@ -1186,7 +1186,7 @@ public: ~ActiveProcess() { - if (readHandle != 0) + if (readHandle != nullptr) fclose (readHandle); if (pipeHandle != 0) @@ -1195,28 +1195,26 @@ public: bool isRunning() const noexcept { - if (childPID != 0) - { - int childState; - const int pid = waitpid (childPID, &childState, WNOHANG); - return pid == 0 || ! (WIFEXITED (childState) || WIFSIGNALED (childState)); - } + if (childPID == 0) + return false; - return false; + int childState; + auto pid = waitpid (childPID, &childState, WNOHANG); + return pid == 0 || ! (WIFEXITED (childState) || WIFSIGNALED (childState)); } - int read (void* const dest, const int numBytes) noexcept + int read (void* dest, int numBytes) noexcept { - jassert (dest != nullptr); + jassert (dest != nullptr && numBytes > 0); #ifdef fdopen - #error // the zlib headers define this function as NULL! + #error // some crazy 3rd party headers (e.g. zlib) define this function as NULL! #endif - if (readHandle == 0 && childPID != 0) + if (childPID != 0) readHandle = fdopen (pipeHandle, "r"); - if (readHandle != 0) + if (readHandle != nullptr) return (int) fread (dest, 1, (size_t) numBytes, readHandle); return 0; @@ -1232,7 +1230,7 @@ public: if (childPID != 0) { int childState = 0; - const int pid = waitpid (childPID, &childState, WNOHANG); + auto pid = waitpid (childPID, &childState, WNOHANG); if (pid >= 0 && WIFEXITED (childState)) return WEXITSTATUS (childState); @@ -1241,11 +1239,9 @@ public: return 0; } - int childPID; - -private: - int pipeHandle; - FILE* readHandle; + int childPID = 0; + int pipeHandle = 0; + FILE* readHandle = {}; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ActiveProcess) };