aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/opendeco-conf.c
blob: e044c9064d13649958c7853919ef9bd650f9ee94 (plain) (blame)
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
/* SPDX-License-Identifier: MIT-0 */

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

#include "opendeco-conf.h"
#include "toml/toml.h"

int opendeco_conf_parse(const char *confpath, struct arguments *arguments)
{
    FILE *fp;
    char errbuf[200];

    /* open config file */
    fp = fopen(confpath, "r");

    if (!fp)
        return -1;

    /* parse config */
    toml_table_t *od_conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
    fclose(fp);

    if (!od_conf)
        return -1;

    fwprintf(stderr, L"Picked up options from %s\n", confpath);

    /* set options in arguments */
    toml_table_t *dive = toml_table_in(od_conf, "dive");

    if (dive) {
        toml_datum_t g = toml_string_in(dive, "gas");

        if (g.ok) {
            if (arguments->gas)
                free(arguments->gas);

            arguments->gas = g.u.s;
        }

        toml_datum_t p = toml_double_in(dive, "surface_pressure");

        if (p.ok)
            arguments->SURFACE_PRESSURE = p.u.d;

        toml_datum_t r = toml_double_in(dive, "rmv");

        if (r.ok)
            arguments->RMV_DIVE = r.u.d;
    }

    toml_table_t *deco = toml_table_in(od_conf, "deco");

    if (deco) {
        toml_datum_t L = toml_int_in(deco, "gflow");

        if (L.ok)
            arguments->gflow = L.u.i;

        toml_datum_t H = toml_int_in(deco, "gfhigh");

        if (H.ok)
            arguments->gfhigh = H.u.i;

        toml_datum_t G = toml_string_in(deco, "decogasses");

        if (G.ok) {
            if (arguments->decogasses)
                free(arguments->decogasses);

            arguments->decogasses = G.u.s;
        }

        toml_datum_t _6 = toml_bool_in(deco, "last_stop_at_six");

        if (_6.ok)
            arguments->LAST_STOP_AT_SIX = _6.u.b;

        toml_datum_t S = toml_bool_in(deco, "switch_intermediate");

        if (S.ok)
            arguments->SWITCH_INTERMEDIATE = S.u.b;

        toml_datum_t R = toml_double_in(deco, "rmv");

        if (R.ok)
            arguments->RMV_DECO = R.u.d;
    }

    toml_table_t *conf = toml_table_in(od_conf, "conf");

    if (conf) {
        toml_datum_t T = toml_bool_in(conf, "show_travel");

        if (T.ok)
            arguments->SHOW_TRAVEL = T.u.b;
    }

    toml_free(od_conf);
    return 0;
}