Skip to content

Commit

Permalink
Use portable printf formatters for 64-bit integers, also for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkelfj committed Dec 21, 2020
1 parent fb32d63 commit 53d54f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 9 additions & 6 deletions samples/reflection/bfbs2json.c
Original file line number Diff line number Diff line change
@@ -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 <inttypes.h>
#endif


/*
* Reads a binary schema generated by `flatcc` or Googles `flatc` tool,
Expand All @@ -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 '.'
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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\":");
Expand All @@ -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("]");
Expand Down
8 changes: 6 additions & 2 deletions test/reflection_test/reflection_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <inttypes.h>
#endif


/* This is not an exhaustive test. */
int test_schema(const char *monster_bfbs)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 53d54f9

Please sign in to comment.