From 8cf2356fa8aa1dda644314e6e656b3df1586e297 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Wed, 30 Nov 2011 17:03:53 +0100 Subject: [PATCH] * common/estream.c (es_fopenmem_init): New. * common/estream.h (es_fopenmem_init): New. --- common/estream.c | 37 +++++++++++++++++++++++++++++++++++-- common/estream.h | 5 ++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/common/estream.c b/common/estream.c index 20d365a63..c55c7f26d 100644 --- a/common/estream.c +++ b/common/estream.c @@ -2606,7 +2606,7 @@ es_fopen (const char *ES__RESTRICT path, const char *ES__RESTRICT mode) function but no free function. Providing only a free function is allowed as long as GROW is false. */ estream_t -es_mopen (unsigned char *ES__RESTRICT data, size_t data_n, size_t data_len, +es_mopen (void *ES__RESTRICT data, size_t data_n, size_t data_len, unsigned int grow, func_realloc_t func_realloc, func_free_t func_free, const char *ES__RESTRICT mode) @@ -2657,7 +2657,6 @@ es_fopenmem (size_t memlimit, const char *ES__RESTRICT mode) return NULL; modeflags |= O_RDWR; - if (func_mem_create (&cookie, NULL, 0, 0, BUFFER_BLOCK_SIZE, 1, mem_realloc, mem_free, modeflags, @@ -2672,6 +2671,40 @@ es_fopenmem (size_t memlimit, const char *ES__RESTRICT mode) } + +/* This is the same as es_fopenmem but intializes the memory with a + copy of (DATA,DATALEN). The stream is initally set to the + beginning. If MEMLIMIT is not 0 but shorter than DATALEN it + DATALEN will be used as the value for MEMLIMIT. */ +estream_t +es_fopenmem_init (size_t memlimit, const char *ES__RESTRICT mode, + const void *data, size_t datalen) +{ + estream_t stream; + + if (memlimit && memlimit < datalen) + memlimit = datalen; + + stream = es_fopenmem (memlimit, mode); + if (stream && data && datalen) + { + if (es_writen (stream, data, datalen, NULL)) + { + int saveerrno = errno; + es_fclose (stream); + stream = NULL; + _set_errno (saveerrno); + } + else + { + es_seek (stream, 0L, SEEK_SET, NULL); + es_set_indicators (stream, 0, 0); + } + } + return stream; +} + + estream_t es_fopencookie (void *ES__RESTRICT cookie, diff --git a/common/estream.h b/common/estream.h index 432143fd3..49662766e 100644 --- a/common/estream.h +++ b/common/estream.h @@ -76,6 +76,7 @@ #define es_fopen _ESTREAM_PREFIX(es_fopen) #define es_mopen _ESTREAM_PREFIX(es_mopen) #define es_fopenmem _ESTREAM_PREFIX(es_fopenmem) +#define es_fopenmem_init _ESTREAM_PREFIX(es_fopenmem_init) #define es_fdopen _ESTREAM_PREFIX(es_fdopen) #define es_fdopen_nc _ESTREAM_PREFIX(es_fdopen_nc) #define es_sysopen _ESTREAM_PREFIX(es_sysopen) @@ -262,13 +263,15 @@ int es_init (void); estream_t es_fopen (const char *ES__RESTRICT path, const char *ES__RESTRICT mode); -estream_t es_mopen (unsigned char *ES__RESTRICT data, +estream_t es_mopen (void *ES__RESTRICT data, size_t data_n, size_t data_len, unsigned int grow, void *(*func_realloc) (void *mem, size_t size), void (*func_free) (void *mem), const char *ES__RESTRICT mode); estream_t es_fopenmem (size_t memlimit, const char *ES__RESTRICT mode); +estream_t es_fopenmem_init (size_t memlimit, const char *ES__RESTRICT mode, + const void *data, size_t datalen); estream_t es_fdopen (int filedes, const char *mode); estream_t es_fdopen_nc (int filedes, const char *mode); estream_t es_sysopen (es_syshd_t *syshd, const char *mode);