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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
%% options
copyright owner = Dirk Krause
copyright year = 2011-xxxx
license = bsd
%% header
#ifdef __cplusplus
extern "C" {
#endif
/** Create paper sizes collection, read dk3paper.conf files.
@param app Application structure for diagnostics and file search,
must not be NULL.
@return Pointer to new collection on success, NULL on error.
*/
dk3_paper_size_collection_t *
dk3paper_open_app(dk3_app_t *app);
/** Find paper size by name.
@param psc Paper size collection.
@param psn Paper size name.
@return Pointer to paper size on success, NULL on error.
*/
dk3_paper_size_t const *
dk3paper_find(dk3_paper_size_collection_t *psc, dkChar const *psn);
/** Close paper size collection, release memory.
@param psc Collection to close.
*/
void
dk3paper_close(dk3_paper_size_collection_t *psc);
/** Prepare traversing the paper size collection for named paper sizes.
@param psc Paper size collection.
*/
void
dk3paper_reset(dk3_paper_size_collection_t *psc);
/** Retrieve next named paper size from size collection.
@param psc Paper size collection.
@return Pointer to named paper size on success, NULL on error/end.
*/
dk3_named_paper_size_t const *
dk3paper_next(dk3_paper_size_collection_t *psc);
#ifdef __cplusplus
}
#endif
%% module
#include "dk3all.h"
#include "dk3paper.h"
$!trace-include
/** Built-in named paper sizes, may be overwritten in configuration files.
*/
static dkChar const * const dk3paper_default_sizes[] = {
$!text macro=dkT
A4 = 595 842
Letter = 612 792
a4 = 595 842 56 28 14 14
letter = 612 792 56 28 14 14
$!end
};
/** File name for configuration file.
*/
static dkChar const dk3paper_config_file_name[] = { dkT("dk3paper.conf") };
/** Delete named paper size, release memory.
@param nps Data to delete.
*/
static
void
dk3paper_nps_delete(dk3_named_paper_size_t *nps)
{
$? "+ dk3paper_nps_delete \"%!ds\"", TR_STR(nps->name)
if(nps->name) {
dk3_release(nps->name);
}
dk3_delete(nps);
$? "- dk3paper_nps_delete"
}
/** Create named paper size structure, allocate memory.
@param app Application structure for diagnostics.
@param name Paper size name.
@return Pointer to new paper size on success, NULL on error.
*/
static
dk3_named_paper_size_t *
dk3paper_nps_new(dk3_app_t *app, dkChar const *name)
{
dk3_named_paper_size_t *back = NULL;
$? "+ dk3paper_nps_new"
if((app) && (name)) {
back = dk3_new_app(dk3_named_paper_size_t,1,app);
if(back) {
back->name = NULL;
(back->size).w = (back->size).h = 0.0;
(back->size).i = (back->size).o = (back->size).t = (back->size).b = 0.0;
back->name = dk3str_dup_app(name, app);
if(!(back->name)) {
dk3paper_nps_delete(back); back = NULL;
}
}
} $? "- dk3paper_nps_new %s", TR_PTR(back)
return back;
}
/** Compare two named paper sizes by name.
@param l Left structure.
@param r Right structure.
@param cr Comparison criteria (0=size/size, 1=size/name).
@return Comparison result.
*/
static
int
dk3paper_nps_compare(void const *l, void const *r, int cr)
{
int back = 0;
dk3_named_paper_size_t const *pl; /* Pointer to left structure. */
dk3_named_paper_size_t const *pr; /* Pointer to right structure. */
if(l) {
if(r) {
pl = (dk3_named_paper_size_t const *)l;
switch(cr) {
case 1: {
/*
Compare structure against a name string.
*/
if(pl->name) {
back = dk3str_cmp(pl->name, (dkChar const *)r);
} else {
back = -1;
}
} break;
default: {
/*
Compare two structures by name.
*/
pr = (dk3_named_paper_size_t const *)r;
if(pl->name) {
if(pr->name) {
back = dk3str_cmp(pl->name, pr->name);
} else {
back = 1;
}
} else {
if(pr->name) {
back = -1;
}
}
} break;
}
} else {
back = 1;
}
} else {
if(r) {
back = -1;
}
}
if(back < -1) back = -1;
if(back > 1) back = 1;
return back;
}
/** Set values in a named paper size.
@param np Named paper size.
@param v Values array.
@param num Number of values (2 or 6).
Width and height are mandatory, borders are
optional.
@return 1 on success, 0 on error.
*/
static
int
dk3paper_set_values(dk3_named_paper_size_t *np, double const *v, size_t num)
{
int back = 0;
$? "+ dk3paper_set_values"
(np->size).i = (np->size).o = (np->size).t = (np->size).b = 0.0;
(np->size).w = v[0]; (np->size).h = v[1];
if(num > 2) {
(np->size).i = v[2]; (np->size).o = v[3];
(np->size).t = v[4]; (np->size).b = v[5];
}
/*
Indicate success only if all values are reasonable.
*/
if((np->size).w > 0.0) {
if((np->size).h > 0.0) {
if((np->size).i >= 0.0) {
if((np->size).o >= 0.0) {
if((np->size).t >= 0.0) {
if((np->size).b >= 0.0) {
back = 1;
}
}
}
}
}
} $? "- dk3paper_set_values %d", back
return back;
}
/** Handler function for one configuration file line.
@param obj Paper size collection.
@param il Input line to process.
@return 1 for OK, 0 for recoverable error, -1 for abort.
*/
static
int
dk3paper_const_line_handler(void *obj, dkChar const *il)
{
dkChar bu[DK3_PAPER_CONFIG_LINE_SIZE]; /* Private copy. */
dkChar *parts[16]; /* Split number sequence. */
double v[8]; /* Values from text line. */
dk3_paper_size_collection_t *psc; /* Paper size collection. */
dk3_named_paper_size_t *np; /* Paper size to edit. */
dkChar *p1; /* Paper size name. */
dkChar *p2; /* Values for that size. */
size_t nparts; /* Number of values. */
size_t i; /* Traverse all values. */
int back = 1;
int res = 0; /* Flag: No reporting necessary. */
$? "+ dk3paper_const_line_handler"
if((obj) && (il)) { $? ". line=\"%!ds\"", il
psc = (dk3_paper_size_collection_t *)obj;
if(dk3str_len(il) < DK3_SIZEOF(bu,dkChar)) {
dk3str_cpy_not_overlapped(bu, il);
p1 = dk3str_start(bu, NULL);
if(p1) {
if(*p1 != dkT('#')) {
p2 = dk3str_chr(p1, dkT('='));
if(p2) {
*(p2++) = dkT('\0');
p2 = dk3str_start(p2, NULL);
if(p2) {
dk3str_chomp(p1, NULL);
nparts = dk3str_explode(parts, 15, p2, NULL);
if((nparts == 2) || (nparts == 6)) {
/*
Line syntax ok so far.
*/
res = 1;
/*
Read double value from all text words.
*/
for(i = 0; i < nparts; i++) {
#if VERSION_BEFORE_20140716
if(dk3sf_sscanf3(parts[i], dkT("%lg"), &(v[i])) != 1)
#else
/* Bugfix 2014-09-01: If the action fails, the
res variable is set to failure, not on success.
*/
#if VERSION_BEFORE_20140901
/* WRONG*/ if (dk3ma_d_from_string(&(v[i]), parts[i], NULL))
#else
/* OK */ if (0 == dk3ma_d_from_string(&(v[i]), parts[i], NULL))
#endif
#endif
{
res = 0;
}
}
if(res) {
if(dk3str_len(p1) > 0) {
/*
Attempt to find existing size for that name.
*/
np = (dk3_named_paper_size_t *)dk3sto_it_find_like(
psc->i_sizes, (void *)p1, 1
);
if(np) {
/*
Entry exists, reconfigure entry.
*/
res = dk3paper_set_values(np, v, nparts);
} else {
/*
Create new entry.
*/
np = dk3paper_nps_new(psc->app, p1);
if(np) {
if(dk3sto_add(psc->s_sizes, (void *)np)) {
res = dk3paper_set_values(np, v, nparts);
} else {
/* ERROR: Memory */
dk3paper_nps_delete(np); np = NULL;
back = 0;
}
} else {
/* ERROR: Memory */
back = 0;
}
}
} else { res = 0; }
}
}
}
}
} else { res = 1; }
} else { res = 1; }
if(!(res)) {
/* ERROR: Syntax */
dk3str_cpy_not_overlapped(bu, il);
dk3str_normalize(bu, NULL, dkT(' '));
dk3app_log_i3(psc->app, DK3_LL_ERROR, 211, 212, bu);
}
}
} $? "- dk3paper_const_line_handler %d", back
return back;
}
/** Handler function for one configuration file line.
@param obj Paper size collection.
@param il Input line to process.
@return 1 for OK, 0 for recoverable error, -1 for abort.
*/
static
int
dk3paper_line_handler(void *obj, dkChar *il)
{
int back;
back = dk3paper_const_line_handler(obj, il);
return back;
}
dk3_paper_size_collection_t *
dk3paper_open_app(dk3_app_t *app)
{
dkChar bu[DK3_PAPER_CONFIG_LINE_SIZE]; /* Buffer. */
dk3_paper_size_collection_t *back = NULL;
dk3_search_t *sr;
dkChar const *fn;
dkChar const * const *lfdptr;
int ok = 0;
int res;
int de;
int se;
$? "+ dk3paper_open_app"
if(app) {
back = dk3_new_app(dk3_paper_size_collection_t,1,app);
if(back) { $? ". allocation ok"
back->app = app;
back->s_sizes = NULL; back->i_sizes = NULL;
back->s_sizes = dk3sto_open_app(app);
if(back->s_sizes) { $? ". storage ok"
dk3sto_set_comp(back->s_sizes, dk3paper_nps_compare, 0);
back->i_sizes = dk3sto_it_open(back->s_sizes);
if(back->i_sizes) { $? ". iterator ok"
ok = 1;
/*
Read built-in lines.
*/
lfdptr = dk3paper_default_sizes;
while(*lfdptr) { $? ". processing buildin line \"%!ds\"", *lfdptr
if(!dk3paper_const_line_handler((void *)back, *lfdptr)) {
ok = 0; $? "! failed"
}
lfdptr++;
}
/*
Read configuration files.
*/
sr = dk3app_find_config_file(app, dk3paper_config_file_name, 0);
if(sr) { $? ". search results available"
dk3search_reset(sr);
se = dk3app_get_encoding(app);
de = dk3app_get_input_file_encoding(app);
while((fn = dk3search_next(sr)) != NULL) {
$? ". processing file \"%!ds\"", fn
res = dk3stream_process_filename_lines_app(
(void *)back, dk3paper_line_handler, fn,
bu, DK3_SIZEOF(bu,dkChar), se, de, app
);
if(!(res)) {
ok = 0;
}
}
dk3search_close(sr);
} else { $? "! no search results"
}
} else { $? "! iterator alloc failed"
}
} else { $? "! storage alloc failed"
}
if(!(ok)) {
dk3paper_close(back); back = NULL;
}
} else { $? "! allocation failed"
}
} $? "- dk3paper_open_app %!8s", TR_8PTR(back)
return back;
}
void
dk3paper_close(dk3_paper_size_collection_t *psc)
{
dk3_named_paper_size_t *nps; /* Current size to release. */
$? "+ dk3paper_close"
if(psc) {
if(psc->s_sizes) {
if(psc->i_sizes) {
dk3sto_it_reset(psc->i_sizes);
while((nps = (dk3_named_paper_size_t *)dk3sto_it_next(psc->i_sizes))
!= NULL
)
{
dk3paper_nps_delete(nps);
}
dk3sto_it_close(psc->i_sizes);
}
dk3sto_close(psc->s_sizes);
}
psc->s_sizes = NULL;
psc->app = NULL;
dk3_delete(psc);
} $? "- dk3paper_close"
}
dk3_paper_size_t const *
dk3paper_find(dk3_paper_size_collection_t *psc, dkChar const *psn)
{
dk3_paper_size_t const *back = NULL;
dk3_named_paper_size_t *nps; /* Named size. */
$? "+ dk3paper_find \"%!ds\"", psn
if((psc) && (psn)) {
nps = (dk3_named_paper_size_t *)dk3sto_it_find_like(
psc->i_sizes, (void *)psn, 1
);
if(nps) { $? ". nps ok"
back = (dk3_paper_size_t const *)(&(nps->size));
}
} $? "- dk3paper_find %!8s", TR_8PTR(back)
return back;
}
void
dk3paper_reset(dk3_paper_size_collection_t *psc)
{
if(psc) {
if(psc->i_sizes) {
dk3sto_it_reset(psc->i_sizes);
}
}
}
dk3_named_paper_size_t const *
dk3paper_next(dk3_paper_size_collection_t *psc)
{
dk3_named_paper_size_t const *back = NULL;
if(psc) {
if(psc->i_sizes) {
back = (dk3_named_paper_size_t const *)dk3sto_it_next(psc->i_sizes);
}
}
return back;
}
|