]> git.sesse.net Git - vlc/blobdiff - src/misc/configuration.c
* ./src/misc/modules.c: the module linked list is going bye bye. We now use
[vlc] / src / misc / configuration.c
index 03c325ed2813d990c5a16949fe407f1d09d95795..cbec1a9f34394996b204144eb0716e1edb1aac40 100644 (file)
@@ -2,7 +2,7 @@
  * 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>
  *
@@ -258,19 +258,26 @@ void __config_PutFloat( vlc_object_t *p_this,
  * 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++ )
         {
@@ -278,10 +285,15 @@ module_config_t *config_FindConfig( vlc_object_t *p_this, const char *psz_name)
                 /* 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;
 }
 
@@ -378,6 +390,11 @@ void config_Free( module_t *p_module )
     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 )
@@ -446,21 +463,18 @@ void config_UnsetCallbacks( module_config_t *p_new )
  *****************************************************************************/
 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) +
@@ -468,13 +482,15 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     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 )
     {
@@ -485,12 +501,15 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     }
 
     /* 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;
         }
@@ -500,14 +519,15 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
         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;
@@ -537,8 +557,13 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
             *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++ )
             {
@@ -597,6 +622,8 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
 
     }
     
+    vlc_list_release( p_list );
+
     fclose( file );
     free( psz_filename );
 
@@ -626,7 +653,8 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
  *****************************************************************************/
 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;
@@ -694,6 +722,9 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     }
     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;
@@ -701,25 +732,26 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     {
         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 */
@@ -758,6 +790,7 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
         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;
     }
@@ -765,27 +798,28 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     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++ )
         {
@@ -824,6 +858,7 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name )
         fprintf( file, "\n" );
     }
 
+    vlc_list_release( p_list );
 
     /*
      * Restore old settings from the config in file
@@ -851,7 +886,8 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
                           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;
 
@@ -885,25 +921,30 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
     }
 #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;
     }
 
@@ -912,6 +953,7 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
     {
         msg_Err( p_this, "out of memory" );
         free( p_longopts );
+        vlc_list_release( p_list );
         return -1;
     }
 
@@ -926,6 +968,7 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
             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,
@@ -940,11 +983,14 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *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++ )
         {
@@ -1004,6 +1050,9 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
         }
     }
 
+    /* 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';