File: params.c

package info (click to toggle)
xgraph 11.3.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 536 kB
  • ctags: 1,071
  • sloc: ansic: 6,118; makefile: 260
file content (463 lines) | stat: -rw-r--r-- 11,533 bytes parent folder | download
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
/*
 * Xgraph Parameters
 *
 * This file contains routines for setting and retrieving
 * various X style display parameters for xgraph.
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include "st.h"
#include "params.h"

static int do_color(), do_font(), do_style(), do_bool();

/* For use by convenience macros */
params param_temp, *param_temp_ptr;
XColor param_null_color = { 0, 0, 0, 0, 0, 0 };
param_style param_null_style = { STYLE, 0, (char *) 0 };

static ST_TABLE *param_table = (ST_TABLE *) 0;

typedef struct param_full_defn {
    param_types type;
    char *text_form;
    params *real_form;
} param_full;


static Display *param_disp;
static Colormap param_cmap;
static int param_scrn;

static void free_resource();
static params *resolve_entry();
static int strihash();

#define DEF_INT		"0"
#define DEF_STR		""
#define DEF_FONT	"fixed"
#define DEF_PIXEL	"black"
#define DEF_STYLE	"1"
#define DEF_BOOL	"false"
#define DEF_DBL		"0.0"

#define DEF_MAX_FONT	1024
#define DEF_MAX_NAMES	10

#define DUP(str)	\
strcpy((char *) malloc((unsigned) (strlen(str)+1)), (str))


void param_init(disp, cmap)
Display *disp;			/* X Connection        */
Colormap cmap;			/* Colormap for colors */
/*
 * Initializes parameter package.  The display and colormap arguments
 * are used to resolve font and pixel values.
 */
{
    param_table = st_init_table(strcasecmp, strihash);
    param_disp = disp;
    param_cmap = cmap;
    /* This could also be a parameter for greater generality */
    param_scrn = DefaultScreen(disp);
}



void param_set(name, type, val)
char *name;			/* Name of parameter   */
param_types type;		/* Type                */
char *val;			/* Text form for value */
/*
 * Sets the parameter with the given name to have the type
 * `type' and the text value `value'.  This will be evaluated
 * to its full form the first time it is referenced using
 * param_get().  If it is already filled, the old value
 * will be reclaimed.
 */
{
    param_full *entry;

    if (!param_table) {
	(void) fprintf(stderr, "Parameter table not initialized\n");
	return;
    }
    if (st_lookup(param_table, name, (char **) &entry)) {
	if (entry->real_form) free_resource(entry->real_form);
	entry->real_form = (params *) 0;
    } else {
	entry = (param_full *) malloc(sizeof(param_full));
	entry->text_form = (char *) 0;
	entry->real_form = (params *) 0;
	(void) st_insert(param_table, DUP(name), (char *) entry);
    }
    entry->type = type;
    if (entry->text_form) (void) free((char *) (entry->text_form));
    entry->text_form = DUP(val);
}


void param_reset(name, val)
char *name;			/* Name of parameter   */
char *val;			/* Text form for value */
/*
 * This routine sets the value of an existing parameter to a new
 * value.  The type of the parameter remains the same.  Changes
 * in type should be done by using param_set() directly.
 */
{
    param_full *entry;

    if (!param_table) {
	(void) fprintf(stderr, "Parameter table not initialized\n");
	return;
    }
    if (st_lookup(param_table, name, (char **) &entry)) {
	param_set(name, entry->type, val);
    } else {
	(void) fprintf(stderr, "Cannot reset unknown parameter `%s'\n", name);
    }
}




params *param_get(name, val)
char *name;			/* Name of parameter */
params *val;			/* Result value      */
/*
 * Retrieves a value from the parameter table.  The value
 * is placed in `val'.  If successful, the routine will
 * return `val'.  Otherwise, it will return zero.
 */
{
    param_full *entry;

    if (!param_table) {
	(void) fprintf(stderr, "Parameter table not initialized\n");
	return (params *) 0;
    }
    if (st_lookup(param_table, name, (char **) &entry)) {
	if (!entry->real_form) {
	    entry->real_form = resolve_entry(name, entry->type, entry->text_form);
	}
	*val = *(entry->real_form);
	return val;
    } else {
	return (params *) 0;
    }
}


