summaryrefslogtreecommitdiffstats
path: root/source/core/slang-stream.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2021-11-30 16:50:05 -0500
committerGitHub <noreply@github.com>2021-11-30 16:50:05 -0500
commitce12e1d64d6b0b62609f061d3773a7e8b35849c3 (patch)
tree72da79208edff4fdebfc32db759ecca21716c260 /source/core/slang-stream.cpp
parentace4e334bc5fb299d2890b5e3f35dfd84ea32606 (diff)
Auto flush for streams for stdin/out in slang-test (#2035)
* #include an absolute path didn't work - because paths were taken to always be relative. * Move StreamType from Process to StdStreamType in slang-stream.h * Disable buffering for stdout/stderr for slang-test. * Improve comment.
Diffstat (limited to 'source/core/slang-stream.cpp')
-rw-r--r--source/core/slang-stream.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/source/core/slang-stream.cpp b/source/core/slang-stream.cpp
index 7d4fab09e..47cf533eb 100644
--- a/source/core/slang-stream.cpp
+++ b/source/core/slang-stream.cpp
@@ -663,4 +663,50 @@ SlangResult BufferedReadStream::readUntilContains(size_t size)
}
}
+static FILE* _getFileFromStdStreamType(StdStreamType stdStream)
+{
+ switch (stdStream)
+ {
+ case StdStreamType::ErrorOut: return stderr;
+ case StdStreamType::Out: return stdout;
+ case StdStreamType::In: return stdin;
+ default: return nullptr;
+ }
+}
+
+static int _getBufferOptions(StreamBufferStyle style)
+{
+ switch (style)
+ {
+ case StreamBufferStyle::None: return _IONBF;
+ case StreamBufferStyle::Line: return _IOLBF;
+ default:
+ case StreamBufferStyle::Full: return _IOFBF;
+ }
+}
+
+/* static */SlangResult StreamUtil::setStreamBufferStyle(StdStreamType stdStream, StreamBufferStyle style)
+{
+ FILE* file = _getFileFromStdStreamType(stdStream);
+
+ if (file)
+ {
+ auto options = _getBufferOptions(style);
+
+ // https://www.cplusplus.com/reference/cstdio/setvbuf/
+
+ // NOTE! We don't set a buffer here (we pass in nullptr).
+ // Passing nullptr is fine for 'no buffering' and sets a 'dynamic buffer' for others.
+ // But it's not clear the behavior is around the buffer size. It seems the size is a
+ // 'suggestion' so it will set the default but the documentation is unclear.
+ if (setvbuf(file, nullptr, options, 0) == 0)
+ {
+ return SLANG_OK;
+ }
+ return SLANG_FAIL;
+ }
+
+ return SLANG_E_NOT_AVAILABLE;
+}
+
} // namespace Slang