Brian Osman | 71a1889 | 2017-08-10 10:23:25 -0400 | [diff] [blame] | 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 | #define __STDC_FORMAT_MACROS |
| 10 | #include <inttypes.h> |
| 11 | #include <stdarg.h> |
| 12 | |
| 13 | #include "SkJSONWriter.h" |
| 14 | |
| 15 | void SkJSONWriter::appendS64(int64_t value) { |
| 16 | this->beginValue(); |
| 17 | this->appendf("%" PRId64, value); |
| 18 | } |
| 19 | |
| 20 | void SkJSONWriter::appendU64(uint64_t value) { |
| 21 | this->beginValue(); |
| 22 | this->appendf("%" PRIu64, value); |
| 23 | } |
| 24 | |
| 25 | void SkJSONWriter::appendHexU64(uint64_t value) { |
| 26 | this->beginValue(); |
| 27 | this->appendf("\"0x%" PRIx64 "\"", value); |
| 28 | } |
| 29 | |
| 30 | void SkJSONWriter::appendf(const char* fmt, ...) { |
| 31 | const int kBufferSize = 1024; |
| 32 | char buffer[kBufferSize]; |
| 33 | va_list argp; |
| 34 | va_start(argp, fmt); |
| 35 | #ifdef SK_BUILD_FOR_WIN |
| 36 | int length = _vsnprintf_s(buffer, kBufferSize, _TRUNCATE, fmt, argp); |
| 37 | #else |
| 38 | int length = vsnprintf(buffer, kBufferSize, fmt, argp); |
| 39 | #endif |
| 40 | SkASSERT(length >= 0 && length < kBufferSize); |
| 41 | va_end(argp); |
| 42 | this->write(buffer, length); |
| 43 | } |