aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-11-29 13:09:56 +0100
committerFederico Angelilli <code@fedang.net>2024-11-29 13:09:56 +0100
commit728e1e204d39bf11013ee999bcbff779aebc3399 (patch)
treea70b7cdf8576142022fc20b6ef219ecb84fcd028 /src
parent8ad2993b9e6679a669b80154639b4941ed2fafb4 (diff)
Simplify parsing
Diffstat (limited to 'src')
-rw-r--r--src/block.c8
-rw-r--r--src/blocks/date.c2
-rw-r--r--src/blocks/fs.c4
-rw-r--r--src/blocks/group.c2
-rw-r--r--src/blocks/ram.c2
-rw-r--r--src/blocks/script.c2
-rw-r--r--src/blocks/slider.c2
-rw-r--r--src/blocks/text.c2
-rw-r--r--src/config.c146
-rw-r--r--src/config.h4
10 files changed, 70 insertions, 104 deletions
diff --git a/src/block.c b/src/block.c
index c397b5a..97bc3e8 100644
--- a/src/block.c
+++ b/src/block.c
@@ -7,11 +7,17 @@
#include "any_log.h"
extern const block_scheme_t block_text_scheme;
+
extern const block_scheme_t block_group_scheme;
+
extern const block_scheme_t block_ram_scheme;
+
extern const block_scheme_t block_fs_scheme;
+
extern const block_scheme_t block_date_scheme;
+
extern const block_scheme_t block_script_scheme;
+
extern const block_scheme_t block_slider_scheme;
const block_scheme_t *block_schemes[] = {
@@ -90,7 +96,7 @@ bool block_change(block_t *block, config_t *config, const char *key, const char
// Ignore notify invocations
config_status_t status = key == NULL
? CONFIG_UNKNOWN
- : config_read_entry(block_entries, block, NULL, "block", block->label, key, value);
+ : config_read_entry(block_entries, block, NULL, key, value);
if (status == CONFIG_UNKNOWN && block->scheme->change_fn != NULL)
status = block->scheme->change_fn(block, config, key, value);
diff --git a/src/blocks/date.c b/src/blocks/date.c
index cbc6cce..82f5a12 100644
--- a/src/blocks/date.c
+++ b/src/blocks/date.c
@@ -71,7 +71,7 @@ static config_status_t block_date_change(block_t *block, config_t *config, const
}
extern const config_entry_t block_text_entries[];
- return config_read_entry(block_text_entries, block, NULL, "block", block->label, key, value);
+ return config_read_entry(block_text_entries, block, NULL, key, value);
}
const block_scheme_t block_date_scheme = {
diff --git a/src/blocks/fs.c b/src/blocks/fs.c
index ea51a93..1f92776 100644
--- a/src/blocks/fs.c
+++ b/src/blocks/fs.c
@@ -187,10 +187,10 @@ static config_status_t block_fs_change(block_t *block, config_t *config, const c
}
extern const config_entry_t block_text_entries[];
- config_status_t success = config_read_entry(block_text_entries, block, NULL, "block", block->label, key, value);
+ config_status_t success = config_read_entry(block_text_entries, block, NULL, key, value);
return success == CONFIG_UNKNOWN
- ? config_read_entry(block_fs_entries, block, NULL, "block", block->label, key, value)
+ ? config_read_entry(block_fs_entries, block, NULL, key, value)
: success;
}
diff --git a/src/blocks/group.c b/src/blocks/group.c
index da9e6d6..23c7543 100644
--- a/src/blocks/group.c
+++ b/src/blocks/group.c
@@ -76,7 +76,7 @@ static config_status_t block_group_change(block_t *block, config_t *config, cons
}
extern const config_entry_t block_group_entries[];
- return config_read_entry(block_group_entries, block, NULL, "block", block->label, key, value);
+ return config_read_entry(block_group_entries, block, NULL, key, value);
}
const block_scheme_t block_group_scheme = {
diff --git a/src/blocks/ram.c b/src/blocks/ram.c
index 7fd821f..d3fbe88 100644
--- a/src/blocks/ram.c
+++ b/src/blocks/ram.c
@@ -183,7 +183,7 @@ static config_status_t block_ram_change(block_t *block, config_t *config, const
}
extern const config_entry_t block_text_entries[];
- return config_read_entry(block_text_entries, block, NULL, "block", block->label, key, value);
+ return config_read_entry(block_text_entries, block, NULL, key, value);
}
const block_scheme_t block_ram_scheme = {
diff --git a/src/blocks/script.c b/src/blocks/script.c
index 098dc4e..ca0e506 100644
--- a/src/blocks/script.c
+++ b/src/blocks/script.c
@@ -81,7 +81,7 @@ static config_status_t block_script_change(block_t *block, config_t *config, con
}
extern const config_entry_t block_text_entries[];
- return config_read_entry(block_text_entries, block, NULL, "block", block->label, key, value);
+ return config_read_entry(block_text_entries, block, NULL, key, value);
}
const block_scheme_t block_script_scheme = {
diff --git a/src/blocks/slider.c b/src/blocks/slider.c
index 9311274..5246162 100644
--- a/src/blocks/slider.c
+++ b/src/blocks/slider.c
@@ -332,7 +332,7 @@ static const config_entry_t block_slider_entries[] = {
static config_status_t block_slider_change(block_t *block, config_t *config, const char *key, const char *value)
{
- return config_read_entry(block_slider_entries, block, NULL, "block", block->label, key, value);
+ return config_read_entry(block_slider_entries, block, NULL, key, value);
}
const block_scheme_t block_slider_scheme = {
diff --git a/src/blocks/text.c b/src/blocks/text.c
index 4046186..5531196 100644
--- a/src/blocks/text.c
+++ b/src/blocks/text.c
@@ -29,7 +29,7 @@ static int block_text_validate(block_t *block, config_t *config)
static config_status_t block_text_change(block_t *block, config_t *config, const char *key, const char *value)
{
extern const config_entry_t block_text_entries[];
- return config_read_entry(block_text_entries, block, NULL, "block", block->label, key, value);
+ return config_read_entry(block_text_entries, block, NULL, key, value);
}
const block_scheme_t block_text_scheme = {
diff --git a/src/config.c b/src/config.c
index 439a869..71b64de 100644
--- a/src/config.c
+++ b/src/config.c
@@ -14,13 +14,6 @@
#define ANY_INI_IMPLEMENT
#include "any_ini.h"
-config_enum_t text_align_enum[] = {
- { "left", ALIGN_LEFT },
- { "center", ALIGN_CENTER },
- { "right", ALIGN_RIGHT },
- { 0 },
-};
-
const config_entry_t bar_entries[] = {
{ "override-redirect", CONFIG_BOOL, NULL, offsetof(config_t, override_redirect) },
{ "action-strict-run", CONFIG_BOOL, NULL, offsetof(config_t, action_strict_run) },
@@ -39,6 +32,13 @@ const config_entry_t bar_entries[] = {
{ 0 },
};
+const config_enum_t text_align_enum[] = {
+ { "left", ALIGN_LEFT },
+ { "center", ALIGN_CENTER },
+ { "right", ALIGN_RIGHT },
+ { 0 },
+};
+
const config_entry_t block_entries[] = {
{ "hidden", CONFIG_BOOL, NULL, offsetof(block_t, hidden) },
{ "color", CONFIG_GRADIENT, NULL, offsetof(block_t, bg_color) },
@@ -325,30 +325,6 @@ static bool config_read_list(const char *value, char ***result)
return true;
}
-static config_status_t config_read_block(block_t *block, const char **type, const char *key, const char *value)
-{
- const char *section = "block";
- size_t index = 0;
-
- config_status_t status = config_read_entry(block_entries, block, &index, section, block->label, key, value);
- *type = config_type_to_string(block_entries[index].type);
-
- if (status != CONFIG_UNKNOWN) return status;
-
- if (block->type == BLOCK_GROUP)
- status = config_read_entry(block_group_entries, block, &index, section, block->label, key, value);
- else if (block->type == BLOCK_TEXT)
- status = config_read_entry(block_text_entries, block, &index, section, block->label, key, value);
-
- *type = config_type_to_string(block_entries[index].type);
- if (status != CONFIG_UNKNOWN || block->scheme->entries == NULL)
- return status;
-
- status = config_read_entry(block->scheme->entries, block, &index, section, block->label, key, value);
- *type = config_type_to_string(block_entries[index].type);
- return status;
-}
-
static block_t *config_alloc_block(config_t *config, const block_scheme_t *scheme, char *label)
{
config->blocks = realloc(config->blocks, ++config->n_blocks * sizeof(block_t *));
@@ -364,6 +340,31 @@ static block_t *config_alloc_block(config_t *config, const block_scheme_t *schem
return block;
}
+static config_status_t config_read_block(block_t *block, config_type_t *type, const char *key, const char *value)
+{
+ size_t index = 0;
+ config_status_t status = config_read_entry(block_entries, block, &index, key, value);
+ *type = block_entries[index].type;
+
+ if (status != CONFIG_UNKNOWN)
+ return status;
+
+ if (block->type == BLOCK_GROUP) {
+ status = config_read_entry(block_group_entries, block, &index, key, value);
+ *type = block_group_entries[index].type;
+ } else if (block->type == BLOCK_TEXT) {
+ status = config_read_entry(block_text_entries, block, &index, key, value);
+ *type = block_text_entries[index].type;
+ }
+
+ if (status != CONFIG_UNKNOWN || block->scheme->entries == NULL)
+ return status;
+
+ status = config_read_entry(block->scheme->entries, block, &index, key, value);
+ *type = block->scheme->entries[index].type;
+ return status;
+}
+
static config_status_t config_read_action(action_t *action, const char *key, const char *value)
{
action_type_t type;
@@ -408,8 +409,7 @@ void config_init(config_t *config)
bar->spacing = 10;
}
-config_status_t config_read_entry(const config_entry_t *entries, void *result, size_t *index,
- const char *section, const char *label, const char *key, const char *value)
+config_status_t config_read_entry(const config_entry_t *entries, void *result, size_t *index, const char *key, const char *value)
{
for (size_t i = 0; entries[i].key != NULL; i++) {
if (!strcmp(key, entries[i].key)) {
@@ -423,99 +423,53 @@ config_status_t config_read_entry(const config_entry_t *entries, void *result, s
switch (entries[i].type) {
case CONFIG_STRING:
- // NOTE: The previous string is freed by config_read_string
- if (config_read_string(value, (char **)result)) {
- log_debug("Set '%s%s%s.%s' to '%s'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, *(char **)result);
+ if (config_read_string(value, (char **)result))
return CONFIG_SUCCESS;
- }
break;
case CONFIG_INT:
- if (config_read_int(value, (int *)result)) {
- log_debug("Set '%s%s%s.%s' to '%d'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, *(int *)result);
+ if (config_read_int(value, (int *)result))
return CONFIG_SUCCESS;
- }
break;
case CONFIG_UINT:
- if (config_read_uint(value, (unsigned int *)result)) {
- log_debug("Set '%s%s%s.%s' to '%u'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, *(unsigned int *)result);
+ if (config_read_uint(value, (unsigned int *)result))
return CONFIG_SUCCESS;
- }
break;
case CONFIG_DOUBLE:
- if (config_read_double(value, (double *)result)) {
- log_debug("Set '%s%s%s.%s' to '%g'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, *(double *)result);
+ if (config_read_double(value, (double *)result))
return CONFIG_SUCCESS;
- }
break;
case CONFIG_BOOL:
- if (config_read_bool(value, (bool *)result)) {
- log_debug("Set '%s%s%s.%s' to '%s'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, *(bool *)result ? "true" : "false");
+ if (config_read_bool(value, (bool *)result))
return CONFIG_SUCCESS;
- }
break;
case CONFIG_COLOR:
- if (config_read_color(value, (color_t *)result)) {
- char *color = color_to_string((color_t *)result);
- log_debug("Set '%s%s%s.%s' to '%s'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, color);
- free(color);
+ if (config_read_color(value, (color_t *)result))
return CONFIG_SUCCESS;
- }
break;
case CONFIG_GRADIENT:
- if (config_read_gradient(value, (gradient_t *)result)) {
- char *gradient = gradient_to_string((gradient_t *)result);
- log_debug("Set '%s%s%s.%s' to '%s'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, gradient);
- free(gradient);
+ if (config_read_gradient(value, (gradient_t *)result))
return CONFIG_SUCCESS;
- }
break;
case CONFIG_ENUM:
- if (config_read_enum(value, (config_enum_t *)entries[i].data, (int *)result)) {
- log_debug("Set '%s%s%s.%s' to '%d'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, *(int *)result);
+ if (config_read_enum(value, (config_enum_t *)entries[i].data, (int *)result))
return CONFIG_SUCCESS;
- }
break;
case CONFIG_TIME:
- if (config_read_time(value, (struct timespec *)result)) {
- // TODO: Print
- log_debug("Set '%s%s%s.%s' to '%s'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, value);
+ if (config_read_time(value, (struct timespec *)result))
return CONFIG_SUCCESS;
- }
break;
case CONFIG_LIST:
- if (config_read_list(value, (char ***)result)) {
- log_debug("Set '%s%s%s.%s' to '%s'",
- section != NULL ? section : "", section != NULL ? "." : "",
- label, key, value);
+ if (config_read_list(value, (char ***)result))
return CONFIG_SUCCESS;
- }
break;
default:
@@ -649,18 +603,18 @@ int config_read(config_t *config, FILE *file)
if (!bar_section && block == NULL && action == NULL)
goto skip_pair;
- const char *type = "none";
+ config_type_t type;
config_status_t status;
if (bar_section) {
size_t index = 0;
- status = config_read_entry(bar_entries, config, &index, NULL, "bar", key, value);
+ status = config_read_entry(bar_entries, config, &index, key, value);
// Try the block entries
if (status == CONFIG_UNKNOWN)
block = config->blocks[0];
else
- type = config_type_to_string(bar_entries[index].type);
+ type = bar_entries[index].type;
}
if (block != NULL) {
@@ -673,6 +627,12 @@ int config_read(config_t *config, FILE *file)
switch (status) {
case CONFIG_SUCCESS:
+ log_value_debug("Parsed entry",
+ "s:section", section,
+ "s:key", key,
+ "s:value", value,
+ "s:type", config_type_to_string(type),
+ "i:line", ini.line);
break;
case CONFIG_INVALID:
@@ -681,7 +641,7 @@ int config_read(config_t *config, FILE *file)
"s:section", section,
"s:key", key,
"s:value", value,
- "s:type", type,
+ "s:type", config_type_to_string(type),
"i:line", ini.line);
break;
diff --git a/src/config.h b/src/config.h
index 6a13ab3..df511d4 100644
--- a/src/config.h
+++ b/src/config.h
@@ -26,7 +26,7 @@ typedef enum {
typedef struct {
const char *key;
config_type_t type;
- void *data;
+ const void *data;
size_t offset;
} config_entry_t;
@@ -67,7 +67,7 @@ const char *config_type_to_string(config_type_t type);
void config_init(config_t *config);
config_status_t config_read_entry(const config_entry_t *entries, void *result, size_t *index,
- const char *section, const char *label, const char *key, const char *value);
+ const char *key, const char *value);
int config_read(config_t *config, FILE *file);