From d35518ec462f4b32dcce215ec73229511c275784 Mon Sep 17 00:00:00 2001 From: shahramn Date: Tue, 15 Oct 2024 17:20:12 +0100 Subject: [PATCH] Debugging: print grib_values key/value pair array --- src/eccodes_prototypes.h | 2 +- src/grib_value.cc | 20 +++++++++++--------- tests/unit_tests.cc | 6 +++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/eccodes_prototypes.h b/src/eccodes_prototypes.h index 55f889dea..0bf5baac2 100644 --- a/src/eccodes_prototypes.h +++ b/src/eccodes_prototypes.h @@ -733,7 +733,7 @@ int grib_get_long_array_internal(grib_handle* h, const char* name, long* val, si int grib_get_long_array(const grib_handle* h, const char* name, long* val, size_t* length); int grib_set_values(grib_handle* h, grib_values* args, size_t count); int grib_get_nearest_smaller_value(grib_handle* h, const char* name, double val, double* nearest); -void grib_print_values(const char* title, grib_values* values, FILE* out); +void grib_print_values(const char* title, const grib_values* values, FILE* out, int count); int grib_values_check(grib_handle* h, grib_values* values, int count); int codes_copy_key(grib_handle* h1, grib_handle* h2, const char* key, int type); int codes_compare_key(grib_handle* h1, grib_handle* h2, const char* key, int compare_flags); diff --git a/src/grib_value.cc b/src/grib_value.cc index 4b5b71ec7..277966bfc 100644 --- a/src/grib_value.cc +++ b/src/grib_value.cc @@ -1801,7 +1801,7 @@ int grib_set_values(grib_handle* h, grib_values* args, size_t count) if (h->context->debug) { for (i = 0; i < count; i++) { - grib_print_values("ECCODES DEBUG about to set key/value pair", &args[i], stderr); + grib_print_values("ECCODES DEBUG about to set key/value pair", &args[i], stderr, 1); } } @@ -1875,22 +1875,24 @@ int grib_get_nearest_smaller_value(grib_handle* h, const char* name, return act->nearest_smaller_value(val, nearest); } -void grib_print_values(const char* title, grib_values* values, FILE* out) +void grib_print_values(const char* title, const grib_values* values, FILE* out, int count) { - if (values) { - fprintf(out, "%s: %s=", title, values->name); - switch (values->type) { + Assert(values); + for (int i = 0; i < count; ++i) { + const grib_values aVal = values[i]; + fprintf(out, "%s: %s=", title, aVal.name); + switch (aVal.type) { case GRIB_TYPE_LONG: - fprintf(out, "%ld", values->long_value); + fprintf(out, "%ld", aVal.long_value); break; case GRIB_TYPE_DOUBLE: - fprintf(out, "%g", values->double_value); + fprintf(out, "%g", aVal.double_value); break; case GRIB_TYPE_STRING: - fprintf(out, "%s", values->string_value); + fprintf(out, "%s", aVal.string_value); break; } - fprintf(out, " (type=%s)\n", grib_get_type_name(values->type)); + fprintf(out, " (type=%s)\n", grib_get_type_name(aVal.type)); } } diff --git a/tests/unit_tests.cc b/tests/unit_tests.cc index 4ab3ead6d..aad854fec 100644 --- a/tests/unit_tests.cc +++ b/tests/unit_tests.cc @@ -486,7 +486,7 @@ static void test_parse_keyval_string() values_required, GRIB_TYPE_UNDEFINED, values1, &count); Assert( !err ); Assert( count == 2 ); - grib_print_values("print values test: values1", values1, stdout); + grib_print_values("print values test: values1", values1, stdout, count); Assert( strcmp(values1[0].name, "key1")==0 ); Assert( strcmp(values1[0].string_value, "value1")==0 ); @@ -506,7 +506,7 @@ static void test_parse_keyval_string() values_required, GRIB_TYPE_LONG, values2, &count); Assert( !err ); Assert( count == 1 ); - grib_print_values("print values test: values2", values2, stdout); + grib_print_values("print values test: values2", values2, stdout, count); Assert( strcmp(values2[0].name, "x")==0 ); Assert( values2[0].long_value == 14 ); Assert( values2[0].equal == 1 ); @@ -517,7 +517,7 @@ static void test_parse_keyval_string() values_required, GRIB_TYPE_DOUBLE, values3, &count); Assert( !err ); Assert( count == 1 ); - grib_print_values("print values test: values3", values3, stdout); + grib_print_values("print values test: values3", values3, stdout, count); Assert( strcmp(values3[0].name, "mars.level")==0 ); free( (void*)values3[0].name ); }