ECC-961: Refactoring

This commit is contained in:
Shahram Najm 2019-07-30 15:47:27 +01:00
parent 7c572c8b97
commit 42c723f8df
3 changed files with 42 additions and 25 deletions

View File

@ -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_block(grib_dumper *dumper, grib_block_of_accessors *block);
void grib_dump_accessors_list(grib_dumper *dumper, grib_accessors_list *al); void grib_dump_accessors_list(grib_dumper *dumper, grib_accessors_list *al);
int grib_print(grib_handle *h, const char *name, grib_dumper *d); 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); void grib_dump_content(grib_handle *h, FILE *f, const char *mode, unsigned long 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 grib_dump_keys(grib_handle* h, FILE* f, const char* mode, unsigned long flags, void *data, const char **keys, size_t num_keys);
void codes_dump_bufr_flat(grib_accessors_list *al, 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 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 */ /* grib_context.c */
size_t grib_context_read(const grib_context *c, void *ptr, size_t size, void *stream); size_t grib_context_read(const grib_context *c, void *ptr, size_t size, void *stream);

View File

@ -80,24 +80,36 @@ int grib_print(grib_handle* h, const char* name, grib_dumper *d ){
return GRIB_NOT_FOUND; 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; 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_header(dumper,h);
grib_dump_accessors_block(dumper,h->root->block); grib_dump_accessors_block(dumper,h->root->block);
grib_dump_footer(dumper,h); grib_dump_footer(dumper,h);
grib_dumper_delete(dumper); 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; i<num_keys;++i) {
acc = grib_find_accessor(h, keys[i]);
if (acc) grib_accessor_dump(acc, dumper);
}
grib_dumper_delete(dumper);
}
grib_dumper* grib_dump_content_with_dumper(grib_handle* h, grib_dumper* dumper, FILE* f,const char* mode,unsigned long flags,void *data)
{ {
long count=1; long count=1;
if (dumper!=NULL) { if (dumper!=NULL) {
count=dumper->count; count=dumper->count;
count++; 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; if (!dumper) return NULL;
dumper->count=count; dumper->count=count;
@ -107,11 +119,11 @@ grib_dumper* grib_dump_content_with_dumper(grib_handle* h, grib_dumper* dumper,
return 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; grib_dumper* dumper = NULL;
Assert(h->product_kind == PRODUCT_BUFR); 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_header(dumper,h);
grib_dump_accessors_list(dumper,al); grib_dump_accessors_list(dumper,al);
grib_dump_footer(dumper,h); grib_dump_footer(dumper,h);

View File

@ -169,6 +169,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{ {
long length=0; long length=0;
int i,err=0; int i,err=0;
grib_context* c = h->context;
if (grib_get_long(h,"totalLength",&length) != GRIB_SUCCESS) if (grib_get_long(h,"totalLength",&length) != GRIB_SUCCESS)
length=-9999; 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); if( err != GRIB_SUCCESS && options->fail) exit(err);
} }
for (i=0;i<options->print_keys_count;i++) { /* ECC-961 */
const char* key_name = options->print_keys[i].name; if (dump_keys && options->print_keys_count > 0) {
grib_accessor* a = NULL; int errors = 0; /* true if at least one error */
err = grib_set_flag(h, key_name, GRIB_ACCESSOR_FLAG_DUMP); const int key_count_total = options->print_keys_count;
if (err) { int key_count_valid = 0; /* Some requested keys may be invalid */
grib_context_log(h->context,GRIB_LOG_ERROR, "%s: %s", key_name, grib_get_error_message(err)); const char** key_names=(const char**)grib_context_malloc_clear(c, sizeof(char*)*key_count_total);
exit(err); for (i=0;i<key_count_total;i++) {
} const char* key_name = options->print_keys[i].name;
a=grib_find_accessor(h, key_name); err = grib_set_flag(h, key_name, GRIB_ACCESSOR_FLAG_DUMP);
if (a) { if (err) {
grib_dumper *dumper; grib_context_log(c,GRIB_LOG_ERROR, "%s: %s", key_name, grib_get_error_message(err));
dumper = grib_dumper_factory(options->dump_mode?options->dump_mode:"serialize",h,stdout,options->dump_flags,NULL); errors = 1;
grib_accessor_dump(a, dumper); } else {
grib_dumper_delete(dumper); 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(json) {
if (!first_handle && options->handle_count>1) { if (!first_handle && options->handle_count>1) {