-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.c
106 lines (82 loc) · 1.78 KB
/
config.c
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
/*
* config.c
*
* Configuration module
*
* (C)1999 Stefano Busti
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "string.h"
cfg_item_i cfg_int_defs[CFG_IEND] = {
{"comm_baud", 9600},
{"comm_databits", 8},
{"comm_stopbits", 1},
{"comm_port", 0},
{"net_port", 3331},
{"timeout", 300}
};
cfg_item_s cfg_str_defs[CFG_SEND] = {
{"comm_parity", "none"},
{"net_allow", "all"},
{"net_deny", "none"}
};
int cfg_init(cfg_s *cfg, int comm_port)
{
int i;
char buf[BUFSIZ];
sprintf(buf, "%d", comm_port);
for (i = 0; i < CFG_IEND; i++)
{
cfg->ints[i].key = NULL;
str_assign(&cfg->ints[i].key, cfg_int_defs[i].key);
if (comm_port > 0)
str_cat(&cfg->ints[i].key, buf);
cfg->ints[i].val = cfg_int_defs[i].val;
}
for (i = 0; i < CFG_SEND; i++)
{
cfg->strs[i].key = NULL;
str_assign(&cfg->strs[i].key, cfg_str_defs[i].key);
if (comm_port > 0)
str_cat(&cfg->strs[i].key, buf);
cfg->strs[i].val = NULL;;
str_assign(&cfg->strs[i].val, cfg_str_defs[i].val);
}
return 0;
}
void cfg_cleanup(cfg_s *cfg)
{
int i;
for (i = 0; i < CFG_IEND; i++)
{
str_cleanup(&cfg->ints[i].key);
}
for (i = 0; i < CFG_SEND; i++)
{
str_cleanup(&cfg->strs[i].key);
str_cleanup(&cfg->strs[i].val);
}
}
int cfg_fromfile(cfg_s *cfg, char *filename)
{
int i, port;
port = cfg->ints[CFG_IPORT].val;
/* Read integers */
for (i = 0; i < CFG_IEND; i++)
cfg_readint(filename, &cfg->ints[i], cfg->ints[i].val);
/* Read strings */
for (i = 0; i < CFG_SEND; i++)
cfg_readstr(filename, &cfg->strs[i], cfg->strs[i].val);
return 0;
}
void cfg_assign(cfg_s *dst, cfg_s *src)
{
int i;
for (i = 0; i < CFG_IEND; i++)
dst->ints[i].val = src->ints[i].val;
for (i = 0; i < CFG_SEND; i++)
str_assign(&dst->strs[i].val, src->strs[i].val);
}