summaryrefslogtreecommitdiff
path: root/support/dktools/dk4papea.c
blob: c7c2c23f53401dad22a9516394d7b642cf87770e (plain)
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4papea.ctr
*/

/*
Copyright (C) 2016-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file dk4papea.c The dk4papea module.
*/


#line 57 "dk4papea.ctr"


#include "dk4papea.h"
#include "dk4strd.h"
#include "dk4mem.h"
#include "dk4maiddbl.h"
#include "dk4tspdk.h"
#include "dk4fs.h"
#include "dk4mpl.h"
#include "dk4strm.h"
#include "dk4strmr.h"





#line 72 "dk4papea.ctr"



/**	Object passed as pointer to the reader function
*/
typedef struct {
	dk4_paper_size_collection_t	*pscptr;
	dk4_app_t					*app;
} reader_obj_t;



/**	Names of files to read.
*/
static const dkChar * const conf_file_names[] = {
	dkT("dk4paper.conf"),
#if	DK4_HAVE_COMPATDKTOOLS3
	dkT("dk3paper.conf"),
#endif
	NULL
};



/**	Process one input line.
	@param	obj		Reader object.
	@param	line	Current line to process.
	@param	lineno	Line number.
	@param	erp		Error report, may be NULL.
	@return	
	DK4_TSP_RES_OK		if processing succeeded,
	DK4_TSP_RES_ERROR	on errors (can continue),
	DK4_TSP_RES_FATAL	on fatal errors (can not continue).

*/

static
int
dk4papersize_line_handler(
	void			*obj,
	dkChar			*line,
	dk4_um_t		 lineno,
	dk4_er_t		*erp
)
{
	double						 val[6];	/* w, h, i, o, t, b */
	dkChar						*tok[16];	/* tokens from the value side */
	dk4_paper_size_collection_t	*pscptr;	/* collection of paper sizes */
	dk4_paper_size_t			*ps;		/* paper size to modify */
	dkChar						*pls;		/* line start (size name) */
	dkChar						*pval;		/* value side of line */
	const dkChar				*ep;		/* end pointer after double value */
	dk4_app_t					*app;		/* application structure */
	reader_obj_t				*rdo;		/* reader object */
	size_t						 ntok;		/* number of tokens in value side */
	size_t						 i;			/* index for traversal */
	int							 back	= DK4_TSP_RES_OK;
	

#line 130 "dk4papea.ctr"
	/*	Retrieve components from obj pointer
	*/
	rdo = (reader_obj_t *)obj;
	pscptr = rdo->pscptr;
	app    = rdo->app;
	dk4app_set_log_source_line(app, lineno);
	/*
		Find start of line
	*/
	pls = dk4str_start(line, NULL);
	if (NULL == pls) {						

#line 141 "dk4papea.ctr"
		goto finished;
	}
	/*
		Ignore comment lines
	*/
	if (dkT('#') == *pls) {					

#line 147 "dk4papea.ctr"
		goto finished;
	}
	/*
		Line must by key = values
	*/
	pval = dk4str_chr(pls, dkT('='));
	if (NULL == pval) {						

#line 154 "dk4papea.ctr"
		/* ERROR: Syntax */
		dk4app_log_base1(app, DK4_LL_WARNING, 159);
		back = DK4_TSP_RES_ERROR;
		goto finished;
	}										

#line 159 "dk4papea.ctr"
	*(pval++) = dkT('\0');
	dk4str_rtwh(pls, NULL);
	if (0 == dk4str_len(pls)) {				

#line 162 "dk4papea.ctr"
		/* ERROR: Syntax */
		dk4app_log_base1(app, DK4_LL_WARNING, 159);
		back = DK4_TSP_RES_ERROR;
		goto finished;
	}										

#line 167 "dk4papea.ctr"
	pval = dk4str_start(pval, NULL);
	if (NULL == pval) {						

#line 169 "dk4papea.ctr"
		/* ERROR: Syntax */
		dk4app_log_base1(app, DK4_LL_WARNING, 159);
		back = DK4_TSP_RES_ERROR;
		goto finished;
	}										

#line 174 "dk4papea.ctr"
	/*
		Tokenize string
	*/
	ntok = dk4str_tokenize(tok, 16, pval, NULL, NULL);
	if ((2 > ntok) || (6 < ntok)) {			

#line 179 "dk4papea.ctr"
		/* ERROR: Syntax */
		dk4app_log_base1(app, DK4_LL_WARNING, 159);
		back = DK4_TSP_RES_ERROR;
		goto finished;
	}										

#line 184 "dk4papea.ctr"
	/*
		Initialize values
	*/
	for (i = 0; i < 6; i++) { val[i] = 0.0; }
	/*
		Process tokens to values
	*/
	for (i = 0; i < ntok; i++) {
		ep = NULL;							

#line 193 "dk4papea.ctr"
		if (0 == dk4ma_input_dk_double(&(val[i]), tok[i], &ep, 1, NULL)) {
			/* ERROR: Syntax */
			dk4app_log_base1(app, DK4_LL_WARNING, 159);
			back = DK4_TSP_RES_ERROR;		

#line 197 "dk4papea.ctr"
			goto finished;
		}
	}
	/*
		Modify existing or create new papersize
	*/
	ps = (dk4_paper_size_t *)dk4sto_it_find_like(pscptr->i_ps, pls, 1);
	if (NULL == ps) {						

#line 205 "dk4papea.ctr"
		ps = dk4papersize_new(pls, NULL);
		if (NULL != ps) {					

#line 207 "dk4papea.ctr"
			if (0 == dk4sto_add(pscptr->s_ps, ps, NULL)) {
				dk4papersize_delete(ps);	

#line 209 "dk4papea.ctr"
				ps = NULL;
			}
		}
#if TRACE_DEBUG
		else {								

#line 214 "dk4papea.ctr"
		}
#endif
	}
	if (NULL == ps) {						

#line 218 "dk4papea.ctr"
		/* ERROR: Memory */
		dk4app_log_base1(app, DK4_LL_WARNING, 90);
		back = DK4_TSP_RES_ERROR;
		goto finished;
	}										

#line 223 "dk4papea.ctr"
	ps->w = val[0]; ps->h = val[1]; ps->i = val[2];
	ps->o = val[3]; ps->t = val[4]; ps->b = val[5];

	finished:
	

#line 228 "dk4papea.ctr"
	return back;
}



