summaryrefslogtreecommitdiff
path: root/support/dktools/dk4paper.c
blob: 0b29e51593feb8a81e1fc8814f4250847f4bd141 (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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/*
	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: dk4paper.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 dk4paper.c The dk4paper module.
*/


#line 288 "dk4paper.ctr"

#include "dk4mem.h"
#include "dk4strd.h"
#include "dk4paper.h"
#include "dk4maiddbl.h"
#include "dk4fs.h"
#include "dk4mpl.h"
#include "dk4tspdk.h"
#include "dk4strm.h"
#include "dk4strmr.h"
#include "dk4enc.h"
#include "dk4loc.h"
#include "dk4user.h"
#include "dk4inst.h"





#line 306 "dk4paper.ctr"



/**	Object passed as pointer to the line handler.
*/
typedef struct {
	dk4_paper_size_collection_t	*coll;	/**< Collection to modify. */
} reader_obj_t;



/**	Configuration files to search for.
*/
static const dkChar * const	conf_file_names[] = {
	dkT("dk4paper.conf"),
	dkT("dk3paper.conf")
};



/**	Default paper size names always defined.
*/
static const dkChar	*default_paper_sizes[] = {
/* 0 */
dkT("A4"),

/* 1 */
dkT("a4"),

/* 2 */
dkT("Letter"),

/* 3 */
dkT("letter"),

NULL


#line 335 "dk4paper.ctr"
};



void
dk4papersize_delete(dk4_paper_size_t *pps)
{
	

#line 343 "dk4paper.ctr"
	if (NULL != pps) {				

#line 344 "dk4paper.ctr"
		if (NULL != pps->name) {	

#line 345 "dk4paper.ctr"
			

#line 346 "dk4paper.ctr"
			dk4mem_free(pps->name);
			pps->name = NULL;
		}
		dk4mem_free(pps);
	}
#if TRACE_DEBUG
	else {							

#line 353 "dk4paper.ctr"
	}
#endif
	

#line 356 "dk4paper.ctr"
}



dk4_paper_size_t *
dk4papersize_new(const dkChar *name, dk4_er_t *erp)
{
	dk4_paper_size_t	*back	= NULL;
	

#line 365 "dk4paper.ctr"
	if (NULL != name) {
		back = dk4mem_new(dk4_paper_size_t,1,erp);
		if (NULL != back) {
			back->w = 0.0;
			back->h = 0.0;
			back->i = 0.0;
			back->o = 0.0;
			back->t = 0.0;
			back->b = 0.0;
			back->name = dk4str_dup(name, erp);
			if (NULL == back->name) {
				dk4mem_free(back);
				back = NULL;
			}
		}
	}
	else {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	

#line 385 "dk4paper.ctr"
	return back;
}




void
dk4papersize_coll_close(dk4_paper_size_collection_t *ptr)
{
	dk4_paper_size_t	*pps;
	

#line 396 "dk4paper.ctr"
	if (NULL != ptr) {					

#line 397 "dk4paper.ctr"
		if (NULL != ptr->s_ps) {		

#line 398 "dk4paper.ctr"
			if (NULL != ptr->i_ps) {	

#line 399 "dk4paper.ctr"
				dk4sto_it_reset(ptr->i_ps);
				do {
					pps = (dk4_paper_size_t *)dk4sto_it_next(ptr->i_ps);
					if (NULL != pps) {	

#line 403 "dk4paper.ctr"
						dk4papersize_delete(pps);
					}
				} while (NULL != pps);
				dk4sto_it_close(ptr->i_ps);
				ptr->i_ps = NULL;
			}
#if TRACE_DEBUG
			else {						

#line 411 "dk4paper.ctr"
			}
#endif
			dk4sto_close(ptr->s_ps);
			ptr->s_ps = NULL;
		}
#if TRACE_DEBUG
		else {							

#line 418 "dk4paper.ctr"
		}
#endif
		

#line 421 "dk4paper.ctr"
		dk4mem_free(ptr);
	}
#if TRACE_DEBUG
	else {								

#line 425 "dk4paper.ctr"
	}
#endif
	

#line 428 "dk4paper.ctr"
}




int
dk4papersize_cmp(const void *l, const void *r, int cr)
{
	const dk4_paper_size_t	*pl		= NULL;
	const dk4_paper_size_t	*pr		= NULL;
	const dkChar			*nm		= NULL;
	int						 back	= 0;

	if (NULL != l) {
		if (NULL != r) {
			pl = (const dk4_paper_size_t *)l;
			switch (cr) {
				case 1 : {
					nm = (const dkChar *)r;
					back = dk4str_cmp(pl->name, nm);
				} break;
				default : {
					pr = (const dk4_paper_size_t *)r;
					back = dk4str_cmp(pl->name, pr->name);
				} break;
			}
		}
		else {
			back = 1;
		}
	}
	else {
		if (NULL != r) {
			back = -1;
		}
	}
	return back;
}



static
int
dk4papersize_i_add_one_default_size(
	dk4_paper_size_collection_t	*pscptr,
	const dkChar				*name,
	double						 w,
	double						 h,
	double						 i,
	double						 o,
	double						 t,
	double						 b,
	dk4_er_t					*erp
)
{
	dk4_paper_size_t	*ptr;
	int					 back	= 0;
	

#line 486 "dk4paper.ctr"
	ptr = dk4papersize_new(name, erp);
	if (NULL != ptr) {			

#line 488 "dk4paper.ctr"
		ptr->w = w; ptr->h = h; ptr->i = i; ptr->o = o; ptr->t = t; ptr->b = b;
		

#line 490 "dk4paper.ctr"
		

#line 491 "dk4paper.ctr"
		

#line 492 "dk4paper.ctr"
		

#line 493 "dk4paper.ctr"
		

#line 494 "dk4paper.ctr"
		

#line 495 "dk4paper.ctr"
		if (0 != dk4sto_add(pscptr->s_ps, ptr, erp)) {
			back = 1;			

#line 497 "dk4paper.ctr"
		}
		else {					

#line 499 "dk4paper.ctr"
			dk4papersize_delete(ptr);
			ptr = NULL;
		}
	}
#if TRACE_DEBUG
	else {						

#line 505 "dk4paper.ctr"
	}
#endif
	

#line 508 "dk4paper.ctr"
	return back;
}



static
int
dk4papersize_i_add_default_sizes(
	dk4_paper_size_collection_t	*pscptr,
	dk4_er_t					*erp
)
{
	int					 back = 0;
	

#line 522 "dk4paper.ctr"
	back = dk4papersize_i_add_one_default_size(
		pscptr, default_paper_sizes[0], 595.0, 842.0, 0.0, 0.0, 0.0, 0.0, erp
	);
	if (0 == back) { goto finished; }

	back = dk4papersize_i_add_one_default_size(
		pscptr, default_paper_sizes[1], 595.0, 842.0, 56.0, 28.0, 14.0, 14.0, erp
	);
	if (0 == back) { goto finished; }

	back = dk4papersize_i_add_one_default_size(
		pscptr, default_paper_sizes[2], 612.0, 792.0, 0.0, 0.0, 0.0, 0.0, erp
	);
	if (0 == back) { goto finished; }

	back = dk4papersize_i_add_one_default_size(
		pscptr, default_paper_sizes[3], 612.0, 792.0, 56.0, 28.0, 14.0, 14.0, erp
	);
	if (0 == back) { goto finished; }

	back = 1;

	finished:
	

#line 546 "dk4paper.ctr"
	return back;
}




dk4_paper_size_collection_t *
dk4papersize_coll_open(dk4_er_t *erp)
{
	dk4_paper_size_collection_t	*back	= NULL;
	int							 ok		= 0;
	

#line 558 "dk4paper.ctr"
	back = dk4mem_new(dk4_paper_size_collection_t,1,erp);
	if (NULL != back) {
		back->i_ps = NULL;
		back->s_ps = dk4sto_open(erp);
		if (NULL != back->s_ps) {
			(void)dk4sto_set_comp(back->s_ps, dk4papersize_cmp, 0);
			back->i_ps = dk4sto_it_open(back->s_ps, erp);
			if (NULL != back->i_ps) {
				ok = dk4papersize_i_add_default_sizes(back, erp);
			}
		}
		if (0 == ok) {
			dk4papersize_coll_close(back);
			back = NULL;
		}
	} 

#line 574 "dk4paper.ctr"
	return back;
}




const dk4_paper_size_t *
dk4papersize_coll_find(
	dk4_paper_size_collection_t	*ptr,
	const dkChar				*name,
	dk4_er_t					*erp
)
{
	dk4_paper_size_t	*back	= NULL;
	

#line 589 "dk4paper.ctr"
	if ((NULL != ptr) && (NULL != name)) {
		back = (dk4_paper_size_t *)dk4sto_it_find_like(ptr->i_ps, name, 1);
		if (NULL == back) {
			dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
		}
	}
	else {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	} 

#line 598 "dk4paper.ctr"
	return ((const dk4_paper_size_t *)back);
}



double
dk4papersize_get_width(const dk4_paper_size_t *psptr)
{
	double		 back = -1.0;
	if (NULL != psptr) {
		back = psptr->w;
	}
	return back;
}



double
dk4papersize_get_height(const dk4_paper_size_t *psptr)
{
	double		 back = -1.0;
	if (NULL != psptr) {
		back = psptr->h;
	}
	return back;
}



double
dk4papersize_get_left_border(const dk4_paper_size_t *psptr)
{
	double		 back = -1.0;
	if (NULL != psptr) {
		back = psptr->i;
	}
	return back;
}



double
dk4papersize_get_right_border(const dk4_paper_size_t *psptr)
{
	double		 back = -1.0;
	if (NULL != psptr) {
		back = psptr->o;
	}
	return back;
}



double
dk4papersize_get_top_border(const dk4_paper_size_t *psptr)
{
	double		 back = -1.0;
	if (NULL != psptr) {
		back = psptr->t;
	}
	return back;
}



double
dk4papersize_get_bottom_border(const dk4_paper_size_t *psptr)
{
	double		 back = -1.0;
	if (NULL != psptr) {
		back = psptr->b;
	}
	return back;
}



const dkChar *
dk4papersize_get_name(const dk4_paper_size_t *psptr)
{
	const dkChar	*back	= NULL;
	if (NULL != psptr) {
		back = psptr->name;
	}
	return back;
}



size_t
dk4papersize_coll_size(dk4_paper_size_collection_t *ptr)
{
	size_t	back	= 0;
	if (NULL != ptr) {
		dk4sto_it_reset(ptr->i_ps);
		while (NULL != dk4sto_it_next(ptr->i_ps)) {
			back++;
		}
	}
	return back;
}



void
dk4papersize_coll_reset(dk4_paper_size_collection_t *ptr)
{
	if (NULL != ptr) {
		dk4sto_it_reset(ptr->i_ps);
	}
}



const dk4_paper_size_t *
dk4papersize_coll_next(dk4_paper_size_collection_t *ptr)
{
	dk4_paper_size_t	*back	= NULL;
	if (NULL != ptr) {
		back = (dk4_paper_size_t *)dk4sto_it_next(ptr->i_ps);
	}
	return ((const dk4_paper_size_t *)back);
}



const dk4_paper_size_t *
dk4papersize_coll_get(dk4_paper_size_collection_t *ptr, size_t n)
{
	dk4_paper_size_t	*back	= NULL;
	size_t				 i		= 0;

	if (NULL != ptr) {
		dk4sto_it_reset(ptr->i_ps);
		do {
			back = (dk4_paper_size_t *)dk4sto_it_next(ptr->i_ps);
		} while ((NULL != back) && (++i <= n));
	}
	return ((const dk4_paper_size_t *)back);
}



/**	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	*coll;		/* collection to complete */
	dk4_paper_size_t			*ps;		/* paper size to modify */
	reader_obj_t				*rdo;		/* reader object */
	const dkChar				*ep;		/* end pointer after double value */
	dkChar						*pls;		/* Line start */
	dkChar						*pval;		/* Start of value side */
	size_t						 ntok;		/* number of tokens found */
	size_t						 i;			/* traverse tokens */
	int							 back	= DK4_TSP_RES_OK;
	

#line 774 "dk4paper.ctr"
	/*	Retrieve pointers
	*/
	rdo = (reader_obj_t *)obj;
	coll = rdo->coll;
	/*
		Find start of line, ignore empty lines
	*/
	pls = dk4str_start(line, NULL);
	if (NULL == pls) {						

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

#line 789 "dk4paper.ctr"
		goto finished;
	}
	/*	Find value side
	*/
	pval = dk4str_chr(pls, dkT('='));
	if (NULL == pval) {
		/* ERROR: Syntax */
		dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
		back = DK4_TSP_RES_ERROR;
		goto finished;
	}
	/*	Check whether value side contains text
	*/
	*(pval++) = dkT('\0');
	pval = dk4str_start(pval, NULL);
	if (NULL == pval) {
		/* ERROR: Syntax */
		dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
		back = DK4_TSP_RES_ERROR;
		goto finished;
	}
	/*	Check name length
	*/
	dk4str_rtwh(pls, NULL);
	if (0 == dk4str_len(pls)) {
		/* ERROR: Syntax */
		dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
		back = DK4_TSP_RES_ERROR;
		goto finished;
	}
	/*	Tokenize string
	*/
	ntok = dk4str_tokenize(tok, 16, pval, NULL, NULL);
	if ((2 > ntok) || (6 < ntok)) {
		/* ERROR: Syntax */
		dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
		back = DK4_TSP_RES_ERROR;
		goto finished;
	}
	/*	Initialize values
	*/
	for (i = 0; i < 6; i++) { val[i] = 0.0; }
	/*
		Process tokens to values
	*/
	for (i = 0; i < ntok; i++) {
		ep = NULL;
		if (0 == dk4ma_input_dk_double(&(val[i]), tok[i], &ep, 1, NULL)) {
			/* ERROR: Syntax */
			dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
			back = DK4_TSP_RES_ERROR;
			goto finished;
		}
	}
	/*	Modify existing paper size or create a new one
	*/
	ps = (dk4_paper_size_t *)dk4sto_it_find_like(coll->i_ps, pls, 1);
	if (NULL == ps) {
		ps = dk4papersize_new(pls, NULL);
		if (NULL != ps) {
			if (0 == dk4sto_add(coll->s_ps, ps, NULL)) {
				/* ERROR: Memory */
				dk4error_set_simple_error_code(
					erp, DK4_E_MEMORY_ALLOCATION_FAILED
				);
				dk4papersize_delete(ps);
				ps = NULL;
				back = DK4_TSP_RES_FATAL;
			}
		}
		else {
			/* ERROR: Memory */
			dk4error_set_simple_error_code(erp, DK4_E_MEMORY_ALLOCATION_FAILED);
			back = DK4_TSP_RES_FATAL;
		}
	}
	if (NULL == ps) { goto finished; }
	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 871 "dk4paper.ctr"
	return back;
}




/**	Apply a file to a paper size collection.
	@param	coll	Collection to modify.
	@param	fn		Configuration file name.
	@param	enc		Encoding used in memory.
	@param	erp		Error report, may be NULL.
	@return	1 on success, 0 on errors.
*/
static
int
dk4paper_apply_file(
	dk4_paper_size_collection_t	*coll,
	const dkChar				*fn,
	int							 enc,
	dk4_er_t					*erp
)
{
	char			 ib[4096];
	dkChar			 il[1024];
	dk4_tspdk_t		 tsp;
	reader_obj_t	 rdo;
	dk4_stream_t	*istrm		= NULL;
	size_t			 rb			= 0;
	int				 cc			= 1;
	int				 res		= 0;
	int		 		 back		= 1;
	

#line 903 "dk4paper.ctr"
	/*	Open input stream
	*/
	istrm = dk4stream_open_file_reader(fn, erp);
	if (NULL == istrm) { goto finished; }
	/*
		Set up reader structure
	*/
	rdo.coll = coll;
	/*
		Set up text processing
	*/
	res = dk4tspdk_setup_line(
		&tsp, &rdo, dk4papersize_line_handler, il, DK4_SIZEOF(il,dkChar),
		enc,
		(
			(DK4_ENCODING_UTF8 == enc) ?
			(DK4_FILE_ENCODING_UTF8) : (DK4_FILE_ENCODING_ASCII)
		),
		erp
	);
	if (0 == res) { goto finished; }
	/*
		Process input file
	*/
	res = DK4_TSP_RES_OK;
	do {
		

#line 930 "dk4paper.ctr"
		cc = 0;
		rb = sizeof(ib);
		if (0 != dk4stream_read(ib, &rb, istrm, erp)) {	

#line 933 "dk4paper.ctr"
			if (0 < rb) {								

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

#line 936 "dk4paper.ctr"
					back = 0;
				}
				else {									

#line 939 "dk4paper.ctr"
					cc = 1;
				}
			}
#if TRACE_DEBUG
			else {										

#line 944 "dk4paper.ctr"
			}
#endif
		}
#if TRACE_DEBUG
		else {											

#line 949 "dk4paper.ctr"
		}
#endif
		

#line 952 "dk4paper.ctr"
	} while (1 == cc);
	/*
		Finalize text stream processor
	*/
	if (DK4_TSP_RES_FATAL != res) {
		if (DK4_TSP_RES_FATAL == dk4tspdk_finish(&tsp)) {	

#line 958 "dk4paper.ctr"
			back = 0;
		}
	}

	finished:
	if (NULL != istrm) { dk4stream_close(istrm, NULL); }
	

#line 965 "dk4paper.ctr"
	return back;
}



