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_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);

View File

@ -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; 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;
if (dumper!=NULL) {
count=dumper->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;
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);

View File

@ -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;i<options->print_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;i<key_count_total;i++) {
const char* key_name = options->print_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) {