-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.c
199 lines (159 loc) · 4.53 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
static void skip_whitespaces(char **ptr) {
while(0x00 != **ptr && (' ' == **ptr || '\t' == **ptr || '\n' == **ptr)) *ptr+=1;
}
static int parse_section(char *ptr) {
int n = 0;
while (']' != *ptr && 0x00 != *ptr) {
n++;
ptr++;
}
return n;
}
static int parse_option_key_and_skip(char **ptr) {
int n=0, is_equal_found=0;
while (' ' != **ptr && '\t' != **ptr && 0 != **ptr) {
if ('=' == **ptr) {
is_equal_found = 1;
break;
}
n++;
(*ptr)++;
}
skip_whitespaces(ptr);
if(1 == is_equal_found) {
(*ptr)++;
} else {
while ('=' != **ptr && 0 != **ptr) {
(*ptr)++;
}
(*ptr)++;
}
return n;
}
static int parse_option_value_and_skip(char **ptr) {
int n=0, whitespace_count=0;
char *_ptr;
while (1) {
whitespace_count = 0;
_ptr = *ptr;
while(' ' == *_ptr || '\t' == *_ptr) {
whitespace_count += 1;
_ptr = (*ptr) + whitespace_count;
}
if ('\n' == **ptr) {
return n;
}
(*ptr) += whitespace_count;
n += whitespace_count;
(*ptr)++;
n++;
}
return n;
}
static int config_get_return_id(struct config *conf, char *section, char *option) {
int j;
struct config_option* opt;
for (j = 0; j < conf->options_count; j++) {
opt = conf->options[j];
if (strlen(section) != opt->section_len) {
continue;
}
if (0 == strncmp(opt->section, section, opt->section_len)) {
if (0 == strncmp(opt->key, option, opt->key_len)) {
return j;
}
}
}
return -1;
}
struct config *init_config() {
struct config *conf;
conf = malloc(sizeof(struct config));
memset(conf, 0x00, sizeof(struct config));
return conf;
}
void config_read(struct config *conf, char *path) {
FILE* fp;
long len;
char *content, *ptr;
int current_content_id, section_len=0, id;
char* section = NULL;
char tmp_section[512] = "", tmp_option[512] = "";
struct config_option *opt = NULL;
fp = fopen(path, "r");
if (fp == NULL) {
return;
}
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (len < 0) {
return;
}
content = malloc(len + 1);
memset(content, 0x00, len + 1);
fread(content, len, 1, fp);
conf->contents_count += 1;
conf->contents = (char **)realloc(conf->contents, sizeof(char **) * conf->contents_count);
current_content_id = conf->contents_count - 1;
conf->contents[current_content_id] = content;
ptr = content;
while (0x00 != *ptr) {
opt = (struct config_option*)malloc(sizeof(struct config_option));
skip_whitespaces(&ptr);
if (0x00 == *ptr) {
break;
}
if ('[' == *ptr) {
ptr++;//skip [
section = ptr;
section_len = parse_section(ptr);
ptr += section_len;
ptr++;//skip ]
skip_whitespaces(&ptr);
}
opt->section = section;
opt->section_len = section_len;
skip_whitespaces(&ptr);
opt->key = ptr;
opt->key_len = parse_option_key_and_skip(&ptr);
skip_whitespaces(&ptr);
opt->value = ptr;
opt->value_len = parse_option_value_and_skip(&ptr);
strncpy(tmp_section, section, section_len);
tmp_section[section_len] = '\0';
strncpy(tmp_option, opt->key, opt->key_len);
tmp_option[opt->key_len] = '\0';
id = config_get_return_id(conf, tmp_section, tmp_option);
if (id < 0) {
conf->options_count += 1;
conf->options = (struct config_option**)realloc(conf->options, sizeof(struct config_option**) * conf->options_count);
conf->options[conf->options_count-1] = opt;
}
else {
free(conf->options[id]);
conf->options[id] = opt;
}
}
}
struct config_option* config_get(struct config *conf, char *section, char *option) {
struct config_option* opt;
int j = config_get_return_id(conf, section, option);
if (-1 == j) {
return NULL;
}
return conf->options[j];
}
void release_config(struct config **conf) {
int j;
for (j = 0; j < (*conf)->contents_count; j++) {
free((*conf)->options[j]);
free((*conf)->contents[j]);
}
free(*conf);
*conf = NULL;
}