]> git.sesse.net Git - vlc/blobdiff - src/playlist/sort.c
Fix an issu when sorting with the track number : the track numbers were in alphabetic...
[vlc] / src / playlist / sort.c
index b53206452a3a87ba504067823512eadcbb0c649e..c81a2ecb49dad8e1914a4831b095a67c95d53447 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * sort.c : Playlist sorting functions
  *****************************************************************************
- * Copyright (C) 1999-2004 the VideoLAN team
+ * Copyright (C) 1999-2007 the VideoLAN team
  * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
 #include "vlc_playlist.h"
 #include "playlist_internal.h"
@@ -84,6 +88,8 @@ static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
     vlc_value_t val;
     val.b_bool = VLC_TRUE;
 
+    (void)p_playlist; // a bit surprising we don't need p_playlist!
+
     if( i_mode == SORT_RANDOM )
     {
         for( i_position = 0; i_position < i_items ; i_position ++ )
@@ -102,11 +108,18 @@ static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
         return VLC_SUCCESS;
     }
 
-#define DO_META_SORT( node ) { \
-    char *psz_a = pp_items[i]->p_input->p_meta ?  \
-                       pp_items[i]->p_input->p_meta->psz_##node : NULL ; \
-    char *psz_b = pp_items[i_small]->p_input->p_meta ?  \
-                       pp_items[i_small]->p_input->p_meta->psz_##node : NULL; \
+#define META_STRCASECMP_NAME( i, i_small ) { \
+    char *psz_i = input_item_GetName( pp_items[i]->p_input ); \
+    char *psz_ismall = input_item_GetName( pp_items[i_small]->p_input ); \
+    i_test = strcasecmp( psz_i, psz_ismall ); \
+    free( psz_i ); \
+    free( psz_ismall ); \
+}
+
+
+#define DO_META_SORT_ADV( node, integer ) { \
+    char *psz_a = input_item_GetMeta( pp_items[i]->p_input, vlc_meta_##node ); \
+    char *psz_b = input_item_GetMeta( pp_items[i_small]->p_input, vlc_meta_##node ); \
     /* Nodes go first */ \
     if( pp_items[i]->i_children == -1 && pp_items[i_small]->i_children >= 0 ) \
         i_test = 1;\
@@ -117,8 +130,7 @@ static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
     else if( pp_items[i]->i_children >= 0 && \
                pp_items[i_small]->i_children >= 0 ) \
     { \
-         i_test = strcasecmp( pp_items[i]->p_input->psz_name, \
-                              pp_items[i_small]->p_input->psz_name ); \
+        META_STRCASECMP_NAME( i, i_small ) \
     } \
     /* Both are items */ \
     else if( psz_a == NULL && psz_b != NULL ) \
@@ -128,14 +140,17 @@ static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
     /* No meta, sort by name */ \
     else if( psz_a == NULL && psz_b == NULL ) \
     { \
-        i_test = strcasecmp( pp_items[i]->p_input->psz_name, \
-                             pp_items[i_small]->p_input->psz_name ); \
+        META_STRCASECMP_NAME( i, i_small ); \
     } \
     else \
     { \
-        i_test = strcmp( psz_b, psz_a ); \
+        if( !integer ) i_test = strcmp( psz_a, psz_b ); \
+        else           i_test = atoi( psz_a ) - atoi( psz_b ); \
     } \
+    free( psz_a ); \
+    free( psz_b ); \
 }
+#define DO_META_SORT( node ) DO_META_SORT_ADV( node, VLC_FALSE )
 
     for( i_position = 0; i_position < i_items -1 ; i_position ++ )
     {
@@ -146,26 +161,49 @@ static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
 
             if( i_mode == SORT_TITLE )
             {
-                i_test = strcasecmp( pp_items[i]->p_input->psz_name,
-                                     pp_items[i_small]->p_input->psz_name );
+                META_STRCASECMP_NAME( i, i_small );
             }
             else if( i_mode == SORT_TITLE_NUMERIC )
             {
-                i_test = atoi( pp_items[i]->p_input->psz_name ) -
-                         atoi( pp_items[i_small]->p_input->psz_name );
+                char *psz_i = input_item_GetName( pp_items[i]->p_input );
+                char *psz_ismall =
+                        input_item_GetName( pp_items[i_small]->p_input );
+                i_test = atoi( psz_i ) - atoi( psz_ismall );
+                free( psz_i );
+                free( psz_ismall );
             }
             else if( i_mode == SORT_DURATION )
             {
-                i_test = pp_items[i]->p_input->i_duration -
-                             pp_items[i_small]->p_input->i_duration;
+                i_test = input_item_GetDuration( pp_items[i]->p_input ) -
+                         input_item_GetDuration( pp_items[i_small]->p_input );
             }
             else if( i_mode == SORT_ARTIST )
             {
-                DO_META_SORT( artist );
+                DO_META_SORT( Artist );
+            }
+            else if( i_mode == SORT_GENRE )
+            {
+                DO_META_SORT( Genre );
             }
             else if( i_mode == SORT_ALBUM )
             {
-                DO_META_SORT( album );
+                DO_META_SORT( Album );
+            }
+            else if( i_mode == SORT_TRACK_NUMBER )
+            {
+                DO_META_SORT_ADV( TrackNumber, VLC_TRUE );
+            }
+            else if( i_mode == SORT_DESCRIPTION )
+            {
+                DO_META_SORT( Description );
+            }
+            else if( i_mode == SORT_RATING )
+            {
+                DO_META_SORT( Rating );
+            }
+            else if( i_mode == SORT_ID )
+            {
+                i_test = pp_items[i]->i_id - pp_items[i_small]->i_id;
             }
             else if( i_mode == SORT_TITLE_NODES_FIRST )
             {
@@ -198,5 +236,8 @@ static int playlist_ItemArraySort( playlist_t *p_playlist, int i_items,
         pp_items[i_position] = pp_items[i_small];
         pp_items[i_small] = p_temp;
     }
+#undef DO_META_SORT
+#undef DO_META_SORT_ADV
+
     return VLC_SUCCESS;
 }