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: dk4tsp08.ctr
12 */
13 
14 #ifndef DK4TSP08_H_INCLUDED
15 /** Avoid multiple inclusions. */
16 #define DK4TSP08_H_INCLUDED 1
17 
18 
19 /**	@file	dk4tsp08.h	Text stream processing for 8 bit characters.
20 
21 	CRT on Windows: Optional.
22 */
23 
24 #ifndef DK4CONF_H_INCLUDED
25 #if DK4_BUILDING_DKTOOLS4
26 #include "dk4conf.h"
27 #else
28 #include <dktools-4/dk4conf.h>
29 #endif
30 #endif
31 
32 #ifndef DK4TYPES_H_INCLUDED
33 #if DK4_BUILDING_DKTOOLS4
34 #include <libdk4base/dk4types.h>
35 #else
36 #include <dktools-4/dk4types.h>
37 #endif
38 #endif
39 
40 #ifndef DK4ERROR_H_INCLUDED
41 #if DK4_BUILDING_DKTOOLS4
42 #include <libdk4base/dk4error.h>
43 #else
44 #include <dktools-4/dk4error.h>
45 #endif
46 #endif
47 
48 #ifndef DK4BOM_H_INCLUDED
49 #if DK4_BUILDING_DKTOOLS4
50 #include <libdk4c/dk4bom.h>
51 #else
52 #include <dktools-4/dk4bom.h>
53 #endif
54 #endif
55 
56 #ifndef DK4TSP_H_INCLUDED
57 #if DK4_BUILDING_DKTOOLS4
58 #include <libdk4c/dk4tsp.h>
59 #else
60 #include <dktools-4/dk4tsp.h>
61 #endif
62 #endif
63 
64 #ifndef DK4UTF8_H_INCLUDED
65 #if DK4_BUILDING_DKTOOLS4
66 #include <libdk4c/dk4utf8.h>
67 #else
68 #include <dktools-4/dk4utf8.h>
69 #endif
70 #endif
71 
72 #ifndef DK4UTF16_H_INCLUDED
73 #if DK4_BUILDING_DKTOOLS4
74 #include <libdk4c/dk4utf16.h>
75 #else
76 #include <dktools-4/dk4utf16.h>
77 #endif
78 #endif
79 
80 #ifndef DK4C32_H_INCLUDED
81 #if DK4_BUILDING_DKTOOLS4
82 #include <libdk4c/dk4c32.h>
83 #else
84 #include <dktools-4/dk4c32.h>
85 #endif
86 #endif
87 
88 /**	Handler function for single characters.
89 	@param	obj	Object to modify while processing the character.
90 	@param	c	Character to process.
91 	@param	pos	Current position in file or data stream.
92 	@param	erp	Error report, may be NULL.
93 	@return	DK4_TSP_RES_OK		if the character was processed
94 					successfully,
95 		DK4_TSP_RES_ERROR	if there was an error but we can
96 					continue,
97 		DK4_TSP_RES_FATAL	if there was a fatal error so we
98 					should abort processing.
99 */
100 typedef int	dk4_c8_handler_t(
101   void				*obj,
102   char	 			 c,
103   dk4_text_stream_position_t	*pos,
104   dk4_er_t			*erp
105 );
106 
107 /**	Handler function for text lines.
108 	@param	obj	Object to modify while processing the character.
109 	@param	line	Text line to process.
110 	@param	lineno	Current line number.
111 	@param	erp	Error report, may be NULL.
112 	@return	DK4_TSP_RES_OK		if the character was processed
113 					successfully,
114 		DK4_TSP_RES_ERROR	if there was an error but we can
115 					continue,
116 		DK4_TSP_RES_FATAL	if there was a fatal error so we
117 					should abort processing.
118 */
119 typedef int	dk4_c8_line_handler_t(
120   void		*obj,
121   char		*line,
122   dk4_um_t	 lineno,
123   dk4_er_t	*erp
124 );
125 
126 /**	Structure for 8 bit character processing.
127 */
128 typedef struct {
129   union {
130     dk4_utf8_decoder_t		 u08;	/**< UTF-8 decoder. */
131     dk4_utf16_byte_decoder_t	 u16;	/**< UTF-16 decoder. */
132     dk4_c32_byte_decoder_t	 c32;	/**< 32 bit char decoder. */
133   } dec;				/**< Input decoder. */
134   dk4_bom_detector_t		 bomd;	/**< BOM detector. */
135   dk4_text_stream_position_t	 pos;	/**< Current position. */
136   dk4_er_t			 er_en;	/**< Errors in input decoding. */
137   dk4_er_t			 er_pr;	/**< Errors in processing. */
138   union {
139     dk4_c8_line_handler_t	*lh;	/**< Handler function for lines. */
140     dk4_c8_handler_t		*ch;	/**< Handler function for char. */
141   } fct;				/**< Handler function. */
142   char				*inbuf;	/**< Buffer for input line. */
143   void				*obj;	/**< Object to modify in processing. */
144   size_t			 in_sz;	/**< Size of input line buffer. */
145   size_t			 in_us;	/**< Used bytes in input line buffer. */
146   int				 upt;	/**< Flag: Enable UTF-8 pass through. */
147   int				 ief;	/**< Input encoding found. */
148   int				 iee;	/**< Input encoding expected. */
149   int				 pen;	/**< Encoding for processing. */
150   int				 pst;	/**< Processing stage. */
151   int				 pth;	/**< Flag: Pass through. */
152 } dk4_tsp08_t;
153 
154 
155 
156 #ifdef __cplusplus
157 extern "C" {
158 #endif
159 
160 /**	Set up processor for byte by byte processing.
161 	@param	tsp	Processor to set up.
162 	@param	obj	Object to modify when processing input, may be NULL.
163 	@param	fct	Handler function to call for each character.
164 	@param	pre	Encoding required for processing.
165 	@param	eie	Expected input encoding.
166 	@param	erp	Error report, may be NULL.
167 	@return	1 on success, 0 on error.
168 
169 	Error codes:
170 	- DK4_E_INVALID_ARGUMENTS<br>
171 	  if tsp or fct is NULL or pre is not an encoding for 8-bit characters.
172 */
173 int
174 dk4tsp08_setup_char(
175   dk4_tsp08_t		*tsp,
176   void			*obj,
177   dk4_c8_handler_t	*fct,
178   int			 pre,
179   int			 eie,
180   dk4_er_t		*erp
181 );
182 
183 /**	Set up processor for line processing.
184 	@param	tsp	Processor to set up.
185 	@param	obj	Object to modify when processing input, may be NULL.
186 	@param	fct	Handler function to invoke for each line.
187 	@param	inbuf	Input line buffer.
188 	@param	szin	Size of input line buffer.
189 	@param	pre	Encoding required for processing.
190 	@param	eie	Expected input encoding.
191 	@param	erp	Error report, may be NULL.
192 	@return	1 on success, 0 on error.
193 
194 	Error codes:
195 	- DK4_E_INVALID_ARGUMENTS<br>
196 	  if tsp, fct or inbuf is NULL, szin is 0 or pre is not a
197 	  valid encoding for 8-bit characters.
198 */
199 int
200 dk4tsp08_setup_line(
201   dk4_tsp08_t		*tsp,
202   void			*obj,
203   dk4_c8_line_handler_t	*fct,
204   char			*inbuf,
205   size_t		 szin,
206   int			 pre,
207   int			 eie,
208   dk4_er_t		*erp
209 );
210 
211 /**	Enable or disable pass-through of UTF-8 encoded data.
212 	Use this function after dk4tsp08_setup_line() or
213 	dk4tsp08_setup_char() but before adding the first byte.
214 	@param	tsp	Text stream processor to configure.
215 	@param	fl	Flag: Enabled.
216 */
217 void
218 dk4tsp08_enable_utf8_pass_through(dk4_tsp08_t *tsp, int fl);
219 
220 /**	Add one single byte.
221 	@param	tsp	Text stream processor.
222 	@param	inbyte	Byte to process.
223 	@return	DK4_TSP_RES_OK		if the character was processed
224 					successfully,
225 		DK4_TSP_RES_ERROR	if there was an error but we can
226 					continue,
227 		DK4_TSP_RES_FATAL	if there was a fatal error so we
228 					should abort processing.
229 */
230 int
231 dk4tsp08_add_one_byte(
232   dk4_tsp08_t	*tsp,
233   unsigned char	 inbyte
234 );
235 
236 /**	Add multiple bytes.
237 	@param	tsp	Text stream processor.
238 	@param	buffer	Buffer start address.
239 	@param	sz	Number of bytes in buffer.
240 	@return	DK4_TSP_RES_OK		if the character was processed
241 					successfully,
242 		DK4_TSP_RES_ERROR	if there was an error but we can
243 					continue,
244 		DK4_TSP_RES_FATAL	if there was a fatal error so we
245 					should abort processing.
246 */
247 int
248 dk4tsp08_add_bytes(
249   dk4_tsp08_t		*tsp,
250   const unsigned char	*buffer,
251   size_t		 sz
252 );
253 
254 /**	Finish processing.
255 	@param	tsp	Text stream processor.
256 	@return	DK4_TSP_RES_OK		if processing was finished
257 					successfully,
258 		DK4_TSP_RES_ERROR	if there was an error,
259 		DK4_TSP_RES_FATAL	if there was a fatal error.
260 */
261 int
262 dk4tsp08_finish(dk4_tsp08_t *tsp);
263 
264 /**	Retrieve error reports for encoding/decoding and processing.
265 	@param	er_en	Destination error report buffer for encoding/decoding.
266 	@param	er_pr	Destination error report buffer for processing.
267 	@param	tsp	Text stream processor to retrieve errors from.
268 
269 	Error codes er_en:
270 	- DK4_E_BUFFER_TOO_SMALL<br>
271 	  if the input buffer provided in dk4tsp08_setup_line() is too
272 	  small,
273 	- DK4_E_INVALID_ARGUMENTS<br>
274 	  if no character processing function was provided,
275 	- DK4_E_DECODING_FAILED<br>
276 	  if there were decoding errors,
277 	- DK4_E_ENCODING_FAILED<br>
278 	  if there were encoding errors.
279 
280 	Error codes er_pr:
281 	- The error codes returned in er_pr depend on the processing
282 	  function.
283 */
284 void
285 dk4tsp08_get_errors(dk4_er_t *er_en, dk4_er_t *er_pr, dk4_tsp08_t const *tsp);
286 
287 #ifdef __cplusplus
288 }
289 #endif
290 
291 
292 
293 
294 #endif
295