1 /*
2 Copyright (C) 2015-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: dk4strmb.ctr
12 */
13 
14 /**	@file dk4strmb.c The dk4strmb module.
15 */
16 
17 
18 #include "dk4conf.h"
19 
20 #if DK4_HAVE_BZLIB_H
21 
22 #include <libdk4c/dk4strm.h>
23 #include <libdk4c/dk4strmb.h>
24 
25 #if DK4_HAVE_ASSERT_H
26 #ifndef	ASSERT_H_INCLUDED
27 #include <assert.h>
28 #define	ASSERT_H_INCLUDED 1
29 #endif
30 #endif
31 
32 
33 void
dk4stream_bzfile_with_close_function(dk4_stream_api_t * api)34 dk4stream_bzfile_with_close_function(dk4_stream_api_t *api)
35 {
36   BZFILE	*fipo;
37   int		 res;
38 #if	DK4_USE_ASSERT
39   assert(NULL != api);
40 #endif
41   if (NULL != api) {
42     api->res = 0;
43     api->sz_out = 0;
44     fipo = (BZFILE *)(api->d);
45     if (NULL != fipo) {
46       switch (api->cmd) {
47         case DK4_STREAM_API_READ: {
48 	  res = BZ2_bzread(fipo, api->b, (int)(api->sz_in));
49 	  if (0 < res) {
50 	    api->res = 1;
51 	    api->sz_out = (size_t)res;
52 	  }
53 	} break;
54 	case DK4_STREAM_API_WRITE: {
55 	  if (0 < api->sz_in) {
56 	    res = BZ2_bzwrite(fipo, api->b, (int)(api->sz_in));
57 	    if (0 < res) {
58 	      if (res == (int)(api->sz_in)) {
59 	        api->res = 1;
60 	      }
61 	      api->sz_out = (size_t)res;
62 	    }
63 	  } else {
64 	    api->res = 1;
65 	  }
66 	} break;
67 	case DK4_STREAM_API_FLUSH: {
68 	  BZ2_bzflush(fipo);
69 	  api->res = 1;
70 	} break;
71 	case DK4_STREAM_API_AT_END: {
72 	} break;
73 	case DK4_STREAM_API_CLOSE: {
74 	  BZ2_bzclose(fipo);
75 	  api->res = 1;
76 	} break;
77 	case DK4_STREAM_API_ZERO_READ_IS_EOF: {
78 	  api->res = 1;
79 	} break;
80       }
81     }
82   }
83 }
84 
85 
86 
87 void
dk4stream_bzfile_function(dk4_stream_api_t * api)88 dk4stream_bzfile_function(dk4_stream_api_t *api)
89 {
90 #if	DK4_USE_ASSERT
91   assert(NULL != api);
92 #endif
93   if (NULL != api) {
94     switch (api->cmd) {
95       case DK4_STREAM_API_CLOSE: {
96         api->res = 1;
97 	api->sz_out = 0;
98       } break;
99       default: {
100         dk4stream_bzfile_with_close_function(api);
101       } break;
102     }
103   }
104 }
105 
106 
107 
108 dk4_stream_t *
dk4stream_open_for_bzfile(BZFILE * fipo,int fl,size_t ibs,size_t obs,dk4_er_t * erp)109 dk4stream_open_for_bzfile(
110   BZFILE	*fipo,
111   int		 fl,
112   size_t	 ibs,
113   size_t	 obs,
114   dk4_er_t	*erp
115 )
116 {
117 #if	DK4_USE_ASSERT
118   assert(NULL != fipo);
119 #endif
120   return (
121     dk4stream_open( (void *)fipo, dk4stream_bzfile_function, fl, ibs, obs, erp)
122   );
123 }
124 
125 
126 
127 dk4_stream_t *
dk4stream_open_for_bzfile_with_close(BZFILE * fipo,int fl,size_t ibs,size_t obs,dk4_er_t * erp)128 dk4stream_open_for_bzfile_with_close(
129   BZFILE	*fipo,
130   int		 fl,
131   size_t	 ibs,
132   size_t	 obs,
133   dk4_er_t	*erp
134 )
135 {
136 #if	DK4_USE_ASSERT
137   assert(NULL != fipo);
138 #endif
139   return (
140     dk4stream_open(
141       (void *)fipo, dk4stream_bzfile_with_close_function, fl, ibs, obs, erp
142     )
143   );
144 }
145 
146 
147 #endif
148 
149