xref: /aosp_15_r20/external/skia/src/utils/SkJSONWriter.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2017 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 // Make sure that the PRI format string macros are defined
9 #ifndef __STDC_FORMAT_MACROS
10 #define __STDC_FORMAT_MACROS
11 #endif
12 
13 #include "src/utils/SkJSONWriter.h"
14 
15 #include <inttypes.h>
16 #include <stdarg.h>
17 #include <stdio.h>
18 
appendS64(int64_t value)19 void SkJSONWriter::appendS64(int64_t value) {
20     this->beginValue();
21     this->appendf("%" PRId64, value);
22 }
23 
appendU64(uint64_t value)24 void SkJSONWriter::appendU64(uint64_t value) {
25     this->beginValue();
26     this->appendf("%" PRIu64, value);
27 }
28 
appendHexU64(uint64_t value)29 void SkJSONWriter::appendHexU64(uint64_t value) {
30     this->beginValue();
31     this->appendf("\"0x%" PRIx64 "\"", value);
32 }
33 
appendf(const char * fmt,...)34 void SkJSONWriter::appendf(const char* fmt, ...) {
35     const int kBufferSize = 1024;
36     char buffer[kBufferSize];
37     va_list argp;
38     va_start(argp, fmt);
39 #ifdef SK_BUILD_FOR_WIN
40     int length = _vsnprintf_s(buffer, kBufferSize, _TRUNCATE, fmt, argp);
41 #else
42     int length = vsnprintf(buffer, kBufferSize, fmt, argp);
43 #endif
44     SkASSERT(length >= 0 && length < kBufferSize);
45     va_end(argp);
46     this->write(buffer, length);
47 }
48