-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
105 lines (98 loc) · 1.97 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
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include "config.h"
typedef struct _var {
char var[MAXLEN]; /* variable name */
union { /* value */
char value[MAXLEN];
int val;
};
} variables;
typedef struct _config {
int id; /* id header */
char *var; /* name of header */
variables vr[MAXVARIABLES]; /* variables in my header */
} Config;
Config ConfigIndex[] = {
{ 0, "[general]",
{ { "device", 1 } },
},
{ 1, "[mixer]",
{ { "phonehead", 0 }, { "phoneviasysctl", 0 }, { "out", 0 }, },
},
{ 2, "[window]",
{ { "pixel", 0 }, { "red", 0 }, { "green", 0 }, { "blue", 0 }, { "font", 0 } }
}
};
int
parseconfig (char *config) {
char str[MAXLEN];
char variable[MAXLEN];
char value[MAXLEN];
int res, id=-1, error;
int i;
FILE* f;
f = fopen(config, "r");
while (!feof(f))
{
if(fgets(str, READFBUF, f))
{
res = sscanf(str, "%[^ ^\t^\n^=] = %[^\t^\n;#]", variable, value);
if (!res) continue;
if (res==1)
{
error=YES;
for (i=0; i<INDEXLEN(ConfigIndex,Config); i++)
{
if (!strcmp(ConfigIndex[i].var, variable))
{
id = i;
error=NO;
break;
}
}
PERROR("Config \"%s\" is not correct!\n", config);
}
if (res==2)
{
error=YES;
for (i=0; i<MAXVARIABLES; i++)
{
if (!strcmp(ConfigIndex[id].vr[i].var, variable))
{
error=NO;
strcpy(ConfigIndex[id].vr[i].value, value);
continue;
};
}
PERROR("Config \"%s\" is not correct!\n", config);
}
}
CLEAR(variable, value);
}
fclose(f);
return 0;
}
const char
*get_variable(const char *global, const char *variables)
{
int i,id;
i=id=0;
for (i=0; i<INDEXLEN(ConfigIndex,Config); i++)
{
if (!strcmp(ConfigIndex[i].var, global))
{
for (id=0; id<MAXVARIABLES; id++)
{
if (!strcmp(ConfigIndex[i].vr[id].var, variables))
{
return ConfigIndex[i].vr[id].value;
}
}
break;
}
}
return NULL;
}