mirror of https://github.com/ecmwf/eccodes.git
ECC-991: Add more error handling
This commit is contained in:
parent
1094f9d631
commit
ff8931f72f
|
@ -537,6 +537,35 @@ static int bufr_decode_header(grib_context* c, const void* message, off_t offset
|
|||
return err;
|
||||
}
|
||||
|
||||
static int count_bufr_messages(grib_context* c, FILE* f, int* n)
|
||||
{
|
||||
int err=0;
|
||||
void* mesg=NULL;
|
||||
size_t size=0;
|
||||
off_t offset=0;
|
||||
int done = 0;
|
||||
|
||||
*n = 0;
|
||||
if (!c) c=grib_context_get_default();
|
||||
|
||||
while(!done) {
|
||||
mesg = wmo_read_bufr_from_file_malloc(f, 0, &size, &offset, &err);
|
||||
/*printf("Count so far=%ld, mesg=%x, err=%d (%s)\n", *count, mesg, err, grib_get_error_message(err));*/
|
||||
if (!mesg) {
|
||||
if (err == GRIB_END_OF_FILE || err == GRIB_PREMATURE_END_OF_FILE) {
|
||||
done = 1; /* reached the end */
|
||||
}
|
||||
}
|
||||
if (mesg && !err) {
|
||||
grib_context_free(c,mesg);
|
||||
(*n)++;
|
||||
}
|
||||
}
|
||||
rewind(f);
|
||||
if (err==GRIB_END_OF_FILE) err=GRIB_SUCCESS;
|
||||
return err;
|
||||
}
|
||||
|
||||
int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, codes_bufr_header** result, int* num_messages)
|
||||
{
|
||||
int err = 0, i = 0;
|
||||
|
@ -552,13 +581,18 @@ int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, cod
|
|||
perror(filename);
|
||||
return GRIB_IO_PROBLEM;
|
||||
}
|
||||
err = grib_count_in_file(c, fp, num_messages);
|
||||
err = count_bufr_messages(c, fp, num_messages);
|
||||
if (err) {
|
||||
grib_context_log(c, GRIB_LOG_ERROR, "codes_bufr_extract_headers_malloc: Unable to count BUFR messages in file \"%s\"", filename);
|
||||
fclose(fp);
|
||||
return err;
|
||||
}
|
||||
|
||||
size = *num_messages;
|
||||
if (size == 0) {
|
||||
grib_context_log(c, GRIB_LOG_ERROR, "codes_bufr_extract_headers_malloc: No BUFR messages in file \"%s\"", filename);
|
||||
return GRIB_INVALID_MESSAGE;
|
||||
}
|
||||
*result = (codes_bufr_header*)calloc(size, sizeof(codes_bufr_header));
|
||||
if (!*result) {
|
||||
fclose(fp);
|
||||
|
@ -575,6 +609,12 @@ int codes_bufr_extract_headers_malloc(grib_context* c, const char* filename, cod
|
|||
}
|
||||
grib_context_free(c, mesg);
|
||||
}
|
||||
if (!mesg) {
|
||||
if (err != GRIB_END_OF_FILE && err != GRIB_PREMATURE_END_OF_FILE) {
|
||||
/* An error occurred */
|
||||
grib_context_log(c, GRIB_LOG_ERROR, "codes_bufr_extract_headers_malloc: Unable to read BUFR message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
|
|
@ -46,7 +46,10 @@ int main(int argc, char* argv[])
|
|||
filename = argv[2];
|
||||
|
||||
err = codes_bufr_extract_headers_malloc(c, filename, &header_array, &num_messages);
|
||||
if (err) return 1;
|
||||
if (err) {
|
||||
printf("ERROR: %s\n",grib_get_error_message(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (i=0; i < num_messages; ++i) {
|
||||
codes_bufr_header bh = header_array[i];
|
||||
|
|
Loading…
Reference in New Issue