[vlc-commits] avahi: Split string list value extraction in a separate function

Hugo Beauzée-Luyssen git at videolan.org
Mon Jun 29 11:52:38 CEST 2020


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Fri Jun 26 16:26:46 2020 +0200| [0eec6a86808dfec7cb046ce18735ab03259702df] | committer: Hugo Beauzée-Luyssen

avahi: Split string list value extraction in a separate function

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=0eec6a86808dfec7cb046ce18735ab03259702df
---

 modules/services_discovery/avahi.c | 63 ++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 36 deletions(-)

diff --git a/modules/services_discovery/avahi.c b/modules/services_discovery/avahi.c
index 560c48dfc3..7fe25e29e3 100644
--- a/modules/services_discovery/avahi.c
+++ b/modules/services_discovery/avahi.c
@@ -103,6 +103,27 @@ static const struct
 };
 #define NB_PROTOCOLS (sizeof(protocols) / sizeof(*protocols))
 
+static char* get_string_list_value( AvahiStringList* txt, const char* key )
+{
+    AvahiStringList *asl = avahi_string_list_find( txt, key );
+    if( asl == NULL )
+        return NULL;
+    char* res = NULL;
+    char *sl_key = NULL;
+    char *sl_value = NULL;
+    if( avahi_string_list_get_pair( asl, &sl_key, &sl_value, NULL ) == 0 &&
+        sl_value != NULL )
+    {
+        res = strdup( sl_value );
+    }
+
+    if( sl_key != NULL )
+        avahi_free( (void *)sl_key );
+    if( sl_value != NULL )
+        avahi_free( (void *)sl_value );
+    return res;
+}
+
 /*****************************************************************************
  * helpers
  *****************************************************************************/
@@ -120,8 +141,6 @@ static void add_renderer( const char *psz_protocol, const char *psz_name,
     int renderer_flags = 0;
 
     if( !strcmp( "chromecast", psz_protocol ) ) {
-        int ret = 0;
-
         /* Capabilities */
         asl = avahi_string_list_find( txt, "ca" );
         if( asl != NULL ) {
@@ -145,43 +164,15 @@ static void add_renderer( const char *psz_protocol, const char *psz_name,
         }
 
         /* Friendly name */
-        asl = avahi_string_list_find( txt, "fn" );
-        if( asl != NULL )
-        {
-            char *key = NULL;
-            char *value = NULL;
-            if( avahi_string_list_get_pair( asl, &key, &value, NULL ) == 0 &&
-                value != NULL )
-            {
-                friendly_name = strdup( value );
-                if( !friendly_name )
-                    ret = -1;
-            }
-
-            if( key != NULL )
-                avahi_free( (void *)key );
-            if( value != NULL )
-                avahi_free( (void *)value );
-        }
-        if( ret < 0 )
-            goto error;
+        friendly_name = get_string_list_value( txt, "fn" );
 
         /* Icon */
-        asl = avahi_string_list_find( txt, "ic" );
-        if( asl != NULL ) {
-            char *key = NULL;
-            char *value = NULL;
-            if( avahi_string_list_get_pair( asl, &key, &value, NULL ) == 0 &&
-                value != NULL )
-                ret = asprintf( &icon_uri, "http://%s:8008%s", psz_addr, value);
-
-            if( key != NULL )
-                avahi_free( (void *)key );
-            if( value != NULL )
-                avahi_free( (void *)value );
+        char* icon_raw = get_string_list_value( txt, "ic" );
+        if( icon_raw != NULL ) {
+            if( asprintf( &icon_uri, "http://%s:8008%s", psz_addr, icon_raw) < 0 )
+                icon_uri = NULL;
+            free( icon_raw );
         }
-        if( ret < 0 )
-            goto error;
 
         if( asprintf( &uri, "%s://%s:%u", psz_protocol, psz_addr, i_port ) < 0 )
             goto error;



More information about the vlc-commits mailing list