[vlc-devel] [PATCH 4/4] input item: use vlc_list for per-category infos

RĂ©mi Denis-Courmont remi at remlab.net
Tue Jun 12 20:27:07 CEST 2018


---
 include/vlc_input_item.h                  |  8 +++-
 modules/control/oldrc.c                   |  8 ++--
 modules/gui/macosx/VLCPlaylistInfo.m      |  7 ++--
 modules/gui/ncurses.c                     |  6 +--
 modules/gui/qt/components/info_panels.cpp | 11 +++--
 modules/lua/libs/input.c                  |  9 ++--
 src/input/info.h                          | 50 +++++++++++------------
 src/input/item.c                          | 10 +++--
 8 files changed, 55 insertions(+), 54 deletions(-)

diff --git a/include/vlc_input_item.h b/include/vlc_input_item.h
index cb6a9b61a1..57d8311f83 100644
--- a/include/vlc_input_item.h
+++ b/include/vlc_input_item.h
@@ -33,6 +33,7 @@
 #include <vlc_meta.h>
 #include <vlc_epg.h>
 #include <vlc_events.h>
+#include <vlc_list.h>
 
 #include <string.h>
 
@@ -43,13 +44,16 @@ struct info_t
 {
     char *psz_name;            /**< Name of this info */
     char *psz_value;           /**< Value of the info */
+    struct vlc_list node;
 };
 
