[vlc-commits] luasd: helper function to fetch longname

Pierre Ynard git at videolan.org
Wed Nov 16 08:27:57 CET 2016


vlc | branch: master | Pierre Ynard <linkfanel at yahoo.fr> | Wed Nov 16 08:25:56 2016 +0100| [8a57fce912d9b3cc44b3f0124722db48475a5ee7] | committer: Pierre Ynard

luasd: helper function to fetch longname

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

 modules/lua/services_discovery.c | 35 +++++++++++++++++++++++++++++++++++
 modules/lua/vlc.c                | 17 ++---------------
 modules/lua/vlc.h                |  4 ++++
 3 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/modules/lua/services_discovery.c b/modules/lua/services_discovery.c
index 923f602..51d9d9f 100644
--- a/modules/lua/services_discovery.c
+++ b/modules/lua/services_discovery.c
@@ -40,6 +40,41 @@ static int DoSearch( services_discovery_t *p_sd, const char *psz_query );
 static int FillDescriptor( services_discovery_t *, services_discovery_descriptor_t * );
 static int Control( services_discovery_t *p_sd, int i_command, va_list args );
 
+// When successful, the returned string is stored on top of the lua
+// stack and remains valid as long as it is kept in the stack.
+#undef vlclua_sd_description
+const char *vlclua_sd_description( vlc_object_t *obj, lua_State *L,
+                                   const char *filename )
+{
+    lua_getglobal( L, "descriptor" );
+    if( !lua_isfunction( L, -1 ) )
+    {
+        msg_Warn( obj, "No 'descriptor' function in '%s'", filename );
+        lua_pop( L, 1 );
+        return NULL;
+    }
+
+    if( lua_pcall( L, 0, 1, 0 ) )
+    {
+        msg_Warn( obj, "Error while running script %s, "
+                  "function descriptor(): %s", filename,
+                  lua_tostring( L, -1 ) );
+        lua_pop( L, 1 );
+        return NULL;
+    }
+
+    lua_getfield( L, -1, "title" );
+    if ( !lua_isstring( L, -1 ) )
+    {
+        msg_Warn( obj, "'descriptor' function in '%s' returned no title",
+                  filename );
+        lua_pop( L, 2 );
+        return NULL;
+    }
+
+    return lua_tostring( L, -1 );
+}
+
 static const char * const ppsz_sd_options[] = { "sd", "longname", NULL };
 
 /*****************************************************************************
diff --git a/modules/lua/vlc.c b/modules/lua/vlc.c
index 59eeb2f..bae806d 100644
--- a/modules/lua/vlc.c
+++ b/modules/lua/vlc.c
@@ -678,25 +678,12 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
                 lua_close( L );
                 continue;
             }
-            const char *psz_longname;
             char *temp = strchr( *ppsz_file, '.' );
             if( temp )
                 *temp = '\0';
-            lua_getglobal( L, "descriptor" );
-            if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
-            {
-                msg_Warn( probe, "No 'descriptor' function in '%s'", psz_filename );
-                lua_pop( L, 1 );
+            const char *psz_longname = vlclua_sd_description( probe, L, psz_filename );
+            if( psz_longname == NULL )
                 psz_longname = *ppsz_file;
-            }
-            else
-            {
-                lua_getfield( L, -1, "title" );
-                if( lua_isstring( L, -1 ) )
-                    psz_longname = lua_tostring( L, -1 );
-                else
-                    psz_longname = *ppsz_file;
-            }
 
             char *psz_file_esc = config_StringEscape( *ppsz_file );
             char *psz_longname_esc = config_StringEscape( psz_longname );
diff --git a/modules/lua/vlc.h b/modules/lua/vlc.h
index 0325edd..6d55b3d 100644
--- a/modules/lua/vlc.h
+++ b/modules/lua/vlc.h
@@ -91,6 +91,10 @@ void Close_Extension( vlc_object_t * );
 int Open_LuaSD( vlc_object_t * );
 void Close_LuaSD( vlc_object_t * );
 
+// Helper
+const char *vlclua_sd_description( vlc_object_t *, lua_State *, const char * );
+#define vlclua_sd_description(a, b, c) vlclua_sd_description(VLC_OBJECT(a), b, c)
+
 /*****************************************************************************
  * Lua debug
  *****************************************************************************/



More information about the vlc-commits mailing list