From 53d54f9c56611106982f879289ad8ab93c16c579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikkel=20Fahn=C3=B8e=20J=C3=B8rgensen?= Date: Mon, 21 Dec 2020 23:45:43 +0100 Subject: [PATCH] Use portable printf formatters for 64-bit integers, also for tests --- samples/reflection/bfbs2json.c | 15 +++++++++------ test/reflection_test/reflection_test.c | 8 ++++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/samples/reflection/bfbs2json.c b/samples/reflection/bfbs2json.c index cbd316bc4..cd7ac0d20 100644 --- a/samples/reflection/bfbs2json.c +++ b/samples/reflection/bfbs2json.c @@ -1,8 +1,11 @@ #include "flatcc/support/readfile.h" #include "flatcc/reflection/reflection_reader.h" -/* Portable way to print 64-bit integers. */ -#define lld(x) (long long int)(x) +/* -DFLATCC_PORTABLE may help if inttypes.h is missing. */ +#ifndef PRId64 +#include +#endif + /* * Reads a binary schema generated by `flatcc` or Googles `flatc` tool, @@ -11,7 +14,7 @@ * Note: This is completely unrelated to `flatcc's` JSON support - we * just needed to do something tangible with the data we read from the * binary schema and opted to print it as JSON. - * + * * The JSON can be pretty printed with an external tool, for example: * * cat monster_test_schema.json | jq '.' @@ -96,7 +99,7 @@ void print_object(reflection_Object_table_t O) printf(",\"id\":%hu", reflection_Field_id(F)); } if (reflection_Field_default_integer_is_present(F)) { - printf(",\"default_integer\":%lld", lld(reflection_Field_default_integer(F))); + printf(",\"default_integer\":%"PRId64"", (int64_t)reflection_Field_default_integer(F)); } if (reflection_Field_default_real_is_present(F)) { printf(",\"default_integer\":%lf", reflection_Field_default_real(F)); @@ -146,7 +149,7 @@ void print_enum(reflection_Enum_table_t E) } printf("{\"name\":\"%s\"", reflection_EnumVal_name(EV)); if (reflection_EnumVal_value_is_present(EV)) { - printf(",\"value\":%lld", lld(reflection_EnumVal_value(EV))); + printf(",\"value\":%"PRId64"", (int64_t)reflection_EnumVal_value(EV)); } if (reflection_EnumVal_object_is_present(EV)) { printf(",\"object\":"); @@ -155,7 +158,7 @@ void print_enum(reflection_Enum_table_t E) if (reflection_EnumVal_union_type_is_present(EV)) { printf(",\"union_type\":"); print_type(reflection_EnumVal_union_type(EV)); - } + } printf("}"); } printf("]"); diff --git a/test/reflection_test/reflection_test.c b/test/reflection_test/reflection_test.c index 92f0409e4..de940c27d 100644 --- a/test/reflection_test/reflection_test.c +++ b/test/reflection_test/reflection_test.c @@ -2,7 +2,11 @@ #include "flatcc/reflection/reflection_reader.h" #include "flatcc/portable/pcrt.h" -#define lld(x) (long long int)(x) +/* -DFLATCC_PORTABLE may help if inttypes.h is missing. */ +#ifndef PRId64 +#include +#endif + /* This is not an exhaustive test. */ int test_schema(const char *monster_bfbs) @@ -50,7 +54,7 @@ int test_schema(const char *monster_bfbs) if (reflection_Field_default_integer(F) != 150) { printf("mana field has wrong default value\n"); printf("field name: %s\n", reflection_Field_name(F)); - printf("%lld\n", lld(reflection_Field_default_integer(F))); + printf("%"PRId64"\n", (int64_t)reflection_Field_default_integer(F)); goto done; } T = reflection_Field_type(F);