-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
executable file
·52 lines (41 loc) · 1.22 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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "utility.h"
#define MAXLINESIZE 256
static void parsePairs(connectionProfile *profile, const char *key, const char *value) {
if (strncmp(key, "hostname", 8) == 0) {
profile->hostname = strdup(value);
} else if (strncmp(key, "portnumber", 10) == 0) {
profile->portnumber = strtoul(value, NULL, 10);
} else if (strncmp(key, "gamekindname", 12) == 0) {
profile->gamekindname = strdup(value);
}
}
connectionProfile *readConfig(connectionProfile *profile, const char *filename) {
FILE *file = fopen(filename, "r");
if (NULL == file) {
perror("fopen");
return NULL;
}
char line[MAXLINESIZE];
while (fgets(line, sizeof(line), file) != NULL) {
int n_tokens = 0;
char **tokens = splitBy(line, " =\n");
if (NULL == tokens) {
perror("splitBy");
return NULL;
}
char **p = tokens;
while (*p != NULL) {
n_tokens++;
p++;
}
if (n_tokens >= 1) parsePairs(profile, tokens[0], tokens[1]);
free(tokens);
}
fclose(file);
return profile;
}