-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
67 lines (58 loc) · 1.72 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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "optparser.h"
#include "utils.h"
Config *create_config(int argc, char *argv[]) {
Config *config = malloc(sizeof(Config));
config->host = HOST;
config->port = PORT;
if (argc == 1) {
return config;
} else {
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0) {
printf("%s\n", FRLS_VERSION);
exit(0);
} else if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
printf("%s\n", HELP_MESSAGE);
exit(0);
}
size_t length;
ArgKV *args = parse_options(argc, argv, &length);
ArgKV *ptr = args;
ArgKV *end = args + length;
while (ptr < end) {
if (strcmp(ptr->key, "host") == 0) {
config->host = malloc(sizeof(char) * strlen(ptr->value) + 1);
strncpy(config->host, ptr->value, strlen(ptr->value));
config->host[strlen(ptr->value)] = '\0';
} else if (strcmp(ptr->key, "port") == 0) {
config->port = atoi(ptr->value);
}
free(ptr->key);
free(ptr->value);
ptr++;
}
free(args);
}
return config;
}
void print_config(Config *config) {
log_info("Config contents:");
printf("Host: %s\n", config->host);
printf("Port: %d\n", config->port);
printf("Project root: %s\n", config->project_root);
printf("Client process id: %d\n", config->client_process_id);
printf("Client name: %s\n", config->client_name);
printf("Client version: %s\n", config->client_version);
}
void destroy_config(Config *config) {
free(config->host);
free(config->project_root);
free(config->client_name);
free(config->client_version);
cJSON_free(config->client_capabilities);
free(config);
}