From c41e438dac9f1829563303ee784ee43fc83f77a7 Mon Sep 17 00:00:00 2001 From: Baudouin Raoult Date: Sun, 20 Aug 2017 07:52:59 +0100 Subject: [PATCH] ECC-528: Add wmo_read_any_from_stream_malloc() --- VERSION.cmake | 2 +- src/grib_api.h | 1 + src/grib_api_prototypes.h | 1 + src/grib_io.c | 50 ++++++++++++++++++++++++++++++++------- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/VERSION.cmake b/VERSION.cmake index c7efa990b..c1a410ed6 100644 --- a/VERSION.cmake +++ b/VERSION.cmake @@ -1 +1 @@ -set( ${PROJECT_NAME}_VERSION_STR "2.5.0" ) +set( ${PROJECT_NAME}_VERSION_STR "2.5.1" ) diff --git a/src/grib_api.h b/src/grib_api.h index b16afd3d7..306b9c17f 100644 --- a/src/grib_api.h +++ b/src/grib_api.h @@ -1348,6 +1348,7 @@ int wmo_read_grib_from_file(FILE* f,void* buffer,size_t* len); int wmo_read_bufr_from_file(FILE* f,void* buffer,size_t* len); int wmo_read_gts_from_file(FILE* f,void* buffer,size_t* len); int wmo_read_any_from_stream(void *stream_data, long (*stream_proc )(void *, void *buffer, long len ), void *buffer, size_t *len); +void* wmo_read_any_from_stream_malloc(void* stream_data,long (*stream_proc)(void*,void* buffer,long len) ,size_t *size, int* err); void *wmo_read_any_from_file_malloc(FILE* f,int headers_only,size_t *size,off_t *offset,int* err); void *wmo_read_gts_from_file_malloc(FILE* f,int headers_only,size_t *size,off_t *offset,int* err); void *wmo_read_bufr_from_file_malloc(FILE* f,int headers_only,size_t *size,off_t *offset,int* err); diff --git a/src/grib_api_prototypes.h b/src/grib_api_prototypes.h index c12e74e91..34332864c 100644 --- a/src/grib_api_prototypes.h +++ b/src/grib_api_prototypes.h @@ -1115,6 +1115,7 @@ int wmo_read_gts_from_file(FILE *f, void *buffer, size_t *len); int wmo_read_taf_from_file(FILE *f, void *buffer, size_t *len); int wmo_read_metar_from_file(FILE *f, void *buffer, size_t *len); int wmo_read_any_from_stream(void *stream_data, long (*stream_proc )(void *, void *buffer, long len ), void *buffer, size_t *len); +void* wmo_read_any_from_stream_malloc(void* stream_data,long (*stream_proc)(void*,void* buffer,long len) ,size_t *size, int* err); void *wmo_read_gts_from_file_malloc(FILE *f, int headers_only, size_t *size, off_t *offset, int *err); void *wmo_read_taf_from_file_malloc(FILE *f, int headers_only, size_t *size, off_t *offset, int *err); void *wmo_read_metar_from_file_malloc(FILE *f, int headers_only, size_t *size, off_t *offset, int *err); diff --git a/src/grib_io.c b/src/grib_io.c index 38a395d3c..584814f93 100644 --- a/src/grib_io.c +++ b/src/grib_io.c @@ -1167,6 +1167,19 @@ static size_t stream_read(void* data,void* buffer,size_t len,int* err) return n; } +/*================== */ + + +static void* allocate_buffer(void *data,size_t* length,int *err) +{ + alloc_buffer *u = (alloc_buffer*)data; + u->buffer = malloc(*length); + u->size=*length; + if(u->buffer == NULL) + *err = GRIB_OUT_OF_MEMORY; /* Cannot allocate buffer */ + return u->buffer; +} + int wmo_read_any_from_stream(void* stream_data,long (*stream_proc)(void*,void* buffer,long len) ,void* buffer,size_t* len) { int err; @@ -1181,6 +1194,7 @@ int wmo_read_any_from_stream(void* stream_data,long (*stream_proc)(void*,void* b u.buffer_size = *len; r.message_size = 0; + r.offset = 0; r.read_data = &s; r.read = &stream_read; r.seek = &stream_seek; @@ -1196,17 +1210,35 @@ int wmo_read_any_from_stream(void* stream_data,long (*stream_proc)(void*,void* b return err; } +void* wmo_read_any_from_stream_malloc(void* stream_data,long (*stream_proc)(void*,void* buffer,long len) ,size_t *size, int* err) +{ + alloc_buffer u; + + stream_struct s; + reader r; + + s.stream_data = stream_data; + s.stream_proc = stream_proc; + + r.message_size = 0; + r.offset = 0; + r.read_data = &s; + r.read = &stream_read; + r.seek = &stream_seek; + r.seek_from_start = &stream_seek; + r.tell = &stream_tell; + r.alloc_data = &u; + r.alloc = &allocate_buffer; + r.headers_only = 0; + + *err = read_any(&r, 1, 1, 1, 1); + *size = r.message_size; + + return u.buffer; +} + /*================== */ -static void* allocate_buffer(void *data,size_t* length,int *err) -{ - alloc_buffer *u = (alloc_buffer*)data; - u->buffer = malloc(*length); - u->size=*length; - if(u->buffer == NULL) - *err = GRIB_OUT_OF_MEMORY; /* Cannot allocate buffer */ - return u->buffer; -} void *wmo_read_gts_from_file_malloc(FILE* f,int headers_only,size_t *size,off_t *offset,int* err) {