format_macros.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2009 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_FORMAT_MACROS_H_
  5. #define BASE_FORMAT_MACROS_H_
  6. // This file defines the format macros for some integer types.
  7. // To print a 64-bit value in a portable way:
  8. // int64_t value;
  9. // printf("xyz:%" PRId64, value);
  10. // The "d" in the macro corresponds to %d; you can also use PRIu64 etc.
  11. //
  12. // For wide strings, prepend "Wide" to the macro:
  13. // int64_t value;
  14. // StringPrintf(L"xyz: %" WidePRId64, value);
  15. //
  16. // To print a size_t value in a portable way:
  17. // size_t size;
  18. // printf("xyz: %" PRIuS, size);
  19. // The "u" in the macro corresponds to %u, and S is for "size".
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "build/build_config.h"
  23. #if (defined(OS_POSIX) || defined(OS_FUCHSIA)) && \
  24. (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && !defined(PRId64)
  25. #error "inttypes.h has already been included before this header file, but "
  26. #error "without __STDC_FORMAT_MACROS defined."
  27. #endif
  28. #if (defined(OS_POSIX) || defined(OS_FUCHSIA)) && !defined(__STDC_FORMAT_MACROS)
  29. #define __STDC_FORMAT_MACROS
  30. #endif
  31. #include <inttypes.h>
  32. #if defined(OS_WIN)
  33. #if !defined(PRId64) || !defined(PRIu64) || !defined(PRIx64)
  34. #error "inttypes.h provided by win toolchain should define these."
  35. #endif
  36. #define WidePRId64 L"I64d"
  37. #define WidePRIu64 L"I64u"
  38. #define WidePRIx64 L"I64x"
  39. #if !defined(PRIuS)
  40. #define PRIuS "Iu"
  41. #endif
  42. #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
  43. // GCC will concatenate wide and narrow strings correctly, so nothing needs to
  44. // be done here.
  45. #define WidePRId64 PRId64
  46. #define WidePRIu64 PRIu64
  47. #define WidePRIx64 PRIx64
  48. #if !defined(PRIuS)
  49. #define PRIuS "zu"
  50. #endif
  51. #endif // defined(OS_WIN)
  52. // The size of NSInteger and NSUInteger varies between 32-bit and 64-bit
  53. // architectures and Apple does not provides standard format macros and
  54. // recommends casting. This has many drawbacks, so instead define macros
  55. // for formatting those types.
  56. #if defined(OS_APPLE)
  57. #if defined(ARCH_CPU_64_BITS)
  58. #if !defined(PRIdNS)
  59. #define PRIdNS "ld"
  60. #endif
  61. #if !defined(PRIuNS)
  62. #define PRIuNS "lu"
  63. #endif
  64. #if !defined(PRIxNS)
  65. #define PRIxNS "lx"
  66. #endif
  67. #else // defined(ARCH_CPU_64_BITS)
  68. #if !defined(PRIdNS)
  69. #define PRIdNS "d"
  70. #endif
  71. #if !defined(PRIuNS)
  72. #define PRIuNS "u"
  73. #endif
  74. #if !defined(PRIxNS)
  75. #define PRIxNS "x"
  76. #endif
  77. #endif
  78. #endif // defined(OS_APPLE)
  79. #endif // BASE_FORMAT_MACROS_H_