/**	Apply contents of one configuration file to paper size collection.
	@param	pscptr	Paper size collection to modify.
	@param	fn		File name.
	@param	app		Application structure.
*/
static
void
dk4app_apply_paper_size_file(
	dk4_paper_size_collection_t	*pscptr,
	const dkChar				*fn,
	dk4_app_t					*app
)
{
	char			 ib[4096];						/* Byte buffer */
	dkChar			 il[1024];						/* Text line buffer */
	dk4_tspdk_t		 tsp;							/* Text stream processor */
	dk4_er_t		 er;							/* Error report */
	reader_obj_t	 rdo;							/* Passed to text proc */
	const dkChar	*oldsourcefile	= NULL;			/* Saved source file name */
	dk4_stream_t	*istrm			= NULL;			/* Input stream */
	dk4_um_t		 oldsourceline	= (dk4_um_t)0UL;	/* Saved source line */
	size_t			 rb				= 0;			/* Number of bytes read */
	int				 cc				= 1;			/* Flag: Can continue */
	int				 res			= 0;			/* Operation result */
	

#line 258 "dk4papea.ctr"
	/*	Save current file name and file line for logging
	*/
	oldsourcefile = dk4app_get_log_source_file(app);
	oldsourceline = dk4app_get_log_source_line(app);
	/*
		We will process the specified input file, so set file name for logging
	*/
	dk4app_set_log_source_file(app, fn);
	dk4app_set_log_source_line(app, (dk4_um_t)0UL);
	/*
		Open stream for read access
	*/
	dk4error_init(&er);
	istrm = dk4stream_open_file_reader(fn, &er);
	if (NULL == istrm) {						

#line 273 "dk4papea.ctr"
		goto finished;
	}											

#line 275 "dk4papea.ctr"
	/*
		Set up reader object
	*/
	rdo.pscptr = pscptr;
	rdo.app = app;
	/*
		Set up text processor
	*/
	res = dk4tspdk_setup_line(
		&tsp, &rdo, dk4papersize_line_handler, il, DK4_SIZEOF(il,dkChar),
		app->encoding, app->enc_in_file, &er
	);
	if (0 == res) {								

#line 288 "dk4papea.ctr"
		goto finished;
	}											

#line 290 "dk4papea.ctr"
	/*
		Read and process contents from stream
	*/
	res = DK4_TSP_RES_OK;
	do {										

#line 295 "dk4papea.ctr"
		cc = 0;
		rb = sizeof(ib);
		if (0 != dk4stream_read(ib, &rb, istrm, &er)) {	

#line 298 "dk4papea.ctr"
			if (0 < rb) {						

#line 299 "dk4papea.ctr"
				res = dk4tspdk_add_bytes(&tsp, (const unsigned char *)ib, rb);
				if (DK4_TSP_RES_FATAL != res) {	

#line 301 "dk4papea.ctr"
					cc = 1;
				}
			}
#if TRACE_DEBUG
			else {								

#line 306 "dk4papea.ctr"
			}
#endif
		}
#if	TRACE_DEBUG
		else {									

#line 311 "dk4papea.ctr"
		}
#endif
		

#line 314 "dk4papea.ctr"
	} while (1 == cc);
	if (DK4_TSP_RES_FATAL != res) {
		dk4tspdk_finish(&tsp);
	}

	finished:
	/*
		Close input stream
	*/
	if (NULL != istrm) { dk4stream_close(istrm, NULL); }
	/*
		Restore file name and file line for logging
	*/
	dk4app_set_log_source_file(app, oldsourcefile);
	dk4app_set_log_source_line(app, oldsourceline);
	

#line 330 "dk4papea.ctr"
}



