1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
%% options
copyright owner = Dirk Krause
copyright year = 2015-xxxx
SPDX-License-Identifier: BSD-3-Clause
%% header
/** @file
Text stream processing for dkChar characters.
CRT on Windows: Optional.
*/
#ifndef DK4CONF_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif
#ifndef DK4TYPES_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4types.h"
#else
#include <dktools-4/dk4types.h>
#endif
#endif
#ifndef DK4ERROR_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4error.h"
#else
#include <dktools-4/dk4error.h>
#endif
#endif
#if DK4_CHAR_SIZE > 1
#if DK4_CHAR_SIZE > 2
/* +++++ dkChar 4 bytes */
#ifndef DK4TSP32_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4tsp32.h"
#else
#include <dktools-4/dk4tsp32.h>
#endif
#endif
/** Handler function to process a single character.
*/
typedef dk4_c32_handler_t dk4_dk_handler_t;
/** Handler function to process an input line.
*/
typedef dk4_c32_line_handler_t dk4_dk_line_handler_t;
/** Text stream processor.
*/
typedef dk4_tsp32_t dk4_tspdk_t;
/* ----- dkChar 4 bytes */
#else
/* +++++ dkChar 2 bytes */
#ifndef DK4TSP16_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4tsp16.h"
#else
#include <dktools-4/dk4tsp16.h>
#endif
#endif
/** Handler function to process a single character.
*/
typedef dk4_c16_handler_t dk4_dk_handler_t;
/** Handler function to process an input line.
*/
typedef dk4_c16_line_handler_t dk4_dk_line_handler_t;
/** Text stream processor.
*/
typedef dk4_tsp16_t dk4_tspdk_t;
/* ----- dkChar 2 bytes */
#endif
#else
/* +++++ dkChar 1 byte */
#ifndef DK4TSP08_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4tsp08.h"
#else
#include <dktools-4/dk4tsp08.h>
#endif
#endif
/** Handler function to process a single character.
*/
typedef dk4_c8_handler_t dk4_dk_handler_t;
/** Handler function to process an input line.
*/
typedef dk4_c8_line_handler_t dk4_dk_line_handler_t;
/** Text stream processor.
*/
typedef dk4_tsp08_t dk4_tspdk_t;
/* ----- dkChar 1 byte */
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** Set up processor for char by char processing.
@param tsp Processor to set up.
@param obj Object to modify when processing input, may be NULL.
@param fct Handler function to call for each character.
@param pre Encoding required for processing.
@param eie Expected input encoding.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
*/
int
dk4tspdk_setup_char(
dk4_tspdk_t *tsp,
void *obj,
dk4_dk_handler_t *fct,
int pre,
int eie,
dk4_er_t *erp
);
/** Set up processor for line processing.
@param tsp Processor to set up.
@param obj Object to modify when processing input, may be NULL.
@param fct Handler function to invoke for each line.
@param inbuf Input line buffer.
@param szin Size of input line buffer.
@param pre Encoding required for processing.
@param eie Expected input encoding.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
*/
int
dk4tspdk_setup_line(
dk4_tspdk_t *tsp,
void *obj,
dk4_dk_line_handler_t *fct,
dkChar *inbuf,
size_t szin,
int pre,
int eie,
dk4_er_t *erp
);
/** Allow pass-through of UTF-8 encoded data.
@param tsp Text stream processor to set up.
@param fl Flag: Enable pass-through of UTF-8 encoded data.
*/
void
dk4tspdk_enable_utf8_pass_through(dk4_tspdk_t *tsp, int fl);
/** Add one single byte.
@param tsp Text stream processor.
@param inbyte Byte to process.
@return DK4_TSP_RES_OK if the character was processed
successfully,
DK4_TSP_RES_ERROR if there was an error but we can
continue,
DK4_TSP_RES_FATAL if there was a fatal error so we
should abort processing.
*/
int
dk4tspdk_add_one_byte(
dk4_tspdk_t *tsp,
unsigned char inbyte
);
/** Add multiple bytes.
@param tsp Text stream processor.
@param buffer Buffer start address.
@param sz Number of bytes in buffer.
@return DK4_TSP_RES_OK if the character was processed
successfully,
DK4_TSP_RES_ERROR if there was an error but we can
continue,
DK4_TSP_RES_FATAL if there was a fatal error so we
should abort processing.
*/
int
dk4tspdk_add_bytes(
dk4_tspdk_t *tsp,
const unsigned char *buffer,
size_t sz
);
/** Finish processing.
@param tsp Text stream processor.
@return DK4_TSP_RES_OK if processing was finished
successfully,
DK4_TSP_RES_ERROR if there was an error,
DK4_TSP_RES_FATAL if there was a fatal error.
*/
int
dk4tspdk_finish(dk4_tspdk_t *tsp);
/** Retrieve error reports for encoding/decoding and processing.
@param er_en Destination error report buffer for encoding/decoding.
@param er_pr Destination error report buffer for processing.
@param tsp Text stream processor to retrieve errors from.
*/
void
dk4tspdk_get_errors(dk4_er_t *er_en, dk4_er_t *er_pr, dk4_tspdk_t const *tsp);
#ifdef __cplusplus
}
#endif
%% module
#include "dk4conf.h"
#include "dk4tspdk.h"
#include "dk4unused.h"
#if DK4_HAVE_ASSERT_H
#ifndef ASSERT_H_INCLUDED
#include <assert.h>
#define ASSERT_H_INCLUDED 1
#endif
#endif
int
dk4tspdk_setup_char(
dk4_tspdk_t *tsp,
void *obj,
dk4_dk_handler_t *fct,
#if 1 == DK4_CHAR_SIZE
int pre,
#else
int DK4_ARG_UNUSED(pre),
#endif
int eie,
dk4_er_t *erp
)
{
#if DK4_CHAR_SIZE > 1
DK4_UNUSED_ARG(pre)
#endif
#if DK4_USE_ASSERT
assert(NULL != tsp);
#endif
#if DK4_CHAR_SIZE > 1
#if DK4_CHAR_SIZE > 2
return (dk4tsp32_setup_char(tsp, obj, fct, eie, erp));
#else
return (dk4tsp16_setup_char(tsp, obj, fct, eie, erp));
#endif
#else
return (dk4tsp08_setup_char(tsp, obj, fct, pre, eie, erp));
#endif
}
int
dk4tspdk_setup_line(
dk4_tspdk_t *tsp,
void *obj,
dk4_dk_line_handler_t *fct,
dkChar *inbuf,
size_t szin,
#if 1 == DK4_CHAR_SIZE
int pre,
#else
int DK4_ARG_UNUSED(pre),
#endif
int eie,
dk4_er_t *erp
)
{
#if DK4_CHAR_SIZE > 1
DK4_UNUSED_ARG(pre)
#endif
#if DK4_USE_ASSERT
assert(NULL != tsp);
assert(NULL != inbuf);
assert(0 < szin);
#endif
#if DK4_CHAR_SIZE > 1
#if DK4_CHAR_SIZE > 2
return (dk4tsp32_setup_line(tsp, obj, fct, inbuf, szin, eie, erp));
#else
return (dk4tsp16_setup_line(tsp, obj, fct, inbuf, szin, eie, erp));
#endif
#else
return (dk4tsp08_setup_line(tsp, obj, fct, inbuf, szin, pre, eie, erp));
#endif
}
int
dk4tspdk_add_one_byte(
dk4_tspdk_t *tsp,
unsigned char inbyte
)
{
#if DK4_USE_ASSERT
assert(NULL != tsp);
#endif
#if DK4_CHAR_SIZE > 1
#if DK4_CHAR_SIZE > 2
return (dk4tsp32_add_one_byte(tsp, inbyte));
#else
return (dk4tsp16_add_one_byte(tsp, inbyte));
#endif
#else
return (dk4tsp08_add_one_byte(tsp, inbyte));
#endif
}
int
dk4tspdk_add_bytes(
dk4_tspdk_t *tsp,
const unsigned char *buffer,
size_t sz
)
{
#if DK4_USE_ASSERT
assert(NULL != tsp);
assert(NULL != buffer);
assert(0 < sz);
#endif
#if DK4_CHAR_SIZE > 1
#if DK4_CHAR_SIZE > 2
return (dk4tsp32_add_bytes(tsp, buffer, sz));
#else
return (dk4tsp16_add_bytes(tsp, buffer, sz));
#endif
#else
return (dk4tsp08_add_bytes(tsp, buffer, sz));
#endif
}
int
dk4tspdk_finish(dk4_tspdk_t *tsp)
{
#if DK4_USE_ASSERT
assert(NULL != tsp);
#endif
#if DK4_CHAR_SIZE > 1
#if DK4_CHAR_SIZE > 2
return (dk4tsp32_finish(tsp));
#else
return (dk4tsp16_finish(tsp));
#endif
#else
return (dk4tsp08_finish(tsp));
#endif
}
void
dk4tspdk_get_errors(dk4_er_t *er_en, dk4_er_t *er_pr, dk4_tspdk_t const *tsp)
{
#if DK4_USE_ASSERT
assert(NULL != tsp);
#endif
#if DK4_CHAR_SIZE > 1
#if DK4_CHAR_SIZE > 2
dk4tsp32_get_errors(er_en, er_pr, tsp);
#else
dk4tsp16_get_errors(er_en, er_pr, tsp);
#endif
#else
dk4tsp08_get_errors(er_en, er_pr, tsp);
#endif
}
void
dk4tspdk_enable_utf8_pass_through(
#if 1 == DK4_CHAR_SIZE
dk4_tspdk_t *tsp,
int fl
#else
dk4_tspdk_t * DK4_ARG_UNUSED(tsp),
int DK4_ARG_UNUSED(fl)
#endif
)
{
#if DK4_CHAR_SIZE > 1
DK4_UNUSED_ARG(tsp)
DK4_UNUSED_ARG(fl)
#endif
#if DK4_USE_ASSERT
assert(NULL != tsp);
#endif
#if DK4_CHAR_SIZE == 1
dk4tsp08_enable_utf8_pass_through(tsp, fl);
#endif
}
|