From: Antonio Ospite Date: Mon, 25 Sep 2017 09:39:53 +0000 (+0200) Subject: Port parse_ports() over from alsamidisrc, to reduce the code differences X-Git-Url: https://git.ao2.it/gst-aseq-appsrc.git/commitdiff_plain/536a560a52c2315d44a873ee140d2c4ea1b1494a?ds=sidebyside Port parse_ports() over from alsamidisrc, to reduce the code differences Also, use g_free() to deallocate app->seq_ports: since it is allocated with g_try_new(), use a glib function to deallocate it as well. --- diff --git a/gst-aseq-appsrc.c b/gst-aseq-appsrc.c index 7d4a885..d15ed6e 100644 --- a/gst-aseq-appsrc.c +++ b/gst-aseq-appsrc.c @@ -52,7 +52,7 @@ struct _App snd_seq_t *seq; int port_count; - snd_seq_addr_t *ports; + snd_seq_addr_t *seq_ports; snd_midi_event_t *parser; unsigned char *buffer; @@ -89,55 +89,50 @@ error: return ret; } -/* parses one or more port addresses from the string */ +/* Parses one or more port addresses from the string */ static int parse_ports (const char *arg, App * app) { - char *buf, *s, *port_name; - int ret; + gchar **ports_list; + guint i; + int ret = 0; GST_DEBUG ("ports: %s", arg); - /* make a copy of the string because we're going to modify it */ - buf = strdup (arg); - if (!buf) { + /* + * Assume that ports are separated by commas. + * + * Commas are used instead of spaces because those are valid in client + * names. + */ + ports_list = g_strsplit (arg, ",", 0); + + app->port_count = g_strv_length (ports_list); + app->seq_ports = g_try_new (snd_seq_addr_t, app->port_count); + if (!app->seq_ports) { GST_ERROR ("Out of memory"); ret = -ENOMEM; - goto out; + goto out_free_ports_list; } - for (port_name = s = buf; s; port_name = s + 1) { - /* Assume that ports are separated by commas. We don't use - * spaces because those are valid in client names. */ - s = strchr (port_name, ','); - if (s) - *s = '\0'; - - app->port_count++; - app->ports = - realloc (app->ports, app->port_count * sizeof (snd_seq_addr_t)); - if (!app->ports) { - GST_ERROR ("Out of memory"); - ret = -ENOMEM; - goto out_free_buf; - } + for (i = 0; i < (guint)app->port_count; i++) { + gchar *port_name = ports_list[i]; - ret = - snd_seq_parse_address (app->seq, &app->ports[app->port_count - 1], + ret = snd_seq_parse_address (app->seq, &app->seq_ports[i], port_name); if (ret < 0) { - GST_ERROR ("Invalid port %s - %s", port_name, snd_strerror (ret)); - goto error_free_ports; + GST_ERROR ("Invalid port %s - %s", port_name, + snd_strerror (ret)); + goto error_free_seq_ports; } } - goto out_free_buf; + goto out_free_ports_list; -error_free_ports: - free (app->ports); -out_free_buf: - free (buf); -out: +error_free_seq_ports: + g_free (app->seq_ports); +out_free_ports_list: + g_strfreev (ports_list); return ret; } @@ -164,12 +159,12 @@ connect_ports (App * app) for (i = 0; i < app->port_count; ++i) { ret = - snd_seq_connect_from (app->seq, 0, app->ports[i].client, - app->ports[i].port); + snd_seq_connect_from (app->seq, 0, app->seq_ports[i].client, + app->seq_ports[i].port); if (ret < 0) /* warning */ GST_WARNING ("Cannot connect from port %d:%d - %s", - app->ports[i].client, app->ports[i].port, snd_strerror (ret)); + app->seq_ports[i].client, app->seq_ports[i].port, snd_strerror (ret)); } } @@ -360,7 +355,7 @@ err_free_buffer: err_free_parser: snd_midi_event_free (app->parser); err_free_ports: - free (app->ports); + g_free (app->seq_ports); err_seq_close: snd_seq_close (app->seq); err: @@ -374,7 +369,7 @@ app_finalize (App * app) free (app->pfds); free (app->buffer); snd_midi_event_free (app->parser); - free (app->ports); + g_free (app->seq_ports); snd_seq_close (app->seq); }