dk4_paper_size_collection_t *
dk4app_get_paper_sizes(dk4_app_t *app)
{
	dkChar						 fnb[DK4_MAX_PATH];	/* File name buffer */
	dk4_er_t					 er;				/* Error result */
	const dkChar				*msg[3];
	dk4_paper_size_collection_t	*back	= NULL;		/* Function result */
	const size_t				 szfnb	= DK4_SIZEOF(fnb,dkChar);	/* Size */
	int							 res;				/* Operation result */
	int							 i;					/* Index for traversal */
	

#line 345 "dk4papea.ctr"
	/*	Check app pointer
	*/
	if (NULL == app) {							

#line 348 "dk4papea.ctr"
		goto finished;
	}
	/*	Allocate new size collection with default sizes
	*/
	back = dk4papersize_coll_open(NULL);
	if (NULL == back) {							

#line 354 "dk4papea.ctr"
		/* ERROR: Memory */
		dk4app_log_base1(app, DK4_LL_WARNING, 90);
		goto finished;
	}
	/*	Find all configuration files and apply contents.
	*/
	if (0 != dk4app_log_do(app, DK4_LL_DEBUG)) {
		if ((NULL != app->msg_debug) && (36 < app->sz_msg_debug)) {
			msg[0] = (app->msg_debug)[36];
			dk4app_log_msg(app, DK4_LL_DEBUG, msg, 1);
		}
	}
	for (i = 0; i <= DK4_FS_CONF_MAX; i++) {	

#line 367 "dk4papea.ctr"
#if DK4_HAVE_COMPATDKTOOLS3
#if VERSION_BEFORE_2017_03_09
		dk4error_init(&er);
		res = dk4fs_config_compat_one(
			fnb, szfnb, conf_file_names[1],
			app->dir_share, app->dir_etc, app->user_home,
			app->prog_name, app->group_name, i, 1, 1, &er
	    );
		if (0 != res) {							

#line 376 "dk4papea.ctr"
			dk4app_apply_paper_size_file(back, fnb, app);
		}
#else
		dk4error_init(&er);
		res = dk4fs_config_construct_compat_one(
			fnb, szfnb, conf_file_names[1],
			app->dir_share, app->dir_etc, app->user_home,
			app->prog_name, app->group_name, i, 1, &er
		);
		if (0 != res) {
			res = dk4fs_attempt_file_name(fnb, szfnb, 1, &er);
			if (0 != res) {
				if (0 != dk4app_log_do(app, DK4_LL_DEBUG)) {
					if ((NULL != app->msg_debug) && (30 < app->sz_msg_debug)) {
						msg[0] = (app->msg_debug)[30];
						msg[1] = fnb;
						dk4app_log_msg(app, DK4_LL_DEBUG, msg, 2);
					}
				}
				dk4app_apply_paper_size_file(back, fnb, app);
			} else {
				if (0 != dk4app_log_do(app, DK4_LL_DEBUG)) {
					if ((NULL != app->msg_debug) && (31 < app->sz_msg_debug)) {
						msg[0] = (app->msg_debug)[31];
						msg[1] = fnb;
						dk4app_log_msg(app, DK4_LL_DEBUG, msg, 2);
					}
				}
			}
		}
#endif
#endif

#if VERSION_BEFORE_2017_03_09
		dk4error_init(&er);
		res = dk4fs_config_one(
			fnb, szfnb, conf_file_names[0],
			app->dir_share, app->dir_etc, app->user_home,
			app->prog_name, app->group_name, i, 1, &er
		);
		if (0 != res) {							

#line 417 "dk4papea.ctr"
			dk4app_apply_paper_size_file(back, fnb, app);
		}
#else
		dk4error_init(&er);
		res = dk4fs_config_construct_compat_one(
			fnb, szfnb, conf_file_names[0],
			app->dir_share, app->dir_etc, app->user_home,
			app->prog_name, app->group_name, i, 0, &er
		);
		if (0 != res) {
			res = dk4fs_attempt_file_name(fnb, szfnb, 1, &er);
			if (0 != res) {
				if (0 != dk4app_log_do(app, DK4_LL_DEBUG)) {
					if ((NULL != app->msg_debug) && (30 < app->sz_msg_debug)) {
						msg[0] = (app->msg_debug)[30];
						msg[1] = fnb;
						dk4app_log_msg(app, DK4_LL_DEBUG, msg, 2);
					}
				}
				dk4app_apply_paper_size_file(back, fnb, app);
			} else {
				if (0 != dk4app_log_do(app, DK4_LL_DEBUG)) {
					if ((NULL != app->msg_debug) && (31 < app->sz_msg_debug)) {
						msg[0] = (app->msg_debug)[31];
						msg[1] = fnb;
						dk4app_log_msg(app, DK4_LL_DEBUG, msg, 2);
					}
				}
			}
		} else {
		}
#endif
	}

	finished:
	

#line 453 "dk4papea.ctr"
	return back;
}



