yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
2.7 KiB95 linesraw
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(
14    const CommandLine& commandLine,
15    ExecuteResult& outExecuteResult)
16{
17    RefPtr<Process> process;
18    SLANG_RETURN_ON_FAIL(Process::create(commandLine, 0, process));
19    SLANG_RETURN_ON_FAIL(readUntilTermination(process, outExecuteResult));
20    return SLANG_OK;
21}
22
23static Index _getCount(List<Byte>* buf)
24{
25    return 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{
31    StringBuilder buf;
32    StringUtil::appendStandardLines(
33        UnownedStringSlice((const char*)bytes.begin(), (const char*)bytes.end()),
34        buf);
35    return buf.produceString();
36}
37
38/* static */ SlangResult ProcessUtil::readUntilTermination(
39    Process* process,
40    ExecuteResult& outExecuteResult)
41{
42    List<Byte> stdOut;
43    List<Byte> stdError;
44
45    SLANG_RETURN_ON_FAIL(readUntilTermination(process, &stdOut, &stdError));
46
47    // Get the return code
48    outExecuteResult.resultCode = ExecuteResult::ResultCode(process->getReturnValue());
49
50    outExecuteResult.standardOutput = _getText(stdOut.getArrayView());
51    outExecuteResult.standardError = _getText(stdError.getArrayView());
52
53    return SLANG_OK;
54}
55
56/* static */ SlangResult ProcessUtil::readUntilTermination(
57    Process* process,
58    List<Byte>* outStdOut,
59    List<Byte>* outStdError)
60{
61    Stream* stdOutStream = process->getStream(StdStreamType::Out);
62    Stream* stdErrorStream = process->getStream(StdStreamType::ErrorOut);
63
64    while (!process->isTerminated())
65    {
66        const auto preCount = _getCount(outStdOut) + _getCount(outStdError);
67
68        SLANG_RETURN_ON_FAIL(StreamUtil::readOrDiscard(stdOutStream, 0, outStdOut));
69        if (stdErrorStream)
70            SLANG_RETURN_ON_FAIL(StreamUtil::readOrDiscard(stdErrorStream, 0, outStdError));
71
72        const auto postCount = _getCount(outStdOut) + _getCount(outStdError);
73
74        // If nothing was read, we can yield
75        if (preCount == postCount)
76        {
77            Process::sleepCurrentThread(0);
78        }
79    }
80
81    // Read anything remaining
82    for (;;)
83    {
84        const auto preCount = _getCount(outStdOut) + _getCount(outStdError);
85        StreamUtil::readOrDiscard(stdOutStream, 0, outStdOut);
86        if (stdErrorStream)
87            StreamUtil::readOrDiscard(stdErrorStream, 0, outStdError);
88        const auto postCount = _getCount(outStdOut) + _getCount(outStdError);
89        if (preCount == postCount)
90            break;
91    }
92    return SLANG_OK;
93}
94
95} // namespace Slang