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