aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c83
1 files changed, 58 insertions, 25 deletions
diff --git a/src/config.c b/src/config.c
index 55dd133..82d6231 100644
--- a/src/config.c
+++ b/src/config.c
@@ -6,14 +6,15 @@
#include "any_log.h"
#include "config.h"
+#include "util.h"
#define ANY_INI_IMPLEMENT
#include "any_ini.h"
typedef enum {
CONFIG_STRING,
- CONFIG_INT_ANY,
- CONFIG_INT_POSITIVE,
+ CONFIG_INT,
+ CONFIG_UINT,
CONFIG_DOUBLE,
} config_type_t;
@@ -24,10 +25,10 @@ typedef struct {
} config_entry_t;
static const config_entry_t config_entries[] = {
- { "width", CONFIG_INT_POSITIVE, offsetof(config_t, width) },
- { "height", CONFIG_INT_POSITIVE, offsetof(config_t, height) },
- { "font", CONFIG_STRING, offsetof(config_t, font) },
- { "monitor", CONFIG_STRING, offsetof(config_t, monitor) },
+ { "width", CONFIG_UINT, offsetof(config_t, width) },
+ { "height", CONFIG_UINT, offsetof(config_t, height) },
+ { "font", CONFIG_STRING, offsetof(config_t, font) },
+ { "monitor", CONFIG_STRING, offsetof(config_t, monitor) },
};
static bool config_read_string(const char *value, char **result)
@@ -41,9 +42,8 @@ static bool config_read_string(const char *value, char **result)
end--;
}
- *result = malloc(end - start + 1);
- memcpy(*result, value + start, end - start);
- *result[end - start] = '\0';
+ free(*result);
+ *result = strslice(value, start, end);
return true;
}
@@ -54,11 +54,23 @@ static bool config_read_int(const char *value, int *result)
*result = n;
if (n > INT_MAX || n < INT_MIN)
- log_debug("Integer is too big");
+ log_debug("Integer %ld is out of range", n);
return *end == '\0' && n != LONG_MIN && n != LONG_MAX;
}
+static bool config_read_uint(const char *value, unsigned int *result)
+{
+ char *end;
+ unsigned long n = strtoul(value, &end, 0);
+ *result = n;
+
+ if (n > UINT_MAX)
+ log_debug("Integer %lu is out of range", n);
+
+ return *end == '\0' && n != ULONG_MAX;
+}
+
static bool config_read_double(const char *value, double *result)
{
char *end;
@@ -93,23 +105,32 @@ static bool config_entry(config_t *config, int line, const char *section, const
void *result = (uint8_t *)config + config_entries[i].offset;
switch (config_entries[i].type) {
case CONFIG_STRING:
- if (config_read_string(value, (char **)result))
+ // NOTE: The previous string is freed by config_read_string
+ if (config_read_string(value, (char **)result)) {
+ log_debug("Config '%s' set to '%s'", key, *(char **)result);
return true;
+ }
break;
- case CONFIG_INT_ANY:
- if (config_read_int(value, (int *)result))
+ case CONFIG_INT:
+ if (config_read_int(value, (int *)result)) {
+ log_debug("Config '%s' set to '%d'", key, *(int *)result);
return true;
+ }
break;
- case CONFIG_INT_POSITIVE:
- if (config_read_int(value, (int *)result) && *(int *)result >= 0)
+ case CONFIG_UINT:
+ if (config_read_uint(value, (unsigned int *)result)) {
+ log_debug("Config '%s' set to '%u'", key, *(unsigned int *)result);
return true;
+ }
break;
case CONFIG_DOUBLE:
- if (config_read_double(value, (double *)result))
+ if (config_read_double(value, (double *)result)) {
+ log_debug("Config '%s' set to '%lf'", key, *(double *)result);
return true;
+ }
break;
default:
@@ -136,18 +157,24 @@ static bool config_entry(config_t *config, int line, const char *section, const
return true;
}
-static const config_t config_default = {
- .font = "monospace 10",
- .monitor = NULL,
- .height = 50,
- .width = 100,
-};
-
-void config_init(config_t *config, FILE *file)
+void config_init(config_t *config)
{
+ const config_t config_default = {
+ .font = "monospace 10",
+ .monitor = NULL,
+ .height = 50,
+ .width = 100,
+ };
+
memcpy(config, &config_default, sizeof(config_t));
- log_debug("Copied default config");
+ // NOTE: Strings must be copied
+ config->font = strcopy(config_default.font);
+ config->monitor = strcopy(config_default.monitor);
+}
+
+void config_read(config_t *config, FILE *file)
+{
any_ini_stream_t ini;
any_ini_file_init(&ini, file);
@@ -183,3 +210,9 @@ void config_init(config_t *config, FILE *file)
if (errors > 0)
log_panic("Config file contained %d errors", errors);
}
+
+void config_free(config_t *config)
+{
+ free(config->font);
+ free(config->monitor);
+}