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