File: param.c

package info (click to toggle)
klustakwik 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 168 kB
  • ctags: 119
  • sloc: cpp: 812; ansic: 103; makefile: 21
file content (104 lines) | stat: -rw-r--r-- 2,005 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
#include "param.h"

typedef struct entry_t {
	int t;
	char *name;
	void *addr;
	struct entry_t *next;
} entry;


entry *top, *bottom;
int argc;
char **argv;
extern char HelpString[];

char help = 0;

/* returns 1 if the parameter was found and changed, else zero. */
int change_param(char *name, char *value)
{
	entry *e;
	int changed = 0;

	for(e=bottom; e; e = e->next) if (!strcmp(name, e->name)) {
		switch (e->t) {
		case FLOAT:
			*((float *) e->addr) = atof(value); break;
		case INT: 
			*((int *) e->addr) = atoi(value); break;
		case BOOLEAN: 
			if (*value == '0')
				*((char *) e->addr) = 0;
			else
				*((char *) e->addr) = 1;
			break;
		case STRING: 
			strncpy((char *)e->addr, value, STRLEN); break;
		}
		changed = 1;
		break;
	}
	return changed;
}

void init_params(int ac, char **av)
{
	argc = ac;
	argv = av;
}

void search_command_line(char *name)
{
	int i;

	for(i=0; i<argc-1; i++)
		if (argv[i][0] == '-' && !strcmp(argv[i]+1, name))
			change_param(argv[i] + 1, argv[i+1]);
	if (argv[argc-1][0] == '-' && !strcmp(argv[argc-1]+1, name))
		change_param(argv[argc-1] + 1, "");
}

void add_param(int t, char *name, void *addr)
{
	entry *e;
	if (top == NULL) {
		bottom = top = e = (entry *) malloc(sizeof(entry));
	} else {
		e = (entry *) malloc(sizeof(entry));
		top->next = e;
		top = e;
	}
	if (e == NULL) {printf("parameter manager out of memory!\n"); exit(1);}
	top->t = t;
	top->name = name;
	top->addr = addr;
	top->next = NULL;
	search_command_line(name);
}

void print_params(FILE *fp)
{
	entry *e;
	int changed = 0;

	add_param(BOOLEAN, "help", &help);
	if (help) {
		fprintf(fp, HelpString);
	}
	
	for(e=bottom; e; e = e->next) {
		fprintf(fp, "%s\t", e->name);
		switch (e->t) {
		case FLOAT:
			fprintf(fp, "%f\n", *(float *)(e->addr)); break;
		case INT:
			fprintf(fp, "%d\n", *(int *)(e->addr)); break;
		case BOOLEAN:
			fprintf(fp, "%d\n", *(char *)(e->addr)); break;
		case STRING:
			fprintf(fp, "%s\n", (char *)(e->addr)); break;
		}
	}
	if (help) exit(0);
}