ab81efdd7d6e7dba09dd65f311eee96eeaa1ad82
[vlc.git] / 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.4 2002/07/25 21:53:53 sigmunau 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 input_info_category_t * input_InfoCategory( input_thread_t * p_this,
40                                             char * psz_name)
41 {
42     input_info_category_t * p_category, * p_prev;
43     p_prev = NULL;
44     for ( p_category = p_this->stream.p_info;
45           (p_category != NULL) && strcmp( p_category->psz_name, psz_name ); 
46           p_category = p_category->p_next)
47     {
48         p_prev = p_category;
49     }
50     if ( p_category )
51     {
52         return p_category;
53     }
54     else
55     {
56         p_category = malloc( sizeof( input_info_category_t ) );
57         if ( !p_category )
58         {
59             msg_Err( p_this, "No mem" );
60             return 0;
61         }
62         p_category->psz_name = strdup( psz_name );
63         p_category->p_next = NULL;
64         p_category->p_info = NULL;
65         p_prev->p_next = p_category;
66         return p_category;
67     }
68 }
69
70 int input_AddInfo( input_info_category_t * p_category, char * psz_name,
71                    char * psz_format, ...)
72 {
73     input_info_t * p_info, * p_prev;
74     char * psz_str = NULL;
75     va_list args;
76
77     p_prev = NULL;
78     if ( !p_category )
79     {
80         return -1;
81     }
82     
83     va_start( args, psz_format );
84     
85     /*
86      * Convert message to string
87      */
88 #ifdef HAVE_VASPRINTF
89     vasprintf( &psz_str, psz_format, args );
90 #else
91     psz_str = (char*) malloc( strlen(psz_format) + INTF_MAX_MSG_SIZE );
92     if( psz_str == NULL )
93     {
94         return -1;
95     }
96
97     vsprintf( psz_str, psz_format, args );
98 #endif
99
100     va_end( args );
101     p_info = p_category->p_info;
102     while ( p_info )
103     {
104         p_prev = p_info;
105         p_info = p_info->p_next;
106     }
107     p_info = malloc( sizeof( input_info_t ) );
108     if( !p_info )
109     {
110         return -1;
111     }
112     p_info->psz_name = strdup( psz_name );
113     p_info->psz_value = psz_str;
114     p_info->p_next = NULL;
115     if ( p_prev )
116     {
117         p_prev->p_next = p_info;
118     }
119     else
120     {
121         p_category->p_info = p_info;
122     }
123     return 0;
124 }
125
126 int input_DelInfo( input_thread_t * p_input )
127 {
128     input_info_category_t * p_category, * p_prev_category;
129     input_info_t * p_info, * p_prev_info;
130     
131     p_category = p_input->stream.p_info;
132     while ( p_category )
133     {
134         p_info = p_category->p_info;
135         while ( p_info )
136         {
137             if ( p_info->psz_name )
138             {
139                 free( p_info->psz_name );
140             }
141             if ( p_info->psz_value )
142             {
143                 free( p_info->psz_value );
144             }
145             p_prev_info = p_info;
146             p_info = p_info->p_next;
147             free( p_prev_info );
148         }
149         if ( p_category->psz_name )
150         {
151             free( p_category->psz_name );
152         }
153         p_prev_category = p_category;
154         p_category = p_category->p_next;
155         free( p_prev_category );
156     }
157     return 0;
158 }