-
Notifications
You must be signed in to change notification settings - Fork 0
/
configparser.c
158 lines (121 loc) · 3.2 KB
/
configparser.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
146
147
148
149
150
151
152
153
154
155
156
157
#include "configparser.h"
char *read_line (FILE *const file, const int buffer_size)
{
char c;
char *line = malloc(sizeof(char) * buffer_size);
for (int i = 0; i < buffer_size; i++)
line[i] = 0;
int pos = 0;
c = getc(file);
while(c != '\n' && c != EOF)
{
line[pos] = c;
if (++pos >= buffer_size)
break;
c = getc(file);
}
if (c == EOF && pos == 0)
{
free(line);
return NULL;
}
if (c == '\n' && pos == 0) // removes empty lines
{
free(line);
return read_line(file, buffer_size);
}
if (line[0] == '#')
{
free(line);
return read_line(file, buffer_size);
}
char *ptr = strrchr(line, '#');
if (ptr)
*ptr = '\0';
return line;
}
void configparser_init (configparser *const cp, char *const file, const int size, const char delimiter)
{
cp->file = fopen(file, "r"); // open file
if (! cp->file)
logcmd(LOG_ERROR_IO, "configparser: configparser_init: could not open config file (check ~/.config/clapl/config");
cp->delimiter = delimiter;
cp->ht = ht_create(size); // create hashtable
if (! cp->ht)
logcmd(LOG_ERROR_MALLOC, "configparser: configparser_init: hashtable could not be created");
}
void configparser_free (configparser **cp)
{
ht_free(&(*cp)->ht);
free(*cp);
*cp = NULL;
}
void configparser_init_file (configparser *const cp, FILE *file, const int size, const char delimiter)
{
cp->file = file;
cp->delimiter = delimiter;
cp->ht = ht_create(size);
if (! cp->ht)
logcmd(LOG_ERROR_MALLOC, "configparser: configparser_init: hashtable could not be created");
}
void configparser_load (configparser *const cp)
{
char *line = read_line (cp->file, 255);
char *value = NULL;
while (line)
{
value = configparser_split_string(line, cp->delimiter);
ht_set(cp->ht, line, value);
free(line);
value = NULL;
line = read_line(cp->file, 255);
}
if (line)
free(line);
}
char *configparser_split_string (char *const string, const char delimiter)
{
char *new = strchr(string, delimiter);
if (!new)
return NULL;
*new = '\0';
return ++new;
}
int configparser_get_bool (configparser *const cp, char *const key)
{
char *value = ht_get(cp->ht, key);
switch(*value)
{
case 't': case 'T':
return 1;
case 'f': case 'F':
return 0;
case '0':
return 0;
case '1':
return 1;
default:
return 0;
}
return 0;
}
int configparser_get_int (configparser *const cp, char *const key)
{
char *value = ht_get(cp->ht, key);
if (value)
return (atoi(value));
return 0;
}
char *configparser_get_string (configparser *const cp, char *const key)
{
char *value = ht_get(cp->ht, key);
if (value)
{
// copies the string in to a new block of memory
// this way the configparser never looses its data due to unexpected memory frees
char *ptr = malloc(sizeof(char) * (strlen(value) + 1));
strcpy(ptr, value);
return ptr;
}
return NULL;
}