Debian Codegraph @

src/main/print-vars.c

raw symbols SCIP
1 /*
2 *
3 * Print plug-in driver utility functions for the GIMP.
4 *
5 * Copyright 1997-2000 Michael Sweet ([email protected]) and
6 * Robert Krawitz ([email protected])
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 /*
23 * This file must include only standard C header files. The core code must
24 * compile on generic platforms that don't support glib, gimp, gtk, etc.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <math.h>
31 #ifdef HAVE_LIMITS_H
32 #include <limits.h>
33 #endif
34 #include <string.h>
35 #include <gutenprint/gutenprint.h>
36 #include "gutenprint-internal.h"
37 #include <gutenprint/gutenprint-intl-internal.h>
38 #include "generic-options.h"
39
40 typedef struct
41 {
42 char *name;
43 stp_parameter_type_t typ;
44 stp_parameter_activity_t active;
45 union
46 {
47 int ival;
48 int bval;
49 double dval;
50 stp_dimension_t sval;
51 stp_curve_t *cval;
52 stp_array_t *aval;
53 stp_raw_t rval;
54 } value;
55 } value_t;
56
57 struct stp_compdata
58 {
59 char *name;
60 stp_copy_data_func_t copyfunc;
61 stp_free_data_func_t freefunc;
62 void *data;
63 };
64
65 struct stp_vars /* Plug-in variables */
66 {
67 char *driver; /* Name of printer "driver" */
68 char *color_conversion; /* Color module in use */
69 stp_dimension_t left; /* Offset from left-upper corner, points */
70 stp_dimension_t top; /* ... */
71 stp_dimension_t width; /* Width of the image, points */
72 stp_dimension_t height; /* ... */
73 stp_dimension_t page_width; /* Width of page in points */
74 stp_dimension_t page_height; /* Height of page in points */
75 stp_list_t *params[STP_PARAMETER_TYPE_INVALID];
76 stp_list_t *internal_data;
77 void (*outfunc)(void *data, const char *buffer, size_t bytes);
78 void *outdata;
79 void (*errfunc)(void *data, const char *buffer, size_t bytes);
80 void *errdata;
81 void (*dbgfunc)(void *data, const char *buffer, size_t bytes);
82 void *dbgdata;
83 int verified; /* Ensure that params are OK! */
84 };
85
86 static int standard_vars_initialized = 0;
87
88
89 void
90 stp_parameter_description_destroy(stp_parameter_t *desc)
91 {
92 switch (desc->p_type)
93 {
94 case STP_PARAMETER_TYPE_CURVE:
95 if (desc->bounds.curve)
96 stp_curve_destroy(desc->bounds.curve);
97 desc->bounds.curve = NULL;
98 break;
99 case STP_PARAMETER_TYPE_ARRAY:
100 if (desc->bounds.array)
101 stp_array_destroy(desc->bounds.array);
102 desc->bounds.array = NULL;
103 break;
104 case STP_PARAMETER_TYPE_STRING_LIST:
105 if (desc->bounds.str)
106 stp_string_list_destroy(desc->bounds.str);
107 desc->bounds.str = NULL;
108 break;
109 default:
110 break;
111 }
112 }
113 static stp_vars_t default_vars;
114
115 #define CHECK_VARS(v) STPI_ASSERT(v, NULL)
116
117 static const char *
118 value_namefunc(const void *item)
119 {
120 const value_t *v = (const value_t *) (item);
121 return v->name;
122 }
123
124 static void
125 value_freefunc(void *item)
126 {
127 value_t *v = (value_t *) (item);
128 switch (v->typ)
129 {
130 case STP_PARAMETER_TYPE_STRING_LIST:
131 case STP_PARAMETER_TYPE_FILE:
132 case STP_PARAMETER_TYPE_RAW:
133 stp_free(stpi_cast_safe(v->value.rval.data));
134 break;
135 case STP_PARAMETER_TYPE_CURVE:
136 if (v->value.cval)
137 stp_curve_destroy(v->value.cval);
138 break;
139 case STP_PARAMETER_TYPE_ARRAY:
140 stp_array_destroy(v->value.aval);
141 break;
142 default:
143 break;
144 }
145 stp_free(v->name);
146 stp_free(v);
147 }
148
149 static stp_list_t *
150 create_vars_list(void)
151 {
152 stp_list_t *ret = stp_list_create();
153 stp_list_set_freefunc(ret, value_freefunc);
154 stp_list_set_namefunc(ret, value_namefunc);
155 return ret;
156 }
157
158 static void
159 copy_to_raw(stp_raw_t *raw, const void *data, size_t bytes)
160 {
161 char *ndata = NULL;
162 if (data)
163 {
164 ndata = stp_malloc(bytes + 1);
165 memcpy(ndata, data, bytes);
166 ndata[bytes] = '\0';
167 }
168 else
169 bytes = 0;
170 raw->data = (void *) ndata;
171 raw->bytes = bytes;
172 }
173
174 static value_t *
175 value_copy(const void *item)
176 {
177 value_t *ret = stp_malloc(sizeof(value_t));
178 const value_t *v = (const value_t *) (item);
179 ret->name = stp_strdup(v->name);
180 ret->typ = v->typ;
181 ret->active = v->active;
182 switch (v->typ)
183 {
184 case STP_PARAMETER_TYPE_CURVE:
185 ret->value.cval = stp_curve_create_copy(v->value.cval);
186 break;
187 case STP_PARAMETER_TYPE_ARRAY:
188 ret->value.aval = stp_array_create_copy(v->value.aval);
189 break;
190 case STP_PARAMETER_TYPE_STRING_LIST:
191 case STP_PARAMETER_TYPE_FILE:
192 case STP_PARAMETER_TYPE_RAW:
193 copy_to_raw(&(ret->value.rval), v->value.rval.data, v->value.rval.bytes);
194 break;
195 case STP_PARAMETER_TYPE_DIMENSION:
196 ret->value.sval = v->value.sval;
197 break;
198 case STP_PARAMETER_TYPE_INT:
199 case STP_PARAMETER_TYPE_BOOLEAN:
200 ret->value.ival = v->value.ival;
201 break;
202 case STP_PARAMETER_TYPE_DOUBLE:
203 ret->value.dval = v->value.dval;
204 break;
205 default:
206 break;
207 }
208 return ret;
209 }
210
211 static stp_list_t *
212 copy_value_list(const stp_list_t *src)
213 {
214 stp_list_t *ret = create_vars_list();
215 const stp_list_item_t *item = stp_list_get_start((const stp_list_t *)src);
216 while (item)
217 {
218 stp_list_item_create(ret, NULL, value_copy(stp_list_item_get_data(item)));
219 item = stp_list_item_next(item);
220 }
221 return ret;
222 }
223
224 static const char *
225 compdata_namefunc(const void *item)
226 {
227 const compdata_t *cd = (const compdata_t *) (item);
228 return cd->name;
229 }
230
231 static void
232 compdata_freefunc(void *item)
233 {
234 compdata_t *cd = (compdata_t *) (item);
235 if (cd->freefunc)
236 (cd->freefunc)(cd->data);
237 stp_free(cd->name);
238 stp_free(cd);
239 }
240
241 static void *
242 compdata_copyfunc(const void *item)
243 {
244 const compdata_t *cd = (const compdata_t *) (item);
245 if (cd->copyfunc)
246 return (cd->copyfunc)(cd->data);
247 else
248 return cd->data;
249 }
250
251 void
252 stp_allocate_component_data(stp_vars_t *v,
253 const char *name,
254 stp_copy_data_func_t copyfunc,
255 stp_free_data_func_t freefunc,
256 void *data)
257 {
258 compdata_t *cd;
259 stp_list_item_t *item;
260 CHECK_VARS(v);
261 cd = stp_malloc(sizeof(compdata_t));
262 item = stp_list_get_item_by_name(v->internal_data, name);
263 if (item)
264 stp_list_item_destroy(v->internal_data, item);
265 cd->name = stp_strdup(name);
266 cd->copyfunc = copyfunc;
267 cd->freefunc = freefunc;
268 cd->data = data;
269 stp_list_item_create(v->internal_data, NULL, cd);
270 }
271
272 void
273 stp_destroy_component_data(stp_vars_t *v, const char *name)
274 {
275 stp_list_item_t *item;
276 CHECK_VARS(v);
277 item = stp_list_get_item_by_name(v->internal_data, name);
278 if (item)
279 stp_list_item_destroy(v->internal_data, item);
280 }
281
282 void *
283 stp_get_component_data(const stp_vars_t *v, const char *name)
284 {
285 stp_list_item_t *item;
286 CHECK_VARS(v);
287 item = stp_list_get_item_by_name(v->internal_data, name);
288 if (item)
289 return ((compdata_t *) stp_list_item_get_data(item))->data;
290 else
291 return NULL;
292 }
293
294 static stp_list_t *
295 create_compdata_list(void)
296 {
297 stp_list_t *ret = stp_list_create();
298 stp_list_set_freefunc(ret, compdata_freefunc);
299 stp_list_set_namefunc(ret, compdata_namefunc);
300 return ret;
301 }
302
303 static stp_list_t *
304 copy_compdata_list(const stp_list_t *src)
305 {
306 stp_list_t *ret = create_compdata_list();
307 const stp_list_item_t *item = stp_list_get_start(src);
308 while (item)
309 {
310 stp_list_item_create(ret, NULL, compdata_copyfunc(item));
311 item = stp_list_item_next(item);
312 }
313 return ret;
314 }
315
316 static void
317 initialize_standard_vars(void)
318 {
319 if (!standard_vars_initialized)
320 {
321 int i;
322 for (i = 0; i < STP_PARAMETER_TYPE_INVALID; i++)
323 default_vars.params[i] = create_vars_list();
324 default_vars.driver = stp_strdup("ps2");
325 default_vars.color_conversion = stp_strdup("traditional");
326 default_vars.internal_data = create_compdata_list();
327 standard_vars_initialized = 1;
328 }
329 }
330
331 const stp_vars_t *
332 stp_default_settings(void)
333 {
334 initialize_standard_vars();
335 return (stp_vars_t *) &default_vars;
336 }
337
338 stp_vars_t *
339 stp_vars_create(void)
340 {
341 int i;
342 stp_vars_t *retval = stp_zalloc(sizeof(stp_vars_t));
343 initialize_standard_vars();
344 for (i = 0; i < STP_PARAMETER_TYPE_INVALID; i++)
345 retval->params[i] = create_vars_list();
346 retval->internal_data = create_compdata_list();
347 stp_vars_copy(retval, (stp_vars_t *)&default_vars);
348 return (retval);
349 }
350
351 void
352 stp_vars_destroy(stp_vars_t *v)
353 {
354 int i;
355 CHECK_VARS(v);
356 for (i = 0; i < STP_PARAMETER_TYPE_INVALID; i++)
357 stp_list_destroy(v->params[i]);
358 stp_list_destroy(v->internal_data);
359 STP_SAFE_FREE(v->driver);
360 STP_SAFE_FREE(v->color_conversion);
361 stp_free(v);
362 }
363
364 #define DEF_STRING_FUNCS(s, pre) \
365 void \
366 pre##_set_##s(stp_vars_t *v, const char *val) \
367 { \
368 CHECK_VARS(v); \
369 if (val) \
370 stp_dprintf(STP_DBG_VARS, v, "set %s to %s (0x%p)\n", #s, val, \
371 (const void *) v); \
372 else \
373 stp_dprintf(STP_DBG_VARS, v, "clear %s (0x%p)\n", #s, \
374 (const void *) v); \
375 if (v->s == val) \
376 return; \
377 STP_SAFE_FREE(v->s); \
378 v->s = stp_strdup(val); \
379 v->verified = 0; \
380 } \
381 \
382 void \
383 pre##_set_##s##_n(stp_vars_t *v, const char *val, int n) \
384 { \
385 CHECK_VARS(v); \
386 if (v->s == val) \
387 return; \
388 STP_SAFE_FREE(v->s); \
389 v->s = stp_strndup(val, n); \
390 v->verified = 0; \
391 } \
392 \
393 const char * \
394 pre##_get_##s(const stp_vars_t *v) \
395 { \
396 CHECK_VARS(v); \
397 return v->s; \
398 }
399
400 #define DEF_FUNCS(s, t, pre) \
401 void \
402 pre##_set_##s(stp_vars_t *v, t val) \
403 { \
404 CHECK_VARS(v); \
405 v->verified = 0; \
406 v->s = val; \
407 } \
408 \
409 t \
410 pre##_get_##s(const stp_vars_t *v) \
411 { \
412 CHECK_VARS(v); \
413 return v->s; \
414 }
415
416 DEF_STRING_FUNCS(driver, stp)
417 DEF_STRING_FUNCS(color_conversion, stp)
418 DEF_FUNCS(left, stp_dimension_t, stp)
419 DEF_FUNCS(top, stp_dimension_t, stp)
420 DEF_FUNCS(width, stp_dimension_t, stp)
421 DEF_FUNCS(height, stp_dimension_t, stp)
422 DEF_FUNCS(page_width, stp_dimension_t, stp)
423 DEF_FUNCS(page_height, stp_dimension_t, stp)
424 DEF_FUNCS(outdata, void *, stp)
425 DEF_FUNCS(errdata, void *, stp)
426 DEF_FUNCS(dbgdata, void *, stp)
427 DEF_FUNCS(outfunc, stp_outfunc_t, stp)
428 DEF_FUNCS(errfunc, stp_outfunc_t, stp)
429 DEF_FUNCS(dbgfunc, stp_outfunc_t, stp)
430
431 void
432 stp_set_verified(stp_vars_t *v, int val)
433 {
434 CHECK_VARS(v);
435 v->verified = val;
436 }
437
438 int
439 stp_get_verified(const stp_vars_t *v)
440 {
441 CHECK_VARS(v);
442 return v->verified;
443 }
444
445 static void
446 set_default_raw_parameter(stp_list_t *list, const char *parameter,
447 const char *value, size_t bytes, int typ)
448 {
449 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
450 if (value && !item)
451 {
452 value_t *val = stp_malloc(sizeof(value_t));
453 val->name = stp_strdup(parameter);
454 val->typ = typ;
455 val->active = STP_PARAMETER_DEFAULTED;
456 stp_list_item_create(list, NULL, val);
457 copy_to_raw(&(val->value.rval), value, bytes);
458 }
459 }
460
461 static void
462 set_raw_parameter(stp_list_t *list, const char *parameter, const char *value,
463 size_t bytes, int typ)
464 {
465 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
466 if (value)
467 {
468 value_t *val;
469 if (item)
470 {
471 val = (value_t *) stp_list_item_get_data(item);
472 if (val->active == STP_PARAMETER_DEFAULTED)
473 val->active = STP_PARAMETER_ACTIVE;
474 stp_free(stpi_cast_safe(val->value.rval.data));
475 }
476 else
477 {
478 val = stp_malloc(sizeof(value_t));
479 val->name = stp_strdup(parameter);
480 val->typ = typ;
481 val->active = STP_PARAMETER_ACTIVE;
482 stp_list_item_create(list, NULL, val);
483 }
484 copy_to_raw(&(val->value.rval), value, bytes);
485 }
486 else if (item)
487 stp_list_item_destroy(list, item);
488 }
489
490 void
491 stp_set_string_parameter_n(stp_vars_t *v, const char *parameter,
492 const char *value, size_t bytes)
493 {
494 stp_list_t *list = v->params[STP_PARAMETER_TYPE_STRING_LIST];
495 if (value)
496 stp_dprintf(STP_DBG_VARS, v, "stp_set_string_parameter(0x%p, %s, %s)\n",
497 (const void *) v, parameter, value);
498 else
499 stp_dprintf(STP_DBG_VARS, v, "stp_set_string_parameter(0x%p, %s)\n",
500 (const void *) v, parameter);
501 set_raw_parameter(list, parameter, value, bytes,
502 STP_PARAMETER_TYPE_STRING_LIST);
503 stp_set_verified(v, 0);
504 }
505
506 void
507 stp_set_string_parameter(stp_vars_t *v, const char *parameter,
508 const char *value)
509 {
510 int byte_count = 0;
511 if (value)
512 byte_count = strlen(value);
513 stp_dprintf(STP_DBG_VARS, v, "stp_set_string_parameter(0x%p, %s, %s)\n",
514 (const void *) v, parameter, value ? value : "NULL");
515 stp_set_string_parameter_n(v, parameter, value, byte_count);
516 stp_set_verified(v, 0);
517 }
518
519 void
520 stp_set_default_string_parameter_n(stp_vars_t *v, const char *parameter,
521 const char *value, size_t bytes)
522 {
523 stp_list_t *list = v->params[STP_PARAMETER_TYPE_STRING_LIST];
524 stp_dprintf(STP_DBG_VARS, v, "stp_set_default_string_parameter(0x%p, %s, %s)\n",
525 (const void *) v, parameter, value ? value : "NULL");
526 set_default_raw_parameter(list, parameter, value, bytes,
527 STP_PARAMETER_TYPE_STRING_LIST);
528 stp_set_verified(v, 0);
529 }
530
531 void
532 stp_set_default_string_parameter(stp_vars_t *v, const char *parameter,
533 const char *value)
534 {
535 int byte_count = 0;
536 if (value)
537 byte_count = strlen(value);
538 stp_set_default_string_parameter_n(v, parameter, value, byte_count);
539 stp_set_verified(v, 0);
540 }
541
542 void
543 stp_clear_string_parameter(stp_vars_t *v, const char *parameter)
544 {
545 stp_set_string_parameter(v, parameter, NULL);
546 }
547
548 const char *
549 stp_get_string_parameter(const stp_vars_t *v, const char *parameter)
550 {
551 stp_list_t *list = v->params[STP_PARAMETER_TYPE_STRING_LIST];
552 value_t *val;
553 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
554 if (item)
555 {
556 val = (value_t *) stp_list_item_get_data(item);
557 return val->value.rval.data;
558 }
559 else
560 return NULL;
561 }
562
563 void
564 stp_set_raw_parameter(stp_vars_t *v, const char *parameter,
565 const void *value, size_t bytes)
566 {
567 stp_list_t *list = v->params[STP_PARAMETER_TYPE_RAW];
568 set_raw_parameter(list, parameter, value, bytes, STP_PARAMETER_TYPE_RAW);
569 stp_set_verified(v, 0);
570 }
571
572 void
573 stp_set_default_raw_parameter(stp_vars_t *v, const char *parameter,
574 const void *value, size_t bytes)
575 {
576 stp_list_t *list = v->params[STP_PARAMETER_TYPE_RAW];
577 set_default_raw_parameter(list, parameter, value, bytes,
578 STP_PARAMETER_TYPE_RAW);
579 stp_set_verified(v, 0);
580 }
581
582 void
583 stp_clear_raw_parameter(stp_vars_t *v, const char *parameter)
584 {
585 stp_set_raw_parameter(v, parameter, NULL, 0);
586 }
587
588 const stp_raw_t *
589 stp_get_raw_parameter(const stp_vars_t *v, const char *parameter)
590 {
591 const stp_list_t *list = v->params[STP_PARAMETER_TYPE_RAW];
592 const value_t *val;
593 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
594 if (item)
595 {
596 val = (const value_t *) stp_list_item_get_data(item);
597 return &(val->value.rval);
598 }
599 else
600 return NULL;
601 }
602
603 void
604 stp_set_file_parameter(stp_vars_t *v, const char *parameter,
605 const char *value)
606 {
607 stp_list_t *list = v->params[STP_PARAMETER_TYPE_FILE];
608 size_t byte_count = 0;
609 if (value)
610 byte_count = strlen(value);
611 stp_dprintf(STP_DBG_VARS, v, "stp_set_file_parameter(0x%p, %s, %s)\n",
612 (const void *) v, parameter, value ? value : "NULL");
613 set_raw_parameter(list, parameter, value, byte_count,
614 STP_PARAMETER_TYPE_FILE);
615 stp_set_verified(v, 0);
616 }
617
618 void
619 stp_set_file_parameter_n(stp_vars_t *v, const char *parameter,
620 const char *value, size_t byte_count)
621 {
622 stp_list_t *list = v->params[STP_PARAMETER_TYPE_FILE];
623 stp_dprintf(STP_DBG_VARS, v, "stp_set_file_parameter(0x%p, %s, %s)\n",
624 (const void *) v, parameter, value ? value : "NULL");
625 set_raw_parameter(list, parameter, value, byte_count,
626 STP_PARAMETER_TYPE_FILE);
627 stp_set_verified(v, 0);
628 }
629
630 void
631 stp_set_default_file_parameter(stp_vars_t *v, const char *parameter,
632 const char *value)
633 {
634 stp_list_t *list = v->params[STP_PARAMETER_TYPE_FILE];
635 size_t byte_count = 0;
636 if (value)
637 byte_count = strlen(value);
638 stp_dprintf(STP_DBG_VARS, v, "stp_set_default_file_parameter(0x%p, %s, %s)\n",
639 (const void *) v, parameter, value ? value : "NULL");
640 set_default_raw_parameter(list, parameter, value, byte_count,
641 STP_PARAMETER_TYPE_FILE);
642 stp_set_verified(v, 0);
643 }
644
645 void
646 stp_set_default_file_parameter_n(stp_vars_t *v, const char *parameter,
647 const char *value, size_t byte_count)
648 {
649 stp_list_t *list = v->params[STP_PARAMETER_TYPE_FILE];
650 stp_dprintf(STP_DBG_VARS, v, "stp_set_default_file_parameter(0x%p, %s, %s)\n",
651 (const void *) v, parameter, value ? value : "NULL");
652 set_default_raw_parameter(list, parameter, value, byte_count,
653 STP_PARAMETER_TYPE_FILE);
654 stp_set_verified(v, 0);
655 }
656
657 void
658 stp_clear_file_parameter(stp_vars_t *v, const char *parameter)
659 {
660 stp_set_file_parameter(v, parameter, NULL);
661 }
662
663 const char *
664 stp_get_file_parameter(const stp_vars_t *v, const char *parameter)
665 {
666 const stp_list_t *list = v->params[STP_PARAMETER_TYPE_FILE];
667 const value_t *val;
668 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
669 if (item)
670 {
671 val = (const value_t *) stp_list_item_get_data(item);
672 return val->value.rval.data;
673 }
674 else
675 return NULL;
676 }
677
678 void
679 stp_set_curve_parameter(stp_vars_t *v, const char *parameter,
680 const stp_curve_t *curve)
681 {
682 stp_list_t *list = v->params[STP_PARAMETER_TYPE_CURVE];
683 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
684 stp_dprintf(STP_DBG_VARS, v, "stp_set_curve_parameter(0x%p, %s)\n",
685 (const void *) v, parameter);
686 if (curve)
687 {
688 value_t *val;
689 if (item)
690 {
691 val = (value_t *) stp_list_item_get_data(item);
692 if (val->active == STP_PARAMETER_DEFAULTED)
693 val->active = STP_PARAMETER_ACTIVE;
694 if (val->value.cval)
695 stp_curve_destroy(val->value.cval);
696 }
697 else
698 {
699 val = stp_malloc(sizeof(value_t));
700 val->name = stp_strdup(parameter);
701 val->typ = STP_PARAMETER_TYPE_CURVE;
702 val->active = STP_PARAMETER_ACTIVE;
703 stp_list_item_create(list, NULL, val);
704 }
705 val->value.cval = stp_curve_create_copy(curve);
706 }
707 else if (item)
708 stp_list_item_destroy(list, item);
709 stp_set_verified(v, 0);
710 }
711
712 void
713 stp_set_default_curve_parameter(stp_vars_t *v, const char *parameter,
714 const stp_curve_t *curve)
715 {
716 stp_list_t *list = v->params[STP_PARAMETER_TYPE_CURVE];
717 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
718 stp_dprintf(STP_DBG_VARS, v, "stp_set_default_curve_parameter(0x%p, %s)\n",
719 (const void *) v, parameter);
720 if (!item)
721 {
722 if (curve)
723 {
724 value_t *val;
725 val = stp_malloc(sizeof(value_t));
726 val->name = stp_strdup(parameter);
727 val->typ = STP_PARAMETER_TYPE_CURVE;
728 val->active = STP_PARAMETER_DEFAULTED;
729 stp_list_item_create(list, NULL, val);
730 val->value.cval = stp_curve_create_copy(curve);
731 }
732 }
733 stp_set_verified(v, 0);
734 }
735
736 void
737 stp_clear_curve_parameter(stp_vars_t *v, const char *parameter)
738 {
739 stp_set_curve_parameter(v, parameter, NULL);
740 }
741
742 const stp_curve_t *
743 stp_get_curve_parameter(const stp_vars_t *v, const char *parameter)
744 {
745 const stp_list_t *list = v->params[STP_PARAMETER_TYPE_CURVE];
746 const value_t *val;
747 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
748 if (item)
749 {
750 val = (value_t *) stp_list_item_get_data(item);
751 return val->value.cval;
752 }
753 else
754 return NULL;
755 }
756
757 void
758 stp_set_array_parameter(stp_vars_t *v, const char *parameter,
759 const stp_array_t *array)
760 {
761 stp_list_t *list = v->params[STP_PARAMETER_TYPE_ARRAY];
762 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
763 stp_dprintf(STP_DBG_VARS, v, "stp_set_array_parameter(0x%p, %s)\n",
764 (const void *) v, parameter);
765 if (array)
766 {
767 value_t *val;
768 if (item)
769 {
770 val = (value_t *) stp_list_item_get_data(item);
771 if (val->active == STP_PARAMETER_DEFAULTED)
772 val->active = STP_PARAMETER_ACTIVE;
773 stp_array_destroy(val->value.aval);
774 }
775 else
776 {
777 val = stp_malloc(sizeof(value_t));
778 val->name = stp_strdup(parameter);
779 val->typ = STP_PARAMETER_TYPE_ARRAY;
780 val->active = STP_PARAMETER_ACTIVE;
781 stp_list_item_create(list, NULL, val);
782 }
783 val->value.aval = stp_array_create_copy(array);
784 }
785 else if (item)
786 stp_list_item_destroy(list, item);
787 stp_set_verified(v, 0);
788 }
789
790 void
791 stp_set_default_array_parameter(stp_vars_t *v, const char *parameter,
792 const stp_array_t *array)
793 {
794 stp_list_t *list = v->params[STP_PARAMETER_TYPE_ARRAY];
795 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
796 stp_dprintf(STP_DBG_VARS, v, "stp_set_default_array_parameter(0x%p, %s)\n",
797 (const void *) v, parameter);
798 if (!item)
799 {
800 if (array)
801 {
802 value_t *val;
803 val = stp_malloc(sizeof(value_t));
804 val->name = stp_strdup(parameter);
805 val->typ = STP_PARAMETER_TYPE_ARRAY;
806 val->active = STP_PARAMETER_DEFAULTED;
807 stp_list_item_create(list, NULL, val);
808 val->value.aval = stp_array_create_copy(array);
809 }
810 }
811 stp_set_verified(v, 0);
812 }
813
814 void
815 stp_clear_array_parameter(stp_vars_t *v, const char *parameter)
816 {
817 stp_set_array_parameter(v, parameter, NULL);
818 }
819
820 const stp_array_t *
821 stp_get_array_parameter(const stp_vars_t *v, const char *parameter)
822 {
823 const stp_list_t *list = v->params[STP_PARAMETER_TYPE_ARRAY];
824 const value_t *val;
825 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
826 if (item)
827 {
828 val = (const value_t *) stp_list_item_get_data(item);
829 return val->value.aval;
830 }
831 else
832 return NULL;
833 }
834
835 void
836 stp_set_int_parameter(stp_vars_t *v, const char *parameter, int ival)
837 {
838 stp_list_t *list = v->params[STP_PARAMETER_TYPE_INT];
839 value_t *val;
840 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
841 stp_dprintf(STP_DBG_VARS, v, "stp_set_int_parameter(0x%p, %s, %d)\n",
842 (const void *) v, parameter, ival);
843 if (item)
844 {
845 val = (value_t *) stp_list_item_get_data(item);
846 if (val->active == STP_PARAMETER_DEFAULTED)
847 val->active = STP_PARAMETER_ACTIVE;
848 }
849 else
850 {
851 val = stp_malloc(sizeof(value_t));
852 val->name = stp_strdup(parameter);
853 val->typ = STP_PARAMETER_TYPE_INT;
854 val->active = STP_PARAMETER_ACTIVE;
855 stp_list_item_create(list, NULL, val);
856 }
857 val->value.ival = ival;
858 stp_set_verified(v, 0);
859 }
860
861 void
862 stp_set_default_int_parameter(stp_vars_t *v, const char *parameter, int ival)
863 {
864 stp_list_t *list = v->params[STP_PARAMETER_TYPE_INT];
865 value_t *val;
866 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
867 stp_dprintf(STP_DBG_VARS, v, "stp_set_default_int_parameter(0x%p, %s, %d)\n",
868 (const void *) v, parameter, ival);
869 if (!item)
870 {
871 val = stp_malloc(sizeof(value_t));
872 val->name = stp_strdup(parameter);
873 val->typ = STP_PARAMETER_TYPE_INT;
874 val->active = STP_PARAMETER_DEFAULTED;
875 stp_list_item_create(list, NULL, val);
876 val->value.ival = ival;
877 }
878 stp_set_verified(v, 0);
879 }
880
881 void
882 stp_clear_int_parameter(stp_vars_t *v, const char *parameter)
883 {
884 stp_list_t *list = v->params[STP_PARAMETER_TYPE_INT];
885 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
886 stp_dprintf(STP_DBG_VARS, v, "stp_clear_int_parameter(0x%p, %s)\n",
887 (const void *) v, parameter);
888 if (item)
889 stp_list_item_destroy(list, item);
890 stp_set_verified(v, 0);
891 }
892
893 int
894 stp_get_int_parameter(const stp_vars_t *v, const char *parameter)
895 {
896 const stp_list_t *list = v->params[STP_PARAMETER_TYPE_INT];
897 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
898 if (item)
899 {
900 const value_t *val = (const value_t *) stp_list_item_get_data(item);
901 return val->value.ival;
902 }
903 else
904 {
905 stp_parameter_t desc;
906 stp_describe_parameter(v, parameter, &desc);
907 if (desc.p_type == STP_PARAMETER_TYPE_INT)
908 {
909 int intval = desc.deflt.integer;
910 stp_parameter_description_destroy(&desc);
911 return intval;
912 }
913 else
914 {
915 stp_parameter_description_destroy(&desc);
916 stp_eprintf
917 (v, "Gutenprint: Attempt to retrieve unset integer parameter %s\n",
918 parameter);
919 return 0;
920 }
921 }
922 }
923
924 void
925 stp_set_boolean_parameter(stp_vars_t *v, const char *parameter, int ival)
926 {
927 stp_list_t *list = v->params[STP_PARAMETER_TYPE_BOOLEAN];
928 value_t *val;
929 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
930 stp_dprintf(STP_DBG_VARS, v, "stp_set_boolean_parameter(0x%p, %s, %d)\n",
931 (const void *) v, parameter, ival);
932 if (item)
933 {
934 val = (value_t *) stp_list_item_get_data(item);
935 if (val->active == STP_PARAMETER_DEFAULTED)
936 val->active = STP_PARAMETER_ACTIVE;
937 }
938 else
939 {
940 val = stp_malloc(sizeof(value_t));
941 val->name = stp_strdup(parameter);
942 val->typ = STP_PARAMETER_TYPE_BOOLEAN;
943 val->active = STP_PARAMETER_ACTIVE;
944 stp_list_item_create(list, NULL, val);
945 }
946 if (ival)
947 val->value.ival = 1;
948 else
949 val->value.ival = 0;
950 stp_set_verified(v, 0);
951 }
952
953 void
954 stp_set_default_boolean_parameter(stp_vars_t *v, const char *parameter,
955 int ival)
956 {
957 stp_list_t *list = v->params[STP_PARAMETER_TYPE_BOOLEAN];
958 value_t *val;
959 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
960 stp_dprintf(STP_DBG_VARS, v, "stp_set_default_boolean_parameter(0x%p, %s, %d)\n",
961 (const void *) v, parameter, ival);
962 if (!item)
963 {
964 val = stp_malloc(sizeof(value_t));
965 val->name = stp_strdup(parameter);
966 val->typ = STP_PARAMETER_TYPE_BOOLEAN;
967 val->active = STP_PARAMETER_DEFAULTED;
968 stp_list_item_create(list, NULL, val);
969 if (ival)
970 val->value.ival = 1;
971 else
972 val->value.ival = 0;
973 }
974 stp_set_verified(v, 0);
975 }
976
977 void
978 stp_clear_boolean_parameter(stp_vars_t *v, const char *parameter)
979 {
980 stp_list_t *list = v->params[STP_PARAMETER_TYPE_BOOLEAN];
981 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
982 stp_dprintf(STP_DBG_VARS, v, "stp_clear_boolean_parameter(0x%p, %s)\n",
983 (const void *) v, parameter);
984 if (item)
985 stp_list_item_destroy(list, item);
986 stp_set_verified(v, 0);
987 }
988
989 int
990 stp_get_boolean_parameter(const stp_vars_t *v, const char *parameter)
991 {
992 const stp_list_t *list = v->params[STP_PARAMETER_TYPE_BOOLEAN];
993 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
994 if (item)
995 {
996 const value_t *val = (const value_t *) stp_list_item_get_data(item);
997 return val->value.ival;
998 }
999 else
1000 {
1001 stp_parameter_t desc;
1002 stp_describe_parameter(v, parameter, &desc);
1003 if (desc.p_type == STP_PARAMETER_TYPE_BOOLEAN)
1004 {
1005 int boolean = desc.deflt.boolean;
1006 stp_parameter_description_destroy(&desc);
1007 return boolean;
1008 }
1009 else
1010 {
1011 stp_parameter_description_destroy(&desc);
1012 stp_eprintf
1013 (v, "Gutenprint: Attempt to retrieve unset boolean parameter %s\n",
1014 parameter);
1015 return 0;
1016 }
1017 }
1018 }
1019
1020 void
1021 stp_set_dimension_parameter(stp_vars_t *v, const char *parameter, stp_dimension_t sval)
1022 {
1023 stp_list_t *list = v->params[STP_PARAMETER_TYPE_DIMENSION];
1024 value_t *val;
1025 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1026 stp_dprintf(STP_DBG_VARS, v, "stp_set_dimension_parameter(0x%p, %s, %f)\n",
1027 (const void *) v, parameter, sval);
1028 if (item)
1029 {
1030 val = (value_t *) stp_list_item_get_data(item);
1031 if (val->active == STP_PARAMETER_DEFAULTED)
1032 val->active = STP_PARAMETER_ACTIVE;
1033 }
1034 else
1035 {
1036 val = stp_malloc(sizeof(value_t));
1037 val->name = stp_strdup(parameter);
1038 val->typ = STP_PARAMETER_TYPE_DIMENSION;
1039 val->active = STP_PARAMETER_ACTIVE;
1040 stp_list_item_create(list, NULL, val);
1041 }
1042 val->value.sval = sval;
1043 stp_set_verified(v, 0);
1044 }
1045
1046 void
1047 stp_set_default_dimension_parameter(stp_vars_t *v, const char *parameter,
1048 stp_dimension_t sval)
1049 {
1050 stp_list_t *list = v->params[STP_PARAMETER_TYPE_DIMENSION];
1051 value_t *val;
1052 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1053 stp_dprintf(STP_DBG_VARS, v, "stp_set_default_dimension_parameter(0x%p, %s, %f)\n",
1054 (const void *) v, parameter, sval);
1055 if (!item)
1056 {
1057 val = stp_malloc(sizeof(value_t));
1058 val->name = stp_strdup(parameter);
1059 val->typ = STP_PARAMETER_TYPE_DIMENSION;
1060 val->active = STP_PARAMETER_DEFAULTED;
1061 stp_list_item_create(list, NULL, val);
1062 val->value.sval = sval;
1063 }
1064 stp_set_verified(v, 0);
1065 }
1066
1067 void
1068 stp_clear_dimension_parameter(stp_vars_t *v, const char *parameter)
1069 {
1070 stp_list_t *list = v->params[STP_PARAMETER_TYPE_DIMENSION];
1071 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1072 stp_dprintf(STP_DBG_VARS, v, "stp_clear_dimension_parameter(0x%p, %s)\n",
1073 (const void *) v, parameter);
1074 if (item)
1075 stp_list_item_destroy(list, item);
1076 stp_set_verified(v, 0);
1077 }
1078
1079 stp_dimension_t
1080 stp_get_dimension_parameter(const stp_vars_t *v, const char *parameter)
1081 {
1082 const stp_list_t *list = v->params[STP_PARAMETER_TYPE_DIMENSION];
1083 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1084 if (item)
1085 {
1086 const value_t *val = (const value_t *) stp_list_item_get_data(item);
1087 return val->value.sval;
1088 }
1089 else
1090 {
1091 stp_parameter_t desc;
1092 stp_describe_parameter(v, parameter, &desc);
1093 if (desc.p_type == STP_PARAMETER_TYPE_DIMENSION)
1094 {
1095 stp_dimension_t intval = desc.deflt.dimension;
1096 stp_parameter_description_destroy(&desc);
1097 return intval;
1098 }
1099 else
1100 {
1101 stp_parameter_description_destroy(&desc);
1102 stp_eprintf
1103 (v, "Gutenprint: Attempt to retrieve unset dimension parameter %s\n",
1104 parameter);
1105 return 0;
1106 }
1107 }
1108 }
1109
1110 void
1111 stp_set_float_parameter(stp_vars_t *v, const char *parameter, double dval)
1112 {
1113 stp_list_t *list = v->params[STP_PARAMETER_TYPE_DOUBLE];
1114 value_t *val;
1115 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1116 stp_dprintf(STP_DBG_VARS, v, "stp_set_float_parameter(0x%p, %s, %f)\n",
1117 (const void *) v, parameter, dval);
1118 if (item)
1119 {
1120 val = (value_t *) stp_list_item_get_data(item);
1121 if (val->active == STP_PARAMETER_DEFAULTED)
1122 val->active = STP_PARAMETER_ACTIVE;
1123 }
1124 else
1125 {
1126 val = stp_malloc(sizeof(value_t));
1127 val->name = stp_strdup(parameter);
1128 val->typ = STP_PARAMETER_TYPE_DOUBLE;
1129 val->active = STP_PARAMETER_ACTIVE;
1130 stp_list_item_create(list, NULL, val);
1131 }
1132 val->value.dval = dval;
1133 stp_set_verified(v, 0);
1134 }
1135
1136 void
1137 stp_set_default_float_parameter(stp_vars_t *v, const char *parameter,
1138 double dval)
1139 {
1140 stp_list_t *list = v->params[STP_PARAMETER_TYPE_DOUBLE];
1141 value_t *val;
1142 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1143 stp_dprintf(STP_DBG_VARS, v, "stp_set_default_float_parameter(0x%p, %s, %f)\n",
1144 (const void *) v, parameter, dval);
1145 if (!item)
1146 {
1147 val = stp_malloc(sizeof(value_t));
1148 val->name = stp_strdup(parameter);
1149 val->typ = STP_PARAMETER_TYPE_DOUBLE;
1150 val->active = STP_PARAMETER_DEFAULTED;
1151 stp_list_item_create(list, NULL, val);
1152 val->value.dval = dval;
1153 }
1154 stp_set_verified(v, 0);
1155 }
1156
1157 void
1158 stp_clear_float_parameter(stp_vars_t *v, const char *parameter)
1159 {
1160 stp_list_t *list = v->params[STP_PARAMETER_TYPE_DOUBLE];
1161 stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1162 stp_dprintf(STP_DBG_VARS, v, "stp_clear_float_parameter(0x%p, %s)\n",
1163 (const void *) v, parameter);
1164 if (item)
1165 stp_list_item_destroy(list, item);
1166 stp_set_verified(v, 0);
1167 }
1168
1169 double
1170 stp_get_float_parameter(const stp_vars_t *v, const char *parameter)
1171 {
1172 const stp_list_t *list = v->params[STP_PARAMETER_TYPE_DOUBLE];
1173 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1174 if (item)
1175 {
1176 const value_t *val = (value_t *) stp_list_item_get_data(item);
1177 return val->value.dval;
1178 }
1179 else
1180 {
1181 stp_parameter_t desc;
1182 stp_describe_parameter(v, parameter, &desc);
1183 if (desc.p_type == STP_PARAMETER_TYPE_DOUBLE)
1184 {
1185 double dbl = desc.deflt.dbl;
1186 stp_parameter_description_destroy(&desc);
1187 return dbl;
1188 }
1189 else
1190 {
1191 stp_parameter_description_destroy(&desc);
1192 stp_eprintf
1193 (v, "Gutenprint: Attempt to retrieve unset float parameter %s\n",
1194 parameter);
1195 return 1.0;
1196 }
1197 }
1198 }
1199
1200 void
1201 stp_scale_float_parameter(stp_vars_t *v, const char *parameter,
1202 double scale)
1203 {
1204 double val;
1205 if (stp_check_float_parameter(v, parameter, STP_PARAMETER_DEFAULTED))
1206 val = stp_get_float_parameter(v, parameter);
1207 else
1208 {
1209 stp_parameter_t desc;
1210 stp_describe_parameter(v, parameter, &desc);
1211 if (desc.p_type != STP_PARAMETER_TYPE_DOUBLE)
1212 {
1213 stp_parameter_description_destroy(&desc);
1214 return;
1215 }
1216 val = desc.deflt.dbl;
1217 stp_parameter_description_destroy(&desc);
1218 }
1219 stp_dprintf(STP_DBG_VARS, v, "stp_scale_float_parameter(%p, %s, %f*%f)\n",
1220 (const void *) v, parameter, val, scale);
1221 stp_set_float_parameter(v, parameter, val * scale);
1222 }
1223
1224 void
1225 stp_clear_parameter(stp_vars_t *v, const char *parameter, stp_parameter_type_t type)
1226 {
1227 switch (type)
1228 {
1229 case STP_PARAMETER_TYPE_STRING_LIST:
1230 stp_clear_string_parameter(v, parameter);
1231 break;
1232 case STP_PARAMETER_TYPE_FILE:
1233 stp_clear_file_parameter(v, parameter);
1234 break;
1235 case STP_PARAMETER_TYPE_DOUBLE:
1236 stp_clear_float_parameter(v, parameter);
1237 break;
1238 case STP_PARAMETER_TYPE_INT:
1239 stp_clear_int_parameter(v, parameter);
1240 break;
1241 case STP_PARAMETER_TYPE_DIMENSION:
1242 stp_clear_dimension_parameter(v, parameter);
1243 break;
1244 case STP_PARAMETER_TYPE_BOOLEAN:
1245 stp_clear_boolean_parameter(v, parameter);
1246 break;
1247 case STP_PARAMETER_TYPE_CURVE:
1248 stp_clear_curve_parameter(v, parameter);
1249 break;
1250 case STP_PARAMETER_TYPE_ARRAY:
1251 stp_clear_array_parameter(v, parameter);
1252 break;
1253 case STP_PARAMETER_TYPE_RAW:
1254 stp_clear_raw_parameter(v, parameter);
1255 break;
1256 default:
1257 stp_eprintf(v, "Attempt to clear unknown type parameter!\n");
1258 }
1259 }
1260
1261
1262 int
1263 stp_check_parameter(const stp_vars_t *v,
1264 const char *parameter,
1265 stp_parameter_activity_t active,
1266 stp_parameter_type_t p_type)
1267 {
1268 if (p_type >= STP_PARAMETER_TYPE_STRING_LIST &&
1269 p_type < STP_PARAMETER_TYPE_INVALID)
1270 {
1271 const stp_list_t *list = v->params[p_type];
1272 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1273 if (item &&
1274 active <= ((const value_t *) stp_list_item_get_data(item))->active)
1275 return 1;
1276 else
1277 return 0;
1278 }
1279 return 0;
1280 }
1281
1282 #define CHECK_FUNCTION(type, index) \
1283 int \
1284 stp_check_##type##_parameter(const stp_vars_t *v, const char *parameter, \
1285 stp_parameter_activity_t active) \
1286 { \
1287 return stp_check_parameter(v, parameter, active, index); \
1288 }
1289
1290 CHECK_FUNCTION(string, STP_PARAMETER_TYPE_STRING_LIST)
1291 CHECK_FUNCTION(file, STP_PARAMETER_TYPE_FILE)
1292 CHECK_FUNCTION(float, STP_PARAMETER_TYPE_DOUBLE)
1293 CHECK_FUNCTION(int, STP_PARAMETER_TYPE_INT)
1294 CHECK_FUNCTION(dimension, STP_PARAMETER_TYPE_DIMENSION)
1295 CHECK_FUNCTION(boolean, STP_PARAMETER_TYPE_BOOLEAN)
1296 CHECK_FUNCTION(curve, STP_PARAMETER_TYPE_CURVE)
1297 CHECK_FUNCTION(array, STP_PARAMETER_TYPE_ARRAY)
1298 CHECK_FUNCTION(raw, STP_PARAMETER_TYPE_RAW)
1299
1300 stp_string_list_t *
1301 stp_list_parameters(const stp_vars_t *v, stp_parameter_type_t p_type)
1302 {
1303 if (p_type >= STP_PARAMETER_TYPE_STRING_LIST &&
1304 p_type < STP_PARAMETER_TYPE_INVALID)
1305 {
1306 const stp_list_t *list = v->params[p_type];
1307 stp_string_list_t *answer = stp_string_list_create();
1308 const stp_list_item_t *li = stp_list_get_start(list);
1309 while (li)
1310 {
1311 const value_t *val = (value_t *) stp_list_item_get_data(li);
1312 stp_string_list_add_string(answer, val->name, val->name);
1313 li = stp_list_item_next(li);
1314 }
1315 return answer;
1316 }
1317 return NULL;
1318 }
1319
1320 #define LIST_FUNCTION(type, index) \
1321 stp_string_list_t * \
1322 stp_list_##type##_parameters(const stp_vars_t *v) \
1323 { \
1324 return stp_list_parameters(v, index); \
1325 }
1326
1327 LIST_FUNCTION(string, STP_PARAMETER_TYPE_STRING_LIST)
1328 LIST_FUNCTION(file, STP_PARAMETER_TYPE_FILE)
1329 LIST_FUNCTION(float, STP_PARAMETER_TYPE_DOUBLE)
1330 LIST_FUNCTION(int, STP_PARAMETER_TYPE_INT)
1331 LIST_FUNCTION(dimension, STP_PARAMETER_TYPE_DIMENSION)
1332 LIST_FUNCTION(boolean, STP_PARAMETER_TYPE_BOOLEAN)
1333 LIST_FUNCTION(curve, STP_PARAMETER_TYPE_CURVE)
1334 LIST_FUNCTION(array, STP_PARAMETER_TYPE_ARRAY)
1335 LIST_FUNCTION(raw, STP_PARAMETER_TYPE_RAW)
1336
1337 stp_parameter_activity_t
1338 stp_get_parameter_active(const stp_vars_t *v, const char *parameter,
1339 stp_parameter_type_t p_type)
1340 {
1341 if (p_type >= STP_PARAMETER_TYPE_STRING_LIST &&
1342 p_type < STP_PARAMETER_TYPE_INVALID)
1343 {
1344 const stp_list_t *list = v->params[p_type];
1345 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1346 if (item)
1347 return ((const value_t *) stp_list_item_get_data(item))->active;
1348 else
1349 return 0;
1350 }
1351 return 0;
1352 }
1353
1354 #define GET_PARAMETER_ACTIVE_FUNCTION(type, index) \
1355 stp_parameter_activity_t \
1356 stp_get_##type##_parameter_active(const stp_vars_t *v, const char *parameter) \
1357 { \
1358 return stp_get_parameter_active(v, parameter, index); \
1359 }
1360
1361 GET_PARAMETER_ACTIVE_FUNCTION(string, STP_PARAMETER_TYPE_STRING_LIST)
1362 GET_PARAMETER_ACTIVE_FUNCTION(file, STP_PARAMETER_TYPE_FILE)
1363 GET_PARAMETER_ACTIVE_FUNCTION(float, STP_PARAMETER_TYPE_DOUBLE)
1364 GET_PARAMETER_ACTIVE_FUNCTION(int, STP_PARAMETER_TYPE_INT)
1365 GET_PARAMETER_ACTIVE_FUNCTION(dimension, STP_PARAMETER_TYPE_DIMENSION)
1366 GET_PARAMETER_ACTIVE_FUNCTION(boolean, STP_PARAMETER_TYPE_BOOLEAN)
1367 GET_PARAMETER_ACTIVE_FUNCTION(curve, STP_PARAMETER_TYPE_CURVE)
1368 GET_PARAMETER_ACTIVE_FUNCTION(array, STP_PARAMETER_TYPE_ARRAY)
1369 GET_PARAMETER_ACTIVE_FUNCTION(raw, STP_PARAMETER_TYPE_RAW)
1370
1371 void
1372 stp_set_parameter_active(stp_vars_t *v,
1373 const char *parameter,
1374 stp_parameter_activity_t active,
1375 stp_parameter_type_t p_type)
1376 {
1377 if (p_type >= STP_PARAMETER_TYPE_STRING_LIST &&
1378 p_type < STP_PARAMETER_TYPE_INVALID)
1379 {
1380 const stp_list_t *list = v->params[p_type];
1381 const stp_list_item_t *item = stp_list_get_item_by_name(list, parameter);
1382 if (item && (active == STP_PARAMETER_ACTIVE ||
1383 active == STP_PARAMETER_INACTIVE))
1384 ((value_t *) stp_list_item_get_data(item))->active = active;
1385 }
1386 }
1387
1388 #define SET_PARAMETER_ACTIVE_FUNCTION(type, index) \
1389 void \
1390 stp_set_##type##_parameter_active(stp_vars_t *v, const char *parameter, \
1391 stp_parameter_activity_t active) \
1392 { \
1393 stp_dprintf(STP_DBG_VARS, v, \
1394 "stp_set_%s_parameter_active(0x%p, %s, %d)\n", \
1395 #type, (const void *) v, parameter, active); \
1396 stp_set_parameter_active(v, parameter, active, index); \
1397 }
1398
1399 SET_PARAMETER_ACTIVE_FUNCTION(string, STP_PARAMETER_TYPE_STRING_LIST)
1400 SET_PARAMETER_ACTIVE_FUNCTION(file, STP_PARAMETER_TYPE_FILE)
1401 SET_PARAMETER_ACTIVE_FUNCTION(float, STP_PARAMETER_TYPE_DOUBLE)
1402 SET_PARAMETER_ACTIVE_FUNCTION(int, STP_PARAMETER_TYPE_INT)
1403 SET_PARAMETER_ACTIVE_FUNCTION(dimension, STP_PARAMETER_TYPE_DIMENSION)
1404 SET_PARAMETER_ACTIVE_FUNCTION(boolean, STP_PARAMETER_TYPE_BOOLEAN)
1405 SET_PARAMETER_ACTIVE_FUNCTION(curve, STP_PARAMETER_TYPE_CURVE)
1406 SET_PARAMETER_ACTIVE_FUNCTION(array, STP_PARAMETER_TYPE_ARRAY)
1407 SET_PARAMETER_ACTIVE_FUNCTION(raw, STP_PARAMETER_TYPE_RAW)
1408
1409 void
1410 stp_fill_parameter_settings(stp_parameter_t *desc,
1411 const stp_parameter_t *param)
1412 {
1413 if (param)
1414 {
1415 desc->p_type = param->p_type;
1416 desc->p_level = param->p_level;
1417 desc->p_class = param->p_class;
1418 desc->is_mandatory = param->is_mandatory;
1419 desc->is_active = param->is_active;
1420 desc->channel = param->channel;
1421 desc->verify_this_parameter = param->verify_this_parameter;
1422 desc->read_only = param->read_only;
1423 desc->name = param->name;
1424 STPI_ASSERT(param->text, NULL);
1425 desc->text = gettext(param->text);
1426 STPI_ASSERT(param->category, NULL);
1427 desc->category = gettext(param->category);
1428 desc->help = param->help ? gettext(param->help) : NULL;
1429 return;
1430 }
1431 }
1432
1433 void
1434 stp_vars_copy(stp_vars_t *vd, const stp_vars_t *vs)
1435 {
1436 int i;
1437
1438 if (vs == vd)
1439 return;
1440 stp_set_outdata(vd, stp_get_outdata(vs));
1441 stp_set_errdata(vd, stp_get_errdata(vs));
1442 stp_set_dbgdata(vd, stp_get_dbgdata(vs));
1443 stp_set_outfunc(vd, stp_get_outfunc(vs));
1444 stp_set_errfunc(vd, stp_get_errfunc(vs));
1445 stp_set_dbgfunc(vd, stp_get_dbgfunc(vs));
1446 stp_set_driver(vd, stp_get_driver(vs));
1447 stp_set_color_conversion(vd, stp_get_color_conversion(vs));
1448 stp_set_left(vd, stp_get_left(vs));
1449 stp_set_top(vd, stp_get_top(vs));
1450 stp_set_width(vd, stp_get_width(vs));
1451 stp_set_height(vd, stp_get_height(vs));
1452 stp_set_page_width(vd, stp_get_page_width(vs));
1453 stp_set_page_height(vd, stp_get_page_height(vs));
1454 for (i = 0; i < STP_PARAMETER_TYPE_INVALID; i++)
1455 {
1456 stp_list_destroy(vd->params[i]);
1457 vd->params[i] = copy_value_list(vs->params[i]);
1458 }
1459 stp_list_destroy(vd->internal_data);
1460 vd->internal_data = copy_compdata_list(vs->internal_data);
1461 stp_set_verified(vd, stp_get_verified(vs));
1462 }
1463
1464 void
1465 stp_vars_print_error(const stp_vars_t *v, const char *prefix)
1466 {
1467 int i;
1468 char *cptr;
1469 static const char *data_types[] = {
1470 "String",
1471 "Int",
1472 "Bool",
1473 "Double",
1474 "Curve",
1475 "File",
1476 "Raw",
1477 "Array",
1478 "Dimension",
1479 "(Inactive)"
1480 };
1481 if (! prefix)
1482 prefix = "";
1483 stp_eprintf(v, "%s: Gutenprint: === BEGIN GUTENPRINT SETTINGS ===\n", prefix);
1484 stp_eprintf(v, "%s: Gutenprint: Driver: %s\n", prefix, stp_get_driver(v));
1485 stp_eprintf(v, "%s: Gutenprint: L: %f T: %f W: %f H: %f\n", prefix, stp_get_left(v),
1486 stp_get_top(v), stp_get_width(v), stp_get_height(v));
1487 stp_eprintf(v, "%s: Gutenprint: Page: %fx%f\n", prefix, stp_get_page_width(v),
1488 stp_get_page_height(v));
1489 stp_eprintf(v, "%s: Gutenprint: Conversion: %s\n", prefix, stp_get_color_conversion(v));
1490 for (i = 0; i < STP_PARAMETER_TYPE_INVALID; i++)
1491 {
1492 const stp_list_item_t *item =
1493 stp_list_get_start((const stp_list_t *) v->params[i]);
1494 while (item)
1495 {
1496 char *crep;
1497 const value_t *val = (const value_t *) stp_list_item_get_data(item);
1498 switch (val->typ)
1499 {
1500 case STP_PARAMETER_TYPE_CURVE:
1501 crep = stp_curve_write_string(val->value.cval);
1502 cptr = crep;
1503 while (cptr && *cptr)
1504 {
1505 if (*cptr == '\n')
1506 *cptr = ' ';
1507 cptr++;
1508 }
1509 stp_eprintf(v, "%s: Gutenprint: (%s) (%i) (%s) [%s]\n", prefix,
1510 val->name, val->active, data_types[val->typ],
1511 crep ? crep : "NULL");
1512 if (crep)
1513 stp_free(crep);
1514 break;
1515 case STP_PARAMETER_TYPE_ARRAY:
1516 break;
1517 case STP_PARAMETER_TYPE_STRING_LIST:
1518 case STP_PARAMETER_TYPE_FILE:
1519 case STP_PARAMETER_TYPE_RAW:
1520 crep = stp_rawtoxmlstr(&(val->value.rval));
1521 stp_eprintf(v, "%s: Gutenprint: (%s) (%i) (%s) [%s]\n", prefix,
1522 val->name, val->active, data_types[val->typ],
1523 crep ? crep : "NULL");
1524 if (crep)
1525 stp_free(crep);
1526 break;
1527 case STP_PARAMETER_TYPE_DIMENSION:
1528 stp_eprintf(v, "%s: Gutenprint: (%s) (%i) (%s) [%f]\n", prefix,
1529 val->name, val->active, data_types[val->typ],
1530 val->value.sval);
1531 break;
1532 case STP_PARAMETER_TYPE_INT:
1533 case STP_PARAMETER_TYPE_BOOLEAN:
1534 stp_eprintf(v, "%s: Gutenprint: (%s) (%i) (%s) [%d]\n", prefix,
1535 val->name, val->active, data_types[val->typ],
1536 val->value.ival);
1537 break;
1538 case STP_PARAMETER_TYPE_DOUBLE:
1539 stp_eprintf(v, "%s: Gutenprint: (%s) (%i) (%s) [%f]\n", prefix,
1540 val->name, val->active, data_types[val->typ],
1541 val->value.dval);
1542 break;
1543 default:
1544 break;
1545 }
1546 item = stp_list_item_next(item);
1547 }
1548 }
1549 stp_eprintf(v, "%s: Gutenprint: === END GUTENPRINT SETTINGS ===\n", prefix);
1550 }
1551
1552 void
1553 stp_prune_inactive_options(stp_vars_t *v)
1554 {
1555 stp_parameter_list_t params = stp_get_parameter_list(v);
1556 int i;
1557 for (i = 0; i < STP_PARAMETER_TYPE_INVALID; i++)
1558 {
1559 stp_list_t *list = v->params[i];
1560 stp_list_item_t *item = stp_list_get_start(list);
1561 while (item)
1562 {
1563 stp_list_item_t *next = stp_list_item_next(item);
1564 value_t *var = (value_t *)stp_list_item_get_data(item);
1565 if (var->active < STP_PARAMETER_DEFAULTED ||
1566 !(stp_parameter_find(params, var->name)))
1567 stp_list_item_destroy(list, item);
1568 item = next;
1569 }
1570 }
1571 stp_parameter_list_destroy(params);
1572 }
1573
1574 stp_vars_t *
1575 stp_vars_create_copy(const stp_vars_t *vs)
1576 {
1577 stp_vars_t *vd = stp_vars_create();
1578 stp_vars_copy(vd, vs);
1579 return (vd);
1580 }
1581
1582 static const char *
1583 param_namefunc(const void *item)
1584 {
1585 const stp_parameter_t *param = (const stp_parameter_t *)(item);
1586 return param->name;
1587 }
1588
1589 static const char *
1590 param_longnamefunc(const void *item)
1591 {
1592 const stp_parameter_t *param = (const stp_parameter_t *) (item);
1593 return param->text;
1594 }
1595
1596 stp_parameter_list_t
1597 stp_parameter_list_create(void)
1598 {
1599 stp_list_t *ret = stp_list_create();
1600 stp_list_set_namefunc(ret, param_namefunc);
1601 stp_list_set_long_namefunc(ret, param_longnamefunc);
1602 return (stp_parameter_list_t) ret;
1603 }
1604
1605 void
1606 stp_parameter_list_add_param(stp_parameter_list_t list,
1607 const stp_parameter_t *item)
1608 {
1609 stp_list_t *ilist = (stp_list_t *) list;
1610 stp_list_item_create(ilist, NULL, item);
1611 }
1612
1613 static void
1614 debug_print_parameter_description(const stp_parameter_t *desc, const char *who,
1615 const stp_vars_t *v)
1616 {
1617 unsigned int i;
1618 char *curve;
1619 if (! (stp_get_debug_level() & STP_DBG_VARS))
1620 return;
1621 stp_dprintf(STP_DBG_VARS, v, "Describe %s: vars 0x%p from %s type %d class %d level %d\n",
1622 desc->name, (const void *) v, who,
1623 desc->p_type, desc->p_class, desc->p_level);
1624 stp_dprintf(STP_DBG_VARS, v, " driver %s mandatory %d active %d channel %d verify %d ro %d\n",
1625 stp_get_driver(v), desc->is_mandatory, desc->is_active,
1626 desc->channel, desc->verify_this_parameter, desc->read_only);
1627 switch (desc->p_type)
1628 {
1629 case STP_PARAMETER_TYPE_STRING_LIST:
1630 stp_dprintf(STP_DBG_VARS, v,
1631 " String default: %s\n",
1632 desc->deflt.str ? desc->deflt.str : "(null)");
1633 if (desc->bounds.str)
1634 for (i = 0; i < stp_string_list_count(desc->bounds.str); i++)
1635 {
1636 if (i == 0)
1637 stp_dprintf(STP_DBG_VARS, v, " Choices: %s\n",
1638 stp_string_list_param(desc->bounds.str, i)->name);
1639 else
1640 stp_dprintf(STP_DBG_VARS, v, " : %s\n",
1641 stp_string_list_param(desc->bounds.str, i)->name);
1642 }
1643 break;
1644 case STP_PARAMETER_TYPE_INT:
1645 stp_dprintf(STP_DBG_VARS, v,
1646 " Integer default: %d Bounds: %d %d\n",
1647 desc->deflt.integer,
1648 desc->bounds.integer.lower, desc->bounds.integer.upper);
1649 break;
1650 case STP_PARAMETER_TYPE_DIMENSION:
1651 stp_dprintf(STP_DBG_VARS, v,
1652 " Dimension default: %f Bounds: %f %f\n",
1653 desc->deflt.dimension,
1654 desc->bounds.dimension.lower, desc->bounds.dimension.upper);
1655 break;
1656 case STP_PARAMETER_TYPE_BOOLEAN:
1657 stp_dprintf(STP_DBG_VARS, v,
1658 " Boolean default: %d\n", desc->deflt.boolean);
1659 break;
1660 case STP_PARAMETER_TYPE_DOUBLE:
1661 stp_dprintf(STP_DBG_VARS, v,
1662 " Double default: %f Bounds: %f %f\n",
1663 desc->deflt.dbl,
1664 desc->bounds.dbl.lower, desc->bounds.dbl.upper);
1665 break;
1666 case STP_PARAMETER_TYPE_FILE:
1667 stp_dprintf(STP_DBG_VARS, v, " File (no default)\n");
1668 break;
1669 case STP_PARAMETER_TYPE_RAW:
1670 stp_dprintf(STP_DBG_VARS, v, " Raw (no default)\n");
1671 break;
1672 case STP_PARAMETER_TYPE_CURVE:
1673 curve = stp_curve_write_string(desc->deflt.curve);
1674 stp_dprintf(STP_DBG_VARS, v,
1675 " Curve default: %s\n", curve);
1676 stp_free(curve);
1677 curve = stp_curve_write_string(desc->bounds.curve);
1678 stp_dprintf(STP_DBG_VARS, v,
1679 " bounds: %s\n", curve);
1680 stp_free(curve);
1681 break;
1682 case STP_PARAMETER_TYPE_ARRAY:
1683 stp_dprintf(STP_DBG_VARS, v, " Array\n");
1684 break;
1685 case STP_PARAMETER_TYPE_INVALID:
1686 stp_dprintf(STP_DBG_VARS, v, " *** Invalid ***\n");
1687 break;
1688 default:
1689 stp_dprintf(STP_DBG_VARS, v, " Unknown type!\n");
1690 }
1691 }
1692
1693 void
1694 stp_describe_parameter(const stp_vars_t *v, const char *name,
1695 stp_parameter_t *description)
1696 {
1697 description->p_type = STP_PARAMETER_TYPE_INVALID;
1698 /* Set these to NULL in case stpi_*_describe_parameter() doesn't */
1699 description->bounds.str = NULL;
1700 description->deflt.str = NULL;
1701 stp_printer_describe_parameter(v, name, description);
1702 if (description->p_type != STP_PARAMETER_TYPE_INVALID)
1703 {
1704 debug_print_parameter_description(description, "driver", v);
1705 return;
1706 }
1707 stp_color_describe_parameter(v, name, description);
1708 if (description->p_type != STP_PARAMETER_TYPE_INVALID)
1709 {
1710 debug_print_parameter_description(description, "color", v);
1711 return;
1712 }
1713 stp_dither_describe_parameter(v, name, description);
1714 if (description->p_type != STP_PARAMETER_TYPE_INVALID)
1715 {
1716 debug_print_parameter_description(description, "dither", v);
1717 return;
1718 }
1719 stpi_describe_generic_parameter(v, name, description);
1720 if (description->p_type != STP_PARAMETER_TYPE_INVALID)
1721 debug_print_parameter_description(description, "generic", v);
1722 else
1723 stp_dprintf(STP_DBG_VARS, v, "Describing invalid parameter %s\n", name);
1724 }
1725
1726 stp_string_list_t *
1727 stp_parameter_get_categories(const stp_vars_t *v, const stp_parameter_t *desc)
1728 {
1729 const char *dptr;
1730 stp_string_list_t *answer;
1731 int count = 0;
1732 if (!v || !desc || !(desc->category))
1733 return NULL;
1734 answer = stp_string_list_create();
1735 dptr = desc->category;
1736 while (dptr)
1737 {
1738 const char *xptr = strchr(dptr, '=');
1739 if (xptr)
1740 {
1741 char *name = stp_strndup(dptr, xptr - dptr);
1742 char *text;
1743 dptr = xptr + 1;
1744 xptr = strchr(dptr, ',');
1745 if (xptr)
1746 {
1747 text = stp_strndup(dptr, xptr - dptr);
1748 dptr = xptr + 1;
1749 }
1750 else
1751 {
1752 text = stp_strdup(dptr);
1753 dptr = NULL;
1754 }
1755 stp_string_list_add_string(answer, name, text);
1756 stp_free(name);
1757 stp_free(text);
1758 count++;
1759 }
1760 else
1761 dptr = NULL;
1762 }
1763 if (count == 0)
1764 {
1765 stp_string_list_destroy(answer);
1766 return NULL;
1767 }
1768 else
1769 return answer;
1770 }
1771
1772 char *
1773 stp_parameter_get_category(const stp_vars_t *v, const stp_parameter_t *desc,
1774 const char *category)
1775 {
1776 const char *dptr;
1777 char *cptr;
1778 int len;
1779 if (!v || !desc || !(desc->category) || !category)
1780 return NULL;
1781 dptr = desc->category;
1782 stp_asprintf(&cptr, "%s=", category);
1783 len = stp_strlen(cptr);
1784 while (dptr)
1785 {
1786 if (strncmp(dptr, cptr, len) == 0)
1787 {
1788 const char *xptr;
1789 char *answer;
1790 dptr += len;
1791 xptr = strchr(dptr, ',');
1792 if (xptr)
1793 answer = stp_strndup(dptr, xptr - dptr);
1794 else
1795 answer = stp_strdup(dptr);
1796 stp_free(cptr);
1797 return answer;
1798 }
1799 dptr = strchr(dptr, ',');
1800 if (dptr)
1801 dptr++;
1802 }
1803 return NULL;
1804 }
1805
1806 int
1807 stp_parameter_has_category_value(const stp_vars_t *v,
1808 const stp_parameter_t *desc,
1809 const char *category, const char *value)
1810 {
1811 char *cptr;
1812 int answer = 0;
1813 if (!v || !desc || !category)
1814 return -1;
1815 cptr = stp_parameter_get_category(v, desc, category);
1816 if (cptr == NULL)
1817 return 0;
1818 if (value == NULL || strcmp(value, cptr) == 0)
1819 answer = 1;
1820 stp_free(cptr);
1821 return answer;
1822 }
1823
1824 const stp_parameter_t *
1825 stp_parameter_find_in_settings(const stp_vars_t *v, const char *name)
1826 {
1827 stp_parameter_list_t param_list = stp_get_parameter_list(v);
1828 const stp_parameter_t *param = stp_parameter_find(param_list, name);
1829 stp_parameter_list_destroy(param_list);
1830 return param;
1831 }
1832
1833 size_t
1834 stp_parameter_list_count(stp_const_parameter_list_t list)
1835 {
1836 const stp_list_t *ilist = (const stp_list_t *)list;
1837 return stp_list_get_length(ilist);
1838 }
1839
1840 const stp_parameter_t *
1841 stp_parameter_find(stp_const_parameter_list_t list, const char *name)
1842 {
1843 const stp_list_t *ilist = (const stp_list_t *)list;
1844 const stp_list_item_t *item = stp_list_get_item_by_name(ilist, name);
1845 if (item)
1846 return (const stp_parameter_t *) stp_list_item_get_data(item);
1847 else
1848 return NULL;
1849 }
1850
1851 const stp_parameter_t *
1852 stp_parameter_list_param(stp_const_parameter_list_t list, size_t item)
1853 {
1854 const stp_list_t *ilist = (const stp_list_t *)list;
1855 stp_list_item_t *i = NULL;
1856 if (item >= stp_list_get_length(ilist))
1857 return NULL;
1858 else
1859 {
1860 i = stp_list_get_item_by_index(ilist, item);
1861 if (i == NULL)
1862 return NULL;
1863 return (const stp_parameter_t *) stp_list_item_get_data(i);
1864 }
1865 }
1866
1867 void
1868 stp_parameter_list_destroy(stp_parameter_list_t list)
1869 {
1870 stp_list_destroy((stp_list_t *)list);
1871 }
1872
1873 stp_parameter_list_t
1874 stp_parameter_list_copy(stp_const_parameter_list_t list)
1875 {
1876 stp_list_t *ret = stp_parameter_list_create();
1877 unsigned int i;
1878 size_t count = stp_parameter_list_count(list);
1879 for (i = 0; i < count; i++)
1880 stp_list_item_create(ret, NULL, stp_parameter_list_param(list, i));
1881 return (stp_parameter_list_t) ret;
1882 }
1883
1884 void
1885 stp_parameter_list_append(stp_parameter_list_t list,
1886 stp_const_parameter_list_t append)
1887 {
1888 unsigned int i;
1889 stp_list_t *ilist = (stp_list_t *)list;
1890 size_t count = stp_parameter_list_count(append);
1891 for (i = 0; i < count; i++)
1892 {
1893 const stp_parameter_t *param = stp_parameter_list_param(append, i);
1894 if (!stp_list_get_item_by_name(ilist, param->name))
1895 stp_list_item_create(ilist, NULL, param);
1896 }
1897 }
1898
1899 void
1900 stp_copy_vars_from(stp_vars_t *to, const stp_vars_t *from)
1901 {
1902 int i;
1903 if (!from || !to)
1904 return;
1905 for (i = 0; i < STP_PARAMETER_TYPE_INVALID; i++)
1906 {
1907 const stp_list_item_t *item =
1908 stp_list_get_start((const stp_list_t *) from->params[i]);
1909 while (item)
1910 {
1911 const value_t *val = (const value_t *) stp_list_item_get_data(item);
1912 switch (val->typ)
1913 {
1914 case STP_PARAMETER_TYPE_CURVE:
1915 stp_set_curve_parameter(to, val->name, val->value.cval);
1916 break;
1917 case STP_PARAMETER_TYPE_ARRAY:
1918 stp_set_array_parameter(to, val->name, val->value.aval);
1919 break;
1920 case STP_PARAMETER_TYPE_STRING_LIST:
1921 stp_set_string_parameter(to, val->name, val->value.rval.data);
1922 break;
1923 case STP_PARAMETER_TYPE_FILE:
1924 stp_set_file_parameter(to, val->name, val->value.rval.data);
1925 break;
1926 case STP_PARAMETER_TYPE_RAW:
1927 stp_set_raw_parameter(to, val->name, val->value.rval.data,
1928 val->value.rval.bytes);
1929 break;
1930 case STP_PARAMETER_TYPE_INT:
1931 stp_set_int_parameter(to, val->name, val->value.ival);
1932 break;
1933 case STP_PARAMETER_TYPE_DIMENSION:
1934 stp_set_dimension_parameter(to, val->name, val->value.ival);
1935 break;
1936 case STP_PARAMETER_TYPE_BOOLEAN:
1937 stp_set_boolean_parameter(to, val->name, val->value.bval);
1938 break;
1939 case STP_PARAMETER_TYPE_DOUBLE:
1940 stp_set_float_parameter(to, val->name, val->value.dval);
1941 break;
1942 default:
1943 break;
1944 }
1945 item = stp_list_item_next(item);
1946 }
1947 }
1948 }
1949
1950 static void
1951 fill_vars_from_xmltree(stp_mxml_node_t *prop, stp_mxml_node_t *root,
1952 stp_vars_t *v)
1953 {
1954 #ifdef HAVE_LOCALE_H
1955 char *locale = stp_strdup(setlocale(LC_ALL, NULL));
1956 setlocale(LC_ALL, "C");
1957 #endif
1958 stp_dprintf(STP_DBG_XML, v, "Enter fill_vars_from_xmltree()\n");
1959 while (prop)
1960 {
1961 stp_dprintf(STP_DBG_XML, v, "Property type %d (%s)\n", prop->type,
1962 prop->type == STP_MXML_ELEMENT ? prop->value.element.name :
1963 (prop->type == STP_MXML_TEXT ? prop->value.text.string : "(nil)"));
1964 if (prop->type == STP_MXML_ELEMENT &&
1965 (prop->child || stp_mxmlElementGetAttr(prop, "name")))
1966 {
1967 stp_mxml_node_t *child = prop->child;
1968 const char *prop_name = prop->value.element.name;
1969 const char *p_type = stp_mxmlElementGetAttr(prop, "type");
1970 const char *p_name = stp_mxmlElementGetAttr(prop, "name");
1971 if (!strcmp(prop_name, "parameter") && (!p_type || !p_name))
1972 stp_eprintf(v, "Bad property found!\n");
1973 else if (!strcmp(prop_name, "parameter"))
1974 {
1975 const char *active = stp_mxmlElementGetAttr(prop, "active");
1976 const char *cref = stp_mxmlElementGetAttr(prop, "ref");
1977 stp_mxml_node_t *cnode = child;
1978 stp_parameter_type_t type = STP_PARAMETER_TYPE_INVALID;
1979 if (cref && root)
1980 {
1981 cnode = stp_mxmlFindElement(root, root, "namedParam",
1982 "name", cref,
1983 STP_MXML_DESCEND);
1984 STPI_ASSERT(cnode && cnode->type == STP_MXML_ELEMENT &&
1985 cnode->child, v);
1986 stp_dprintf(STP_DBG_XML, v, "Found parameter ref %s\n", cref);
1987 cnode = cnode->child;
1988 }
1989 if (strcmp(p_type, "float") == 0)
1990 {
1991 if (cnode->type == STP_MXML_TEXT)
1992 {
1993 stp_set_float_parameter
1994 (v, p_name, stp_xmlstrtod(cnode->value.text.string));
1995 type = STP_PARAMETER_TYPE_DOUBLE;
1996 if (stp_get_debug_level() & STP_DBG_XML)
1997 stp_dprintf(STP_DBG_XML, v, " Set float '%s' to '%s' (%f)\n",
1998 p_name, cnode->value.text.string,
1999 stp_get_float_parameter(v, p_name));
2000 }
2001 }
2002 else if (strcmp(p_type, "integer") == 0)
2003 {
2004 if (cnode->type == STP_MXML_TEXT)
2005 {
2006 stp_set_int_parameter
2007 (v, p_name, (int) stp_xmlstrtol(cnode->value.text.string));
2008 type = STP_PARAMETER_TYPE_DOUBLE;
2009 if (stp_get_debug_level() & STP_DBG_XML)
2010 stp_dprintf(STP_DBG_XML, v, " Set int '%s' to '%s' (%d)\n",
2011 p_name, cnode->value.text.string,
2012 stp_get_int_parameter(v, p_name));
2013 }
2014 }
2015 else if (strcmp(p_type, "dimension") == 0)
2016 {
2017 if (cnode->type == STP_MXML_TEXT)
2018 {
2019 stp_set_dimension_parameter
2020 (v, p_name, (int) stp_xmlstrtodim(cnode->value.text.string));
2021 type = STP_PARAMETER_TYPE_DOUBLE;
2022 if (stp_get_debug_level() & STP_DBG_XML)
2023 stp_dprintf(STP_DBG_XML, v, " Set dimension '%s' to '%s' (%f)\n",
2024 p_name, cnode->value.text.string,
2025 stp_get_dimension_parameter(v, p_name));
2026 }
2027 }
2028 else if (strcmp(p_type, "boolean") == 0)
2029 {
2030 if (cnode->type == STP_MXML_TEXT)
2031 {
2032 stp_set_boolean_parameter
2033 (v, p_name, (int) stp_xmlstrtol(cnode->value.text.string));
2034 type = STP_PARAMETER_TYPE_DOUBLE;
2035 if (stp_get_debug_level() & STP_DBG_XML)
2036 stp_dprintf(STP_DBG_XML, v, " Set bool '%s' to '%s' (%d)\n",
2037 p_name, cnode->value.text.string,
2038 stp_get_boolean_parameter(v, p_name));
2039 }
2040 }
2041 else if (strcmp(p_type, "string") == 0)
2042 {
2043 if (cnode->type == STP_MXML_TEXT)
2044 {
2045 stp_set_string_parameter
2046 (v, p_name, cnode->value.text.string);
2047 type = STP_PARAMETER_TYPE_DOUBLE;
2048 if (stp_get_debug_level() & STP_DBG_XML)
2049 stp_dprintf(STP_DBG_XML, v, " Set string '%s' to '%s' (%s)\n",
2050 p_name, cnode->value.text.string,
2051 stp_get_string_parameter(v, p_name));
2052 }
2053 }
2054 else if (strcmp(p_type, "file") == 0)
2055 {
2056 if (cnode->type == STP_MXML_TEXT)
2057 {
2058 stp_set_file_parameter
2059 (v, p_name, cnode->value.text.string);
2060 type = STP_PARAMETER_TYPE_DOUBLE;
2061 if (stp_get_debug_level() & STP_DBG_XML)
2062 stp_dprintf(STP_DBG_XML, v, " Set file '%s' to '%s' (%s)\n",
2063 p_name, cnode->value.text.string,
2064 stp_get_file_parameter(v, p_name));
2065 }
2066 }
2067 else if (strcmp(p_type, "raw") == 0)
2068 {
2069 if (cnode->type == STP_MXML_TEXT)
2070 {
2071 stp_raw_t *raw = stp_xmlstrtoraw(cnode->value.text.string);
2072 if (raw)
2073 {
2074 stp_set_raw_parameter(v, p_name, raw->data,raw->bytes);
2075 type = STP_PARAMETER_TYPE_DOUBLE;
2076 stp_dprintf(STP_DBG_XML, v, " Set raw '%s' to '%s'\n",
2077 p_name, cnode->value.text.string);
2078 stp_free(stpi_cast_safe(raw->data));
2079 stp_free(raw);
2080 }
2081 }
2082 }
2083 else if (strcmp(p_type, "curve") == 0)
2084 {
2085 stp_curve_t *curve;
2086 while (cnode->type != STP_MXML_ELEMENT && cnode->next)
2087 cnode = cnode->next;
2088 STPI_ASSERT(cnode, v);
2089 curve = stp_curve_create_from_xmltree(cnode);
2090 STPI_ASSERT(curve, v);
2091 stp_set_curve_parameter(v, p_name, curve);
2092 type = STP_PARAMETER_TYPE_DOUBLE;
2093 if (stp_get_debug_level() & STP_DBG_XML)
2094 {
2095 char *cv = stp_curve_write_string(curve);
2096 stp_dprintf(STP_DBG_XML, v, " Set curve '%s' (%s)\n",
2097 p_name, cv);
2098 stp_free(cv);
2099 }
2100 stp_curve_destroy(curve);
2101 }
2102 else if (strcmp(p_type, "array") == 0)
2103 {
2104 stp_array_t *array;
2105 while (cnode->type != STP_MXML_ELEMENT && cnode->next)
2106 cnode = cnode->next;
2107 STPI_ASSERT(cnode, v);
2108 array = stp_array_create_from_xmltree(cnode);
2109 STPI_ASSERT(array, v);
2110 type = STP_PARAMETER_TYPE_DOUBLE;
2111 stp_set_array_parameter(v, p_name, array);
2112 stp_dprintf(STP_DBG_XML, v, " Set array '%s'\n", p_name);
2113 stp_array_destroy(array);
2114 }
2115 else
2116 {
2117 stp_eprintf(v, "Bad property %s type %s\n", p_name, p_type);
2118 }
2119 if (active && type != STP_PARAMETER_TYPE_INVALID)
2120 {
2121 if (strcmp(active, "active") == 0)
2122 stp_set_parameter_active(v, p_name, STP_PARAMETER_ACTIVE, type);
2123 else if (strcmp(active, "inactive") == 0)
2124 stp_set_parameter_active(v, p_name, STP_PARAMETER_INACTIVE, type);
2125 else if (strcmp(active, "default") == 0)
2126 stp_set_parameter_active(v, p_name, STP_PARAMETER_DEFAULTED, type);
2127 }
2128
2129 }
2130 else if (child->type == STP_MXML_TEXT)
2131 {
2132 stp_dprintf(STP_DBG_XML, v, " Set property %s ('%s')\n",
2133 prop_name, child->value.text.string ?
2134 child->value.text.string : "(nil)");
2135 if (!strcmp(prop_name, "driver"))
2136 stp_set_driver(v, child->value.text.string);
2137 else if (!strcmp(prop_name, "color_conversion"))
2138 stp_set_color_conversion(v, child->value.text.string);
2139 else if (!strcmp(prop_name, "left"))
2140 stp_set_left(v, stp_xmlstrtol(child->value.text.string));
2141 else if (!strcmp(prop_name, "top"))
2142 stp_set_top(v, stp_xmlstrtol(child->value.text.string));
2143 else if (!strcmp(prop_name, "width"))
2144 stp_set_width(v, stp_xmlstrtol(child->value.text.string));
2145 else if (!strcmp(prop_name, "height"))
2146 stp_set_height(v, stp_xmlstrtol(child->value.text.string));
2147 else if (!strcmp(prop_name, "page_width"))
2148 stp_set_page_width(v, stp_xmlstrtol(child->value.text.string));
2149 else if (!strcmp(prop_name, "page_height"))
2150 stp_set_page_height(v, stp_xmlstrtol(child->value.text.string));
2151 }
2152 }
2153 else
2154 {
2155 if (prop->type == STP_MXML_ELEMENT)
2156 stp_dprintf(STP_DBG_XML, v, "Unexpected node type %d name %s\n",
2157 prop->type, prop->value.element.name);
2158 }
2159 prop = prop->next;
2160 }
2161 stp_dprintf(STP_DBG_XML, v, "End fill_vars_from_xmltree()\n");
2162 #ifdef HAVE_LOCALE_H
2163 setlocale(LC_ALL, locale);
2164 stp_free(locale);
2165 #endif
2166 }
2167
2168 void
2169 stp_vars_fill_from_xmltree(stp_mxml_node_t *prop, stp_vars_t *v)
2170 {
2171 fill_vars_from_xmltree(prop, NULL, v);
2172 }
2173
2174 stp_vars_t *
2175 stp_vars_create_from_xmltree(stp_mxml_node_t *da)
2176 {
2177 stp_vars_t *v = stp_vars_create();
2178 fill_vars_from_xmltree(da, NULL, v);
2179 return v;
2180 }
2181
2182 void
2183 stp_vars_fill_from_xmltree_ref(stp_mxml_node_t *prop, stp_mxml_node_t *root,
2184 stp_vars_t *v)
2185 {
2186 fill_vars_from_xmltree(prop, root, v);
2187 }
2188
2189 stp_vars_t *
2190 stp_vars_create_from_xmltree_ref(stp_mxml_node_t *da, stp_mxml_node_t *root)
2191 {
2192 stp_vars_t *v = stp_vars_create();
2193 fill_vars_from_xmltree(da, root, v);
2194 return v;
2195 }
2196
2197 static void
2198 add_text_node(stp_mxml_node_t *node, const char *element, const char *value)
2199 {
2200 if (value)
2201 stp_mxmlNewOpaque(stp_mxmlNewElement(node, element), value);
2202 }
2203
2204 stp_mxml_node_t *
2205 stp_xmltree_create_from_vars(const stp_vars_t *v)
2206 {
2207 stp_mxml_node_t *varnode;
2208 int i;
2209 if (!v)
2210 return NULL;
2211 varnode = stp_mxmlNewElement(NULL, "vars");
2212 add_text_node(varnode, "driver", stp_get_driver(v));
2213 add_text_node(varnode, "color_conversion", stp_get_color_conversion(v));
2214 stp_mxmlNewInteger(stp_mxmlNewElement(varnode, "left"), stp_get_left(v));
2215 stp_mxmlNewInteger(stp_mxmlNewElement(varnode, "top"), stp_get_top(v));
2216 stp_mxmlNewInteger(stp_mxmlNewElement(varnode, "width"), stp_get_width(v));
2217 stp_mxmlNewInteger(stp_mxmlNewElement(varnode, "height"), stp_get_height(v));
2218 stp_mxmlNewInteger(stp_mxmlNewElement(varnode, "page_width"), stp_get_page_width(v));
2219 stp_mxmlNewInteger(stp_mxmlNewElement(varnode, "page_height"), stp_get_page_height(v));
2220 for (i = STP_PARAMETER_TYPE_STRING_LIST; i < STP_PARAMETER_TYPE_INVALID; i++)
2221 {
2222 stp_string_list_t *list = stp_list_parameters(v, i);
2223 if (list)
2224 {
2225 int j;
2226 int count = stp_string_list_count(list);
2227 for (j = 0; j < count; j++)
2228 {
2229 const stp_param_string_t *pstr = stp_string_list_param(list, j);
2230 const char *name = pstr->name;
2231 char *data;
2232 stp_mxml_node_t *node = stp_mxmlNewElement(varnode, "parameter");
2233 stp_parameter_activity_t active =
2234 stp_get_parameter_active(v, name, i);
2235 stp_mxmlElementSetAttr(node, "name", name);
2236 stp_mxmlElementSetAttr(node, "active",
2237 (active == STP_PARAMETER_INACTIVE ?
2238 "inactive" :
2239 (active == STP_PARAMETER_DEFAULTED ?
2240 "default" : "active")));
2241 switch (i)
2242 {
2243 case STP_PARAMETER_TYPE_STRING_LIST:
2244 stp_mxmlElementSetAttr(node, "type", "string");
2245 data = stp_strtoxmlstr(stp_get_string_parameter(v, name));
2246 if (data)
2247 {
2248 stp_mxmlNewOpaque(node, data);
2249 stp_free(data);
2250 }
2251 break;
2252 case STP_PARAMETER_TYPE_INT:
2253 stp_mxmlElementSetAttr(node, "type", "integer");
2254 stp_mxmlNewInteger(node, stp_get_int_parameter(v, name));
2255 break;
2256 case STP_PARAMETER_TYPE_BOOLEAN:
2257 stp_mxmlElementSetAttr(node, "type", "boolean");
2258 stp_mxmlNewInteger(node, stp_get_boolean_parameter(v, name));
2259 break;
2260 case STP_PARAMETER_TYPE_DOUBLE:
2261 stp_mxmlElementSetAttr(node, "type", "float");
2262 stp_mxmlNewReal(node, stp_get_float_parameter(v, name));
2263 break;
2264 case STP_PARAMETER_TYPE_CURVE:
2265 stp_mxmlElementSetAttr(node, "type", "curve");
2266 stp_mxmlAdd(node, STP_MXML_ADD_AFTER, NULL,
2267 stp_xmltree_create_from_curve(stp_get_curve_parameter(v, name)));
2268 break;
2269 case STP_PARAMETER_TYPE_FILE:
2270 stp_mxmlElementSetAttr(node, "type", "file");
2271 data = stp_strtoxmlstr(stp_get_file_parameter(v, name));
2272 if (data)
2273 {
2274 stp_mxmlNewOpaque(node, data);
2275 stp_free(data);
2276 }
2277 break;
2278 case STP_PARAMETER_TYPE_RAW:
2279 stp_mxmlElementSetAttr(node, "type", "raw");
2280 data = stp_rawtoxmlstr(stp_get_raw_parameter(v, name));
2281 if (data)
2282 {
2283 stp_mxmlNewOpaque(node, data);
2284 stp_free(data);
2285 }
2286 break;
2287 case STP_PARAMETER_TYPE_ARRAY:
2288 stp_mxmlElementSetAttr(node, "type", "array");
2289 stp_mxmlAdd(node, STP_MXML_ADD_AFTER, NULL,
2290 stp_xmltree_create_from_array(stp_get_array_parameter(v, name)));
2291 break;
2292 case STP_PARAMETER_TYPE_DIMENSION:
2293 stp_mxmlElementSetAttr(node, "type", "dimension");
2294 stp_mxmlNewDimension(node, stp_get_dimension_parameter(v, name));
2295 break;
2296 default:
2297 stp_mxmlElementSetAttr(node, "type", "INVALID!");
2298 break;
2299 }
2300 }
2301 stp_string_list_destroy(list);
2302 }
2303 }
2304 return varnode;
2305 }