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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * playlist.c : Playlist groups management functions
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 VideoLAN
  5.  * $Id: group.c 7209 2004-03-31 20:52:31Z gbazin $
  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_playlist.h"
  29. /**
  30.  * Create a group
  31.  *
  32.  * Create a new group
  33.  * param p_playlist pointer to a playlist
  34.  * param psz_name the name of the group to be created
  35.  * return a pointer to the created group, or NULL on error
  36.  */
  37. playlist_group_t *playlist_CreateGroup( playlist_t *p_playlist, char *psz_name)
  38. {
  39.     playlist_group_t *p_group;
  40.     int i;
  41.     for( i = 0 ; i < p_playlist->i_groups; i++ )
  42.     {
  43.         if( !strcasecmp( p_playlist->pp_groups[i]->psz_name , psz_name ) )
  44.         {
  45.             msg_Info( p_playlist, "this group already exists");
  46.             return p_playlist->pp_groups[i];
  47.         }
  48.     }
  49.     /* Allocate the group structure */
  50.     if( ( p_group = malloc( sizeof(playlist_group_t) ) ) == NULL )
  51.     {
  52.         msg_Err( p_playlist, "out of memory" );
  53.         return NULL;
  54.     }
  55.     p_group->psz_name = strdup( psz_name );
  56.     p_group->i_id = ++p_playlist->i_last_group;
  57.     msg_Dbg(p_playlist,"creating group %s with id %i at position %i",
  58.                      p_group->psz_name,
  59.                      p_group->i_id,
  60.                      p_playlist->i_groups);
  61.     INSERT_ELEM ( p_playlist->pp_groups,
  62.                   p_playlist->i_groups,
  63.                   p_playlist->i_groups,
  64.                   p_group );
  65.     return p_group;
  66. }
  67. /**
  68.  * Destroy a group
  69.  *
  70.  * param p_playlist the playlist to remove the group from
  71.  * param i_id the identifier of the group to remove
  72.  * return VLC_SUCCESS
  73.  */
  74. int playlist_DeleteGroup( playlist_t *p_playlist, int i_id )
  75. {
  76.     int i;
  77.     for( i=0 ; i<= p_playlist->i_groups; i++ )
  78.     {
  79.         playlist_group_t *p_group = p_playlist->pp_groups[i];
  80.         if( p_group->i_id == i_id )
  81.         {
  82.             if( p_group->psz_name )
  83.             {
  84.                 free( p_group->psz_name );
  85.             }
  86.             REMOVE_ELEM( p_playlist->pp_groups,
  87.                          p_playlist->i_groups,
  88.                          i );
  89.             free( p_group );
  90.             return VLC_SUCCESS;
  91.         }
  92.     }
  93.     return VLC_SUCCESS;
  94. }
  95. /**
  96.  * Find the name of the group given its ID
  97.  *
  98.  * param p_playlist the playlist where to find the group
  99.  * param i_id the ID to search for
  100.  * return the name of the group
  101.  */
  102. char *playlist_FindGroup( playlist_t *p_playlist, int i_id )
  103. {
  104.     int i;
  105.     for( i=0 ; i< p_playlist->i_groups; i++ )
  106.     {
  107.         if( p_playlist->pp_groups[i]->i_id == i_id )
  108.         {
  109.             if( p_playlist->pp_groups[i]->psz_name)
  110.                 return strdup( p_playlist->pp_groups[i]->psz_name );
  111.         }
  112.     }
  113.     return NULL;
  114. }
  115. /**
  116.  * Find the id of a group given its name
  117.  *
  118.  * param p_playlist the playlist where to find the group
  119.  * param psz_name the name to search for
  120.  * return the id of the group
  121.  */
  122. int playlist_GroupToId( playlist_t *p_playlist, char *psz_name )
  123. {
  124.     int i;
  125.     for( i = 0 ; i< p_playlist->i_groups; i++ )
  126.     {
  127.         if( p_playlist->pp_groups[i]->psz_name)
  128.         {
  129.             if( ! strcasecmp( p_playlist->pp_groups[i]->psz_name, psz_name ) )
  130.             {
  131.                 return p_playlist->pp_groups[i]->i_id;
  132.             }
  133.         }
  134.     }
  135.     return VLC_SUCCESS;
  136. }