+#define info_foreach(info, it, cat) \
+        vlc_list_foreach(info, it, cat, info_t, node)
+
 struct info_category_t
 {
     char   *psz_name;      /**< Name of this category */
-    int    i_infos;        /**< Number of infos in the category */
-    struct info_t **pp_infos;     /**< Pointer to an array of infos */
+    struct vlc_list infos; /**< Infos in the category */
 };
 
 /**
diff --git a/modules/control/oldrc.c b/modules/control/oldrc.c
index 40d004b871..59cf8774c9 100644
--- a/modules/control/oldrc.c
+++ b/modules/control/oldrc.c
@@ -659,21 +659,19 @@ static void *Run( void *data )
         {
             if( p_sys->p_input )
             {
-                int i, j;
+                int i;
                 vlc_mutex_lock( &input_GetItem(p_sys->p_input)->lock );
                 for ( i = 0; i < input_GetItem(p_sys->p_input)->i_categories; i++ )
                 {
                     info_category_t *p_category = input_GetItem(p_sys->p_input)
                                                         ->pp_categories[i];
+                    info_t *p_info;
 
                     msg_rc( "+----[ %s ]", p_category->psz_name );
                     msg_rc( "| " );
-                    for ( j = 0; j < p_category->i_infos; j++ )
-                    {
-                        info_t *p_info = p_category->pp_infos[j];
+                    info_foreach(p_info, it, &p_category->infos)
                         msg_rc( "| %s: %s", p_info->psz_name,
                                 p_info->psz_value );
-                    }
                     msg_rc( "| " );
                 }
                 msg_rc( "+----[ end of stream info ]" );
diff --git a/modules/gui/macosx/VLCPlaylistInfo.m b/modules/gui/macosx/VLCPlaylistInfo.m
index 04fc4f7c4f..a5371ff94e 100644
--- a/modules/gui/macosx/VLCPlaylistInfo.m
+++ b/modules/gui/macosx/VLCPlaylistInfo.m
@@ -297,6 +297,7 @@ FREENULL( psz_##foo );
 
         for (int i = 0; i < p_item->i_categories; i++) {
             info_category_t *cat = p_item->pp_categories[i];
+            info_t *info;
 
             VLCInfoTreeItem *subItem = [[VLCInfoTreeItem alloc] init];
             subItem.name = toNSStr(cat->psz_name);
@@ -304,10 +305,10 @@ FREENULL( psz_##foo );
             // Build list of codec details
             NSMutableArray *infos = [NSMutableArray array];
 
-            for (int j = 0; j < cat->i_infos; j++) {
+            info_foreach(info, it, &cat->infos) {
                 VLCInfoTreeItem *infoItem = [[VLCInfoTreeItem alloc] init];
-                infoItem.name = toNSStr(cat->pp_infos[j]->psz_name);
-                infoItem.value = toNSStr(cat->pp_infos[j]->psz_value);
+                infoItem.name = toNSStr(info->psz_name);
+                infoItem.value = toNSStr(info->psz_value);
                 [infos addObject:infoItem];
             }
 
diff --git a/modules/gui/ncurses.c b/modules/gui/ncurses.c
index f745e6359e..deaa8f90a6 100644
--- a/modules/gui/ncurses.c
+++ b/modules/gui/ncurses.c
@@ -776,14 +776,14 @@ static int DrawInfo(intf_thread_t *intf, input_thread_t *p_input)
     vlc_mutex_lock(&item->lock);
     for (int i = 0; i < item->i_categories; i++) {
         info_category_t *p_category = item->pp_categories[i];
+        info_t *p_info;
+
         if (sys->color) color_set(C_CATEGORY, NULL);
         MainBoxWrite(sys, l++, _("  [%s]"), p_category->psz_name);
         if (sys->color) color_set(C_DEFAULT, NULL);
-        for (int j = 0; j < p_category->i_infos; j++) {
-            info_t *p_info = p_category->pp_infos[j];
+        info_foreach(p_info, it, &p_category->infos)
             MainBoxWrite(sys, l++, _("      %s: %s"),
                          p_info->psz_name, p_info->psz_value);
-        }
     }
     vlc_mutex_unlock(&item->lock);
 
diff --git a/modules/gui/qt/components/info_panels.cpp b/modules/gui/qt/components/info_panels.cpp
index d465993107..bdcfda5f0f 100644
--- a/modules/gui/qt/components/info_panels.cpp
+++ b/modules/gui/qt/components/info_panels.cpp
@@ -502,18 +502,17 @@ void InfoPanel::update( input_item_t *p_item)
 
     for( int i = 0; i< p_item->i_categories ; i++)
     {
+        info_t *info;
+
         current_item = new QTreeWidgetItem();
         current_item->setText( 0, qfu(p_item->pp_categories[i]->psz_name) );
         InfoTree->addTopLevelItem( current_item );
 
-        for( int j = 0 ; j < p_item->pp_categories[i]->i_infos ; j++ )
+        info_foreach(info, it, &p_item->pp_categories[i]->infos)
         {
             child_item = new QTreeWidgetItem ();
-            child_item->setText( 0,
-                    qfu(p_item->pp_categories[i]->pp_infos[j]->psz_name)
-                    + ": "
-                    + qfu(p_item->pp_categories[i]->pp_infos[j]->psz_value));
-
+            child_item->setText( 0, qfu(info->psz_name) + ": "
+                                    + qfu(info->psz_value));
             current_item->addChild(child_item);
         }
         InfoTree->setItemExpanded( current_item, true);
diff --git a/modules/lua/libs/input.c b/modules/lua/libs/input.c
index a38eaf62be..47760997dd 100644
--- a/modules/lua/libs/input.c
+++ b/modules/lua/libs/input.c
@@ -80,13 +80,12 @@ static int vlclua_input_item_info( lua_State *L )
     for( i = 0; i < i_cat; i++ )
     {
         info_category_t *p_category = p_item->pp_categories[i];
-        int i_infos = p_category->i_infos;
-        int j;
+        info_t *p_info;
+
         lua_pushstring( L, p_category->psz_name );
-        lua_createtable( L, 0, i_infos );
-        for( j = 0; j < i_infos; j++ )
+        lua_newtable( L );
+        info_foreach(p_info, it, &p_category->infos)
         {
-            info_t *p_info = p_category->pp_infos[j];
             lua_pushstring( L, p_info->psz_name );
             lua_pushstring( L, p_info->psz_value );
             lua_settable( L, -3 );
diff --git a/src/input/info.h b/src/input/info.h
index 07fc2c3eef..713e9f19b6 100644
--- a/src/input/info.h
+++ b/src/input/info.h
@@ -50,47 +50,42 @@ static inline info_category_t *info_category_New(const char *name)
     if (!cat)
         return NULL;
     cat->psz_name = strdup(name);
-    cat->i_infos  = 0;
-    cat->pp_infos = NULL;
-
+    vlc_list_init(&cat->infos);
     return cat;
 }
 
 static inline info_t *info_category_FindInfo(const info_category_t *cat,
-                                             int *index, const char *name)
+                                             const char *name)
 {
-    for (int i = 0; i < cat->i_infos; i++) {
-        if (!strcmp(cat->pp_infos[i]->psz_name, name)) {
-            if (index)
-                *index = i;
-            return cat->pp_infos[i];
-        }
-    }
+    info_t *info;
+
+    info_foreach(info, it, &cat->infos)
+        if (!strcmp(info->psz_name, name))
+            return info;
     return NULL;
 }
 
 static inline void info_category_ReplaceInfo(info_category_t *cat,
                                              info_t *info)
 {
-    int index;
-    info_t *old = info_category_FindInfo(cat, &index, info->psz_name);
+    info_t *old = info_category_FindInfo(cat, info->psz_name);
     if (old) {
-        info_Delete(cat->pp_infos[index]);
-        cat->pp_infos[index] = info;
-    } else
-        TAB_APPEND(cat->i_infos, cat->pp_infos, info);
+        vlc_list_remove(&old->node);
+        info_Delete(old);
+    }
+    vlc_list_append(&info->node, &cat->infos);
 }
 
 static inline info_t *info_category_VaAddInfo(info_category_t *cat,
                                               const char *name,
                                               const char *format, va_list args)
 {
-    info_t *info = info_category_FindInfo(cat, NULL, name);
+    info_t *info = info_category_FindInfo(cat, name);
     if (!info) {
         info = info_New(name);
         if (!info)
             return NULL;
-        TAB_APPEND(cat->i_infos, cat->pp_infos, info);
+        vlc_list_append(&info->node, &cat->infos);
     } else
         free(info->psz_value);
     if (vasprintf(&info->psz_value, format, args) == -1)
@@ -113,10 +108,10 @@ static inline info_t *info_category_AddInfo(info_category_t *cat,
 
 static inline int info_category_DeleteInfo(info_category_t *cat, const char *name)
 {
-    int index;
-    if (info_category_FindInfo(cat, &index, name)) {
-        info_Delete(cat->pp_infos[index]);
-        TAB_ERASE(cat->i_infos, cat->pp_infos, index);
+    info_t *info = info_category_FindInfo(cat, name);
+    if (info != NULL) {
+        vlc_list_remove(&info->node);
+        info_Delete(info);
         return VLC_SUCCESS;
     }
     return VLC_EGENERIC;
@@ -124,9 +119,12 @@ static inline int info_category_DeleteInfo(info_category_t *cat, const char *nam
 
 static inline void info_category_Delete(info_category_t *cat)
 {
-    for (int i = 0; i < cat->i_infos; i++)
-        info_Delete(cat->pp_infos[i]);
-    free(cat->pp_infos);
+    info_t *info;
+
+    while ((info = vlc_list_first_entry_or_null(&cat->infos, info_t, node))) {
+        vlc_list_remove(&info->node);
+        info_Delete(info);
+    }
     free(cat->psz_name);
     free(cat);
 }
diff --git a/src/input/item.c b/src/input/item.c
index 05b7ea11cb..cd53cb019c 100644
--- a/src/input/item.c
+++ b/src/input/item.c
@@ -731,7 +731,7 @@ char *input_item_GetInfo( input_item_t *p_i,
     const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
     if( p_cat )
     {
-        info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
+        info_t *p_info = info_category_FindInfo( p_cat, psz_name );
         if( p_info && p_info->psz_value )
         {
             char *psz_ret = strdup( p_info->psz_value );
@@ -847,9 +847,11 @@ void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
     info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
     if( p_old )
     {
-        for( int i = 0; i < p_cat->i_infos; i++ )
-            info_category_ReplaceInfo( p_old, p_cat->pp_infos[i] );
-        TAB_CLEAN( p_cat->i_infos, p_cat->pp_infos );
+        info_t *info;
+
+        info_foreach(info, it, &p_cat->infos)
+            info_category_ReplaceInfo( p_old, info );
+        vlc_list_init( &p_cat->infos );
         info_category_Delete( p_cat );
     }
     else
-- 
2.17.1



More information about the vlc-devel mailing list