]> git.sesse.net Git - vlc/blob - src/misc/epg.c
Moved out epg functions from vlc_epg.h and es_out.c to src/misc/epg.c.
[vlc] / src / misc / epg.c
1 /*****************************************************************************
2  * epg.c: Electronic Program Guide
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <ctype.h>
33 #include <errno.h>
34
35 #include <vlc_common.h>
36 #include <vlc_epg.h>
37
38 void vlc_epg_Init( vlc_epg_t *p_epg, const char *psz_name )
39 {
40     p_epg->psz_name = psz_name ? strdup( psz_name ) : NULL;
41     p_epg->p_current = NULL;
42     TAB_INIT( p_epg->i_event, p_epg->pp_event );
43 }
44
45 void vlc_epg_Clean( vlc_epg_t *p_epg )
46 {
47     int i;
48     for( i = 0; i < p_epg->i_event; i++ )
49     {
50         vlc_epg_event_t *p_evt = p_epg->pp_event[i];
51         free( p_evt->psz_name );
52         free( p_evt->psz_short_description );
53         free( p_evt->psz_description );
54         free( p_evt );
55     }
56     TAB_CLEAN( p_epg->i_event, p_epg->pp_event );
57     free( p_epg->psz_name );
58 }
59
60 void vlc_epg_AddEvent( vlc_epg_t *p_epg, int64_t i_start, int i_duration,
61                        const char *psz_name, const char *psz_short_description, const char *psz_description )
62 {
63     vlc_epg_event_t *p_evt = (vlc_epg_event_t*)malloc( sizeof(vlc_epg_event_t) );
64     if( !p_evt )
65         return;
66     p_evt->i_start = i_start;
67     p_evt->i_duration = i_duration;
68     p_evt->psz_name = psz_name ? strdup( psz_name ) : NULL;
69     p_evt->psz_short_description = psz_short_description ? strdup( psz_short_description ) : NULL;
70     p_evt->psz_description = psz_description ? strdup( psz_description ) : NULL;
71     TAB_APPEND_CPP( vlc_epg_event_t, p_epg->i_event, p_epg->pp_event, p_evt );
72 }
73
74 vlc_epg_t *vlc_epg_New( const char *psz_name )
75 {
76     vlc_epg_t *p_epg = (vlc_epg_t*)malloc( sizeof(vlc_epg_t) );
77     if( p_epg )
78         vlc_epg_Init( p_epg, psz_name );
79     return p_epg;
80 }
81
82 void vlc_epg_Delete( vlc_epg_t *p_epg )
83 {
84     vlc_epg_Clean( p_epg );
85     free( p_epg );
86 }
87
88 void vlc_epg_SetCurrent( vlc_epg_t *p_epg, int64_t i_start )
89 {
90     int i;
91     p_epg->p_current = NULL;
92     if( i_start < 0 )
93         return;
94
95     for( i = 0; i < p_epg->i_event; i++ )
96     {
97         if( p_epg->pp_event[i]->i_start == i_start )
98         {
99             p_epg->p_current = p_epg->pp_event[i];
100             break;
101         }
102     }
103 }
104
105 void vlc_epg_Merge( vlc_epg_t *p_dst, const vlc_epg_t *p_src )
106 {
107     int i;
108
109     /* Add new event */
110     for( i = 0; i < p_src->i_event; i++ )
111     {
112         vlc_epg_event_t *p_evt = p_src->pp_event[i];
113         bool b_add = true;
114         int j;
115
116         for( j = 0; j < p_dst->i_event; j++ )
117         {
118             if( p_dst->pp_event[j]->i_start == p_evt->i_start && p_dst->pp_event[j]->i_duration == p_evt->i_duration )
119             {
120                 b_add = false;
121                 break;
122             }
123             if( p_dst->pp_event[j]->i_start > p_evt->i_start )
124                 break;
125         }
126         if( b_add )
127         {
128             vlc_epg_event_t *p_copy = calloc( 1, sizeof(vlc_epg_event_t) );
129             if( !p_copy )
130                 break;
131             p_copy->i_start = p_evt->i_start;
132             p_copy->i_duration = p_evt->i_duration;
133             p_copy->psz_name = p_evt->psz_name ? strdup( p_evt->psz_name ) : NULL;
134             p_copy->psz_short_description = p_evt->psz_short_description ? strdup( p_evt->psz_short_description ) : NULL;
135             p_copy->psz_description = p_evt->psz_description ? strdup( p_evt->psz_description ) : NULL;
136             TAB_INSERT( p_dst->i_event, p_dst->pp_event, p_copy, j );
137         }
138     }
139     /* Update current */
140     if( p_src->p_current )
141         vlc_epg_SetCurrent( p_dst, p_src->p_current->i_start );
142
143     /* Keep only 1 old event  */
144     if( p_dst->p_current )
145     {
146         while( p_dst->i_event > 1 && p_dst->pp_event[0] != p_dst->p_current && p_dst->pp_event[1] != p_dst->p_current )
147             TAB_REMOVE( p_dst->i_event, p_dst->pp_event, p_dst->pp_event[0] );
148     }
149 }
150