[vlc-commits] lua: sd: factor common input item node creation code

Rémi Denis-Courmont git at videolan.org
Thu May 11 19:15:03 CEST 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed May 10 20:20:23 2017 +0300| [b8eaab64d9eac9c34ccc063b7ddd2e1f34c64a1d] | committer: Rémi Denis-Courmont

lua: sd: factor common input item node creation code

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

 modules/lua/libs/sd.c | 169 +++++++++++++++++++++-----------------------------
 1 file changed, 72 insertions(+), 97 deletions(-)

diff --git a/modules/lua/libs/sd.c b/modules/lua/libs/sd.c
index 86ac9da49b..d7aea6c460 100644
--- a/modules/lua/libs/sd.c
+++ b/modules/lua/libs/sd.c
@@ -127,7 +127,7 @@ static input_item_t *vlclua_sd_create_item( services_discovery_t *p_sd,
     lua_getfield( L, -1, "path" );
     if( !lua_isstring( L, -1 ) )
     {
-        msg_Err( p_sd, "Error: \"path\" parameter is required" );
+        msg_Err( p_sd, "Error: \"%s\" parameter is required", "path" );
         return NULL;
     }
 
@@ -230,55 +230,68 @@ static int vlclua_node_add_subitem( lua_State *L )
 
 static const luaL_Reg vlclua_node_reg[];
 
+static input_item_t *vlclua_sd_create_node( services_discovery_t *p_sd,
+                                            lua_State *L )
+{
+    if( !lua_istable( L, -1 ) )
+    {
+        msg_Err( p_sd, "Error: argument must be table" );
+        return NULL;
+    }
+
+    lua_getfield( L, -1, "title" );
+    if( !lua_isstring( L, -1 ) )
+    {
+        msg_Err( p_sd, "Error: \"%s\" parameter is required", "title" );
+        return NULL;
+    }
+
+    const char *psz_name = lua_tostring( L, -1 );
+    input_item_t *p_input = input_item_NewExt( "vlc://nop", psz_name, -1,
+                                               ITEM_TYPE_NODE,
+                                               ITEM_NET_UNKNOWN );
+    lua_pop( L, 1 );
+
+    if( unlikely(p_input == NULL) )
+        return NULL;
+
+    lua_getfield( L, -1, "arturl" );
+    if( lua_isstring( L, -1 ) && strcmp( lua_tostring( L, -1 ), "" ) )
+    {
+        char *psz_value = strdup( lua_tostring( L, -1 ) );
+        EnsureUTF8( psz_value );
+        msg_Dbg( p_sd, "ArtURL: %s", psz_value );
+        /* TODO: ask for art download if not local file */
+        input_item_SetArtURL( p_input, psz_value );
+        free( psz_value );
+    }
+    lua_pop( L, 1 );
+
+    input_item_t **udata = lua_newuserdata( L, sizeof( input_item_t * ) );
+    *udata = p_input;
+    if( luaL_newmetatable( L, "node" ) )
+    {
+        lua_newtable( L );
+        luaL_register( L, NULL, vlclua_node_reg );
+        lua_setfield( L, -2, "__index" );
+    }
+    lua_setmetatable( L, -2 );
+
+    return p_input;
+}
+
 static int vlclua_node_add_subnode( lua_State *L )
 {
     services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
+
     input_item_t **pp_node = (input_item_t **)luaL_checkudata( L, 1, "node" );
-    if( *pp_node )
-    {
-        if( lua_istable( L, -1 ) )
-        {
-            lua_getfield( L, -1, "title" );
-            if( lua_isstring( L, -1 ) )
-            {
-                const char *psz_name = lua_tostring( L, -1 );
-                input_item_t *p_input = input_item_NewExt( "vlc://nop",
-                                                           psz_name, -1,
-                                                           ITEM_TYPE_NODE, ITEM_NET_UNKNOWN );
-                lua_pop( L, 1 );
-
-                if( p_input )
-                {
-                    lua_getfield( L, -1, "arturl" );
-                    if( lua_isstring( L, -1 ) && strcmp( lua_tostring( L, -1 ), "" ) )
-                    {
-                        char *psz_value = strdup( lua_tostring( L, -1 ) );
-                        EnsureUTF8( psz_value );
-                        msg_Dbg( p_sd, "ArtURL: %s", psz_value );
-                        input_item_SetArtURL( p_input, psz_value );
-                        free( psz_value );
-                    }
-
-                    input_item_t **udata = (input_item_t **)
-                                           lua_newuserdata( L, sizeof( input_item_t * ) );
-                    *udata = p_input;
-                    if( luaL_newmetatable( L, "node" ) )
-                    {
-                        lua_newtable( L );
-                        luaL_register( L, NULL, vlclua_node_reg );
-                        lua_setfield( L, -2, "__index" );
-                    }
-                    lua_setmetatable( L, -2 );
-
-                    input_item_PostSubItem( *pp_node, p_input );
-                }
-            }
-            else
-                msg_Err( p_sd, "node:add_node: the \"title\" parameter can't be empty" );
-        }
-        else
-            msg_Err( p_sd, "Error parsing add_node arguments" );
-    }
+    if( *pp_node == NULL )
+        return 1;
+
+    input_item_t *p_input = vlclua_sd_create_node( p_sd, L );
+    if( p_input != NULL )
+        input_item_PostSubItem( *pp_node, p_input );
+
     return 1;
 }
 
@@ -291,11 +304,9 @@ static const luaL_Reg vlclua_node_reg[] = {
 
 /*** Services discovery instance ***/
 
-static int vlclua_sd_add_item( lua_State *L )
+static int vlclua_sd_add_common( services_discovery_t *p_sd, lua_State *L,
+                                 input_item_t *p_input )
 {
-    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
-
-    input_item_t *p_input = vlclua_sd_create_item( p_sd, L );
     if( p_input == NULL )
         return 1;
 
@@ -310,56 +321,20 @@ static int vlclua_sd_add_item( lua_State *L )
     return 1;
 }
 
+static int vlclua_sd_add_item( lua_State *L )
+{
+    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
+    input_item_t *p_input = vlclua_sd_create_item( p_sd, L );
+
+    return vlclua_sd_add_common( p_sd, L, p_input );
+}
+
 static int vlclua_sd_add_node( lua_State *L )
 {
     services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
-    if( lua_istable( L, -1 ) )
-    {
-        lua_getfield( L, -1, "title" );
-        if( lua_isstring( L, -1 ) )
-        {
-            const char *psz_name = lua_tostring( L, -1 );
-            input_item_t *p_input = input_item_NewExt( "vlc://nop",
-                                                       psz_name, -1,
-                                                       ITEM_TYPE_NODE, ITEM_NET_UNKNOWN );
-            lua_pop( L, 1 );
-
-            if( p_input )
-            {
-                lua_getfield( L, -1, "arturl" );
-                if( lua_isstring( L, -1 ) && strcmp( lua_tostring( L, -1 ), "" ) )
-                {
-                    char *psz_value = strdup( lua_tostring( L, -1 ) );
-                    EnsureUTF8( psz_value );
-                    msg_Dbg( p_sd, "ArtURL: %s", psz_value );
-                    /** @todo Ask for art download if not local file */
-                    input_item_SetArtURL( p_input, psz_value );
-                    free( psz_value );
-                }
-                lua_pop( L, 1 );
-                lua_getfield( L, -1, "category" );
-                if( lua_isstring( L, -1 ) )
-                    services_discovery_AddItem( p_sd, p_input, luaL_checkstring( L, -1 ) );
-                else
-                    services_discovery_AddItem( p_sd, p_input, NULL );
-                input_item_t **udata = (input_item_t **)
-                                       lua_newuserdata( L, sizeof( input_item_t * ) );
-                *udata = p_input;
-                if( luaL_newmetatable( L, "node" ) )
-                {
-                    lua_newtable( L );
-                    luaL_register( L, NULL, vlclua_node_reg );
-                    lua_setfield( L, -2, "__index" );
-                }
-                lua_setmetatable( L, -2 );
-            }
-        }
-        else
-            msg_Err( p_sd, "vlc.sd.add_node: the \"title\" parameter can't be empty" );
-    }
-    else
-        msg_Err( p_sd, "Error parsing add_node arguments" );
-    return 1;
+    input_item_t *p_input = vlclua_sd_create_node( p_sd, L );
+
+    return vlclua_sd_add_common( p_sd, L, p_input );
 }
 
 static int vlclua_sd_remove_item( lua_State *L )



More information about the vlc-commits mailing list