aboutsummaryrefslogtreecommitdiffstats
path: root/gcfg.c
blob: 90148e419b2b0765486b2e7e9828e323d158c333 (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
77
78
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#include "gcfg.h"

const gcfg_opt *gcfg_opt_find(const gcfg_opt *opts, int nopts,
                              char *arg)
{
    const gcfg_opt *opt = NULL;
    int i;

    if (arg[0] != '-' && arg[0] != '+')
        return NULL;
    for (i = 0; i < nopts; i++) {
        if (strcmp(opts[i].opt, arg + 1) == 0) {
            opt = opts + i;
            break;
        }
    }
    if (!opt)
        return NULL;
    if (arg[0] == '+' && !opt->is_bool)
        return NULL;
    return opt;
}

void gcfg_set(GKeyFile *cfg, char *key, char *value)
{
    g_key_file_set_string(cfg, GCFG_GROUP_CMDLINE, key, value);
}

char *gcfg_get(GKeyFile *cfg, const char *key)
{
    char *profile;
    char *value;

    profile = g_key_file_get_string(cfg, GCFG_GROUP_CMDLINE,
                                    GCFG_KEY_PROFILE, NULL);

    value = g_key_file_get_string(cfg, GCFG_GROUP_CMDLINE, key, NULL);
    if (value)
        return value;

    if (profile) {
        value = g_key_file_get_string(cfg, profile, key, NULL);
        if (value)
            return value;
    }

    value = g_key_file_get_string(cfg, GCFG_GROUP_DEFAULT, key, NULL);
    if (value)
        return value;

    return NULL;
}

gcfg_bool gcfg_get_bool(GKeyFile *cfg, const char *key)
{
    char *value;

    value = gcfg_get(cfg, key);
    if (!value)
        return GCFG_BOOL_UNSET;

    if (strcasecmp(value, "true") == 0 ||
        strcasecmp(value, "on") == 0)
        return GCFG_BOOL_TRUE;

    if (strcasecmp(value, "false") == 0 ||
        strcasecmp(value, "off") == 0)
        return GCFG_BOOL_FALSE;

    return GCFG_BOOL_UNSET;
}