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 $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
24 #include <stdlib.h> /* free(), strtol() */
25 #include <stdio.h> /* sprintf() */
26 #include <string.h> /* strdup() */
28 #include <videolan/vlc.h>
30 /* TODO: implement locking for config_PutPszVariable and config_GetPszVariable
31 * because they are not thread safe */
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 )
42 module_config_t *p_config;
44 p_config = config_FindConfig( psz_name );
49 intf_ErrMsg( "config_GetIntVariable: option %s doesn't exist",
53 if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
54 (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
56 intf_ErrMsg( "config_GetIntVariable: option %s doesn't refer to an int"
61 return p_config->i_value;
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).
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 )
77 module_config_t *p_config;
78 char *psz_value = NULL;
80 p_config = config_FindConfig( psz_name );
85 intf_ErrMsg( "config_GetPszVariable: option %s doesn't exist",
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) )
93 intf_ErrMsg( "config_GetPszVariable: option %s doesn't refer to a "
98 /* return a copy of the string */
99 if( p_config->psz_value ) psz_value = strdup( p_config->psz_value );
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 )
113 module_config_t *p_config;
116 p_config = config_FindConfig( psz_name );
121 intf_ErrMsg( "config_PutPszVariable: option %s doesn't exist",
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) )
129 intf_ErrMsg( "config_PutPszVariable: option %s doesn't refer to a "
130 "string", psz_name );
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;
138 /* free old string */
139 if( psz_tmp ) free( psz_tmp );
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 )
152 module_config_t *p_config;
154 p_config = config_FindConfig( psz_name );
159 intf_ErrMsg( "config_PutIntVariable: option %s doesn't exist",
163 if( (p_config->i_type!=MODULE_CONFIG_ITEM_INTEGER) &&
164 (p_config->i_type!=MODULE_CONFIG_ITEM_BOOL) )
166 intf_ErrMsg( "config_PutIntVariable: option %s doesn't refer to an int"
171 p_config->i_value = i_value;
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 )
184 if( !psz_name ) return NULL;
186 for( p_module = p_module_bank->first ;
188 p_module = p_module->next )
190 for( i = 0; i < (p_module->i_config_options -1); i++ )
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) )
199 if( !strcmp( psz_name, p_module->p_config[i].psz_name ) )
200 return &p_module->p_config[i];
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 )
218 module_config_t *p_config;
220 /* allocate memory */
221 p_config = (module_config_t *)malloc( sizeof(module_config_t)
222 * i_config_options );
223 if( p_config == NULL )
225 intf_ErrMsg( "config_Duplicate error: can't allocate p_config" );
229 for( i = 0; i < i_config_options ; i++ )
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;
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;