blob: f242e81f2a6958788b68bb72ecf89d93fa2cae96 [file] [log] [blame]
Ethan Nicholas842d31b2019-01-22 10:59:11 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLOutputStream.h"
Ethan Nicholas842d31b2019-01-22 10:59:11 -05009
Brian Osmand7c4f9d2020-07-31 10:24:01 -040010#include <memory>
11
Ethan Nicholas842d31b2019-01-22 10:59:11 -050012namespace SkSL {
13
John Stilese1589a12020-10-08 13:56:46 -040014void OutputStream::writeString(const String& s) {
Ethan Nicholas842d31b2019-01-22 10:59:11 -050015 this->write(s.c_str(), s.size());
16}
17
18void 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
25void OutputStream::appendVAList(const char format[], va_list args) {
26 char buffer[kBufferSize];
Ethan Nicholasc18bb512020-07-28 14:46:53 -040027 va_list copy;
28 va_copy(copy, args);
Ethan Nicholas842d31b2019-01-22 10:59:11 -050029 int length = vsnprintf(buffer, kBufferSize, format, args);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040030 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 Osman1d4b92d2020-08-13 12:52:38 -040037 va_end(copy);
Ethan Nicholas842d31b2019-01-22 10:59:11 -050038}
39
John Stilesa6841be2020-08-06 14:11:56 -040040} // namespace SkSL