1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2019 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker #include "src/sksl/SkSLOutputStream.h"
9*c8dee2aaSAndroid Build Coastguard Worker
10*c8dee2aaSAndroid Build Coastguard Worker #include <stdio.h>
11*c8dee2aaSAndroid Build Coastguard Worker #include <memory>
12*c8dee2aaSAndroid Build Coastguard Worker
13*c8dee2aaSAndroid Build Coastguard Worker namespace SkSL {
14*c8dee2aaSAndroid Build Coastguard Worker
writeString(const std::string & s)15*c8dee2aaSAndroid Build Coastguard Worker void OutputStream::writeString(const std::string& s) {
16*c8dee2aaSAndroid Build Coastguard Worker this->write(s.c_str(), s.size());
17*c8dee2aaSAndroid Build Coastguard Worker }
18*c8dee2aaSAndroid Build Coastguard Worker
printf(const char format[],...)19*c8dee2aaSAndroid Build Coastguard Worker void OutputStream::printf(const char format[], ...) {
20*c8dee2aaSAndroid Build Coastguard Worker va_list args;
21*c8dee2aaSAndroid Build Coastguard Worker va_start(args, format);
22*c8dee2aaSAndroid Build Coastguard Worker this->appendVAList(format, args);
23*c8dee2aaSAndroid Build Coastguard Worker va_end(args);
24*c8dee2aaSAndroid Build Coastguard Worker }
25*c8dee2aaSAndroid Build Coastguard Worker
appendVAList(const char format[],va_list args)26*c8dee2aaSAndroid Build Coastguard Worker void OutputStream::appendVAList(const char format[], va_list args) {
27*c8dee2aaSAndroid Build Coastguard Worker char buffer[kBufferSize];
28*c8dee2aaSAndroid Build Coastguard Worker va_list copy;
29*c8dee2aaSAndroid Build Coastguard Worker va_copy(copy, args);
30*c8dee2aaSAndroid Build Coastguard Worker int length = vsnprintf(buffer, kBufferSize, format, args);
31*c8dee2aaSAndroid Build Coastguard Worker if (length > (int) kBufferSize) {
32*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<char[]> bigBuffer(new char[length + 1]);
33*c8dee2aaSAndroid Build Coastguard Worker vsnprintf(bigBuffer.get(), length + 1, format, copy);
34*c8dee2aaSAndroid Build Coastguard Worker this->write(bigBuffer.get(), length);
35*c8dee2aaSAndroid Build Coastguard Worker } else {
36*c8dee2aaSAndroid Build Coastguard Worker this->write(buffer, length);
37*c8dee2aaSAndroid Build Coastguard Worker }
38*c8dee2aaSAndroid Build Coastguard Worker va_end(copy);
39*c8dee2aaSAndroid Build Coastguard Worker }
40*c8dee2aaSAndroid Build Coastguard Worker
41*c8dee2aaSAndroid Build Coastguard Worker } // namespace SkSL
42