From a3ad7e5bfdbb54f59555bfe408c52a5237db14d3 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sun, 11 Jun 2023 20:01:46 -0700 Subject: til_settings: migrate til_settings_as_arg() to til_str First elimination of open_memstream() usage... --- src/til_settings.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/til_settings.c b/src/til_settings.c index 9fe57e6..b7ebe4d 100644 --- a/src/til_settings.c +++ b/src/til_settings.c @@ -6,6 +6,7 @@ #include #include "til_settings.h" +#include "til_str.h" #include "til_util.h" #ifdef __WIN32__ @@ -513,7 +514,7 @@ int til_setting_spec_check(const til_setting_spec_t *spec, const char *value) } -static inline void fputc_escaped(FILE *out, int c, unsigned depth) +static inline void fputc_escaped(til_str_t *out, int c, unsigned depth) { unsigned escapes = 0; @@ -523,13 +524,13 @@ static inline void fputc_escaped(FILE *out, int c, unsigned depth) } for (unsigned i = 0; i < escapes; i++) - fputc('\\', out); + til_str_appendf(out, "\\"); - fputc(c, out); + til_str_appendf(out, "%c", c); } -static inline void fputs_escaped(FILE *out, const char *value, unsigned depth) +static inline void fputs_escaped(til_str_t *out, const char *value, unsigned depth) { char c; @@ -542,16 +543,17 @@ static inline void fputs_escaped(FILE *out, const char *value, unsigned depth) fputc_escaped(out, c, depth); break; default: - fputc(c, out); + til_str_appendf(out, "%c", c); break; } } } -static void settings_as_arg(const til_settings_t *settings, unsigned depth, FILE *out) +static int settings_as_arg(const til_settings_t *settings, unsigned depth, til_str_t *out) { for (size_t i = 0; i < settings->num; i++) { + /* FIXME TODO: detect errors */ if (i > 0) fputc_escaped(out, ',', depth); @@ -567,24 +569,23 @@ static void settings_as_arg(const til_settings_t *settings, unsigned depth, FILE fputs_escaped(out, settings->entries[i]->value, depth); } } + + return 0; } char * til_settings_as_arg(const til_settings_t *settings) { - FILE *out; - char *outbuf; - size_t outsize; + til_str_t *str; - out = open_memstream(&outbuf, &outsize); /* TODO FIXME: open_memstream() isn't portable */ - if (!out) + str = til_str_new(""); + if (!str) return NULL; - settings_as_arg(settings, 0, out); - - fclose(out); + if (settings_as_arg(settings, 0, str) < 0) + return til_str_free(str); - return outbuf; + return til_str_to_buf(str, NULL); } -- cgit v1.2.3