%% options copyright owner = Dirk Krause copyright year = 2015-xxxx SPDX-License-Identifier: BSD-3-Clause %% header /** @file dk4membf.h Save buffer data to file. */ #ifndef DK4CONF_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4conf.h" #else #include #endif #endif #ifndef STDIO_H_INCLUDED #include #define STDIO_H_INCLUDED 1 #endif #ifndef DK4MEMBUF_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4membuf.h" #else #include #endif #endif #ifdef __cplusplus extern "C" { #endif /** Save buffer contents to file. @param fipo Output file. @param mbptr Buffer containing data to write. @param erp Error report, may be NULL. @return 1 on success, 0 on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if fipo or mbptr is NULL, - DK4_E_WRITE_FAILED
if a write attempt to file failed. */ int dk4membuf_to_file(FILE *fipo, dk4_membuf_t const *mbptr, dk4_er_t *erp); #ifdef __cplusplus } #endif %% module #include "dk4conf.h" #include "dk4membf.h" #if DK4_HAVE_ERRNO_H #ifndef ERRNO_H_INCLUDED #include #define ERRNO_H_INCLUDED 1 #endif #endif #if DK4_HAVE_ASSERT_H #ifndef ASSERT_H_INCLUDED #include #define ASSERT_H_INCLUDED 1 #endif #endif int dk4membuf_to_file(FILE *fipo, dk4_membuf_t const *mbptr, dk4_er_t *erp) { dk4_membuf_cell_t *pc = NULL; dk4_membuf_cell_t *pn = NULL; size_t wrb = 0; int back = 0; #if DK4_USE_ASSERT assert(NULL != fipo); assert(NULL != mbptr); #endif if ((NULL != fipo) && (NULL != mbptr)) { back = 1; pc = mbptr->first; while (NULL != pc) { pn = pc->next; if (0 < pc->used) { errno = 0; wrb = fwrite(pc->buf, 1, pc->used, fipo); if (wrb != pc->used) { back = 0; dk4error_set_idetails(erp, DK4_E_WRITE_FAILED, errno); } } pc = pn; } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; }