diff options
author | Tim Segers <tsegers@pm.me> | 2022-12-23 20:01:46 +0100 |
---|---|---|
committer | Tim Segers <tsegers@pm.me> | 2023-01-02 15:06:03 +0100 |
commit | 72640eecc20e9babed3661335e2ea71dd10ee618 (patch) | |
tree | 52abbb575b18aeaef4a08728aaaae439aca76b19 /src | |
parent | c0f2ea4176b967ba902bb5e0e5c4ee3f28530beb (diff) | |
download | opendeco-72640eecc20e9babed3661335e2ea71dd10ee618.tar.gz |
Add support for toml-based config file
Diffstat (limited to 'src')
-rw-r--r-- | src/opendeco-cli.c | 11 | ||||
-rw-r--r-- | src/opendeco-conf.c | 101 | ||||
-rw-r--r-- | src/opendeco-conf.h | 2 | ||||
-rw-r--r-- | src/opendeco.c | 12 |
4 files changed, 122 insertions, 4 deletions
diff --git a/src/opendeco-cli.c b/src/opendeco-cli.c index 4e21405..e200fcd 100644 --- a/src/opendeco-cli.c +++ b/src/opendeco-cli.c @@ -2,6 +2,7 @@ #include <argp.h> #include <stdlib.h> +#include <string.h> #include "opendeco-cli.h" @@ -47,7 +48,10 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) arguments->time = arg ? atof(arg) : -1; break; case 'g': - arguments->gas = arg; + if (arguments->gas) + free(arguments->gas); + + arguments->gas = strdup(arg); break; case 'p': arguments->SURFACE_PRESSURE = arg ? atof(arg) : -1; @@ -62,7 +66,10 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) arguments->gfhigh = arg ? atoi(arg) : 100; break; case 'G': - arguments->decogasses = arg; + if (arguments->decogasses) + free(arguments->decogasses); + + arguments->decogasses = strdup(arg); break; case 'S': arguments->SWITCH_INTERMEDIATE = 0; diff --git a/src/opendeco-conf.c b/src/opendeco-conf.c new file mode 100644 index 0000000..e6b957f --- /dev/null +++ b/src/opendeco-conf.c @@ -0,0 +1,101 @@ +#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; +} diff --git a/src/opendeco-conf.h b/src/opendeco-conf.h index cb5b863..48b1ad0 100644 --- a/src/opendeco-conf.h +++ b/src/opendeco-conf.h @@ -21,4 +21,6 @@ struct arguments { int SHOW_TRAVEL; }; +int opendeco_conf_parse(const char *confpath, struct arguments *arguments); + #endif /* end of include guard: OPENDECOCONF_H */ diff --git a/src/opendeco.c b/src/opendeco.c index 862923e..029a81f 100644 --- a/src/opendeco.c +++ b/src/opendeco.c @@ -138,13 +138,16 @@ int main(int argc, char *argv[]) setlocale(LC_ALL, "en_US.utf8"); /* argp */ + char *gas_default = strdup("Air"); + char *decogasses_default = strdup(""); + struct arguments arguments = { .depth = -1, .time = -1, - .gas = "Air", + .gas = gas_default, .gflow = 30, .gfhigh = 75, - .decogasses = "", + .decogasses = decogasses_default, .SURFACE_PRESSURE = SURFACE_PRESSURE_DEFAULT, .SWITCH_INTERMEDIATE = SWITCH_INTERMEDIATE_DEFAULT, .LAST_STOP_AT_SIX = LAST_STOP_AT_SIX_DEFAULT, @@ -153,6 +156,7 @@ int main(int argc, char *argv[]) .SHOW_TRAVEL = SHOW_TRAVEL_DEFAULT, }; + opendeco_conf_parse("opendeco.toml", &arguments); opendeco_argp_parse(argc, argv, &arguments); /* apply global options */ @@ -213,6 +217,10 @@ int main(int argc, char *argv[]) wprintf(L"\nNDL: %i TTS: %i TTS @+5: %i\n", (int) floor(di.ndl), (int) ceil(di.tts), (int) ceil(di_plus5.tts)); print_planfoot(&ds); + /* cleanup */ free(deco_gasses); + free(arguments.gas); + free(arguments.decogasses); + return 0; } |