blob: 54a9315b1764c8970c6c08e84f3edd1e370fecb7 [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
14void OutputStream::writeString(String s) {
15 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 }
Ethan Nicholas842d31b2019-01-22 10:59:11 -050037}
38
John Stilesa6841be2020-08-06 14:11:56 -040039} // namespace SkSL