sort.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:5k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * sort.c : Playlist sorting functions
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 VideoLAN
  5.  * $Id: sort.c 8109 2004-07-01 12:37:53Z sigmunau $
  6.  *
  7.  * Authors: Cl閙ent Stenac <zorglub@videolan.org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. #include <stdlib.h>                                      /* free(), strtol() */
  24. #include <stdio.h>                                              /* sprintf() */
  25. #include <string.h>                                            /* strerror() */
  26. #include <vlc/vlc.h>
  27. #include <vlc/input.h>
  28. #include <vlc/vout.h>
  29. #include <vlc/sout.h>
  30. #include "vlc_playlist.h"
  31. /**
  32.  * Sort the playlist
  33.  * param p_playlist the playlist
  34.  * param i_mode: SORT_ID, SORT_TITLE, SORT_GROUP, SORT_AUTHOR, SORT_RANDOM
  35.  * param i_type: ORDER_NORMAL or ORDER_REVERSE (reversed order)
  36.  * return VLC_SUCCESS on success
  37.  */
  38. int playlist_Sort( playlist_t * p_playlist , int i_mode, int i_type )
  39. {
  40.     int i , i_small , i_position;
  41.     playlist_item_t *p_temp;
  42.     vlc_value_t val;
  43.     val.b_bool = VLC_TRUE;
  44.     vlc_mutex_lock( &p_playlist->object_lock );
  45.     p_playlist->i_sort = i_mode;
  46.     p_playlist->i_order = i_type;
  47.     /* playlist with one or less items are allways sorted in all
  48.        manners, quit fast. */
  49.     if( p_playlist->i_size <= 1 )
  50.     {
  51.         vlc_mutex_unlock( &p_playlist->object_lock );
  52.         /* Notify the interfaces, is this necessary? */
  53.         var_Set( p_playlist, "intf-change", val );
  54.         return VLC_SUCCESS;
  55.     }
  56.     if( i_mode == SORT_RANDOM )
  57.     {
  58.         for( i_position = 0; i_position < p_playlist->i_size ; i_position ++ )
  59.         {
  60.             int i_new  = rand() % (p_playlist->i_size - 1);
  61.             /* Keep the correct current index */
  62.             if( i_new == p_playlist->i_index )
  63.                 p_playlist->i_index = i_position;
  64.             else if( i_position == p_playlist->i_index )
  65.                 p_playlist->i_index = i_new;
  66.             p_temp = p_playlist->pp_items[i_position];
  67.             p_playlist->pp_items[i_position] = p_playlist->pp_items[i_new];
  68.             p_playlist->pp_items[i_new] = p_temp;
  69.         }
  70.         vlc_mutex_unlock( &p_playlist->object_lock );
  71.         /* Notify the interfaces */
  72.         var_Set( p_playlist, "intf-change", val );
  73.         return VLC_SUCCESS;
  74.     }
  75.     for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
  76.     {
  77.         i_small  = i_position;
  78.         for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
  79.         {
  80.             int i_test = 0;
  81.             if( i_mode == SORT_ID )
  82.             {
  83.                 i_test = p_playlist->pp_items[i]->i_id -
  84.                                  p_playlist->pp_items[i_small]->i_id;
  85.             }
  86.             else if( i_mode == SORT_TITLE )
  87.             {
  88.                 i_test = strcasecmp( p_playlist->pp_items[i]->input.psz_name,
  89.                              p_playlist->pp_items[i_small]->input.psz_name );
  90.             }
  91.             else if( i_mode == SORT_GROUP )
  92.             {
  93.                 i_test = p_playlist->pp_items[i]->i_group -
  94.                              p_playlist->pp_items[i_small]->i_group;
  95.             }
  96.             else if( i_mode == SORT_DURATION )
  97.             {
  98.                 i_test = p_playlist->pp_items[i]->input.i_duration -
  99.                              p_playlist->pp_items[i_small]->input.i_duration;
  100.             }
  101.             else if( i_mode == SORT_AUTHOR )
  102.             {
  103.                  i_test = strcasecmp(
  104.                           playlist_GetInfo( p_playlist, i,
  105.                                             _("General") , _("Author") ),
  106.                           playlist_GetInfo( p_playlist, i_small,
  107.                                             _("General") , _("Author") ) );
  108.             }
  109.             if( ( i_type == ORDER_NORMAL  && i_test < 0 ) ||
  110.                 ( i_type == ORDER_REVERSE && i_test > 0 ) )
  111.             {
  112.                 i_small = i;
  113.             }
  114.         }
  115.         /* Keep the correct current index */
  116.         if( i_small == p_playlist->i_index )
  117.             p_playlist->i_index = i_position;
  118.         else if( i_position == p_playlist->i_index )
  119.             p_playlist->i_index = i_small;
  120.         p_temp = p_playlist->pp_items[i_position];
  121.         p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
  122.         p_playlist->pp_items[i_small] = p_temp;
  123.     }
  124.     vlc_mutex_unlock( &p_playlist->object_lock );
  125.     /* Notify the interfaces  */
  126.     var_Set( p_playlist, "intf-change", val );
  127.     return VLC_SUCCESS;
  128. }