Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLOutputStream.h" |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 9 | |
Brian Osman | d7c4f9d | 2020-07-31 10:24:01 -0400 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 12 | namespace SkSL { |
| 13 | |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 14 | void OutputStream::writeString(const String& s) { |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 15 | this->write(s.c_str(), s.size()); |
| 16 | } |
| 17 | |
| 18 | void OutputStream::printf(const char format[], ...) { |
| 19 | va_list args; |
| 20 | va_start(args, format); |
| 21 | this->appendVAList(format, args); |
| 22 | va_end(args); |
| 23 | } |
| 24 | |
| 25 | void OutputStream::appendVAList(const char format[], va_list args) { |
| 26 | char buffer[kBufferSize]; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 27 | va_list copy; |
| 28 | va_copy(copy, args); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 29 | int length = vsnprintf(buffer, kBufferSize, format, args); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 30 | if (length > (int) kBufferSize) { |
| 31 | std::unique_ptr<char[]> bigBuffer(new char[length + 1]); |
| 32 | vsnprintf(bigBuffer.get(), length + 1, format, copy); |
| 33 | this->write(bigBuffer.get(), length); |
| 34 | } else { |
| 35 | this->write(buffer, length); |
| 36 | } |
Brian Osman | 1d4b92d | 2020-08-13 12:52:38 -0400 | [diff] [blame] | 37 | va_end(copy); |
Ethan Nicholas | 842d31b | 2019-01-22 10:59:11 -0500 | [diff] [blame] | 38 | } |
| 39 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 40 | } // namespace SkSL |