[vlc-commits] lua: Allow the content of a (virtual) directory to be listed

Hugo Beauzée-Luyssen git at videolan.org
Thu Sep 21 17:49:28 CEST 2017


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Wed Sep 20 17:31:10 2017 +0200| [afc91404e485c484c2f7b8c338cad18195e03001] | committer: Hugo Beauzée-Luyssen

lua: Allow the content of a (virtual) directory to be listed

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

 modules/lua/libs/stream.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)

diff --git a/modules/lua/libs/stream.c b/modules/lua/libs/stream.c
index 2e8ac96d9f..9da67d4c41 100644
--- a/modules/lua/libs/stream.c
+++ b/modules/lua/libs/stream.c
@@ -48,11 +48,13 @@ static int vlclua_stream_read( lua_State * );
 static int vlclua_stream_readline( lua_State * );
 static int vlclua_stream_delete( lua_State * );
 static int vlclua_stream_add_filter( lua_State *L );
+static int vlclua_stream_readdir( lua_State *L );
 
 static const luaL_Reg vlclua_stream_reg[] = {
     { "read", vlclua_stream_read },
     { "readline", vlclua_stream_readline },
     { "addfilter", vlclua_stream_add_filter },
+    { "readdir", vlclua_stream_readdir },
     { NULL, NULL }
 };
 
@@ -98,6 +100,21 @@ static int vlclua_memory_stream_new( lua_State *L )
     return vlclua_stream_new_inner( L, p_stream );
 }
 
+static int vlclua_directory_stream_new( lua_State *L )
+{
+    vlc_object_t * p_this = vlclua_get_this( L );
+    const char * psz_url = luaL_checkstring( L, 1 );
+    stream_t *p_stream = vlc_stream_NewURL( p_this, psz_url );
+    if( !p_stream )
+        return vlclua_error( L );
+    if( vlc_stream_directory_Attach( &p_stream, NULL ) != VLC_SUCCESS )
+    {
+        vlc_stream_Delete( p_stream );
+        return vlclua_error( L );
+    }
+    return vlclua_stream_new_inner( L, p_stream );
+}
+
 static int vlclua_stream_read( lua_State *L )
 {
     int i_read;
@@ -179,6 +196,61 @@ static int vlclua_stream_add_filter( lua_State *L )
     return 1;
 }
 
+static int vlclua_stream_readdir( lua_State *L )
+{
+    stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
+    const char *psz_filter = NULL;
+    bool b_show_hidden = false;
+    if( lua_gettop( L ) >= 2 )
+    {
+        psz_filter = lua_tostring( L, 2 );
+        if( lua_gettop( L ) >= 3 )
+            b_show_hidden = lua_toboolean( L, 3 );
+    }
+
+    if( !pp_stream || !*pp_stream )
+        return vlclua_error( L );
+    if ( vlc_stream_Control( *pp_stream, STREAM_IS_DIRECTORY ) != VLC_SUCCESS )
+        return vlclua_error( L );
+
+    input_item_t *p_input = input_item_New( (*pp_stream)->psz_url, NULL );
+    if( psz_filter )
+    {
+        char *psz_opt;
+        if( asprintf( &psz_opt, ":ignore-filetype=\"%s\"", psz_filter ) < 0 )
+        {
+            input_item_Release( p_input );
+            return vlclua_error( L );
+        }
+        input_item_AddOption( p_input, psz_opt, VLC_INPUT_OPTION_TRUSTED );
+        free( psz_opt );
+    }
+    else
+        input_item_AddOption( p_input, "ignore-filetypes=\"\"",
+                              VLC_INPUT_OPTION_TRUSTED );
+    if( b_show_hidden )
+        input_item_AddOption( p_input, "show-hiddenfiles",
+                              VLC_INPUT_OPTION_TRUSTED );
+    input_item_node_t *p_items = input_item_node_Create( p_input );
+    if( !p_items )
+        return vlclua_error( L );
+    if ( vlc_stream_ReadDir( *pp_stream, p_items ) )
+    {
+        input_item_node_Delete( p_items );
+        return vlclua_error( L );
+    }
+    lua_newtable( L );
+    for ( int i = 0; i < p_items->i_children; ++i )
+    {
+        lua_pushinteger( L, i + 1 );
+        vlclua_input_item_get( L, p_items->pp_children[i]->p_item );
+        lua_settable( L, -3 );
+    }
+    input_item_node_Delete( p_items );
+    input_item_Release( p_input );
+    return 1;
+}
+
 static int vlclua_stream_delete( lua_State *L )
 {
     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
@@ -195,4 +267,6 @@ void luaopen_stream( lua_State *L )
     lua_setfield( L, -2, "stream" );
     lua_pushcfunction( L, vlclua_memory_stream_new );
     lua_setfield( L, -2, "memory_stream" );
+    lua_pushcfunction( L, vlclua_directory_stream_new );
+    lua_setfield( L, -2, "directory_stream" );
 }



More information about the vlc-commits mailing list