From 1d3c4501eff3392e0cd858d980c742c7f88c95e7 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 21 Mar 2023 09:47:10 +0100 Subject: [PATCH] alsa: use pa_strbuf --- spa/plugins/alsa/acp/alsa-mixer.c | 42 +++++++++++-------------------- spa/plugins/alsa/acp/compat.h | 11 ++++++++ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/spa/plugins/alsa/acp/alsa-mixer.c b/spa/plugins/alsa/acp/alsa-mixer.c index f7ad885af..7688b030c 100644 --- a/spa/plugins/alsa/acp/alsa-mixer.c +++ b/spa/plugins/alsa/acp/alsa-mixer.c @@ -4734,35 +4734,29 @@ static int profile_verify(pa_alsa_profile *p) { PA_ELEMENTSOF(well_known_descriptions))); if (!p->description) { + pa_strbuf *sb; uint32_t idx; pa_alsa_mapping *m; - char *ptr; - size_t size; - FILE *f; - int count = 0; - f = open_memstream(&ptr, &size); - if (f == NULL) { - pa_log("failed to open memstream: %m"); - return -1; - } + sb = pa_strbuf_new(); if (p->output_mappings) PA_IDXSET_FOREACH(m, p->output_mappings, idx) { - if (count++ > 0) - fprintf(f, " + "); - fprintf(f, _("%s Output"), m->description); + if (!pa_strbuf_isempty(sb)) + pa_strbuf_puts(sb, " + "); + + pa_strbuf_printf(sb, _("%s Output"), m->description); } if (p->input_mappings) PA_IDXSET_FOREACH(m, p->input_mappings, idx) { - if (count++ > 0) - fprintf(f, " + "); - fprintf(f, _("%s Input"), m->description); + if (!pa_strbuf_isempty(sb)) + pa_strbuf_puts(sb, " + "); + + pa_strbuf_printf(sb, _("%s Input"), m->description); } - fclose(f); - p->description = ptr; + p->description = pa_strbuf_to_string_free(sb); } return 0; @@ -4814,23 +4808,17 @@ void pa_alsa_decibel_fix_dump(pa_alsa_decibel_fix *db_fix) { pa_assert(db_fix); if (db_fix->db_values) { + pa_strbuf *buf; unsigned long i, nsteps; - FILE *f; - char *ptr; - size_t size; - - f = open_memstream(&ptr, &size); - if (f == NULL) - return; pa_assert(db_fix->min_step <= db_fix->max_step); nsteps = db_fix->max_step - db_fix->min_step + 1; + buf = pa_strbuf_new(); for (i = 0; i < nsteps; ++i) - fprintf(f, "[%li]:%0.2f ", i + db_fix->min_step, db_fix->db_values[i] / 100.0); + pa_strbuf_printf(buf, "[%li]:%0.2f ", i + db_fix->min_step, db_fix->db_values[i] / 100.0); - fclose(f); - db_values = ptr; + db_values = pa_strbuf_to_string_free(buf); } pa_log_debug("Decibel fix %s, min_step=%li, max_step=%li, db_values=%s", diff --git a/spa/plugins/alsa/acp/compat.h b/spa/plugins/alsa/acp/compat.h index 48568129a..5b92d14d2 100644 --- a/spa/plugins/alsa/acp/compat.h +++ b/spa/plugins/alsa/acp/compat.h @@ -373,6 +373,17 @@ static PA_PRINTF_FUNC(2,3) inline size_t pa_strbuf_printf(pa_strbuf *sb, const c return ret > 0 ? ret : 0; } +static inline void pa_strbuf_puts(pa_strbuf *sb, const char *t) +{ + fputs(t, sb->f); +} + +static inline bool pa_strbuf_isempty(pa_strbuf *sb) +{ + fflush(sb->f); + return sb->size == 0; +} + static inline char *pa_strbuf_to_string_free(pa_strbuf *sb) { char *ptr;