]> git.sesse.net Git - vlc/blob - modules/codec/cmml/history.c
702ae300d3ed4bab1d467d80e6cd8f4379db8b85
[vlc] / modules / codec / cmml / history.c
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$
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
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_input.h>
33
34 #include "history.h"
35
36 #include "xarray.h"
37
38 #ifdef HAVE_STDLIB_H
39 #   include <stdlib.h>                                          /* realloc() */
40 #endif
41
42 #undef HISTORY_DEBUG
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 #ifdef HISTORY_DEBUG
48 static void history_Dump( history_t *p_history );
49 #endif
50
51 /*****************************************************************************
52  * Local structure lock
53  *****************************************************************************/
54
55 /*****************************************************************************
56  * Actual history code
57  *****************************************************************************/
58
59 history_t *history_New( void )
60 {
61    history_t *p_new_history;
62  
63    p_new_history = calloc( 1, sizeof( struct history_t ) );
64    if( p_new_history == NULL ) return NULL;
65
66    p_new_history->p_xarray = xarray_New( 0 );
67    if( p_new_history->p_xarray == NULL )
68    {
69        free( p_new_history );
70        return NULL;
71    }
72
73    return p_new_history;
74 }
75
76 bool history_GoBackSavingCurrentItem ( history_t *p_history,
77                                              history_item_t *p_item )
78 {
79     history_PruneAndInsert( p_history, p_item );
80
81     /* PruneAndInsert will increment the index, so we need to go
82      * back one position to reset the index to the place we were at
83      * before saving the current state, and then go back one more to
84      * actually go back */
85     p_history->i_index -= 2;
86
87 #ifdef HISTORY_DEBUG
88     history_Dump( p_history );
89 #endif
90     return true;
91 }
92
93 #ifdef HISTORY_DEBUG
94 static void history_Dump( history_t *p_history )
95 {
96     unsigned int i_count;
97     int i;
98
99     if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
100         return;
101
102     for (i = 0; i < (int) i_count; i++)
103     {
104         history_item_t *p_item;
105         void *pv_item;
106
107         xarray_ObjectAtIndex( p_history->p_xarray, i, &pv_item );
108
109         p_item = (history_item_t *) pv_item;
110
111         if( p_item == NULL )
112             fprintf( stderr, "HISTORY: [%d] NULL\n", i );
113         else
114         {
115             fprintf( stderr, "HISTORY: [%d] %p (%p->%s)\n", i, p_item,
116                      p_item->psz_uri, p_item->psz_uri );
117         }
118     }
119 }
120 #endif
121
122 bool history_GoForwardSavingCurrentItem ( history_t *p_history,
123                                                 history_item_t *p_item )
124 {
125 #ifdef HISTORY_DEBUG
126     history_Dump( p_history );
127 #endif
128
129     if( xarray_ReplaceObject( p_history->p_xarray, p_history->i_index, p_item )
130         == XARRAY_SUCCESS )
131     {
132         p_history->i_index++;
133         return true;
134     }
135     else
136     {
137         return false;
138     }
139 }
140
141 bool history_CanGoBack( history_t *p_history )
142 {
143     if( p_history->i_index > 0 )
144         return true;
145     else
146         return false;
147 }
148
149 bool history_CanGoForward( history_t *p_history )
150 {
151     unsigned int i_count;
152
153     if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
154         return false;
155
156     if( p_history->i_index < i_count )
157         return true;
158     else
159         return false;
160 }
161
162 history_item_t *history_Item( history_t *p_history )
163 {
164     history_item_t *p_item;
165     void *pv_item;
166
167     if( xarray_ObjectAtIndex( p_history->p_xarray, p_history->i_index,
168                               &pv_item )
169         == XARRAY_SUCCESS )
170     {
171         p_item = (history_item_t *) pv_item;
172         return p_item;
173     }
174     else
175     {
176         return NULL;
177     }
178 }
179
180 void history_Prune( history_t *p_history )
181 {
182     xarray_RemoveObjectsAfter( p_history->p_xarray, p_history->i_index );
183     xarray_RemoveObject( p_history->p_xarray, p_history->i_index );
184 }
185
186 void history_PruneAndInsert( history_t *p_history, history_item_t *p_item )
187 {
188     unsigned int i_count;
189
190     xarray_Count( p_history->p_xarray, &i_count );
191
192     if( i_count == 0 )
193     {
194         xarray_InsertObject( p_history->p_xarray, p_item, 0 );
195         p_history->i_index = 1;
196     }
197     else
198     {
199         history_Prune( p_history );
200         xarray_InsertObject( p_history->p_xarray, p_item, p_history->i_index );
201         p_history->i_index++;
202     }
203 }
204
205 unsigned int history_Count( history_t *p_history )
206 {
207     unsigned int i_count;
208     xarray_Count( p_history->p_xarray, &i_count );
209     return i_count;
210 }
211
212 unsigned int history_Index( history_t *p_history )
213 {
214     return p_history->i_index;
215 }
216
217 history_item_t * historyItem_New( char *psz_name, char *psz_uri )
218 {
219     history_item_t *p_history_item = NULL;
220
221     p_history_item = (history_item_t *) malloc( sizeof(history_item_t) );
222     if( !p_history_item ) return NULL;
223
224     p_history_item->psz_uri = strdup( psz_uri );
225     p_history_item->psz_name = strdup( psz_name );
226
227     return p_history_item;
228 }
229