yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// slang-process-util.cpp 2 3#include "slang-process-util.h" 4 5#include "slang-com-helper.h" 6#include "slang-string-escape-util.h" 7#include "slang-string-util.h" 8#include "slang-string.h" 9 10namespace Slang 11{ 12 13/* static */ SlangResult ProcessUtil ::execute ( 14const CommandLine & commandLine , 15ExecuteResult & outExecuteResult ) 16{ 17RefPtr < Process > process ; 18SLANG_RETURN_ON_FAIL (Process ::create (commandLine ,0 ,process )); 19SLANG_RETURN_ON_FAIL (readUntilTermination (process ,outExecuteResult )); 20return SLANG_OK ; 21} 22 23static Index _getCount (List < Byte >* buf ) 24{ 25return buf ?buf -> getCount () :0 ; 26} 27 28// We may want something more sophisticated here, if bytes is something other than ascii/utf8 29static String _getText (const ConstArrayView < Byte >& bytes ) 30{ 31StringBuilder buf ; 32StringUtil ::appendStandardLines ( 33UnownedStringSlice ((const char * )bytes .begin (), (const char * )bytes .end ()), 34buf ); 35return buf .produceString (); 36} 37 38/* static */ SlangResult ProcessUtil ::readUntilTermination ( 39Process * process , 40ExecuteResult & outExecuteResult ) 41{ 42List < Byte > stdOut ; 43List < Byte > stdError ; 44 45SLANG_RETURN_ON_FAIL (readUntilTermination (process ,& stdOut ,& stdError )); 46 47// Get the return code 48outExecuteResult .resultCode = ExecuteResult ::ResultCode (process -> getReturnValue ()); 49 50outExecuteResult .standardOutput = _getText (stdOut .getArrayView ()); 51outExecuteResult .standardError = _getText (stdError .getArrayView ()); 52 53return SLANG_OK ; 54} 55 56/* static */ SlangResult ProcessUtil ::readUntilTermination ( 57Process * process , 58List < Byte >* outStdOut , 59List < Byte >* outStdError ) 60{ 61Stream * stdOutStream = process -> getStream (StdStreamType ::Out ); 62Stream * stdErrorStream = process -> getStream (StdStreamType ::ErrorOut ); 63 64while (!process -> isTerminated ()) 65 { 66const auto preCount = _getCount (outStdOut )+ _getCount (outStdError ); 67 68SLANG_RETURN_ON_FAIL (StreamUtil ::readOrDiscard (stdOutStream ,0 ,outStdOut )); 69if (stdErrorStream ) 70SLANG_RETURN_ON_FAIL (StreamUtil ::readOrDiscard (stdErrorStream ,0 ,outStdError )); 71 72const auto postCount = _getCount (outStdOut )+ _getCount (outStdError ); 73 74// If nothing was read, we can yield 75if (preCount == postCount ) 76 { 77Process ::sleepCurrentThread (0 ); 78 } 79 } 80 81// Read anything remaining 82for (;;) 83 { 84const auto preCount = _getCount (outStdOut )+ _getCount (outStdError ); 85StreamUtil ::readOrDiscard (stdOutStream ,0 ,outStdOut ); 86if (stdErrorStream ) 87StreamUtil ::readOrDiscard (stdErrorStream ,0 ,outStdError ); 88const auto postCount = _getCount (outStdOut )+ _getCount (outStdError ); 89if (preCount == postCount ) 90break ; 91 } 92return SLANG_OK ; 93} 94 95}// namespace Slang