agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 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 | |
| 5 | #ifndef BASE_FORMAT_MACROS_H_ |
| 6 | #define BASE_FORMAT_MACROS_H_ |
| 7 | |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 8 | // This file defines the format macros for some integer types. |
| 9 | |
| 10 | // To print a 64-bit value in a portable way: |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 11 | // int64_t value; |
| 12 | // printf("xyz:%" PRId64, value); |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 13 | // The "d" in the macro corresponds to %d; you can also use PRIu64 etc. |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 14 | // |
| 15 | // For wide strings, prepend "Wide" to the macro: |
| 16 | // int64_t value; |
| 17 | // StringPrintf(L"xyz: %" WidePRId64, value); |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 18 | // |
| 19 | // To print a size_t value in a portable way: |
| 20 | // size_t size; |
| 21 | // printf("xyz: %" PRIuS, size); |
| 22 | // The "u" in the macro corresponds to %u, and S is for "size". |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 23 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 24 | #include <stddef.h> |
| 25 | #include <stdint.h> |
| 26 | |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 27 | #include "build/build_config.h" |
| 28 | |
chcunningham | 6a103f2 | 2015-05-11 07:56:00 +0900 | [diff] [blame] | 29 | #if defined(OS_POSIX) && (defined(_INTTYPES_H) || defined(_INTTYPES_H_)) && \ |
| 30 | !defined(PRId64) |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 31 | #error "inttypes.h has already been included before this header file, but " |
| 32 | #error "without __STDC_FORMAT_MACROS defined." |
| 33 | #endif |
| 34 | |
chcunningham | 6a103f2 | 2015-05-11 07:56:00 +0900 | [diff] [blame] | 35 | #if defined(OS_POSIX) && !defined(__STDC_FORMAT_MACROS) |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 36 | #define __STDC_FORMAT_MACROS |
| 37 | #endif |
| 38 | |
| 39 | #include <inttypes.h> |
| 40 | |
chcunningham | 6a103f2 | 2015-05-11 07:56:00 +0900 | [diff] [blame] | 41 | #if defined(OS_POSIX) |
| 42 | |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 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 | |
gregoryd@google.com | fad1a46 | 2010-02-26 02:20:07 +0900 | [diff] [blame] | 49 | #if !defined(PRIuS) |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 50 | #define PRIuS "zu" |
gregoryd@google.com | fad1a46 | 2010-02-26 02:20:07 +0900 | [diff] [blame] | 51 | #endif |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 52 | |
sdefresne@chromium.org | c9dcae6 | 2014-03-11 05:16:34 +0900 | [diff] [blame] | 53 | // The size of NSInteger and NSUInteger varies between 32-bit and 64-bit |
| 54 | // architectures and Apple does not provides standard format macros and |
| 55 | // recommends casting. This has many drawbacks, so instead define macros |
| 56 | // for formatting those types. |
| 57 | #if defined(OS_MACOSX) |
| 58 | #if defined(ARCH_CPU_64_BITS) |
| 59 | #if !defined(PRIdNS) |
| 60 | #define PRIdNS "ld" |
| 61 | #endif |
| 62 | #if !defined(PRIuNS) |
| 63 | #define PRIuNS "lu" |
| 64 | #endif |
| 65 | #if !defined(PRIxNS) |
| 66 | #define PRIxNS "lx" |
| 67 | #endif |
| 68 | #else // defined(ARCH_CPU_64_BITS) |
| 69 | #if !defined(PRIdNS) |
| 70 | #define PRIdNS "d" |
| 71 | #endif |
| 72 | #if !defined(PRIuNS) |
| 73 | #define PRIuNS "u" |
| 74 | #endif |
| 75 | #if !defined(PRIxNS) |
| 76 | #define PRIxNS "x" |
| 77 | #endif |
| 78 | #endif |
| 79 | #endif // defined(OS_MACOSX) |
| 80 | |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 81 | #else // OS_WIN |
| 82 | |
chcunningham | 6a103f2 | 2015-05-11 07:56:00 +0900 | [diff] [blame] | 83 | #if !defined(PRId64) || !defined(PRIu64) || !defined(PRIx64) |
| 84 | #error "inttypes.h provided by win toolchain should define these." |
gregoryd@google.com | ddf7151 | 2009-09-29 09:33:46 +0900 | [diff] [blame] | 85 | #endif |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 86 | |
| 87 | #define WidePRId64 L"I64d" |
| 88 | #define WidePRIu64 L"I64u" |
| 89 | #define WidePRIx64 L"I64x" |
| 90 | |
gregoryd@google.com | fad1a46 | 2010-02-26 02:20:07 +0900 | [diff] [blame] | 91 | #if !defined(PRIuS) |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 92 | #define PRIuS "Iu" |
gregoryd@google.com | fad1a46 | 2010-02-26 02:20:07 +0900 | [diff] [blame] | 93 | #endif |
evan@chromium.org | 3f6b260 | 2009-11-20 15:53:28 +0900 | [diff] [blame] | 94 | |
agl@chromium.org | d366e2c | 2009-06-30 02:58:25 +0900 | [diff] [blame] | 95 | #endif |
| 96 | |
dpolukhin@chromium.org | 21b228d | 2010-09-16 16:59:27 +0900 | [diff] [blame] | 97 | #endif // BASE_FORMAT_MACROS_H_ |