-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf.c
55 lines (46 loc) · 1.39 KB
/
conf.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
#include "conf.h"
#include "ini.h"
#include <string.h>
#include <stdlib.h>
#define DEFAULT_PORT 80
#define DEFAULT_ROOT "/var/www/"
#define DEFAULT_SERVER_NAME "localhost"
#define DEFAULT_CONF_FILE "conf.ini"
static int conf_handler(void* user, const char* section, const char* name,
const char* value)
{
configuration* pconfig = (configuration*)user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("http", "listen")) {
pconfig->port = atoi(value);
} else if (MATCH("http", "root")) {
size_t l = strlen(value);
if (value[l - 1] == '/') l--;
pconfig->root = strndup(value, l);
} else if (MATCH("http", "server_name")) {
pconfig->server_name = strdup(value);
} else if (MATCH("http", "index")) {
pconfig->index = strdup(value);
} else if (MATCH("http", "not_found")) {
pconfig->not_found = strdup(value);
} else {
return 0;
}
return 1;
}
void conf_init()
{
global_settings.inited = 1;
global_settings.port = DEFAULT_PORT;
global_settings.root = DEFAULT_ROOT;
global_settings.server_name = DEFAULT_SERVER_NAME;
conf_load(DEFAULT_CONF_FILE);
}
void conf_load(const char* filename)
{
if (ini_parse(filename, conf_handler, &global_settings) < 0) {
global_settings.inited = 0;
return;
}
global_settings.inited = 2;
}