From 0298cf3c8962d28b242a0d55bae87a15ac9d3cff Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Fri, 18 Dec 2015 15:30:50 +0100 Subject: [PATCH] gmio_stl: restore stream pos at exit of gmio_stl_infos_get() --- src/gmio_core/internal/helper_stream.h | 66 +++++++++++++++++++++----- src/gmio_stl/stl_infos.c | 4 ++ src/gmio_stl/stl_infos.h | 15 ++++++ tests/test_stl_infos.c | 2 - 4 files changed, 72 insertions(+), 15 deletions(-) diff --git a/src/gmio_core/internal/helper_stream.h b/src/gmio_core/internal/helper_stream.h index 4b1c429..696d8b5 100644 --- a/src/gmio_core/internal/helper_stream.h +++ b/src/gmio_core/internal/helper_stream.h @@ -19,23 +19,55 @@ #include "../stream.h" /*! Safe and convenient function for gmio_stream::func_at_end() */ -GMIO_INLINE gmio_bool_t gmio_stream_at_end(struct gmio_stream* stream) +GMIO_INLINE gmio_bool_t gmio_stream_at_end(struct gmio_stream* stream); + +/*! Safe and convenient function for gmio_stream::func_error() */ +GMIO_INLINE int gmio_stream_error(struct gmio_stream* stream); + +/*! Safe and convenient function for gmio_stream::func_read() */ +GMIO_INLINE size_t gmio_stream_read( + struct gmio_stream* stream, void *ptr, size_t size, size_t count); + +/*! Safe and convenient function for gmio_stream::func_write() */ +GMIO_INLINE size_t gmio_stream_write( + struct gmio_stream* stream, const void *ptr, size_t size, size_t count); + +/*! Safe and convenient function for gmio_stream::func_size() */ +GMIO_INLINE gmio_streamsize_t gmio_stream_size(struct gmio_stream* stream); + +/*! Safe and convenient function for gmio_stream::func_get_pos() */ +GMIO_INLINE int gmio_stream_get_pos( + struct gmio_stream* stream, struct gmio_streampos* pos); + +/*! Returns the current pos object of \p stream */ +GMIO_INLINE struct gmio_streampos gmio_streampos( + struct gmio_stream* stream, int* error); + +/*! Safe and convenient function for gmio_stream::func_set_pos() */ +GMIO_INLINE int gmio_stream_set_pos( + struct gmio_stream* stream, const struct gmio_streampos* pos); + + + +/* + * Implementation + */ + +gmio_bool_t gmio_stream_at_end(struct gmio_stream* stream) { if (stream != NULL && stream->func_at_end != NULL) return stream->func_at_end(stream->cookie); return GMIO_FALSE; } -/*! Safe and convenient function for gmio_stream::func_error() */ -GMIO_INLINE int gmio_stream_error(struct gmio_stream* stream) +int gmio_stream_error(struct gmio_stream* stream) { if (stream != NULL && stream->func_error != NULL) return stream->func_error(stream->cookie); return 0; } -/*! Safe and convenient function for gmio_stream::func_read() */ -GMIO_INLINE size_t gmio_stream_read( +size_t gmio_stream_read( struct gmio_stream* stream, void *ptr, size_t size, size_t count) { if (stream != NULL && stream->func_read != NULL) @@ -43,8 +75,7 @@ GMIO_INLINE size_t gmio_stream_read( return 0; } -/*! Safe and convenient function for gmio_stream::func_write() */ -GMIO_INLINE size_t gmio_stream_write( +size_t gmio_stream_write( struct gmio_stream* stream, const void *ptr, size_t size, size_t count) { if (stream != NULL && stream->func_write != NULL) @@ -52,16 +83,14 @@ GMIO_INLINE size_t gmio_stream_write( return 0; } -/*! Safe and convenient function for gmio_stream::func_size() */ -GMIO_INLINE gmio_streamsize_t gmio_stream_size(struct gmio_stream* stream) +gmio_streamsize_t gmio_stream_size(struct gmio_stream* stream) { if (stream != NULL && stream->func_size != NULL) return stream->func_size(stream->cookie); return 0; } -/*! Safe and convenient function for gmio_stream::func_get_pos() */ -GMIO_INLINE int gmio_stream_get_pos( +int gmio_stream_get_pos( struct gmio_stream* stream, struct gmio_streampos* pos) { if (stream != NULL && stream->func_get_pos != NULL) @@ -69,12 +98,23 @@ GMIO_INLINE int gmio_stream_get_pos( return -1; } -/*! Safe and convenient function for gmio_stream::func_set_pos() */ -GMIO_INLINE int gmio_stream_set_pos( +struct gmio_streampos gmio_streampos(struct gmio_stream* stream, int* error) +{ + struct gmio_streampos pos = {0}; + if (stream != NULL && stream->func_get_pos != NULL) { + const int errcode = stream->func_get_pos(stream->cookie, &pos); + if (error != NULL) + *error = errcode; + } + return pos; +} + +int gmio_stream_set_pos( struct gmio_stream* stream, const struct gmio_streampos* pos) { if (stream != NULL && stream->func_set_pos != NULL) return stream->func_set_pos(stream->cookie, pos); return -1; } + #endif /* GMIO_INTERNAL_HELPER_STREAM_H */ diff --git a/src/gmio_stl/stl_infos.c b/src/gmio_stl/stl_infos.c index 60e6db0..dfaf79c 100644 --- a/src/gmio_stl/stl_infos.c +++ b/src/gmio_stl/stl_infos.c @@ -17,6 +17,7 @@ #include "../gmio_core/error.h" #include "../gmio_core/internal/helper_memblock.h" +#include "../gmio_core/internal/helper_stream.h" #include "stl_error.h" #include "internal/stla_infos_get.h" #include "internal/stlb_infos_get.h" @@ -29,6 +30,8 @@ int gmio_stl_infos_get( int error = GMIO_ERROR_OK; struct gmio_memblock_helper mblock_helper = gmio_memblock_helper(&args->stream_memblock); + const struct gmio_streampos begin_streampos = + gmio_streampos(&args->stream, NULL); switch (format) { case GMIO_STL_FORMAT_ASCII: @@ -45,6 +48,7 @@ int gmio_stl_infos_get( break; } gmio_memblock_helper_release(&mblock_helper); + gmio_stream_set_pos(&args->stream, &begin_streampos); return error; } diff --git a/src/gmio_stl/stl_infos.h b/src/gmio_stl/stl_infos.h index 094c958..2ec3b95 100644 --- a/src/gmio_stl/stl_infos.h +++ b/src/gmio_stl/stl_infos.h @@ -34,6 +34,7 @@ #include "stl_format.h" #include "stlb_header.h" +/*! Informations retrieved by gmio_stl_infos_get() */ struct gmio_stl_infos { /*! Count of facets(triangles) */ @@ -82,15 +83,29 @@ enum gmio_stl_info_flag GMIO_STL_INFO_FLAG_ALL = 0xFFFF }; +/*! Objects to be passed to gmio_stl_infos_get() */ struct gmio_stl_infos_get_args { + /*! Input stream */ struct gmio_stream stream; + + /*! Optional memory block used by the stream to bufferize read operations + * + * If null, then a temporary memblock is created with the global default + * constructor function (see gmio_memblock_default()) + */ struct gmio_memblock stream_memblock; + + /*! Output informations */ struct gmio_stl_infos infos; }; GMIO_C_LINKAGE_BEGIN +/*! Finds informations about STL contents + * + * \return Error code (see gmio_core/error.h and stl_error.h) + */ GMIO_LIBSTL_EXPORT int gmio_stl_infos_get( struct gmio_stl_infos_get_args* args, diff --git a/tests/test_stl_infos.c b/tests/test_stl_infos.c index be15a2b..3e01557 100644 --- a/tests/test_stl_infos.c +++ b/tests/test_stl_infos.c @@ -30,12 +30,10 @@ const char* test_stl_infos() int error = GMIO_ERROR_OK; args.stream = gmio_stream_stdio(stla_file); - args.stream_memblock = gmio_memblock_malloc(8 * 1024); /* 8Ko */ error = gmio_stl_infos_get( &args, GMIO_STL_FORMAT_ASCII, GMIO_STL_INFO_FLAG_ALL); - gmio_memblock_deallocate(&args.stream_memblock); fclose(stla_file); UTEST_ASSERT(error == GMIO_ERROR_OK);