From 69d3def8de3cb36076aef6e0c6f9b471c545f6d6 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 27 Feb 2022 01:03:44 -0500 Subject: Implement methods for writing settings to tmpfile --- include/xas/vox.h | 12 ++++++++++++ src/vox.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/include/xas/vox.h b/include/xas/vox.h index 7aa43e4..5a1cacf 100644 --- a/include/xas/vox.h +++ b/include/xas/vox.h @@ -54,6 +54,18 @@ int xas_vox_set_parameter(xas_vox *vox, const char *name, const char *value); +int xas_vox_set_parameter_str(xas_vox *vox, + const char *name, + const char *value); + +int xas_vox_set_parameter_float(xas_vox *vox, + const char *name, + float value); + +int xas_vox_set_parameter_int(xas_vox *vox, + const char *name, + int value); + int xas_vox_stop(xas_vox *vox); int xas_vox_active(xas_vox *vox); diff --git a/src/vox.c b/src/vox.c index bcfab5d..475300d 100644 --- a/src/vox.c +++ b/src/vox.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -87,6 +88,12 @@ static int vox_start(xas_vox *vox) { (void)vox_stop(vox); } + if (vox->tmpfh) { + if (fflush(vox->tmpfh) < 0) { + goto error_fflush; + } + } + if (pipe(pipe_stdin) < 0) { goto error_pipe_stdin; } @@ -168,6 +175,7 @@ error_pipe_stdout: (void)close(pipe_stdin[0]); error_pipe_stdin: +error_fflush: return -1; } @@ -276,6 +284,7 @@ xas_vox *xas_vox_new_args(xas_audio_format format, memset(vox->tmpfile, '\0', sizeof(vox->tmpfile)); vox->tmpfd = -1; + vox->tmpfh = NULL; return vox; @@ -317,26 +326,65 @@ error_tmpfile_open: return -1; } -int xas_vox_set_parameter(xas_vox *vox, +static int set_parameter_va(xas_vox *vox, const char *name, - const char *value) { + const char *format, + ...) { + va_list args; + if (tmpfile_open(vox) < 0) { goto error_tmpfile_open; } - if (fprintf(vox->tmpfh, "(Parameter.set '%s %s)\n", - name, - value) < 0) { + va_start(args, format); + + if (fprintf(vox->tmpfh, "(Parameter.set '%s ", name) < 0) { + goto error_fprintf; + } + + if (vfprintf(vox->tmpfh, format, args) < 0) { + goto error_fprintf; + } + + if (fprintf(vox->tmpfh, ")\n") < 0) { goto error_fprintf; } + va_end(args); + return 0; error_fprintf: + va_end(args); + error_tmpfile_open: return -1; } +int xas_vox_set_parameter(xas_vox *vox, + const char *name, + const char *value) { + return set_parameter_va(vox, name, "%s", value); +} + +int xas_vox_set_parameter_str(xas_vox *vox, + const char *name, + const char *value) { + return set_parameter_va(vox, name, "\"%s\"", value); +} + +int xas_vox_set_parameter_float(xas_vox *vox, + const char *name, + float value) { + return set_parameter_va(vox, name, "%f", value); +} + +int xas_vox_set_parameter_int(xas_vox *vox, + const char *name, + int value) { + return set_parameter_va(vox, name, "%d", value); +} + int xas_vox_stop(xas_vox *vox) { if (vox->state == XAS_VOX_ACTIVE) { return vox_stop(vox); -- cgit v1.2.3