static void free_resource(val)
params *val;			/* Value to free */
/*
 * Reclaims a resource based on its type.
 */
{
    switch (val->type) {
    case INT:
    case STR:
    case BOOL:
    case DBL:
	/* No reclaimation necessary */
	break;
    case PIXEL:
	if ((val->pixv.value.pixel != WhitePixel(param_disp, param_scrn)) &&
	    (val->pixv.value.pixel != BlackPixel(param_disp, param_scrn))) {
	    XFreeColors(param_disp, param_cmap, &(val->pixv.value.pixel), 1, 0);
	}
	break;
    case FONT:
	XFreeFont(param_disp, val->fontv.value);
	break;
    case STYLE:
	(void) free(val->stylev.dash_list);
	break;
    }
    (void) free((char *) val);
}



static params *resolve_entry(name, type, form)
char *name;			/* Name of item for errors */
param_types type;		/* What type of thing */
char *form;			/* Textual form       */
/*
 * Allocates and returns an appropriate parameter structure
 * by translating `form' into its native type as given by `type'.
 * If it can't translate the given form, it will fall back onto
 * the default.
 */
{
    params *result = (params *) malloc(sizeof(params));

    result->type = type;
    switch (type) {
    case INT:
	if (sscanf(form, "%d", &result->intv.value) != 1) {
	    (void) fprintf(stderr,
			   "Parameter %s: can't translate `%s' into an integer (defaulting to `%s')\n",
			   name, form, DEF_INT);
	    result->intv.value = atoi(DEF_INT);
	}
	break;
    case STR:
	result->strv.value = form;
	break;
    case PIXEL:
	if (!do_color(form, &result->pixv.value)) {
	    (void) fprintf(stderr, "Parameter %s: can't translate `%s' into a color (defaulting to `%s')\n",
			   name, form, DEF_PIXEL);
	    (void) do_color(DEF_PIXEL, &result->pixv.value);
	}
	break;
    case FONT:
	if (!do_font(form, &result->fontv.value)) {
	    (void) fprintf(stderr, "Parameter %s: can't translate `%s' into a font (defaulting to `%s')\n",
			   name, form, DEF_FONT);
	    (void) do_font(DEF_FONT, &result->fontv.value);
	}
	break;
    case STYLE:
	if (!do_style(form, &result->stylev)) {
	    (void) fprintf(stderr, "Parameter %s: can't translate `%s' into a line style (defaulting to `%s')\n",
			   name, form, DEF_STYLE);
	    (void) do_style(DEF_STYLE, &result->stylev);
	}
	break;
    case BOOL:
	if (!do_bool(form, &result->boolv.value)) {
	    (void) fprintf(stderr, "Parameter %s: can't translate `%s' into a boolean flag (defaulting to `%s')\n",
			   name, form, DEF_BOOL);
	    (void) do_bool(DEF_BOOL, &result->boolv.value);
	}
	break;
    case DBL:
	if (sscanf(form, "%lf", &result->dblv.value) != 1) {
	    (void) fprintf(stderr,
			   "Parameter %s: can't translate `%s' into a double (defaulting to `%s')\n",
			   name, form, DEF_DBL);
	    result->dblv.value = atof(DEF_DBL);
	}
	break;
    }
    return result;
}



static int do_color(name, color)
char *name;			/* Name for color */
XColor *color;			/* Returned color */
/*
 * Translates `name' into a color and attempts to get the pixel
 * for the color using XAllocColor().
 */
{
    int result = 1;

    if (XParseColor(param_disp, param_cmap, name, color)) {
	if (strcasecmp(name, "black") == 0) {
	    color->pixel = BlackPixel(param_disp, param_scrn);
	    XQueryColor(param_disp, param_cmap, color);
	} else if (strcasecmp(name, "white") == 0) {
	    color->pixel = WhitePixel(param_disp, param_scrn);
	    XQueryColor(param_disp, param_cmap, color);
	} else {
	    result = XAllocColor(param_disp, param_cmap, color);
	}
    } else {
	result = 0;
    }
    return result;
}



