]> git.sesse.net Git - vlc/blob - src/misc/configuration.c
3b460c1984fe90fbe6284e481ba0c26be5806388
[vlc] / src / misc / configuration.c
1 /*****************************************************************************
2  * configuration.c management of the modules configuration
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: configuration.c,v 1.2 2002/02/26 22:08:57 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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 #include <stdlib.h>                                      /* free(), strtol() */
25 #include <stdio.h>                                              /* sprintf() */
26 #include <string.h>                                              /* strdup() */
27
28 #include <videolan/vlc.h>
29
30 /* TODO: implement locking for config_PutPszVariable and config_GetPszVariable
31  * because they are not thread safe */
32
33 /*****************************************************************************
34  * config_GetIntVariable: get the value of an int variable
35  *****************************************************************************
36  * This function is used to get the value of variables which are internally
37  * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
38  * MODULE_CONFIG_ITEM_BOOL).
39  *****************************************************************************/
40 int config_GetIntVariable( const char *psz_name )
41 {
42     module_config_t *p_config;
43
44     p_config = config_FindConfig( psz_name );
45
46     /* sanity checks */
47     if( !p_config )
48     {
49         intf_ErrMsg( "config_GetIntVariable: option %s doesn't exist",
50                      psz_name );
51         return -1;
52     }
53     if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
54         (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
55     {
56         intf_ErrMsg( "config_GetIntVariable: option %s doesn't refer to an int"
57                      , psz_name );
58         return -1;
59     }
60
61     return p_config->i_value;
62 }
63
64 /*****************************************************************************
65  * config_GetPszVariable: get the string value of a string variable
66  *****************************************************************************
67  * This function is used to get the value of variables which are internally
68  * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
69  * and MODULE_CONFIG_ITEM_PLUGIN).
70  *
71  * Important note: remember to free() the returned char* because it a duplicate
72  *   of the actual value. It isn't safe to return a pointer to the actual value
73  *   as it can be modified at any time.
74  *****************************************************************************/
75 char * config_GetPszVariable( const char *psz_name )
76 {
77     module_config_t *p_config;
78     char *psz_value = NULL;
79
80     p_config = config_FindConfig( psz_name );
81
82     /* sanity checks */
83     if( !p_config )
84     {
85         intf_ErrMsg( "config_GetPszVariable: option %s doesn't exist",
86                      psz_name );
87         return NULL;
88     }
89     if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
90         (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
91         (p_config->i_type!=MODULE_CONFIG_ITEM_PLUGIN) )
92     {
93         intf_ErrMsg( "config_GetPszVariable: option %s doesn't refer to a "
94                      "string", psz_name );
95         return NULL;
96     }
97
98     /* return a copy of the string */
99     if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
100
101     return psz_value;
102 }
103
104 /*****************************************************************************
105  * config_PutPszVariable: set the string value of a string variable
106  *****************************************************************************
107  * This function is used to set the value of variables which are internally
108  * represented by a string (MODULE_CONFIG_ITEM_STRING, MODULE_CONFIG_ITEM_FILE,
109  * and MODULE_CONFIG_ITEM_PLUGIN).
110  *****************************************************************************/
111 void config_PutPszVariable( const char *psz_name, char *psz_value )
112 {
113     module_config_t *p_config;
114     char *psz_tmp;
115
116     p_config = config_FindConfig( psz_name );
117
118     /* sanity checks */
119     if( !p_config )
120     {
121         intf_ErrMsg( "config_PutPszVariable: option %s doesn't exist",
122                      psz_name );
123         return;
124     }
125     if( (p_config->i_type!=MODULE_CONFIG_ITEM_STRING) &&
126         (p_config->i_type!=MODULE_CONFIG_ITEM_FILE) &&
127         (p_config->i_type!=MODULE_CONFIG_ITEM_PLUGIN) )
128     {
129         intf_ErrMsg( "config_PutPszVariable: option %s doesn't refer to a "
130                      "string", psz_name );
131         return;
132     }
133
134     psz_tmp = p_config->psz_value;
135     if( psz_value ) p_config->psz_value = strdup( psz_value );
136     else p_config->psz_value = NULL;
137
138     /* free old string */
139     if( psz_tmp ) free( psz_tmp );
140
141 }
142
143 /*****************************************************************************
144  * config_PutIntVariable: set the integer value of an int variable
145  *****************************************************************************
146  * This function is used to set the value of variables which are internally
147  * represented by an integer (MODULE_CONFIG_ITEM_INTEGER and
148  * MODULE_CONFIG_ITEM_BOOL).
149  *****************************************************************************/
150 void config_PutIntVariable( const char *psz_name, int i_value )
151 {
152     module_config_t *p_config;
153
154     p_config = config_FindConfig( psz_name );
155
156     /* sanity checks */
157     if( !p_config )
158     {
159         intf_ErrMsg( "config_PutIntVariable: option %s doesn't exist",
160                      psz_name );
161         return;
162     }
163     if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
164         (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
165     {
166         intf_ErrMsg( "config_PutIntVariable: option %s doesn't refer to an int"
167                      , psz_name );
168         return;
169     }
170
171     p_config->i_value = i_value;
172 }
173
174 /*****************************************************************************
175  * config_FindConfig: find the config structure associated with an option.
176  *****************************************************************************
177  * FIXME: This function really needs to be optimized.
178  *****************************************************************************/
179 module_config_t *config_FindConfig( const char *psz_name )
180 {
181     module_t *p_module;
182     int i;
183
184     if( !psz_name ) return NULL;
185
186     for( p_module = p_module_bank->first ;
187          p_module != NULL ;
188          p_module = p_module->next )
189     {
190         for( i = 0; i < (p_module->i_config_options -1); i++ )
191         {
192             if( (p_module->p_config[i].i_type ==
193                      MODULE_CONFIG_ITEM_CATEGORY)||
194                 (p_module->p_config[i].i_type ==
195                      MODULE_CONFIG_ITEM_SUBCATEGORY)||
196                 (p_module->p_config[i].i_type ==
197                      MODULE_CONFIG_ITEM_SUBCATEGORY_END) )
198                 continue;
199             if( !strcmp( psz_name, p_module->p_config[i].psz_name ) )
200                 return &p_module->p_config[i];
201         }
202     }
203
204     return NULL;
205 }
206
207 /*****************************************************************************
208  * config_Duplicate: creates a duplicate of a module's configuration data.
209  *****************************************************************************
210  * Unfortunatly we cannot work directly with the module's config data as
211  * this module might be unloaded from memory at any time (remember HideModule).
212  * This is why we need to create an exact copy of the config data.
213  *****************************************************************************/
214 module_config_t *config_Duplicate( module_config_t *p_config_orig,
215                                    int i_config_options )
216 {
217     int i;
218     module_config_t *p_config;
219
220     /* allocate memory */
221     p_config = (module_config_t *)malloc( sizeof(module_config_t)
222                                           * i_config_options );
223     if( p_config == NULL )
224     {
225         intf_ErrMsg( "config_Duplicate error: can't allocate p_config" );
226         return( NULL );
227     }
228
229     for( i = 0; i < i_config_options ; i++ )
230     {
231         p_config[i].i_type = p_config_orig[i].i_type;
232         p_config[i].i_value = p_config_orig[i].i_value;
233         p_config[i].b_dirty = p_config_orig[i].b_dirty;
234         if( p_config_orig[i].psz_name )
235             p_config[i].psz_name = strdup( p_config_orig[i].psz_name );
236         else p_config[i].psz_name = NULL;
237         if( p_config_orig[i].psz_text )
238             p_config[i].psz_text = strdup( p_config_orig[i].psz_text );
239         else p_config[i].psz_text = NULL;
240         if( p_config_orig[i].psz_longtext )
241             p_config[i].psz_longtext = strdup( p_config_orig[i].psz_longtext );
242         else p_config[i].psz_longtext = NULL;
243         if( p_config_orig[i].psz_value )
244             p_config[i].psz_value = strdup( p_config_orig[i].psz_value );
245         else p_config[i].psz_value = NULL;
246
247         /* the callback pointer is only valid when the module is loaded so this
248          * value is set in ActivateModule() and reset in DeactivateModule() */
249         p_config_orig[i].p_callback = NULL;
250     }
251
252     return p_config;
253 }