diff options
Diffstat (limited to 'source/core/slang-stream.cpp')
| -rw-r--r-- | source/core/slang-stream.cpp | 46 |
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 |
