This repository has been archived by the owner on Feb 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
123 lines (106 loc) · 2.96 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright (C) 2023 Andy Frank Schoknecht
*/
#include "config.h"
#include "engine/dict.h"
#include "engine/log.h"
#include "path.h"
struct Config Config_new(void)
{
struct Config cfg = {
.invalid = 0,
.gfx_window_x = CFG_STD_GFX_WINDOW_X,
.gfx_window_y = CFG_STD_GFX_WINDOW_Y,
.gfx_window_w = CFG_STD_GFX_WINDOW_W,
.gfx_window_h = CFG_STD_GFX_WINDOW_H,
.gfx_window_fullscreen = CFG_STD_GFX_WINDOW_FULLSCREEN,
};
return cfg;
}
void Config_from_file(struct Config *cfg)
{
const char *temp;
struct String filepath = String_new(16);
struct String msg = String_new(1);
// get path
if (get_config_path(&filepath) != 0) {
String_clear(&filepath);
cfg->invalid = 1;
return;
}
// read file
struct Dict dict = Dict_from_file(filepath.str);
if (dict.invalid) {
String_clear(&filepath);
cfg->invalid = 1;
log_warn("Config could not be loaded.");
return;
}
// convert dict into config
for (size_t i = 0; i < dict.len; i++) {
// window pos, size
if (strequal(dict.data[i].key.str, CFG_SETTING_GFX_WINDOW_X))
cfg->gfx_window_x =
strtol(dict.data[i].value.str, NULL, 10);
else if (strequal
(dict.data[i].key.str, CFG_SETTING_GFX_WINDOW_Y))
cfg->gfx_window_y =
strtol(dict.data[i].value.str, NULL, 10);
else if (strequal
(dict.data[i].key.str, CFG_SETTING_GFX_WINDOW_W))
cfg->gfx_window_w =
strtol(dict.data[i].value.str, NULL, 10);
else if (strequal
(dict.data[i].key.str, CFG_SETTING_GFX_WINDOW_H))
cfg->gfx_window_h =
strtol(dict.data[i].value.str, NULL, 10);
else if (strequal
(dict.data[i].key.str,
CFG_SETTING_GFX_WINDOW_FULLSCREEN))
cfg->gfx_window_fullscreen =
strtol(dict.data[i].value.str, NULL, 10);
// unknown option
else {
temp = "Unknown config setting \"";
String_copy(&msg, temp, strlen(temp));
String_append(&msg,
dict.data[i].key.str,
dict.data[i].key.len);
temp = "\".";
String_append(&msg, temp, strlen(temp));
log_warn(msg.str);
}
}
String_clear(&filepath);
String_clear(&msg);
Dict_clear(&dict);
}
void Config_to_file(struct Config *cfg)
{
struct String filepath = String_new(16);
/* get path */
if (get_config_path(&filepath) != 0) {
String_clear(&filepath);
cfg->invalid = 1;
return;
}
// convert config into dict
struct Dict dict = Dict_new(1);
char temp[10];
sprintf(temp, "%i", cfg->gfx_window_x);
Dict_add(&dict, CFG_SETTING_GFX_WINDOW_X, temp);
sprintf(temp, "%i", cfg->gfx_window_y);
Dict_add(&dict, CFG_SETTING_GFX_WINDOW_Y, temp);
sprintf(temp, "%i", cfg->gfx_window_w);
Dict_add(&dict, CFG_SETTING_GFX_WINDOW_W, temp);
sprintf(temp, "%i", cfg->gfx_window_h);
Dict_add(&dict, CFG_SETTING_GFX_WINDOW_H, temp);
sprintf(temp, "%i", cfg->gfx_window_fullscreen);
Dict_add(&dict, CFG_SETTING_GFX_WINDOW_FULLSCREEN, temp);
// save
if (!Dict_to_file(&dict, filepath.str))
cfg->invalid = 1;
// clear
String_clear(&filepath);
Dict_clear(&dict);
}