From: Uwe Hermann Date: Sun, 6 Aug 2017 16:22:26 +0000 (+0200) Subject: drivers: Use g_strdup_printf() where possible. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d2391b5453cd54b4bb087bdfbf2964202ba29098;p=libsigrok.git drivers: Use g_strdup_printf() where possible. --- diff --git a/src/hardware/hameg-hmo/protocol.c b/src/hardware/hameg-hmo/protocol.c index 13063382..0a60127f 100644 --- a/src/hardware/hameg-hmo/protocol.c +++ b/src/hardware/hameg-hmo/protocol.c @@ -481,7 +481,6 @@ SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi) struct dev_context *devc; struct scope_state *state; const struct scope_config *config; - int tmp; unsigned int i; float tmp_float; @@ -635,7 +634,6 @@ SR_PRIV void hmo_scope_state_free(struct scope_state *state) SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) { - char tmp[25]; int model_index; unsigned int i, j, group; struct sr_channel *ch; @@ -684,11 +682,8 @@ SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi) /* Add digital channel groups. */ for (i = 0; i < scope_models[model_index].digital_pods; i++) { - g_snprintf(tmp, 25, "POD%d", i); - devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group)); - - devc->digital_groups[i]->name = g_strdup(tmp); + devc->digital_groups[i]->name = g_strdup_printf("POD%d", i); sdi->channel_groups = g_slist_append(sdi->channel_groups, devc->digital_groups[i]); } diff --git a/src/hardware/yokogawa-dlm/protocol.c b/src/hardware/yokogawa-dlm/protocol.c index d22b223e..cc822eb4 100644 --- a/src/hardware/yokogawa-dlm/protocol.c +++ b/src/hardware/yokogawa-dlm/protocol.c @@ -769,7 +769,6 @@ SR_PRIV int dlm_model_get(char *model_id, char **model_name, int *model_index) */ SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index) { - char tmp[25]; int i; struct sr_channel *ch; struct dev_context *devc; @@ -799,13 +798,10 @@ SR_PRIV int dlm_device_init(struct sr_dev_inst *sdi, int model_index) /* Add digital channel groups. */ for (i = 0; i < scope_models[model_index].pods; i++) { - g_snprintf(tmp, sizeof(tmp), "POD%d", i); - devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group)); if (!devc->digital_groups[i]) return SR_ERR_MALLOC; - - devc->digital_groups[i]->name = g_strdup(tmp); + devc->digital_groups[i]->name = g_strdup_printf("POD%d", i); sdi->channel_groups = g_slist_append(sdi->channel_groups, devc->digital_groups[i]); } diff --git a/src/strutil.c b/src/strutil.c index 38270893..02afc034 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -366,42 +366,31 @@ SR_API char *sr_samplerate_string(uint64_t samplerate) SR_API char *sr_period_string(uint64_t v_p, uint64_t v_q) { double freq, v; - char *o; - int prec, r; + int prec; freq = 1 / ((double)v_p / v_q); - o = g_malloc0(30 + 1); - if (freq > SR_GHZ(1)) { v = (double)v_p / v_q * 1000000000000.0; prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; - r = snprintf(o, 30, "%.*f ps", prec, v); + return g_strdup_printf("%.*f ps", prec, v); } else if (freq > SR_MHZ(1)) { v = (double)v_p / v_q * 1000000000.0; prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; - r = snprintf(o, 30, "%.*f ns", prec, v); + return g_strdup_printf("%.*f ns", prec, v); } else if (freq > SR_KHZ(1)) { v = (double)v_p / v_q * 1000000.0; prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; - r = snprintf(o, 30, "%.*f us", prec, v); + return g_strdup_printf("%.*f us", prec, v); } else if (freq > 1) { v = (double)v_p / v_q * 1000.0; prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; - r = snprintf(o, 30, "%.*f ms", prec, v); + return g_strdup_printf("%.*f ms", prec, v); } else { v = (double)v_p / v_q; prec = ((v - (uint64_t)v) < FLT_MIN) ? 0 : 3; - r = snprintf(o, 30, "%.*f s", prec, v); - } - - if (r < 0) { - /* Something went wrong... */ - g_free(o); - return NULL; + return g_strdup_printf("%.*f s", prec, v); } - - return o; } /** @@ -422,25 +411,12 @@ SR_API char *sr_period_string(uint64_t v_p, uint64_t v_q) */ SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q) { - int r; - char *o; - - o = g_malloc0(30 + 1); - if (v_q == 1000) - r = snprintf(o, 30, "%" PRIu64 "mV", v_p); + return g_strdup_printf("%" PRIu64 "mV", v_p); else if (v_q == 1) - r = snprintf(o, 30, "%" PRIu64 "V", v_p); + return g_strdup_printf("%" PRIu64 "V", v_p); else - r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q); - - if (r < 0) { - /* Something went wrong... */ - g_free(o); - return NULL; - } - - return o; + return g_strdup_printf("%gV", (float)v_p / (float)v_q); } /**