aboutsummaryrefslogtreecommitdiff
path: root/common/config.c
blob: ff55595375fb63afd7e17d6039e19b71a508b455 (plain)
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
#include <libconfig.h>
#include <stdlib.h>

#include "config.h"

config_t * config = NULL;

char // returns 0 if ok, greater than 0 otherwise
config_load(const char *config_file)
{
    config = malloc(sizeof(config_t));
    config_init(config);
    config_set_tab_width(config, 4);
    if (config_read_file(config, config_file) != CONFIG_TRUE) {
        switch(config_error_type(config)) {
          case CONFIG_ERR_NONE:
            fprintf(stderr, "Configuration read error with none type reported... This shouldn't happen!\n");
            break;
          case CONFIG_ERR_FILE_IO:
            fprintf(stderr, "Configuration I/O error, the most common cause is a file not found at %s\n", CONFIG_PATH);
            break;
          case CONFIG_ERR_PARSE:
            fprintf(stderr, "Configuration parse error\n");
            break;
        }
        fprintf(stderr, "Configuration read error occured at %s:%d %s\n", config_error_file(config), config_error_line(config), config_error_text(config));
        return 1;
    }
    return 0;

}

const char * config_get_web_url(void)
{
    const char * url;
    if (config_lookup_string(config, "web_url", &url) != CONFIG_TRUE) {
        return DEFAULT_WEB_URL;
    }
    return url;
}

const char * config_get_wss_url(void)
{
    const char * url;
    if (config_lookup_string(config, "wss_url", &url) != CONFIG_TRUE) {
        return DEFAULT_WSS_URL;
    }
    return url;
}

const char * config_get_login(void)
{
    const char * login;
    if (config_lookup_string(config, "login", &login) != CONFIG_TRUE) {
        return NULL;
    }
    return login;
}

const char * config_get_password(void)
{
    const char * password;
    if (config_lookup_string(config, "password", &password) != CONFIG_TRUE) {
        return NULL;
    }
    return password;
}

void config_clean(void)
{
    if (config != NULL) {
        config_destroy(config);
        free(config);
        config = NULL;
    }
}