static int do_font(name, font_info)
char *name;			/* Name of desired font      */
XFontStruct **font_info;	/* Returned font information */
/*
 * This routine translates a font name into a font structure.  The
 * font name can be in two forms.  The first form is <family>-<size>.
 * The family is a family name (like helvetica) and the size is
 * in points (like 12).  If the font is not in this form, it
 * is assumed to be a regular X font name specification and
 * is looked up using the standard means.
 */
{
    char name_copy[DEF_MAX_FONT], query_spec[DEF_MAX_FONT];
    char *font_family, *font_size, **font_list;
    int font_size_value, font_count, i;

    /* First attempt to interpret as font family/size */
    (void) strcpy(name_copy, name);
    if ((font_size = index(name_copy, '-'))) {
	*font_size = '\0';
	font_family = name_copy;
	font_size++;
	font_size_value = atoi(font_size);
	if (font_size_value > 0) {
	    /* Still a little iffy -- what about weight and roman vs. other */
	    (void) sprintf(query_spec,
			   "*-*-%s-medium-r-normal-*-*-%d-*-*-*-*-iso8859-*",
			   font_family, font_size_value * 10);
	    font_list = XListFonts(param_disp, query_spec,
				   DEF_MAX_NAMES, &font_count);
	    
	    /* Load first one that you can */
	    for (i = 0;  i < font_count;  i++) {
		if ((*font_info = XLoadQueryFont(param_disp, font_list[i]))) {
		    break;
		}
	    }
	    if (*font_info) return 1;
	}
    }
    /* Assume normal font name */
    return (int) (*font_info = XLoadQueryFont(param_disp, name));
}


static int do_style(list, val)
char *list;			/* List of ones and zeros */
param_style *val;		/* Line style returned    */
/*
 * Translates a string representation of a dash specification into
 * a form suitable for use in XSetDashes().  Assumes `list'
 * is a null terminated string of ones and zeros.
 */
{
    char *i, *spot, last_char;
    int count;

    for (i = list;  *i;  i++) {
	if ((*i != '0') && (*i != '1')) break;
    }
    if (!*i) {
	val->len = 0;
	last_char = '\0';
	for (i = list;  *i;  i++) {
	    if (*i != last_char) {
		val->len += 1;
		last_char = *i;
	    }
	}
	val->dash_list = (char *) malloc((unsigned) (sizeof(char)*val->len + 1));
	last_char = *list;
	spot = val->dash_list;
	count = 0;
	for (i = list;  *i;  i++) {
	    if (*i != last_char) {
		*spot++ = (char) count;
		last_char = *i;
		count = 1;
	    } else {
		count++;
	    }
	}
	*spot = (char) count;
	return 1;
    } else {
	return 0;
    }
}


static char *positive[] = { "on", "yes", "true", "1", "affirmative", (char *) 0 };
static char *negative[] = { "off", "no", "false", "0", "negative", (char *) 0 };

static int do_bool(name, val)
char *name;			/* String representation */
int *val;			/* Returned value        */
/*
 * Translates a string representation into a suitable binary value.
 * Can parse all kinds of interesting boolean type words.
 */
{
    char **term;

    for (term = positive;  *term;  term++) {
	if (strcasecmp(name, *term) == 0) break;
    }
    if (*term) {
	*val = 1;
	return 1;
    }
    for (term = negative;  *term;  term++) {
	if (strcasecmp(name, *term) == 0) break;
    }
    if (*term) {
	*val = 0;
	return 1;
    }
    return 0;
}



/*ARGSUSED*/
static enum st_retval dump_it(key, value, arg)
char *key, *value, *arg;
{
    param_full *val = (param_full *) value;

    printf("%s (", key);
    switch (val->type) {
    case INT:
	printf("INT");
	break;
    case STR:
	printf("STR");
	break;
    case PIXEL:
	printf("PIXEL");
	break;
    case FONT:
	printf("FONT");
	break;
    case STYLE:
	printf("STYLE");
	break;
    case BOOL:
	printf("BOOL");
	break;
    case DBL:
	printf("DBL");
	break;
    }
    printf(") = %s\n", val->text_form);
    return ST_CONTINUE;
}

void param_dump()
/*
 * Dumps all of the parameter values to standard output.
 */
{
    st_foreach(param_table, dump_it, (char *) 0);
}



static int strihash(string, modulus)
register char *string;
int modulus;
/* Case insensitive computation */
{
    register int val = 0;
    register int c;
    
    while ((c = *string++) != '\0') {
	if (isupper(c)) c = tolower(c);
	val = val*997 + c;
    }

    return ((val < 0) ? -val : val)%modulus;
}