history.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:6k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * history.c: vlc_history_t (web-browser-like back/forward history) handling
  3.  *****************************************************************************
  4.  * Copyright (C) 2004 Commonwealth Scientific and Industrial Research
  5.  *                    Organisation (CSIRO) Australia
  6.  * Copyright (C) 2004 the VideoLAN team
  7.  *
  8.  * $Id: 702ae300d3ed4bab1d467d80e6cd8f4379db8b85 $
  9.  *
  10.  * Authors: Andre Pang <Andre.Pang@csiro.au>
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_input.h>
  31. #include "history.h"
  32. #include "xarray.h"
  33. #ifdef HAVE_STDLIB_H
  34. #   include <stdlib.h>                                          /* realloc() */
  35. #endif
  36. #undef HISTORY_DEBUG
  37. /*****************************************************************************
  38.  * Local prototypes
  39.  *****************************************************************************/
  40. #ifdef HISTORY_DEBUG
  41. static void history_Dump( history_t *p_history );
  42. #endif
  43. /*****************************************************************************
  44.  * Local structure lock
  45.  *****************************************************************************/
  46. /*****************************************************************************
  47.  * Actual history code
  48.  *****************************************************************************/
  49. history_t *history_New( void )
  50. {
  51.    history_t *p_new_history;
  52.  
  53.    p_new_history = calloc( 1, sizeof( struct history_t ) );
  54.    if( p_new_history == NULL ) return NULL;
  55.    p_new_history->p_xarray = xarray_New( 0 );
  56.    if( p_new_history->p_xarray == NULL )
  57.    {
  58.        free( p_new_history );
  59.        return NULL;
  60.    }
  61.    return p_new_history;
  62. }
  63. bool history_GoBackSavingCurrentItem ( history_t *p_history,
  64.                                              history_item_t *p_item )
  65. {
  66.     history_PruneAndInsert( p_history, p_item );
  67.     /* PruneAndInsert will increment the index, so we need to go
  68.      * back one position to reset the index to the place we were at
  69.      * before saving the current state, and then go back one more to
  70.      * actually go back */
  71.     p_history->i_index -= 2;
  72. #ifdef HISTORY_DEBUG
  73.     history_Dump( p_history );
  74. #endif
  75.     return true;
  76. }
  77. #ifdef HISTORY_DEBUG
  78. static void history_Dump( history_t *p_history )
  79. {
  80.     unsigned int i_count;
  81.     int i;
  82.     if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
  83.         return;
  84.     for (i = 0; i < (int) i_count; i++)
  85.     {
  86.         history_item_t *p_item;
  87.         void *pv_item;
  88.         xarray_ObjectAtIndex( p_history->p_xarray, i, &pv_item );
  89.         p_item = (history_item_t *) pv_item;
  90.         if( p_item == NULL )
  91.             fprintf( stderr, "HISTORY: [%d] NULLn", i );
  92.         else
  93.         {
  94.             fprintf( stderr, "HISTORY: [%d] %p (%p->%s)n", i, p_item,
  95.                      p_item->psz_uri, p_item->psz_uri );
  96.         }
  97.     }
  98. }
  99. #endif
  100. bool history_GoForwardSavingCurrentItem ( history_t *p_history,
  101.                                                 history_item_t *p_item )
  102. {
  103. #ifdef HISTORY_DEBUG
  104.     history_Dump( p_history );
  105. #endif
  106.     if( xarray_ReplaceObject( p_history->p_xarray, p_history->i_index, p_item )
  107.         == XARRAY_SUCCESS )
  108.     {
  109.         p_history->i_index++;
  110.         return true;
  111.     }
  112.     else
  113.     {
  114.         return false;
  115.     }
  116. }
  117. bool history_CanGoBack( history_t *p_history )
  118. {
  119.     if( p_history->i_index > 0 )
  120.         return true;
  121.     else
  122.         return false;
  123. }
  124. bool history_CanGoForward( history_t *p_history )
  125. {
  126.     unsigned int i_count;
  127.     if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
  128.         return false;
  129.     if( p_history->i_index < i_count )
  130.         return true;
  131.     else
  132.         return false;
  133. }
  134. history_item_t *history_Item( history_t *p_history )
  135. {
  136.     history_item_t *p_item;
  137.     void *pv_item;
  138.     if( xarray_ObjectAtIndex( p_history->p_xarray, p_history->i_index,
  139.                               &pv_item )
  140.         == XARRAY_SUCCESS )
  141.     {
  142.         p_item = (history_item_t *) pv_item;
  143.         return p_item;
  144.     }
  145.     else
  146.     {
  147.         return NULL;
  148.     }
  149. }
  150. void history_Prune( history_t *p_history )
  151. {
  152.     xarray_RemoveObjectsAfter( p_history->p_xarray, p_history->i_index );
  153.     xarray_RemoveObject( p_history->p_xarray, p_history->i_index );
  154. }
  155. void history_PruneAndInsert( history_t *p_history, history_item_t *p_item )
  156. {
  157.     unsigned int i_count;
  158.     xarray_Count( p_history->p_xarray, &i_count );
  159.     if( i_count == 0 )
  160.     {
  161.         xarray_InsertObject( p_history->p_xarray, p_item, 0 );
  162.         p_history->i_index = 1;
  163.     }
  164.     else
  165.     {
  166.         history_Prune( p_history );
  167.         xarray_InsertObject( p_history->p_xarray, p_item, p_history->i_index );
  168.         p_history->i_index++;
  169.     }
  170. }
  171. unsigned int history_Count( history_t *p_history )
  172. {
  173.     unsigned int i_count;
  174.     xarray_Count( p_history->p_xarray, &i_count );
  175.     return i_count;
  176. }
  177. unsigned int history_Index( history_t *p_history )
  178. {
  179.     return p_history->i_index;
  180. }
  181. history_item_t * historyItem_New( char *psz_name, char *psz_uri )
  182. {
  183.     history_item_t *p_history_item = NULL;
  184.     p_history_item = (history_item_t *) malloc( sizeof(history_item_t) );
  185.     if( !p_history_item ) return NULL;
  186.     p_history_item->psz_uri = strdup( psz_uri );
  187.     p_history_item->psz_name = strdup( psz_name );
  188.     return p_history_item;
  189. }