From 42c723f8df0ecbc386c5dedf5bf1727e03f9ecd7 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Tue, 30 Jul 2019 15:47:27 +0100 Subject: [PATCH] ECC-961: Refactoring --- src/grib_api_prototypes.h | 7 ++++--- src/grib_dumper_class.c | 24 ++++++++++++++++++------ tools/grib_dump.c | 36 ++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/grib_api_prototypes.h b/src/grib_api_prototypes.h index de77c39be..208004772 100644 --- a/src/grib_api_prototypes.h +++ b/src/grib_api_prototypes.h @@ -954,9 +954,10 @@ grib_dumper *grib_dumper_factory(const char *op, grib_handle *h, FILE *out, unsi void grib_dump_accessors_block(grib_dumper *dumper, grib_block_of_accessors *block); void grib_dump_accessors_list(grib_dumper *dumper, grib_accessors_list *al); int grib_print(grib_handle *h, const char *name, grib_dumper *d); -void grib_dump_content(grib_handle *h, FILE *f, const char *mode, unsigned long option_flags, void *data); -grib_dumper *grib_dump_content_with_dumper(grib_handle *h, grib_dumper *dumper, FILE *f, const char *mode, unsigned long option_flags, void *data); -void codes_dump_bufr_flat(grib_accessors_list *al, grib_handle *h, FILE *f, const char *mode, unsigned long option_flags, void *data); +void grib_dump_content(grib_handle *h, FILE *f, const char *mode, unsigned long flags, void *data); +void grib_dump_keys(grib_handle* h, FILE* f, const char* mode, unsigned long flags, void *data, const char **keys, size_t num_keys); +grib_dumper *grib_dump_content_with_dumper(grib_handle *h, grib_dumper *dumper, FILE *f, const char *mode, unsigned long flags, void *data); +void codes_dump_bufr_flat(grib_accessors_list *al, grib_handle *h, FILE *f, const char *mode, unsigned long flags, void *data); /* grib_context.c */ size_t grib_context_read(const grib_context *c, void *ptr, size_t size, void *stream); diff --git a/src/grib_dumper_class.c b/src/grib_dumper_class.c index 4540773df..02ca5b346 100644 --- a/src/grib_dumper_class.c +++ b/src/grib_dumper_class.c @@ -80,24 +80,36 @@ int grib_print(grib_handle* h, const char* name, grib_dumper *d ){ return GRIB_NOT_FOUND; } -void grib_dump_content(grib_handle* h, FILE* f,const char* mode,unsigned long option_flags,void *data) +void grib_dump_content(grib_handle* h, FILE* f,const char* mode,unsigned long flags,void *data) { grib_dumper *dumper; - dumper = grib_dumper_factory(mode?mode:"serialize",h,f,option_flags,data); + dumper = grib_dumper_factory(mode?mode:"serialize", h, f, flags, data); grib_dump_header(dumper,h); grib_dump_accessors_block(dumper,h->root->block); grib_dump_footer(dumper,h); grib_dumper_delete(dumper); } -grib_dumper* grib_dump_content_with_dumper(grib_handle* h, grib_dumper* dumper, FILE* f,const char* mode,unsigned long option_flags,void *data) +void grib_dump_keys(grib_handle* h, FILE* f, const char* mode, unsigned long flags, void *data, const char **keys, size_t num_keys) +{ + size_t i; + grib_accessor* acc = NULL; + grib_dumper* dumper = grib_dumper_factory(mode?mode:"serialize", h, f, flags, data); + for(i=0; icount; count++; } - dumper = grib_dumper_factory(mode?mode:"serialize",h,f,option_flags,data); + dumper = grib_dumper_factory(mode?mode:"serialize", h, f, flags, data); if (!dumper) return NULL; dumper->count=count; @@ -107,11 +119,11 @@ grib_dumper* grib_dump_content_with_dumper(grib_handle* h, grib_dumper* dumper, return dumper; } -void codes_dump_bufr_flat(grib_accessors_list* al,grib_handle* h, FILE* f,const char* mode,unsigned long option_flags,void *data) +void codes_dump_bufr_flat(grib_accessors_list* al,grib_handle* h, FILE* f,const char* mode,unsigned long flags,void *data) { grib_dumper* dumper = NULL; Assert(h->product_kind == PRODUCT_BUFR); - dumper = grib_dumper_factory(mode?mode:"serialize",h,f,option_flags,data); + dumper = grib_dumper_factory(mode?mode:"serialize", h, f, flags, data); grib_dump_header(dumper,h); grib_dump_accessors_list(dumper,al); grib_dump_footer(dumper,h); diff --git a/tools/grib_dump.c b/tools/grib_dump.c index 0b7c09cbf..dc69c8bba 100644 --- a/tools/grib_dump.c +++ b/tools/grib_dump.c @@ -169,6 +169,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) { long length=0; int i,err=0; + grib_context* c = h->context; if (grib_get_long(h,"totalLength",&length) != GRIB_SUCCESS) length=-9999; @@ -178,24 +179,27 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) if( err != GRIB_SUCCESS && options->fail) exit(err); } - for (i=0;iprint_keys_count;i++) { - const char* key_name = options->print_keys[i].name; - grib_accessor* a = NULL; - err = grib_set_flag(h, key_name, GRIB_ACCESSOR_FLAG_DUMP); - if (err) { - grib_context_log(h->context,GRIB_LOG_ERROR, "%s: %s", key_name, grib_get_error_message(err)); - exit(err); - } - a=grib_find_accessor(h, key_name); - if (a) { - grib_dumper *dumper; - dumper = grib_dumper_factory(options->dump_mode?options->dump_mode:"serialize",h,stdout,options->dump_flags,NULL); - grib_accessor_dump(a, dumper); - grib_dumper_delete(dumper); + /* ECC-961 */ + if (dump_keys && options->print_keys_count > 0) { + int errors = 0; /* true if at least one error */ + const int key_count_total = options->print_keys_count; + int key_count_valid = 0; /* Some requested keys may be invalid */ + const char** key_names=(const char**)grib_context_malloc_clear(c, sizeof(char*)*key_count_total); + for (i=0;iprint_keys[i].name; + err = grib_set_flag(h, key_name, GRIB_ACCESSOR_FLAG_DUMP); + if (err) { + grib_context_log(c,GRIB_LOG_ERROR, "%s: %s", key_name, grib_get_error_message(err)); + errors = 1; + } else { + key_names[key_count_valid]=key_name; + key_count_valid++; + } } + grib_dump_keys(h, stdout, options->dump_mode, options->dump_flags, 0, key_names, key_count_valid); + grib_context_free(c, key_names); + return errors; } - if (dump_keys && options->print_keys_count > 0) - return 0; if(json) { if (!first_handle && options->handle_count>1) {