From 7a6d97ce84989a75ff4c994c5020cdb93a5e92c1 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 12 Jun 2023 17:52:35 -0700 Subject: til_settings: convert til_settings_new() to til_str_t open_memstream() be gone --- src/til_settings.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/til_settings.c b/src/til_settings.c index 49ef3d1..fe4546b 100644 --- a/src/til_settings.c +++ b/src/til_settings.c @@ -84,9 +84,8 @@ til_settings_t * til_settings_new(const char *prefix, const til_settings_t *pare til_settings_fsm_state_t state = TIL_SETTINGS_FSM_STATE_COMMA; const char *p; til_settings_t *settings; - FILE *value_fp; + til_str_t *value_str; char *value_buf; - size_t value_sz; assert(label); @@ -112,8 +111,8 @@ til_settings_t * til_settings_new(const char *prefix, const til_settings_t *pare switch (state) { case TIL_SETTINGS_FSM_STATE_COMMA: - value_fp = open_memstream(&value_buf, &value_sz); /* TODO FIXME: open_memstream() isn't portable */ - if (!value_fp) + value_str = til_str_new(""); + if (!value_str) goto _err; state = TIL_SETTINGS_FSM_STATE_KEY; @@ -123,27 +122,26 @@ til_settings_t * til_settings_new(const char *prefix, const til_settings_t *pare if (*p == '\\') state = TIL_SETTINGS_FSM_STATE_KEY_ESCAPED; else if (*p == '=' || *p == ',' || *p == '\0') { - fclose(value_fp); if (*p == '=') { /* key= */ - (void) add_setting(settings, value_buf, NULL); + (void) add_setting(settings, til_str_to_buf(value_str, NULL), NULL); state = TIL_SETTINGS_FSM_STATE_EQUAL; } else { /* bare value */ - (void) add_setting(settings, NULL, value_buf); + (void) add_setting(settings, NULL, til_str_to_buf(value_str, NULL)); state = TIL_SETTINGS_FSM_STATE_COMMA; } } else - fputc(*p, value_fp); + til_str_appendf(value_str, "%c", *p); /* FIXME: errors */ break; case TIL_SETTINGS_FSM_STATE_KEY_ESCAPED: - fputc(*p, value_fp); + til_str_appendf(value_str, "%c", *p); /* FIXME: errors */ state = TIL_SETTINGS_FSM_STATE_KEY; break; case TIL_SETTINGS_FSM_STATE_EQUAL: - value_fp = open_memstream(&value_buf, &value_sz); /* TODO FIXME: open_memstream() isn't portable */ - if (!value_fp) + value_str = til_str_new(""); + if (!value_str) goto _err; state = TIL_SETTINGS_FSM_STATE_VALUE; @@ -153,11 +151,10 @@ til_settings_t * til_settings_new(const char *prefix, const til_settings_t *pare if (*p == '\\') state = TIL_SETTINGS_FSM_STATE_VALUE_ESCAPED; else if (*p == ',' || *p == '\0') { - fclose(value_fp); - settings->entries[settings->num - 1]->value = value_buf; + settings->entries[settings->num - 1]->value = til_str_to_buf(value_str, NULL); state = TIL_SETTINGS_FSM_STATE_COMMA; } else - fputc(*p, value_fp); + til_str_appendf(value_str, "%c", *p); /* FIXME: errors */ break; @@ -169,7 +166,7 @@ til_settings_t * til_settings_new(const char *prefix, const til_settings_t *pare * unescaping "\n" itself or just look for '\n' (0xa) to insert linefeeds? I think * the latter...) But until there's a real need for that, this can just stay as-is. */ - fputc(*p, value_fp); + til_str_appendf(value_str, "%c", *p); /* FIXME: errors */ state = TIL_SETTINGS_FSM_STATE_VALUE; break; -- cgit v1.2.3