%% options copyright owner = Dirk Krause copyright year = 2015-xxxx license = bsd %% header /** @file Generic dk4_stream_t API for input and output, implementation for gzfile. CRT on Windows: Required. */ #ifndef DK4CONF_H_INCLUDED #include "dk4conf.h" #endif #if DK4_HAVE_ZLIB_H #ifndef DK4STRM_H_INCLUDED #include "dk4strm.h" #endif #ifndef ZLIB_H_INCLUDED #include #define ZLIB_H_INCLUDED 1 #endif #ifdef __cplusplus extern "C" { #endif /** Low level function to access a gzFile. @param api The stream API structure for arguments and results. */ void dk4stream_gzfile_function(dk4_stream_api_t *api); /** Low level function to access a gzFile and close it on dk4stream_close(). @param api The stream API structure for arguments and results. */ void dk4stream_gzfile_with_close_function(dk4_stream_api_t *api); /** Open a stream for an existing gzFile. @param fipo Existing gzFile, not closed on dk4stream_close(). @param fl Access flag: DK4_STREAM_READ,, DK4_STREAM_WRITE or (DK4_STREAM_READ | DK4_STREAM_WRITE). @param ibs Input buffer size (0 for default). @param obs Output buffer size (0 for default). @param erp Error report, may be NULL. @return Pointer to stream on success, NULL on error. */ dk4_stream_t * dk4stream_open_for_gzfile( gzFile fipo, int fl, size_t ibs, size_t obs, dk4_er_t *erp ); /** Open a stream for an existing gzFile. @param fipo Existing gzFile, closed on dk4stream_close(). @param fl Access flag: DK4_STREAM_READ,, DK4_STREAM_WRITE or (DK4_STREAM_READ | DK4_STREAM_WRITE). @param ibs Input buffer size (0 for default). @param obs Output buffer size (0 for default). @param erp Error report, may be NULL. @return Pointer to stream on success, NULL on error. */ dk4_stream_t * dk4stream_open_for_gzfile_with_close( gzFile fipo, int fl, size_t ibs, size_t obs, dk4_er_t *erp ); #ifdef __cplusplus } #endif #endif %% module #include "dk4conf.h" #if DK4_HAVE_ZLIB_H #include "dk4strm.h" #include "dk4strmg.h" void dk4stream_gzfile_with_close_function(dk4_stream_api_t *api) { gzFile fipo; int res; if (NULL != api) { api->res = 0; api->sz_out = 0; fipo = (gzFile)(api->d); if (NULL != fipo) { switch (api->cmd) { case DK4_STREAM_API_READ: { res = gzread(fipo, api->b, (int)(api->sz_in)); if (0 < res) { api->res = 1; api->sz_out = (size_t)res; } } break; case DK4_STREAM_API_WRITE: { res = gzwrite(fipo, api->b, (int)(api->sz_in)); if (res == (int)(api->sz_in)) { api->res = 1; } if (0 <= res) { api->sz_out = (size_t)res; } else { api->sz_out = 0; } } break; case DK4_STREAM_API_FLUSH: { if (Z_OK == gzflush(fipo, Z_FULL_FLUSH)) { api->res = 1; } } break; case DK4_STREAM_API_AT_END: { if (1 == gzeof(fipo)) { api->res = 1; } } break; case DK4_STREAM_API_CLOSE: { if (Z_OK == gzclose(fipo)) { api->res = 1; } } break; } } } else { } } void dk4stream_gzfile_function(dk4_stream_api_t *api) { if (NULL != api) { switch (api->cmd) { case DK4_STREAM_API_CLOSE: { api->res = 1; } break; default: { dk4stream_gzfile_with_close_function(api); } break; } } } dk4_stream_t * dk4stream_open_for_gzfile( gzFile fipo, int fl, size_t ibs, size_t obs, dk4_er_t *erp ) { return ( dk4stream_open( (void *)fipo, dk4stream_gzfile_function, fl, ibs, obs, erp ) ); } dk4_stream_t * dk4stream_open_for_gzfile_with_close( gzFile fipo, int fl, size_t ibs, size_t obs, dk4_er_t *erp ) { return ( dk4stream_open( (void *)fipo, dk4stream_gzfile_with_close_function, fl, ibs, obs, erp ) ); } #endif