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