-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_config.c
145 lines (121 loc) · 4.68 KB
/
parse_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
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
/* parse_config.c
Configuration file parser
*/
#include "parse_config.h"
#define DEBUG
server_config s_config;
device_config ire_config;
device_config kt15_config;
static int s_handler(void* user, const char* section, const char* name, const char* value)
{
server_config* sconfig = (server_config*)user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("server", "ip")) {
sconfig->ip = strdup(value);
} else if (MATCH("server", "controlport")) {
sconfig->controlport = atoi(value);
} else {
return 0; /* unknown section/name, error */
}
return 1;
}
static int kt15_handler(void* user, const char* section, const char* name, const char* value)
{
device_config* dconfig = (device_config*)user;
char device_id[] = "KT15";
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH(device_id, "name")) {
dconfig->name = strdup(value);
} else if (MATCH(device_id, "outputprotocol")) {
dconfig->outputprotocol = strdup(value);
} else if (MATCH(device_id, "inputprotocol")) {
dconfig->inputprotocol = strdup(value);
} else if (MATCH(device_id, "commandport")) {
dconfig->commandport = atoi(value);
} else if (MATCH(device_id, "outputport")) {
dconfig->outputport = atoi(value);
} else if (MATCH(device_id, "outputfreq")) {
dconfig->outputfreq = atoi(value);
} else if (MATCH(device_id, "serialdevice")) {
dconfig->serial_device = strdup(value);
} else if (MATCH(device_id, "baudrate")) {
dconfig->baud_rate = atoi(value);
} else if (MATCH(device_id, "bits")) {
dconfig->bits = atoi(value);
} else if (MATCH(device_id, "parity")) {
dconfig->parity = atoi(value);
} else if (MATCH(device_id, "stopbits")) {
dconfig->stop_bit = atoi(value);
} else {
return 0; /* unknown section/name, error */
}
return 1;
}
static int ire_handler(void* user, const char* section, const char* name, const char* value)
{
device_config* dconfig = (device_config*)user;
char device_id[] = "IRE";
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH(device_id, "name")) {
dconfig->name = strdup(value);
} else if (MATCH(device_id, "outputprotocol")) {
dconfig->outputprotocol = strdup(value);
} else if (MATCH(device_id, "inputprotocol")) {
dconfig->inputprotocol = strdup(value);
} else if (MATCH(device_id, "commandport")) {
dconfig->commandport = atoi(value);
} else if (MATCH(device_id, "outputport")) {
dconfig->outputport = atoi(value);
} else if (MATCH(device_id, "outputfreq")) {
dconfig->outputfreq = atoi(value);
} else if (MATCH(device_id, "serialdevice")) {
dconfig->serial_device = strdup(value);
} else if (MATCH(device_id, "baudrate")) {
dconfig->baud_rate = atoi(value);
} else if (MATCH(device_id, "bits")) {
dconfig->bits = atoi(value);
} else if (MATCH(device_id, "parity")) {
dconfig->parity = atoi(value);
} else if (MATCH(device_id, "stopbits")) {
dconfig->stop_bit = atoi(value);
} else {
return 0; /* unknown section/name, error */
}
return 1;
}
int read_config(void)
{
#ifdef DEBUG
printf("read_config()\n");
#endif
int error = 0;
if (ini_parse("config.ini", s_handler, &s_config) < 0) {
printf("Can't load 'config.ini'\n");
return 1;
}
if ((error=ini_parse("config.ini", kt15_handler, &kt15_config)) < 0) {
printf("Can't load 'config.ini'\n");
return 1;
}
/*printf("DEBUG: error = %d\n",error);*/
if (ini_parse("config.ini", ire_handler, &ire_config) < 0) {
printf("Can't load 'config.ini'\n");
return 1;
}
printf("Config loaded from 'config.ini':\n");
printf("\t server ip = %s\n", s_config.ip);
printf("\t server control port = %d\n", s_config.controlport);
printf("\n\t device name = %s\n", kt15_config.name);
printf("\t device output protocol = %s\n", kt15_config.outputprotocol);
printf("\t device input protocol = %s\n", kt15_config.inputprotocol);
printf("\t device command port = %d\n", kt15_config.commandport);
printf("\t device output port = %d\n", kt15_config.outputport);
printf("\t device output freq = %d Hz\n", kt15_config.outputfreq);
printf("\n\t device name = %s\n", ire_config.name);
printf("\t device output protocol = %s\n", ire_config.outputprotocol);
printf("\t device input protocol = %s\n", ire_config.inputprotocol);
printf("\t device command port = %d\n", ire_config.commandport);
printf("\t device output port = %d\n", ire_config.outputport);
printf("\t device output freq = %d Hz\n", ire_config.outputfreq);
return 0;
}