xref: /aosp_15_r20/external/skia/src/sksl/SkSLOutputStream.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
8 #include "src/sksl/SkSLOutputStream.h"
9 
10 #include <stdio.h>
11 #include <memory>
12 
13 namespace SkSL {
14 
writeString(const std::string & s)15 void OutputStream::writeString(const std::string& s) {
16     this->write(s.c_str(), s.size());
17 }
18 
printf(const char format[],...)19 void OutputStream::printf(const char format[], ...) {
20    va_list args;
21    va_start(args, format);
22    this->appendVAList(format, args);
23    va_end(args);
24 }
25 
appendVAList(const char format[],va_list args)26 void OutputStream::appendVAList(const char format[], va_list args) {
27     char buffer[kBufferSize];
28     va_list copy;
29     va_copy(copy, args);
30     int length = vsnprintf(buffer, kBufferSize, format, args);
31     if (length > (int) kBufferSize) {
32         std::unique_ptr<char[]> bigBuffer(new char[length + 1]);
33         vsnprintf(bigBuffer.get(), length + 1, format, copy);
34         this->write(bigBuffer.get(), length);
35     } else {
36         this->write(buffer, length);
37     }
38     va_end(copy);
39 }
40 
41 }  // namespace SkSL
42