dk4_paper_size_t *
dk4app_get_paper_size(dk4_app_t *app, const dkChar *psname)
{
	dk4_paper_size_collection_t	*coll	= NULL;	/* Paper sizes collection */
	dk4_paper_size_t			*back	= NULL;	/* Function result */
	dk4_paper_size_t			*tmp	= NULL;	/* Temporary found paper size */
	

#line 465 "dk4papea.ctr"
	/*
		Check arguments
	*/
	if ((NULL == app) || (NULL == psname)) {
		goto finished;
	}
	/*
		Retrieve paper size collection
	*/
	coll = dk4app_get_paper_sizes(app);
	if (NULL == coll) {
		goto finished;
	}
	tmp = (dk4_paper_size_t *)dk4sto_it_find_like(coll->i_ps, psname, 1);
	if (NULL == tmp) {
		goto finished;
	}
	back = dk4mem_new(dk4_paper_size_t,1,NULL);
	if (NULL == back) {
		goto finished;
	}
	back->name = NULL;
	back->w = tmp->w;
	back->h = tmp->h;
	back->i = tmp->i;
	back->o = tmp->o;
	back->t = tmp->t;
	back->b = tmp->b;

	finished:
	if (NULL != coll) {
		dk4papersize_coll_close(coll);
	}
	

#line 499 "dk4papea.ctr"
	return back;
}




/* vim: set ai sw=4 ts=4 : */