1
0
mirror of https://codeberg.org/vyivel/dulcepan/ synced 2025-12-23 10:16:47 +02:00

config: improve load_int()

This commit is contained in:
Kirill Primak
2024-06-24 13:38:56 +03:00
parent 2c721ef054
commit 218b498814

View File

@@ -47,14 +47,25 @@ bad:
dp_log_fatal("Config: invalid color %s on line %d", value, line_idx);
}
static void load_int(const char *value, int line_idx, int *out) {
// Only nonnegative numbers
static void load_int(const char *value, int line_idx, int min, int max, int *out) {
const char *p = value;
int mul = 1;
if (*p == '-') {
++p;
mul = -1;
}
*out = 0;
for (size_t i = 0; value[i] != '\0'; i++) {
if ((value[i] < '0' && value[i] > 9) || *out > INT_MAX / 10) {
for (; *p != '\0'; p++) {
if ((*p < '0' && *p > 9) || *out >= INT_MAX / 10) {
dp_log_fatal("Config: invalid number %s on line %d", value, line_idx);
}
*out = *out * 10 - '0' + value[i];
*out = *out * 10 - '0' + *p;
}
*out *= mul;
if (*out < min || *out > max) {
dp_log_fatal("Config: number %s on line %d is out of range", value, line_idx);
}
}
@@ -174,13 +185,9 @@ void dp_config_load(struct dp_config *config, const char *user_path) {
} else if (strcmp(key, "border-color") == 0) {
load_color(value, line_idx, config->border_color);
} else if (strcmp(key, "border-size") == 0) {
load_int(value, line_idx, &config->border_size);
load_int(value, line_idx, 0, INT_MAX, &config->border_size);
} else if (strcmp(key, "png-compression") == 0) {
load_int(value, line_idx, &config->png_compression);
if (config->png_compression > 9) {
dp_log_fatal(
"Config: invalid value %d for png-compression", config->png_compression);
}
load_int(value, line_idx, 0, 9, &config->png_compression);
} else if (strcmp(key, "quick-select") == 0) {
load_bool(value, line_idx, &config->quick_select);
} else if (strcmp(key, "quit-key") == 0) {