mirror of https://github.com/ecmwf/eccodes.git
ECC-991: Strict mode added
This commit is contained in:
parent
4d858df125
commit
54ba4cf72d
|
@ -548,7 +548,7 @@ static int bufr_decode_header(grib_context* c, const void* message, off_t offset
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int count_bufr_messages(grib_context* c, FILE* f, int* n)
|
static int count_bufr_messages(grib_context* c, FILE* f, int* n, int strict_mode)
|
||||||
{
|
{
|
||||||
int err=0;
|
int err=0;
|
||||||
void* mesg=NULL;
|
void* mesg=NULL;
|
||||||
|
@ -567,6 +567,7 @@ static int count_bufr_messages(grib_context* c, FILE* f, int* n)
|
||||||
done = 1; /* reached the end */
|
done = 1; /* reached the end */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (strict_mode) return GRIB_DECODING_ERROR;
|
||||||
}
|
}
|
||||||
if (mesg && !err) {
|
if (mesg && !err) {
|
||||||
grib_context_free(c,mesg);
|
grib_context_free(c,mesg);
|
||||||
|
@ -578,7 +579,7 @@ static int count_bufr_messages(grib_context* c, FILE* f, int* n)
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, codes_bufr_header** result, int* num_messages)
|
int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, codes_bufr_header** result, int* num_messages, int strict_mode)
|
||||||
{
|
{
|
||||||
int err = 0, i = 0;
|
int err = 0, i = 0;
|
||||||
FILE* fp = NULL;
|
FILE* fp = NULL;
|
||||||
|
@ -593,7 +594,7 @@ int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, cod
|
||||||
perror(filename);
|
perror(filename);
|
||||||
return GRIB_IO_PROBLEM;
|
return GRIB_IO_PROBLEM;
|
||||||
}
|
}
|
||||||
err = count_bufr_messages(c, fp, num_messages);
|
err = count_bufr_messages(c, fp, num_messages, strict_mode);
|
||||||
if (err) {
|
if (err) {
|
||||||
grib_context_log(c, GRIB_LOG_ERROR, "codes_bufr_extract_headers_malloc: Unable to count BUFR messages in file \"%s\"", filename);
|
grib_context_log(c, GRIB_LOG_ERROR, "codes_bufr_extract_headers_malloc: Unable to count BUFR messages in file \"%s\"", filename);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
@ -622,10 +623,20 @@ int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, cod
|
||||||
}
|
}
|
||||||
grib_context_free(c, mesg);
|
grib_context_free(c, mesg);
|
||||||
}
|
}
|
||||||
|
if (mesg && err) {
|
||||||
|
if (strict_mode) {
|
||||||
|
fclose(fp);
|
||||||
|
return GRIB_DECODING_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!mesg) {
|
if (!mesg) {
|
||||||
if (err != GRIB_END_OF_FILE && err != GRIB_PREMATURE_END_OF_FILE) {
|
if (err != GRIB_END_OF_FILE && err != GRIB_PREMATURE_END_OF_FILE) {
|
||||||
/* An error occurred */
|
/* An error occurred */
|
||||||
grib_context_log(c, GRIB_LOG_ERROR, "codes_bufr_extract_headers_malloc: Unable to read BUFR message");
|
grib_context_log(c, GRIB_LOG_ERROR, "codes_bufr_extract_headers_malloc: Unable to read BUFR message");
|
||||||
|
if (strict_mode) {
|
||||||
|
fclose(fp);
|
||||||
|
return GRIB_DECODING_ERROR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
|
|
|
@ -1267,9 +1267,10 @@ codes_handle *codes_grib_util_set_spec(codes_handle *h,
|
||||||
* result = array of 'codes_bufr_header' structs with 'num_messages' elements.
|
* result = array of 'codes_bufr_header' structs with 'num_messages' elements.
|
||||||
* This array should be freed by the caller.
|
* This array should be freed by the caller.
|
||||||
* num_messages = number of messages found in the input file.
|
* num_messages = number of messages found in the input file.
|
||||||
|
* strict = If 1 means fail if any message is invalid.
|
||||||
* returns 0 if OK, integer value on error.
|
* returns 0 if OK, integer value on error.
|
||||||
*/
|
*/
|
||||||
int codes_bufr_extract_headers_malloc(codes_context* c, const char* filename, codes_bufr_header** result, int* num_messages);
|
int codes_bufr_extract_headers_malloc(codes_context* c, const char* filename, codes_bufr_header** result, int* num_messages, int strict_mode);
|
||||||
|
|
||||||
/* --------------------------------------- */
|
/* --------------------------------------- */
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -1485,7 +1485,7 @@ size_t sum_of_pl_array(const long* pl, size_t plsize);
|
||||||
int compute_bufr_key_rank(grib_handle *h, grib_string_list *keys, const char *key);
|
int compute_bufr_key_rank(grib_handle *h, grib_string_list *keys, const char *key);
|
||||||
char **codes_bufr_copy_data_return_copied_keys(grib_handle *hin, grib_handle *hout, size_t *nkeys, int *err);
|
char **codes_bufr_copy_data_return_copied_keys(grib_handle *hin, grib_handle *hout, size_t *nkeys, int *err);
|
||||||
int codes_bufr_copy_data(grib_handle *hin, grib_handle *hout);
|
int codes_bufr_copy_data(grib_handle *hin, grib_handle *hout);
|
||||||
int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, codes_bufr_header** result, int* num_messages);
|
int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, codes_bufr_header** result, int* num_messages, int strict_mode);
|
||||||
|
|
||||||
|
|
||||||
/* string_util.c */
|
/* string_util.c */
|
||||||
|
|
|
@ -39,13 +39,14 @@ int main(int argc, char* argv[])
|
||||||
int num_messages = 0;
|
int num_messages = 0;
|
||||||
codes_bufr_header* header_array = NULL;
|
codes_bufr_header* header_array = NULL;
|
||||||
codes_context* c = codes_context_get_default();
|
codes_context* c = codes_context_get_default();
|
||||||
|
const int strict_mode = 1;
|
||||||
|
|
||||||
if (argc != 3) return 1;
|
if (argc != 3) return 1;
|
||||||
|
|
||||||
keys = argv[1]; /* comma-separated like bufr_ls/bufr_get */
|
keys = argv[1]; /* comma-separated like bufr_ls/bufr_get */
|
||||||
filename = argv[2];
|
filename = argv[2];
|
||||||
|
|
||||||
err = codes_bufr_extract_headers_malloc(c, filename, &header_array, &num_messages);
|
err = codes_bufr_extract_headers_malloc(c, filename, &header_array, &num_messages, strict_mode);
|
||||||
if (err) {
|
if (err) {
|
||||||
printf("ERROR: %s\n",grib_get_error_message(err));
|
printf("ERROR: %s\n",grib_get_error_message(err));
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in New Issue