]> git.sesse.net Git - vlc/blob - src/input/input_info.c
7715d46d12d83208a5619bb2e2d1926b151b30fc
[vlc] / src / input / input_info.c
1 /*****************************************************************************
2  * input_info.c: Convenient functions to handle the input info structures
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  * $Id: input_info.c,v 1.14 2004/01/25 17:16:06 zorglub Exp $
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31
32 #include "stream_control.h"
33 #include "input_ext-intf.h"
34 #include "vlc_interface.h"
35
36 /**
37  * \brief Find info category by name.
38  *
39  * Returns a pointer to the info category with the given name, and
40  * creates it if necessary
41  *
42  * \param p_input pointer to the input thread in which the info is to be found
43  * \param psz_name the name of the category to be found
44  * \returns a pointer to the category with the given name
45  */
46 input_info_category_t * input_InfoCategory( input_thread_t * p_input,
47                                             char * psz_name)
48 {
49     input_info_category_t * p_category, * p_prev;
50     p_prev = NULL;
51     for ( p_category = p_input->stream.p_info;
52           (p_category != NULL) && strcmp( p_category->psz_name, psz_name ); 
53           p_category = p_category->p_next)
54     {
55         p_prev = p_category;
56     }
57     if ( p_category )
58     {
59         return p_category;
60     }
61     else
62     {
63         p_category = malloc( sizeof( input_info_category_t ) );
64         if ( !p_category )
65         {
66             msg_Err( p_input, "out of memory" );
67             return NULL;
68         }
69         p_category->psz_name = strdup( psz_name );
70         p_category->p_next = NULL;
71         p_category->p_info = NULL;
72         if( p_prev ) p_prev->p_next = p_category;
73         return p_category;
74     }
75 }
76
77 /**
78  * \brief Add a info item to a category
79  *
80  * \param p_category Pointer to the category to put this info in
81  * \param psz_name Name of the info item to add
82  * \param psz_format printf style format string for the value.
83  * \return a negative number on error. 0 on success.
84  */
85 int input_AddInfo( input_info_category_t * p_category, char * psz_name,
86                    char * psz_format, ...)
87 {
88     input_info_t * p_info, * p_prev;
89     char * psz_str = NULL;
90     va_list args;
91
92     p_prev = NULL;
93     if ( !p_category )
94     {
95         return -1;
96     }
97
98     va_start( args, psz_format );
99
100     /*
101      * Convert message to string
102      */
103 #if defined(HAVE_VASPRINTF) && !defined(SYS_DARWIN) && !defined(SYS_BEOS)
104     vasprintf( &psz_str, psz_format, args );
105 #else
106     psz_str = (char*) malloc( strlen(psz_format) + INTF_MAX_MSG_SIZE );
107     if( psz_str == NULL )
108     {
109         return -1;
110     }
111
112     vsprintf( psz_str, psz_format, args );
113 #endif
114
115     va_end( args );
116     p_info = p_category->p_info;
117     while ( p_info )
118     {
119         p_prev = p_info;
120         p_info = p_info->p_next;
121     }
122     p_info = malloc( sizeof( input_info_t ) );
123     if( !p_info )
124     {
125         return -1;
126     }
127     p_info->psz_name = strdup( psz_name );
128     p_info->psz_value = psz_str;
129     p_info->p_next = NULL;
130     if ( p_prev )
131     {
132         p_prev->p_next = p_info;
133     }
134     else
135     {
136         p_category->p_info = p_info;
137     }
138     return 0;
139 }
140
141 /**
142  * \brief Destroy info structures
143  * \internal
144  *
145  * \param p_input The input thread to be cleaned for info
146  * \returns for the moment VLC_SUCCESS
147  */
148 int input_DelInfo( input_thread_t * p_input )
149 {
150     input_info_category_t * p_category, * p_prev_category;
151     input_info_t * p_info, * p_prev_info;
152
153     p_category = p_input->stream.p_info;
154     while ( p_category )
155     {
156         p_info = p_category->p_info;
157         while ( p_info )
158         {
159             if ( p_info->psz_name )
160             {
161                 free( p_info->psz_name );
162             }
163             if ( p_info->psz_value )
164             {
165                 free( p_info->psz_value );
166             }
167             p_prev_info = p_info;
168             p_info = p_info->p_next;
169             free( p_prev_info );
170         }
171         if ( p_category->psz_name )
172         {
173             free( p_category->psz_name );
174         }
175         p_prev_category = p_category;
176         p_category = p_category->p_next;
177         free( p_prev_category );
178     }
179     return VLC_SUCCESS;
180 }