1 /*
2 Copyright (C) 2016-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5
6 /*
7 WARNING: This file was generated by the dkct program (see
8 http://dktools.sourceforge.net/ for details).
9 Changes you make here will be lost if dkct is run again!
10 You should modify the original source and run dkct on it.
11 Original source: dk4mbstrm.ctr
12 */
13
14 /** @file dk4mbstrm.c The dk4mbstrm module.
15 */
16
17
18 #include "dk4conf.h"
19 #include <libdk4c/dk4mbstrm.h>
20
21 #if DK4_HAVE_ASSERT_H
22 #ifndef ASSERT_H_INCLUDED
23 #include <assert.h>
24 #define ASSERT_H_INCLUDED 1
25 #endif
26 #endif
27
28
29 static
30 void
dk4stream_membuf_function(dk4_stream_api_t * api)31 dk4stream_membuf_function(dk4_stream_api_t *api)
32 {
33 dk4_membuf_t *mb;
34
35 #if DK4_USE_ASSERT
36 assert(NULL != api);
37 #endif
38 if (NULL != api) {
39 api->res = 0;
40 api->sz_out = 0;
41 mb = (dk4_membuf_t *)(api->d);
42 if (NULL != mb) {
43 switch (api->cmd) {
44 case DK4_STREAM_API_WRITE: {
45 if (0 < api->sz_in) {
46 api->res = dk4membuf_store(
47 mb, (const void *)(api->b), api->sz_in, NULL
48 );
49 if (0 != api->res) {
50 api->sz_out = api->sz_in;
51 }
52 }
53 else {
54 api->res = 1;
55 }
56 } break;
57 case DK4_STREAM_API_FLUSH: {
58 /* Do nothing but indicate success. */
59 api->res = 1;
60 } break;
61 case DK4_STREAM_API_CLOSE: {
62 /* Do nothing but indicate success. */
63 api->res = 1;
64 } break;
65 default : {
66 /* Do nothing, calling this is an error. */
67 } break;
68 }
69 }
70 }
71 }
72
73
74
75 dk4_stream_t *
dk4stream_open_membuf_writer(dk4_membuf_t * mb,dk4_er_t * erp)76 dk4stream_open_membuf_writer(dk4_membuf_t *mb, dk4_er_t *erp)
77 {
78 dk4_stream_t *back = NULL;
79
80 #if DK4_USE_ASSERT
81 assert(NULL != mb);
82 #endif
83 if (NULL != mb) {
84 back = dk4stream_open(
85 mb, dk4stream_membuf_function,
86 DK4_STREAM_WRITE, 0, DK4MEMBUF_CHUNK_SIZE, erp
87 );
88 }
89 else {
90 dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
91 }
92 return back;
93 }
94
95
96 /* vim: set ai sw=4 ts=4 : */
97
98