* configuration.c management of the modules configuration
*****************************************************************************
* Copyright (C) 2001 VideoLAN
- * $Id: configuration.c,v 1.36 2002/08/11 08:30:01 gbazin Exp $
+ * $Id: configuration.c,v 1.37 2002/08/15 12:11:15 sam Exp $
*
* Authors: Gildas Bazin <gbazin@netcourrier.com>
*
* config_FindConfig: find the config structure associated with an option.
*****************************************************************************
* FIXME: This function really needs to be optimized.
+ * FIXME: And now even more.
*****************************************************************************/
-module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name)
+module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name )
{
- module_t *p_module;
+ vlc_list_t *p_list;
+ module_t **pp_parser;
module_config_t *p_item;
if( !psz_name ) return NULL;
- for( p_module = p_this->p_vlc->p_module_bank->first ;
- p_module != NULL ;
- p_module = p_module->next )
+ p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+
+ for( pp_parser = (module_t **)p_list->pp_objects ;
+ *pp_parser ;
+ pp_parser++ )
{
- for( p_item = p_module->p_config;
+ if( !(*pp_parser)->i_config_items )
+ continue;
+
+ for( p_item = (*pp_parser)->p_config;
p_item->i_type != CONFIG_HINT_END;
p_item++ )
{
/* ignore hints */
continue;
if( !strcmp( psz_name, p_item->psz_name ) )
+ {
+ vlc_list_release( p_list );
return p_item;
+ }
}
}
+ vlc_list_release( p_list );
+
return NULL;
}
module_config_t *p_item = p_module->p_config;
int i;
+ if( p_item == NULL )
+ {
+ return;
+ }
+
for( ; p_item->i_type != CONFIG_HINT_END ; p_item++ )
{
if( p_item->psz_type )
*****************************************************************************/
int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
{
- module_t *p_module;
+ vlc_list_t *p_list;
+ module_t **pp_parser;
module_config_t *p_item;
FILE *file;
char line[1024];
char *p_index, *psz_option_name, *psz_option_value;
char *psz_filename, *psz_homedir;
- /* Acquire config file lock */
- vlc_mutex_lock( &p_this->p_vlc->config_lock );
-
psz_homedir = p_this->p_vlc->psz_homedir;
if( !psz_homedir )
{
msg_Err( p_this, "psz_homedir is null" );
- vlc_mutex_unlock( &p_this->p_vlc->config_lock );
return -1;
}
psz_filename = (char *)malloc( strlen("/" CONFIG_DIR "/" CONFIG_FILE) +
if( !psz_filename )
{
msg_Err( p_this, "out of memory" );
- vlc_mutex_unlock( &p_this->p_vlc->config_lock );
return -1;
}
sprintf( psz_filename, "%s/" CONFIG_DIR "/" CONFIG_FILE, psz_homedir );
msg_Dbg( p_this, "opening config file %s", psz_filename );
+ /* Acquire config file lock */
+ vlc_mutex_lock( &p_this->p_vlc->config_lock );
+
file = fopen( psz_filename, "rt" );
if( !file )
{
}
/* Look for the selected module, if NULL then save everything */
- for( p_module = p_this->p_vlc->p_module_bank->first ; p_module != NULL ;
- p_module = p_module->next )
+ p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+
+ for( pp_parser = (module_t **)p_list->pp_objects ;
+ *pp_parser ;
+ pp_parser++ )
{
if( psz_module_name
- && strcmp( psz_module_name, p_module->psz_object_name ) )
+ && strcmp( psz_module_name, (*pp_parser)->psz_object_name ) )
{
continue;
}
rewind( file );
while( fgets( line, 1024, file ) )
{
- if( (line[0] == '[') && (p_index = strchr(line,']')) &&
- (p_index - &line[1] == strlen(p_module->psz_object_name) ) &&
- !memcmp( &line[1], p_module->psz_object_name,
- strlen(p_module->psz_object_name) ) )
+ if( (line[0] == '[')
+ && (p_index = strchr(line,']'))
+ && (p_index - &line[1] == strlen((*pp_parser)->psz_object_name))
+ && !memcmp( &line[1], (*pp_parser)->psz_object_name,
+ strlen((*pp_parser)->psz_object_name) ) )
{
#if 0
msg_Dbg( p_this, "loading config for module \"%s\"",
- p_module->psz_object_name );
+ (*pp_parser)->psz_object_name );
#endif
break;
*p_index = (char)0;
psz_option_value = p_index + 1;
+ if( !(*pp_parser)->i_config_items )
+ {
+ continue;
+ }
+
/* try to match this option with one of the module's options */
- for( p_item = p_module->p_config;
+ for( p_item = (*pp_parser)->p_config;
p_item->i_type != CONFIG_HINT_END;
p_item++ )
{
}
+ vlc_list_release( p_list );
+
fclose( file );
free( psz_filename );
*****************************************************************************/
int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
{
- module_t *p_module;
+ module_t **pp_parser;
+ vlc_list_t *p_list;
module_config_t *p_item;
FILE *file;
char p_line[1024], *p_index2;
}
p_bigbuffer[0] = 0;
+ /* List all available modules */
+ p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+
/* backup file into memory, we only need to backup the sections we won't
* save later on */
b_backup = 0;
{
if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']')))
{
+
/* we found a section, check if we need to do a backup */
- for( p_module = p_this->p_vlc->p_module_bank->first;
- p_module != NULL;
- p_module = p_module->next )
+ for( pp_parser = (module_t **)p_list->pp_objects ;
+ *pp_parser ;
+ pp_parser++ )
{
if( ((p_index2 - &p_line[1])
- == strlen(p_module->psz_object_name) ) &&
- !memcmp( &p_line[1], p_module->psz_object_name,
- strlen(p_module->psz_object_name) ) )
+ == strlen((*pp_parser)->psz_object_name) ) &&
+ !memcmp( &p_line[1], (*pp_parser)->psz_object_name,
+ strlen((*pp_parser)->psz_object_name) ) )
{
if( !psz_module_name )
break;
else if( !strcmp( psz_module_name,
- p_module->psz_object_name ) )
+ (*pp_parser)->psz_object_name ) )
break;
}
}
- if( !p_module )
+ if( !(*pp_parser) )
{
/* we don't have this section in our list so we need to back
* it up */
msg_Warn( p_this, "could not open config file %s for writing",
psz_filename );
free( psz_filename );
+ vlc_list_release( p_list );
vlc_mutex_unlock( &p_this->p_vlc->config_lock );
return -1;
}
fprintf( file, "###\n### " COPYRIGHT_MESSAGE "\n###\n\n" );
/* Look for the selected module, if NULL then save everything */
- for( p_module = p_this->p_vlc->p_module_bank->first ; p_module != NULL ;
- p_module = p_module->next )
+ for( pp_parser = (module_t **)p_list->pp_objects ;
+ *pp_parser ;
+ pp_parser++ )
{
if( psz_module_name && strcmp( psz_module_name,
- p_module->psz_object_name ) )
+ (*pp_parser)->psz_object_name ) )
continue;
- if( !p_module->i_config_items )
+ if( !(*pp_parser)->i_config_items )
continue;
msg_Dbg( p_this, "saving config for module \"%s\"",
- p_module->psz_object_name );
+ (*pp_parser)->psz_object_name );
- fprintf( file, "[%s]", p_module->psz_object_name );
- if( p_module->psz_longname )
- fprintf( file, " # %s\n\n", p_module->psz_longname );
+ fprintf( file, "[%s]", (*pp_parser)->psz_object_name );
+ if( (*pp_parser)->psz_longname )
+ fprintf( file, " # %s\n\n", (*pp_parser)->psz_longname );
else
fprintf( file, "\n\n" );
- for( p_item = p_module->p_config;
+ for( p_item = (*pp_parser)->p_config;
p_item->i_type != CONFIG_HINT_END;
p_item++ )
{
fprintf( file, "\n" );
}
+ vlc_list_release( p_list );
/*
* Restore old settings from the config in file
vlc_bool_t b_ignore_errors )
{
int i_cmd, i_index, i_opts, i_shortopts, flag;
- module_t *p_module;
+ module_t **pp_parser;
+ vlc_list_t *p_list;
module_config_t *p_item;
struct option *p_longopts;
}
#endif
+ /* List all modules */
+ p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
+
/*
* Generate the longopts and shortopts structures used by getopt_long
*/
i_opts = 0;
- for( p_module = p_this->p_vlc->p_module_bank->first;
- p_module != NULL ;
- p_module = p_module->next )
+ for( pp_parser = (module_t **)p_list->pp_objects ;
+ *pp_parser ;
+ pp_parser++ )
{
/* count the number of exported configuration options (to allocate
* longopts). We also need to allocate space for too options when
* dealing with boolean to allow for --foo and --no-foo */
- i_opts += (p_module->i_config_items + 2 * p_module->i_bool_items);
+ i_opts += (*pp_parser)->i_config_items
+ + 2 * (*pp_parser)->i_bool_items;
}
p_longopts = malloc( sizeof(struct option) * (i_opts + 1) );
if( p_longopts == NULL )
{
msg_Err( p_this, "out of memory" );
+ vlc_list_release( p_list );
return -1;
}
{
msg_Err( p_this, "out of memory" );
free( p_longopts );
+ vlc_list_release( p_list );
return -1;
}
msg_Err( p_this, "out of memory" );
free( psz_shortopts );
free( p_longopts );
+ vlc_list_release( p_list );
return -1;
}
memcpy( ppsz_argv, p_this->p_vlc->ppsz_argv,
/* Fill the p_longopts and psz_shortopts structures */
i_index = 0;
- for( p_module = p_this->p_vlc->p_module_bank->first ;
- p_module != NULL ;
- p_module = p_module->next )
+ for( pp_parser = (module_t **)p_list->pp_objects ;
+ *pp_parser ;
+ pp_parser++ )
{
- for( p_item = p_module->p_config;
+ if( !(*pp_parser)->i_config_items )
+ continue;
+
+ for( p_item = (*pp_parser)->p_config;
p_item->i_type != CONFIG_HINT_END;
p_item++ )
{
}
}
+ /* We don't need the module list anymore */
+ vlc_list_release( p_list );
+
/* Close the longopts and shortopts structures */
memset( &p_longopts[i_index], 0, sizeof(struct option) );
psz_shortopts[i_shortopts] = '\0';