SkJSONWriter.cpp 1.1 KB

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