From e85ad7414c063e42296f7ef4e202829567484ac0 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Tue, 12 Nov 2024 01:44:14 +0100 Subject: Fix config and blocks --- src/blocks/date.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'src/blocks/date.c') diff --git a/src/blocks/date.c b/src/blocks/date.c index 01c62c3..93747c3 100644 --- a/src/blocks/date.c +++ b/src/blocks/date.c @@ -6,33 +6,43 @@ #include "../any_log.h" +typedef struct { + block_text_t block; + char *format; +} block_date_t; + static void block_date_update(block_t *block) { + block_date_t *date = (block_date_t *)block; + time_t epoch = time(NULL); struct tm *loc_time = localtime(&epoch); char buffer[64]; - strftime(buffer, sizeof(buffer), block->state, loc_time); + strftime(buffer, sizeof(buffer), date->format, loc_time); - free(block->text.text); - block->text.text = strcopy(buffer); - assert(block->text.text != NULL); + free(date->block.text); + date->block.text = strcopy(buffer); + assert(date->block.text != NULL); } -static void block_date_finalize(block_t *block) +static void block_date_clean(block_t *block) { - free(block->state); + block_date_t *date = (block_date_t *)block; + free(date->format); } static bool block_date_validate(block_t *block, const block_scheme_t *scheme) { - if (block->text.text == NULL) { + block_date_t *date = (block_date_t *)block; + + if (date->block.text == NULL) { log_error("Block '%s' requires key '%s'", block->label, "text"); return false; } - block->state = block->text.text; - block->text.text = NULL; + date->format = date->block.text; + date->block.text = NULL; return true; } @@ -44,10 +54,10 @@ const block_scheme_t block_date_scheme = { .tv_sec = 1, .tv_nsec = 0, }, - .update_cb = block_date_update, - .finalize_cb = block_date_finalize, + .update_fn = block_date_update, + .clean_fn = block_date_clean, }, - .size = sizeof(char *), + .size = sizeof(block_date_t), .entries = NULL, .validate = block_date_validate, }; -- cgit v1.2.3