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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
%% options
copyright owner = Dirk Krause
copyright year = 2015-xxxx
license = bsd
%% header
/** @file dk4wxstt.h String tables for wxChars.
*/
#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif
#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif
#ifndef DK4STRM_H_INCLUDED
#include "dk4strm.h"
#endif
#ifndef DK4ERROR_H_INCLUDED
#include "dk4error.h"
#endif
#ifndef DK4WXCS_H_INCLUDED
#include "dk4wxcs.h"
#endif
#ifndef WX_WXPREC_H_INCLUDED
#include <wx/wxprec.h>
#define WX_WXPREC_H_INCLUDED 1
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#if DK4_HAVE_WX_CHARTYPE_H
#ifndef WX_CHARTYPE_H_INCLUDED
#include <wx/chartype.h>
#define WX_CHARTYPE_H_INCLUDED 1
#endif
#else
#if DK4_HAVE_WX_WXCHAR_H
#ifndef WX_WXCHAR_H_INCLUDED
#include <wx/wxchar.h>
#define WX_WXCHAR_H_INCLUDED 1
#endif
#else
#ifndef WX_WX_H_INCLUDED
#include <wx/wx.h>
#define WX_WX_H_INCLUDED
#endif
#endif
#endif
#endif
/** String table for wxChar strings.
*/
typedef struct {
dkChar *shrtfn; /**< Short file name. */
wxChar **strings; /**< Strings read from file. */
size_t nstrings; /**< Number of strings. */
} dk4_wx_string_table_t;
extern "C" {
/** Create string table from file.
@param fn Short file name to store in structure.
@param sz Number of lines required.
@param erp Error report, may be NULL.
@return Pointer to new string table structure on success, NULL
on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if fn is NULL or sz is 0,
- DK4_E_MATH_OVERFLOW<br>
if there was a numeric overflow in size calculations for
dynamic memory allocations,
- DK4_E_MEMORY_ALLOCATION_FAILED<br>
if there is not enough memory available.
*/
dk4_wx_string_table_t *
dk4wxstt_open(const dkChar *fn, size_t sz, dk4_er_t *erp);
/** Apply a text stream to a string table.
@param tptr String table to configure.
@param strm Text stream, opened for reading.
@param pre Processing encoding.
@param erp1 Error report for decoding/encoding, may be NULL.
@param erp2 Error report for processing, may be NULL.
@return True on success, false on error.
Error codes erp1:
Error codes erp2:
- DK4_E_INVALID_ARGUMENTS<br>
if tptr or strm is NULL
*/
bool
dk4wxstt_apply_stream(
dk4_wx_string_table_t *tptr,
dk4_stream_t *strm,
int pre,
dk4_er_t *erp1,
dk4_er_t *erp2
);
/** Destroy a string table, free allocated memory.
@param ptr String table to destroy.
*/
void
dk4wxstt_close(dk4_wx_string_table_t *ptr);
/** Mark string table as empty.
Release memory for the strings.
@param ptr String table to shut down.
*/
void
dk4wxstt_shutdown(dk4_wx_string_table_t *ptr);
/** Compare two string tables by short name.
@param l Left pointer (always string table).
@param r Right pointer (string table or short name).
@param cr Comparison criteria
(0=string table/string table,
1=string table/short name).
@return Comparison result.
*/
int
dk4wxstt_compare(const void *l, const void *r, int cr);
}
%% module
#include "dk4mem.h"
#include "dk4enc.h"
#include "dk4strd.h"
#include "dk4wxtypes.h"
#include "dk4strx.h"
#include "dk4wxstt.h"
#include "dk4tspwx.h"
$!trace-include
void
dk4wxstt_shutdown(dk4_wx_string_table_t *ptr)
{
wxChar **stptr;
size_t nstrings;
$? "+ dk4wxstt_shutdown"
if (NULL != ptr) { $? ". arg ok"
if (NULL != ptr->strings) { $? ". strings ok"
stptr = ptr->strings;
nstrings = ptr->nstrings;
while (0 < nstrings--) { $? ". release one string"
dk4mem_release(*stptr);
stptr++;
} $? ". release array pointer"
dk4mem_release(ptr->strings);
}
}
$? "- dk4wxstt_shutdown"
}
void
dk4wxstt_close(dk4_wx_string_table_t *ptr)
{
$? "+ dk4wxstt_close"
if (NULL != ptr) { $? ". arg ok"
dk4wxstt_shutdown(ptr); $? ". release short file name"
dk4mem_release(ptr->shrtfn);
ptr->nstrings = 0; $? ". release string table"
dk4mem_free(ptr);
}
$? "- dk4wxstt_close"
}
dk4_wx_string_table_t *
dk4wxstt_open(const dkChar *fn, size_t sz, dk4_er_t *erp)
{
dk4_wx_string_table_t *back = NULL;
wxChar **stptr = NULL;
int ok = 0;
$? "+ dk4wxstt_open"
if ((NULL != fn) && (0 < sz)) { $? ". fn, sz ok"
back = dk4mem_new(dk4_wx_string_table_t,1,erp);
if (NULL != back) { $? ". allocated"
back->strings = NULL;
back->nstrings = 0;
back->shrtfn = dk4str_dup(fn, erp);
if (NULL != back->shrtfn) { $? ". short file name"
back->nstrings = sz;
back->strings = dk4mem_new(DK4_PWXCHAR,sz,erp);
if (NULL != back->strings) { $? ". array"
stptr = back->strings;
while (0 < sz--) { *(stptr++) = NULL; }
ok = 1;
}
}
if (1 > ok) { $? "! incomplete"
dk4wxstt_close(back);
back = NULL;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
$? "- dk4wxstt_open %d", ((NULL != back) ? 1 : 0)
return back;
}
int
dk4wxstt_compare(const void *l, const void *r, int cr)
{
const dk4_wx_string_table_t *lptr;
const dk4_wx_string_table_t *rptr;
int back = 0;
if (NULL != l) {
if (NULL != r) {
lptr = (const dk4_wx_string_table_t *)l;
switch (cr) {
case 1: {
if (NULL != lptr->shrtfn) {
back = dk4str_pathcmp(lptr->shrtfn, (const dkChar *)r);
} else {
back = -1;
}
} break;
default : {
rptr = (const dk4_wx_string_table_t *)r;
if (NULL != lptr->shrtfn) {
if (NULL != rptr->shrtfn) {
back = dk4str_pathcmp(lptr->shrtfn, rptr->shrtfn);
} else { back = 1; }
} else {
if (NULL != rptr->shrtfn) { back = -1; }
}
} break;
}
} else { back = 1; }
} else {
if (NULL != r) { back = -1; }
}
return back;
}
/** Support structure for reading string table from stream.
*/
typedef struct {
dk4_wx_string_table_t *dptr; /**< String table to set up. */
size_t curpos; /**< Current string number. */
} dk4_wx_string_table_reader_t;
#if DK4_SIZEOF_WXCHAR > 1
#if DK4_SIZEOF_WXCHAR > 2
#define TO_SIZED_CHAR(x) ((dk4_c32_t)(x))
#else
#define TO_SIZED_CHAR(x) ((dk4_c16_t)(x))
#endif
#else
#define TO_SIZED_CHAR(x) ((char)(x))
#endif
/** Handler function for text lines.
@param obj Object to modify while processing the character.
@param line Text line to process.
@param lineno Current line number.
@param erp Error report, may be NULL.
@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.
*/
static
int
dk4wxstt_line_handler(
void *obj,
#if DK4_SIZEOF_WXCHAR > 1
#if DK4_SIZEOF_WXCHAR > 2
dk4_c32_t *line,
#else
dk4_c16_t *line,
#endif
#else
char *line,
#endif
dk4_um_t lineno,
dk4_er_t *erp
)
{
dk4_wx_string_table_reader_t *reader;
wxChar *linecp;
int back = DK4_TSP_RES_ERROR;
$? "+ dk4wxstt_line_handler"
reader = (dk4_wx_string_table_reader_t *)obj;
if (wxT('#') != (wxChar)(*line)) { $? ". non-comment"
if (reader->curpos < reader->dptr->nstrings) { $? ". save line"
linecp = dk4strx_dup((wxChar *)line, NULL);
if (NULL != linecp) {
(reader->dptr->strings)[reader->curpos] = linecp;
reader->curpos += 1;
back = DK4_TSP_RES_OK;
while (wxT('\0') != (wxChar)(*linecp)) {
if (wxT('\\') == (wxChar)(*linecp)) {
if (wxT('r') == (wxChar)(linecp[1])) {
*linecp = TO_SIZED_CHAR(wxT('\r'));
dk4strx_cpy_to_left(
(wxChar *)(&(linecp[1])), (wxChar *)(&(linecp[2]))
);
} else {
if (wxT('n') == wxChar(linecp[1])) {
*linecp = TO_SIZED_CHAR(wxT('\n'));
dk4strx_cpy_to_left(
(wxChar *)(&(linecp[1])), (wxChar *)(&(linecp[2]))
);
} else {
if (wxT('t') == (wxChar)(linecp[1])) {
*linecp = TO_SIZED_CHAR(wxT('\t'));
dk4strx_cpy_to_left(
(wxChar *)(&(linecp[1])), (wxChar *)(&(linecp[2]))
);
} else {
dk4strx_cpy_to_left(
(wxChar *)(linecp), (wxChar *)(&(linecp[1]))
);
if (wxT('\0') == (wxChar)(*linecp)) { linecp--; }
}
}
}
} else {
if (wxT('\n') == (wxChar)(*linecp)) {
*linecp = TO_SIZED_CHAR(wxT('\0'));
linecp--;
} else {
if ((wxT('\r') == (wxChar)(*linecp))
&& (wxT('\n') == (wxChar)(linecp[1])))
{
*linecp = TO_SIZED_CHAR(wxT('\0'));
linecp--;
}
}
}
linecp++;
}
} else {
back = DK4_TSP_RES_FATAL;
}
} else { $? ". line out of range"
back = DK4_TSP_RES_OK;
}
} else {
back = DK4_TSP_RES_OK;
} $? "- dk4wxstt_line_handler %d", back
return back;
}
bool
dk4wxstt_apply_stream(
dk4_wx_string_table_t *tptr,
dk4_stream_t *strm,
int pre,
dk4_er_t *erp1,
dk4_er_t *erp2
)
{
wxChar line[1024]; /* Line buffer */
char buf[4096]; /* Input bytes from stream */
dk4_wx_string_table_reader_t reader; /* Reader structure */
dk4_tspwx_t tspwx; /* Text stream processor */
dk4_er_t e1; /* Error report decoding */
dk4_er_t e2; /* Error report processing */
size_t rdb; /* Number of bytes read */
int cc; /* Flag: Can continue */
bool back = false; /* Function result */
$? "+ dk4wxstt_apply_stream"
if ((NULL != tptr) && (NULL != strm)) { $? ". args ok"
dk4error_init(&e1);
dk4error_init(&e2);
reader.dptr = tptr;
reader.curpos = 0;
cc = dk4tspwx_setup_line(
&tspwx, (void *)(&reader), dk4wxstt_line_handler,
line, DK4_SIZEOF(line,wxChar),
pre, DK4_FILE_ENCODING_UTF8, &e2
); back = ((cc != 0) ? true : false);
if (back) { $? ". tsp setup ok"
#if VERSION_BEFORE_20150821
cc = 1;
#endif
do {
cc = 0;
rdb = sizeof(buf);
if (0 < dk4stream_read(buf, &rdb, strm, NULL)) {
if (0 < rdb) { $? ". read ok"
switch (dk4tspwx_add_bytes(&tspwx, (unsigned char *)buf, rdb)) {
case DK4_TSP_RES_OK: {
cc = 1; $? ". byte add ok"
} break;
case DK4_TSP_RES_ERROR: {
cc = 1; $? ". byte add error"
} break;
default: {
back = false; $? ". byte add fatal"
} break;
}
}
}
} while (0 != cc);
if (back) { $? ". finalize tsp"
if (DK4_TSP_RES_FATAL == dk4tspwx_finish(&tspwx)) {
back = false; $? "! finalize"
}
if (back) {
if (reader.curpos < (reader.dptr)->nstrings) {
back = false; $? "! too few"
}
}
}
} else { $? "! init"
}
} else {
dk4error_set_simple_error_code(erp2, DK4_E_INVALID_ARGUMENTS);
}
$? "- dk4wxstt_apply_stream %d", back
return back;
}
|