summaryrefslogtreecommitdiff
path: root/support/dktools/dk3paper.c
blob: e62f58e52c0b52fbbfaa806a11ae8b0f50c6be8a (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
/*
	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: dk3paper.ctr
*/

/*
Copyright (C) 2011-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 dk3paper.c The dk3paper module.
*/


#line 57 "dk3paper.ctr"

#include "dk3all.h"
#include "dk3paper.h"





#line 64 "dk3paper.ctr"



/**	Built-in named paper sizes, may be overwritten in configuration files.
*/
static dkChar const * const	dk3paper_default_sizes[] = {
dkT("A4\t\t=\t595\t842"),
dkT("Letter\t\t=\t612\t792"),
dkT("a4\t\t=\t595\t842\t56\t28\t14\t14"),
dkT("letter\t\t=\t612\t792\t56\t28\t14\t14"),
NULL


#line 76 "dk3paper.ctr"
};



/**	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)
{
  

#line 94 "dk3paper.ctr"
  if(nps->name) {
    dk3_release(nps->name);
  }
  dk3_delete(nps);
  

#line 99 "dk3paper.ctr"
}



/**	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;
 

#line 114 "dk3paper.ctr"
 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;
     }
   }
 } 

#line 126 "dk3paper.ctr"
 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;
  

#line 205 "dk3paper.ctr"
  (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;
	    }
	  }
	}
      }
    }
  } 

#line 227 "dk3paper.ctr"
  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. */
  

#line 253 "dk3paper.ctr"
  if((obj) && (il)) {	

#line 254 "dk3paper.ctr"
    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);
      }
    }
  } 

#line 338 "dk3paper.ctr"
  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;
  

#line 372 "dk3paper.ctr"
  if(app) {
    back = dk3_new_app(dk3_paper_size_collection_t,1,app);
    if(back) {					

#line 375 "dk3paper.ctr"
      back->app = app;
      back->s_sizes = NULL; back->i_sizes = NULL;
      back->s_sizes = dk3sto_open_app(app);
      if(back->s_sizes) {			

#line 379 "dk3paper.ctr"
        dk3sto_set_comp(back->s_sizes, dk3paper_nps_compare, 0);
        back->i_sizes = dk3sto_it_open(back->s_sizes);
	if(back->i_sizes) {			

#line 382 "dk3paper.ctr"
	  ok = 1;
	  /*
	  	Read built-in lines.
	  */
	  lfdptr = dk3paper_default_sizes;
	  while(*lfdptr) {	

#line 388 "dk3paper.ctr"
	    if(!dk3paper_const_line_handler((void *)back, *lfdptr)) {
	      ok = 0;		

#line 390 "dk3paper.ctr"
	    }
	    lfdptr++;
	  }
	  /*
	  	Read configuration files.
	  */
	  sr = dk3app_find_config_file(app, dk3paper_config_file_name, 0);
	  if(sr) {				

#line 398 "dk3paper.ctr"
	    dk3search_reset(sr);
	    se = dk3app_get_encoding(app);
	    de = dk3app_get_input_file_encoding(app);
	    while((fn = dk3search_next(sr)) != NULL) {
	      

#line 403 "dk3paper.ctr"
	      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 {				

#line 413 "dk3paper.ctr"
	  }
	} else {				

#line 415 "dk3paper.ctr"
	}
      } else {					

#line 417 "dk3paper.ctr"
      }
      if(!(ok)) {
        dk3paper_close(back); back = NULL;
      }
    } else {					

#line 422 "dk3paper.ctr"
    }
  } 

#line 424 "dk3paper.ctr"
  return back;
}



void
dk3paper_close(dk3_paper_size_collection_t *psc)
{
  dk3_named_paper_size_t	*nps;	/* Current size to release. */
  

#line 434 "dk3paper.ctr"
  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);
  } 

#line 452 "dk3paper.ctr"
}


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. */
  

#line 461 "dk3paper.ctr"
  if((psc) && (psn)) {
    nps = (dk3_named_paper_size_t *)dk3sto_it_find_like(
      psc->i_sizes, (void *)psn, 1
    );
    if(nps) {	

#line 466 "dk3paper.ctr"
      back = (dk3_paper_size_t const *)(&(nps->size));
    }
  } 

#line 469 "dk3paper.ctr"
  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;
}