-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
122 lines (104 loc) · 4.43 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
struct server_config {
char default_ip[16];
int default_port;
char default_dir[512];
int compression;
unsigned long long max_buffer;
unsigned long long receive_buffer;
};
typedef struct server_config s_conf;
s_conf parse_config(char *file_path, int debug_display){
FILE *fp;
char *file_buff;
long file_size;
char *conf_array;
char param_name[64];
char param_value[512];
int param_number;
s_conf config_store;
if((fp = fopen(file_path, "r")) != NULL){
// Get File Size
fseek(fp, 0L, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
file_buff = malloc(file_size + 1);
fread(file_buff, sizeof(char), file_size + 1, fp);
conf_array = strtok(file_buff, "\r\n");
free(file_buff);
while(conf_array != NULL){
if(conf_array[0] == '#'){
sscanf(conf_array, "#%s %s", param_name, param_value);
// Place Appropriately
if(strcmp(param_name, "default_ip") == 0){
strcpy(config_store.default_ip, param_value);
}
if(strcmp(param_name, "default_port") == 0){
sscanf(param_value, "%d", &config_store.default_port);
}
if(strcmp(param_name, "default_dir") == 0){
strcpy(config_store.default_dir, param_value);
}
if(strcmp(param_name, "compression") == 0){
sscanf(param_value, "%d", &config_store.compression);
}
if(strcmp(param_name, "max_buffer") == 0){
config_store.max_buffer = 0;
if(strstr(param_value, "KB") != NULL){
sscanf(param_value, "%dKB", ¶m_number);
config_store.max_buffer = (unsigned long long)param_number * 1024;
}
if(strstr(param_value, "MB") != NULL){
sscanf(param_value, "%dMB", ¶m_number);
config_store.max_buffer = (unsigned long long)param_number * 1024 * 1024;
}
if(strstr(param_value, "GB") != NULL){
sscanf(param_value, "%dGB", ¶m_number);
config_store.max_buffer = (unsigned long long)param_number * 1024 * 1024 * 1024;
}
}
if(strcmp(param_name, "receive_buffer") == 0){
config_store.receive_buffer = 0;
if(strstr(param_value, "KB") != NULL){
sscanf(param_value, "%dKB", ¶m_number);
config_store.receive_buffer = (unsigned long long)param_number * 1024;
}
if(strstr(param_value, "MB") != NULL){
sscanf(param_value, "%dMB", ¶m_number);
config_store.receive_buffer = (unsigned long long)param_number * 1024 * 1024;
}
if(strstr(param_value, "GB") != NULL){
sscanf(param_value, "%dGB", ¶m_number);
config_store.receive_buffer = (unsigned long long)param_number * 1024 * 1024 * 1024;
}
if(config_store.receive_buffer == 0){
// Default Buffer
config_store.receive_buffer = 4096;
}
}
}
conf_array = strtok(NULL, "\r\n");
}
fclose(fp);
}else{
printf("Error: Config File Not Found\n");
strcpy(config_store.default_ip, "127.0.0.1");
config_store.default_port = 8080;
strcpy(config_store.default_dir, "htdocs");
config_store.compression = 0;
config_store.max_buffer = 0;
}
if(debug_display == 1){
printf("Configuration Loaded:\n");
printf("Default IP: %s\n", config_store.default_ip);
printf("Default Port: %d\n", config_store.default_port);
printf("Default Directory: %s\n", config_store.default_dir);
if(config_store.compression == 0){
printf("Compression: Disabled\n");
}else{
printf("Compression: Enabled\n");
}
printf("Receive Buffer: %llu Byte(s)\n", config_store.receive_buffer);
printf("Send Buffer: %llu Byte(s)\n\n", config_store.max_buffer);
}
return config_store;
}