From ab5f546143021bb2852abf0a6446c3951a2fe56e Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Mon, 22 Mar 2010 16:13:12 +0100 Subject: [PATCH] oss4: Refactor code to make it look more modern A side effect is that it passes -Wformat-nonliteral and doesn't read invalid memory in some cases, like when the mixer track contains a % sign or there is a number but not a known mixer name. --- sys/oss4/oss4-mixer.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/sys/oss4/oss4-mixer.c b/sys/oss4/oss4-mixer.c index cabd6b7..e8d8005 100644 --- a/sys/oss4/oss4-mixer.c +++ b/sys/oss4/oss4-mixer.c @@ -854,14 +854,11 @@ static gchar * gst_oss4_mixer_control_get_translated_name (GstOss4MixerControl * mc) { gchar name[128] = { 0, }; - gchar scratch[128] = { 0, }; - gchar fmtbuf[128] = { 0, }; gchar vmix_str[32] = { '\0', }; gchar *ptr; int dummy, i; int num = -1; - - g_strlcpy (fmtbuf, "%s", sizeof (fmtbuf)); + gboolean function_suffix = FALSE; /* main virtual mixer controls (we hide the stream volumes) */ if (sscanf (mc->mixext.extname, "vmix%d-%32c", &dummy, vmix_str) == 2) { @@ -880,13 +877,12 @@ gst_oss4_mixer_control_get_translated_name (GstOss4MixerControl * mc) (g_str_has_prefix (name, "jack."))) { ptr = strchr (mc->mixext.extname, '.'); ptr++; - g_strlcpy (scratch, ptr, sizeof (scratch)); - g_strlcpy (name, scratch, sizeof (name)); + g_strlcpy (name, ptr, sizeof (name)); } /* special handling for jack retasking suffixes */ if (g_str_has_suffix (name, ".function") || g_str_has_suffix (name, ".mode")) { - g_strlcpy (fmtbuf, _("%s Function"), sizeof (fmtbuf)); + function_suffix = TRUE; ptr = strrchr (name, '.'); *ptr = 0; } @@ -900,10 +896,6 @@ gst_oss4_mixer_control_get_translated_name (GstOss4MixerControl * mc) if ((i > 0) && (name[i] != '\0')) { num = atoi (name + i); name[i] = '\0'; - /* format appends a number to the base, but preserves any surrounding - format */ - g_snprintf (scratch, sizeof (scratch), fmtbuf, _("%s %d")); - g_strlcpy (fmtbuf, scratch, sizeof (fmtbuf)); } /* look for a match, progressively skipping '.' delimited prefixes as we go */ @@ -913,16 +905,29 @@ gst_oss4_mixer_control_get_translated_name (GstOss4MixerControl * mc) ptr++; for (i = 0; i < G_N_ELEMENTS (labels); ++i) { if (g_strcasecmp (ptr, labels[i].oss_name) == 0) { - g_snprintf (name, sizeof (name), fmtbuf, _(labels[i].label), num); - return g_strdup (name); + g_strlcpy (name, _(labels[i].label), sizeof (name)); + goto append_suffixes; } } } while ((ptr = strchr (ptr, '.')) != NULL); /* failing that, just replace periods with spaces */ g_strdelimit (name, ".", ' '); - g_snprintf (scratch, sizeof (scratch), fmtbuf, name); - return g_strdup (scratch); + +append_suffixes: + if (num > -1) { + if (function_suffix) { + return g_strdup_printf (_("%s %d Function"), name, num); + } else { + return g_strdup_printf (_("%s %d"), name, num); + } + } else { + if (function_suffix) { + return g_strdup_printf (_("%s Function"), name); + } else { + return g_strdup (name); + } + } } static const gchar * -- 2.7.4