dk4_paper_size_collection_t *
dk4papersize_coll_for_application(
	const dkChar	*progname,
	const dkChar	*grpname,
	dk4_er_t		*erp
)
{
	dkChar						 fnb[DK4_MAX_PATH];		/* Construct filename */
	dkChar					 	 home[DK4_MAX_PATH];	/* User home dir */
	dkChar						 langbuf[16];			/* Language */
	dkChar						 regbuf[16];			/* Region */
	dk4_paper_size_collection_t	*back	= NULL;			/* Result */
	size_t						 i;						/* File search index */
	int							 enc;					/* Encoding */
	int							 res;					/* Operation result */
	int							 ok;					/* Flag: No error */
	

#line 987 "dk4paper.ctr"
	/*	Create default collection
	*/
	back = dk4papersize_coll_open(erp);
	if (NULL == back) {					

#line 991 "dk4paper.ctr"
		goto finished;
	}									

#line 993 "dk4paper.ctr"
	/*	Check program and program group name
	*/
	if ((NULL == progname) || (NULL == grpname)) {	

#line 996 "dk4paper.ctr"
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		goto finished;
	}									

#line 999 "dk4paper.ctr"
	/*	Find users home directory.
	*/
	if (0 == dk4user_get_homedir(home, DK4_SIZEOF(home,dkChar), 0, NULL)) {
		goto finished;
	}									

#line 1004 "dk4paper.ctr"
	/*	Find encoding
	*/
	res = dk4loc_get_settings(
		langbuf, DK4_SIZEOF(langbuf,dkChar), regbuf, DK4_SIZEOF(regbuf,dkChar),
		&enc, NULL
	);									

#line 1010 "dk4paper.ctr"
	if (0 == res) {
		enc = DK4_ENCODING_ASCII;
	}									

#line 1013 "dk4paper.ctr"
	/*	Apply configuration files from build-in directory names
	*/
	ok = 1;
	for (i = 0; i <= DK4_FS_CONF_MAX; i++) {
#if DK4_HAVE_COMPATDKTOOLS3
		res = dk4fs_config_compat_one(
			fnb, DK4_SIZEOF(fnb,dkChar), conf_file_names[1],
			dk4inst_get_directory(2), dk4inst_get_directory(1), home,
			progname, grpname, i, 1, 1, NULL
		);
		if (0 != res) {					

#line 1024 "dk4paper.ctr"
			if (0 == dk4paper_apply_file(back, fnb, enc, erp)) {
				ok = 0;					

#line 1026 "dk4paper.ctr"
			}
		}
#endif
		res = dk4fs_config_one(
			fnb, DK4_SIZEOF(fnb,dkChar), conf_file_names[0],
			dk4inst_get_directory(2), dk4inst_get_directory(1), home,
			progname, grpname, i, 1, NULL
		);
		if (0 != res) {					

#line 1035 "dk4paper.ctr"
			if (0 == dk4paper_apply_file(back, fnb, enc, erp)) {
				ok = 0;					

#line 1037 "dk4paper.ctr"
			}
		}
	}
	if (0 == ok) {						

#line 1041 "dk4paper.ctr"
		dk4papersize_coll_close(back);
		back = NULL;
	}

	finished:
	

#line 1047 "dk4paper.ctr"
	return back;
}



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