[vlc-devel] [PATCH 2/5] lua: sd: factor common input item creation code

RĂ©mi Denis-Courmont remi at remlab.net
Wed May 10 20:46:05 CEST 2017


This also adds title and UID to sub-items, because why not.
---
 modules/lua/libs/sd.c | 248 ++++++++++++++++++++++----------------------------
 1 file changed, 108 insertions(+), 140 deletions(-)

diff --git a/modules/lua/libs/sd.c b/modules/lua/libs/sd.c
index d665c0b5ba..e69ccde37d 100644
--- a/modules/lua/libs/sd.c
+++ b/modules/lua/libs/sd.c
@@ -115,73 +115,113 @@ static const luaL_Reg vlclua_item_reg[] = {
     { NULL, NULL }
 };
 
+static input_item_t *vlclua_sd_create_item( services_discovery_t *p_sd,
+                                            lua_State *L )
+{
+    if( !lua_istable( L, -1 ) )
+    {
+        msg_Err( p_sd, "Error: argument must be table" );
+        return NULL;
+    }
 
-/*** Input item tree node ***/
+    lua_getfield( L, -1, "path" );
+    if( !lua_isstring( L, -1 ) )
+    {
+        msg_Err( p_sd, "Error: \"path\" parameter is required" );
+        return NULL;
+    }
 
-static int vlclua_node_add_subitem( 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 )
+    const char *psz_path = lua_tostring( L, -1 );
+
+    lua_getfield( L, -2, "title" );
+
+    const char *psz_title = luaL_checkstring( L, -1 )
+       ? luaL_checkstring( L, -1 )
+       : psz_path;
+
+    input_item_t *p_input = input_item_New( psz_path, psz_title );
+    if( unlikely(p_input == NULL) )
+        return NULL;
+
+    /* The table must be at the top of the stack when calling
+     * vlclua_read_options() */
+    char **ppsz_options = NULL;
+    int i_options = 0;
+
+    lua_pushvalue( L, -3 );
+    vlclua_read_options( p_sd, L, &i_options, &ppsz_options );
+    lua_pop( L, 3 );
+
+    input_item_AddOptions( p_input, i_options, (const char **)ppsz_options,
+                           VLC_INPUT_OPTION_TRUSTED );
+    while( i_options > 0 )
+        free( ppsz_options[--i_options] );
+    free( ppsz_options );
+
+    vlclua_read_meta_data( p_sd, L, p_input );
+    /* This one is to be tested... */
+    vlclua_read_custom_meta_data( p_sd, L, p_input );
+    /* The duration is given in seconds, convert to microseconds */
+
+    lua_getfield( L, -1, "duration" );
+    if( lua_isnumber( L, -1 ) )
+        input_item_SetDuration( p_input, (lua_tonumber( L, -1 )*1e6) );
+    else if( !lua_isnil( L, -1 ) )
+        msg_Warn( p_sd, "Item duration should be a number (in seconds)." );
+    lua_pop( L, 1 );
+
+    /* string to build the input item uid */
+    lua_getfield( L, -1, "uiddata" );
+    if( lua_isstring( L, -1 ) )
     {
-        if( lua_istable( L, -1 ) )
+        char *s = strdup( luaL_checkstring( L, -1 ) );
+        if ( s )
         {
-            lua_getfield( L, -1, "path" );
-            if( lua_isstring( L, -1 ) )
-            {
-                const char *psz_path = lua_tostring( L, -1 );
+            struct md5_s md5;
+            InitMD5( &md5 );
+            AddMD5( &md5, s, strlen( s ) );
+            EndMD5( &md5 );
+            free( s );
+            s = psz_md5_hash( &md5 );
+            if ( s )
+                 input_item_AddInfo( p_input, "uid", "md5", "%s", s );
+            free( s );
+        }
+    }
+    lua_pop( L, 1 );
 
-                /* The table must be at the top of the stack when calling
-                 * vlclua_read_options() */
-                char **ppsz_options = NULL;
-                int i_options = 0;
-                lua_pushvalue( L, -2 );
-                vlclua_read_options( p_sd, L, &i_options, &ppsz_options );
+    input_item_t **udata = lua_newuserdata( L, sizeof( input_item_t * ) );
+    *udata = p_input;
 
-                input_item_t *p_input = input_item_New( psz_path, psz_path );
-                lua_pop( L, 2 );
+    if( luaL_newmetatable( L, "input_item_t" ) )
+    {
+        lua_newtable( L );
+        luaL_register( L, NULL, vlclua_item_reg );
+        lua_setfield( L, -2, "__index" );
+        lua_pushliteral( L, "none of your business" );
+        lua_setfield( L, -2, "__metatable" );
+    }
+    lua_setmetatable( L, -2 );
 
-                if( p_input )
-                {
-                    input_item_AddOptions( p_input, i_options,
-                                           (const char **)ppsz_options,
-                                           VLC_INPUT_OPTION_TRUSTED );
-
-                    vlclua_read_meta_data( p_sd, L, p_input );
-                    /* This one is to be tested... */
-                    vlclua_read_custom_meta_data( p_sd, L, p_input );
-                    lua_getfield( L, -1, "duration" );
-                    if( lua_isnumber( L, -1 ) )
-                        input_item_SetDuration( p_input, (lua_tonumber( L, -1 )*1e6) );
-                    else if( !lua_isnil( L, -1 ) )
-                        msg_Warn( p_sd, "Item duration should be a number (in seconds)." );
-                    lua_pop( L, 1 );
+    return p_input;
+}
 
-                    input_item_t **udata = (input_item_t **)
-                                           lua_newuserdata( L, sizeof( input_item_t * ) );
-                    *udata = p_input;
-                    if( luaL_newmetatable( L, "input_item_t" ) )
-                    {
-                        lua_newtable( L );
-                        luaL_register( L, NULL, vlclua_item_reg );
-                        lua_setfield( L, -2, "__index" );
-                        lua_pushliteral( L, "none of your business" );
-                        lua_setfield( L, -2, "__metatable" );
-                    }
-                    lua_setmetatable( L, -2 );
 
-                    input_item_PostSubItem( *pp_node, p_input );
-                    input_item_Release( p_input );
-                }
-                while( i_options > 0 )
-                    free( ppsz_options[--i_options] );
-                free( ppsz_options );
-            }
-            else
-                msg_Err( p_sd, "node:add_subitem: the \"path\" parameter can't be empty" );
-        }
-        else
-            msg_Err( p_sd, "Error parsing add_subitem arguments" );
+/*** Input item tree node ***/
+
+static int vlclua_node_add_subitem( 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 == NULL )
+        return 1;
+
+    input_item_t *p_input = vlclua_sd_create_item( p_sd, L );
+    if( p_input != NULL )
+    {
+        input_item_PostSubItem( *pp_node, p_input );
+        input_item_Release( p_input );
     }
     return 1;
 }
@@ -252,91 +292,19 @@ static const luaL_Reg vlclua_node_reg[] = {
 static int vlclua_sd_add_item( lua_State *L )
 {
     services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
-    if( lua_istable( L, -1 ) )
-    {
-        lua_getfield( L, -1, "path" );
-        if( lua_isstring( L, -1 ) )
-        {
-            const char *psz_path = lua_tostring( L, -1 );
-
-            lua_getfield( L, -2, "title" );
-            const char *psz_title = luaL_checkstring( L, -1 ) ? luaL_checkstring( L, -1 ) : psz_path;
-
-            /* The table must be at the top of the stack when calling
-             * vlclua_read_options() */
-            char **ppsz_options = NULL;
-            int i_options = 0;
-            lua_pushvalue( L, -3 );
-            vlclua_read_options( p_sd, L, &i_options, &ppsz_options );
-
-            input_item_t *p_input = input_item_New( psz_path, psz_title );
-            lua_pop( L, 3 );
-
-            if( p_input )
-            {
-                input_item_AddOptions( p_input, i_options,
-                                       (const char **)ppsz_options,
-                                       VLC_INPUT_OPTION_TRUSTED );
-                vlclua_read_meta_data( p_sd, L, p_input );
-                /* This one is to be tested... */
-                vlclua_read_custom_meta_data( p_sd, L, p_input );
-                /* The duration is given in seconds, convert to microseconds */
-                lua_getfield( L, -1, "duration" );
-                if( lua_isnumber( L, -1 ) )
-                   input_item_SetDuration( p_input, (lua_tonumber( L, -1 )*1e6) );
-                else if( !lua_isnil( L, -1 ) )
-                    msg_Warn( p_sd, "Item duration should be a number (in seconds)." );
-                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 );
-                lua_pop( L, 1 );
 
-                /* string to build the input item uid */
-                lua_getfield( L, -1, "uiddata" );
-                if( lua_isstring( L, -1 ) )
-                {
-                    char *s = strdup( luaL_checkstring( L, -1 ) );
-                    if ( s )
-                    {
-                        struct md5_s md5;
-                        InitMD5( &md5 );
-                        AddMD5( &md5, s, strlen( s ) );
-                        EndMD5( &md5 );
-                        free( s );
-                        s = psz_md5_hash( &md5 );
-                        if ( s )
-                            input_item_AddInfo( p_input, "uid", "md5", "%s", s );
-                        free( s );
-                    }
-                }
-                lua_pop( L, 1 );
+    input_item_t *p_input = vlclua_sd_create_item( p_sd, L );
+    if( p_input == NULL )
+        return 1;
 
-                input_item_t **udata = (input_item_t **)
-                                       lua_newuserdata( L, sizeof( input_item_t * ) );
-                *udata = p_input;
-                if( luaL_newmetatable( L, "input_item_t" ) )
-                {
-                    lua_newtable( L );
-                    luaL_register( L, NULL, vlclua_item_reg );
-                    lua_setfield( L, -2, "__index" );
-                    lua_pushliteral( L, "none of your business" );
-                    lua_setfield( L, -2, "__metatable" );
-                }
-                lua_setmetatable( L, -2 );
-                input_item_Release( p_input );
-            }
-            while( i_options > 0 )
-                free( ppsz_options[--i_options] );
-            free( ppsz_options );
-        }
-        else
-            msg_Err( p_sd, "vlc.sd.add_item: the \"path\" parameter can't be empty" );
-    }
+    lua_getfield( L, -2, "category" );
+    if( lua_isstring( L, -1 ) )
+        services_discovery_AddItem( p_sd, p_input, luaL_checkstring( L, -1 ) );
     else
-        msg_Err( p_sd, "Error parsing add_item arguments" );
+        services_discovery_AddItem( p_sd, p_input, NULL );
+    lua_pop( L, 1 );
+
+    input_item_Release( p_input );
     return 1;
 }
 
-- 
2.11.0



More information about the vlc-devel mailing list