]> git.sesse.net Git - vlc/blobdiff - src/misc/configuration.c
A bit of cleanup here and there
[vlc] / src / misc / configuration.c
index b35040919efe06b973393ac60a7c86b9de89c0c4..a36fc1b76c66d9e6d556905459aea1cf0fe8a0c8 100644 (file)
@@ -23,7 +23,7 @@
 
 #include <vlc/vlc.h>
 #include "vlc_keys.h"
-#include "charset.h"
+#include "vlc_charset.h"
 
 #include <stdio.h>                                              /* sprintf() */
 #include <stdlib.h>                                      /* free(), strtol() */
@@ -63,6 +63,8 @@
 #include <tchar.h>
 #endif
 
+#include "configuration.h"
+
 static int ConfigStringToKey( const char * );
 static char *ConfigKeyToString( int );
 
@@ -618,6 +620,7 @@ int config_Duplicate( module_t *p_module, const module_config_t *p_orig,
 
         p_module->p_config[i].pf_callback = p_orig[i].pf_callback;
     }
+    return VLC_SUCCESS;
 }
 
 
@@ -634,20 +637,11 @@ void config_Free( module_t *p_module )
     {
         module_config_t *p_item = p_module->p_config + j;
 
-        if( p_item->psz_type )
-            free( p_item->psz_type );
-
-        if( p_item->psz_name )
-            free( p_item->psz_name );
-
-        if( p_item->psz_current )
-            free( p_item->psz_current );
-
-        if( p_item->psz_text )
-            free( p_item->psz_text );
-
-        if( p_item->psz_longtext )
-            free( p_item->psz_longtext );
+        free( (char*) p_item->psz_type );
+        free( (char*) p_item->psz_name );
+        free( (char*) p_item->psz_current );
+        free( (char*) p_item->psz_text );
+        free( (char*) p_item->psz_longtext );
 
         if (IsConfigStringType (p_item->i_type))
         {
@@ -661,9 +655,9 @@ void config_Free( module_t *p_module )
             for( i = 0; i < p_item->i_list; i++ )
             {
                 if( p_item->ppsz_list && p_item->ppsz_list[i] )
-                    free( p_item->ppsz_list[i] );
+                    free( (char*) p_item->ppsz_list[i] );
                 if( p_item->ppsz_list_text && p_item->ppsz_list_text[i] )
-                    free( p_item->ppsz_list_text[i] );
+                    free( (char*) p_item->ppsz_list_text[i] );
             }
             if( p_item->ppsz_list ) free( p_item->ppsz_list );
             if( p_item->ppsz_list_text ) free( p_item->ppsz_list_text );
@@ -674,8 +668,7 @@ void config_Free( module_t *p_module )
         {
             for( i = 0; i < p_item->i_action; i++ )
             {
-                if( p_item->ppsz_action_text[i] )
-                    free( p_item->ppsz_action_text[i] );
+                free( (char*) p_item->ppsz_action_text[i] );
             }
             if( p_item->ppf_action ) free( p_item->ppf_action );
             if( p_item->ppsz_action_text ) free( p_item->ppsz_action_text );
@@ -761,209 +754,199 @@ void __config_ResetAll( vlc_object_t *p_this )
     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
 }
 
-/*****************************************************************************
- * config_LoadConfigFile: loads the configuration file.
- *****************************************************************************
- * This function is called to load the config options stored in the config
- * file.
- *****************************************************************************/
-int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
+
+static FILE *config_OpenConfigFile (vlc_object_t *obj, const char *mode)
 {
-    vlc_list_t *p_list;
-    FILE *file;
-    char *p_index;
-    char *psz_filename;
-    int i_index;
+    static const char subpath[] = DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE;
+    const char *filename = obj->p_libvlc->psz_configfile;
+    const char *homedir;
+    size_t buflen = 0;
 
-    const char *psz_configfile = p_this->p_libvlc->psz_configfile;
-    if( !psz_configfile || !psz_configfile )
+    if (filename == NULL)
     {
-        const char *psz_homedir = p_this->p_libvlc->psz_homedir;
-        if( !psz_homedir )
+        homedir = obj->p_libvlc->psz_homedir;
+        if (homedir == NULL)
         {
-            msg_Err( p_this, "psz_homedir is null" );
-            return -1;
+            msg_Err (obj, "no home directory defined");
+            return NULL;
         }
 
-        if( asprintf( &psz_filename, "%s" DIR_SEP CONFIG_DIR DIR_SEP CONFIG_FILE,
-                      psz_homedir ) == -1 )
-            psz_filename = NULL;
-    }
-    else
-    {
-        psz_filename = strdup( psz_configfile );
+        buflen = strlen (homedir) + sizeof (subpath);
     }
 
-    if( !psz_filename )
+    char buf[buflen];
+    if (filename == NULL)
     {
-        msg_Err( p_this, "out of memory" );
-        return -1;
+        sprintf (buf, "%s%s", homedir, subpath);
+        filename = buf;
     }
 
-    msg_Dbg( p_this, "opening config file %s", psz_filename );
+    msg_Dbg (obj, "opening config file (%s)", filename);
+    FILE *stream = utf8_fopen (filename, mode);
+    if ((stream == NULL) && (errno != ENOENT))
+        msg_Err (obj, "cannot open config file (%s): %s", strerror (errno));
+
+    return stream;
+}
+
+
+/*****************************************************************************
+ * config_LoadConfigFile: loads the configuration file.
+ *****************************************************************************
+ * This function is called to load the config options stored in the config
+ * file.
+ *****************************************************************************/
+int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
+{
+    vlc_list_t *p_list;
+    FILE *file;
+
+    file = config_OpenConfigFile (p_this, "rt");
+    if (file == NULL)
+        return VLC_EGENERIC;
 
     /* Acquire config file lock */
     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
 
-    file = utf8_fopen( psz_filename, "rt" );
-    if( file == NULL )
-    {
-        msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
-        free( psz_filename );
-        vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
-        return -1;
-    }
-    free( psz_filename );
-
     /* Look for the selected module, if NULL then save everything */
     p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
 
-    for( i_index = 0; i_index < p_list->i_count; i_index++ )
+    /* Look for UTF-8 Byte Order Mark */
+    char * (*convert) (const char *) = strdupnull;
+    char bom[3];
+
+    if ((fread (bom, 1, 3, file) != 3)
+     || memcmp (bom, "\xEF\xBB\xBF", 3))
     {
-        module_t *p_parser = (module_t *)p_list->p_values[i_index].p_object ;
-        char line[1024];
+        convert = FromLocaleDup;
+        rewind (file); // no BOM, rewind
+    }
+
+    module_t *module = NULL;
+    char line[1024], section[1022];
+    section[0] = '\0';
 
-        if( psz_module_name
-             && strcmp( psz_module_name, p_parser->psz_object_name ) )
+    while (fgets (line, 1024, file) != NULL)
+    {
+        // Ignore comments and empty lines
+        switch (line[0])
         {
-            continue;
+            case '#':
+            case '\n':
+            case '\0':
+                continue;
         }
 
-        /* The config file is organized in sections, one per module. Look for
-         * the interesting section ( a section is of the form [foo] ) */
-        fseek( file, 0L, SEEK_SET );
+        if (line[0] == '[')
+        {
+            char *ptr = strchr (line, ']');
+            if (ptr == NULL)
+                continue; // syntax error;
+            *ptr = '\0';
 
-        /* Look for UTF-8 Byte Order Mark */
-        char * (*convert) (const char *) = FromLocaleDup;
-        char bom[3];
+            // New section ( = a given module)
+            strcpy (section, line + 1);
+            module = NULL;
 
-        if ((fread (bom, 1, 3, file) == 3)
-         && (memcmp (bom, "\xEF\xBB\xBF", 3) == 0))
-            convert = strdupnull;
-        else
-            rewind (file); // no BOM, rewind
-
-        while( fgets( line, 1024, file ) )
-        {
-            if( (line[0] == '[')
-               && (p_index = strchr(line,']'))
-               && (p_index - &line[1]
-                    == (int)strlen(p_parser->psz_object_name))
-               && !memcmp( &line[1], p_parser->psz_object_name,
-                           strlen(p_parser->psz_object_name) ) )
+            if ((psz_module_name == NULL)
+             || (strcmp (psz_module_name, section) == 0))
             {
-#if 0
-                msg_Dbg( p_this, "loading config for module \"%s\"",
-                                 p_parser->psz_object_name );
-#endif
+                for (int i = 0; i < p_list->i_count; i++)
+                {
+                    module_t *m = (module_t *)p_list->p_values[i].p_object;
 
-                break;
+                    if ((strcmp (section, m->psz_object_name) == 0)
+                     && (m->i_config_items > 0)) // ignore config-less modules
+                    {
+                        module = m;
+                        if (psz_module_name != NULL)
+                            msg_Dbg (p_this,
+                                     "loading config for module \"%s\"",
+                                     section);
+                        break;
+                    }
+                }
             }
+
+            continue;
         }
-        /* either we found the section or we're at the EOF */
 
-        /* Now try to load the options in this section */
-        while( fgets( line, 1024, file ) )
-        {
-            const char *psz_option_name, *psz_option_value;
-            module_config_t *p_item, *p_end;
+        if (module == NULL)
+            continue; // no need to parse if there is no matching module
 
-            if( line[0] == '[' ) break; /* end of section */
+        char *ptr = strchr (line, '\n');
+        if (ptr != NULL)
+            *ptr = '\0';
 
-            /* ignore comments or empty lines */
-            if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) )
-                continue;
+        /* look for option name */
+        const char *psz_option_name = line;
 
-            /* get rid of line feed */
-            if( line[strlen(line)-1] == '\n' )
-                line[strlen(line)-1] = (char)0;
+        ptr = strchr (line, '=');
+        if (ptr == NULL)
+            continue; // syntax error
 
-            /* look for option name */
-            psz_option_name = line;
-            psz_option_value = NULL;
-            p_index = strchr( line, '=' );
-            if( !p_index ) break; /* this ain't an option!!! */
+        *ptr = '\0';
+        const char *psz_option_value = ptr + 1;
 
-            *p_index = (char)0;
-            psz_option_value = p_index + 1;
+        /* try to match this option with one of the module's options */
+        for (size_t i = 0; i < module->confsize; i++)
+        {
+            module_config_t *p_item = module->p_config + i;
 
-            if( !p_parser->i_config_items )
-            {
+            if ((p_item->i_type & CONFIG_HINT)
+             || strcmp (p_item->psz_name, psz_option_name))
                 continue;
-            }
 
-            /* try to match this option with one of the module's options */
-            for( p_item = p_parser->p_config, p_end = p_item + p_parser->confsize;
-                 p_item < p_end;
-                 p_item++ )
+            /* We found it */
+            switch( p_item->i_type )
             {
-                if( p_item->i_type & CONFIG_HINT )
-                    /* ignore hints */
-                    continue;
+                case CONFIG_ITEM_BOOL:
+                case CONFIG_ITEM_INTEGER:
+                    if( !*psz_option_value )
+                        break;                    /* ignore empty option */
+                    p_item->value.i = strtol( psz_option_value, 0, 0 );
+                    p_item->saved.i = p_item->value.i;
 
-                if( !strcmp( p_item->psz_name, psz_option_name ) )
-                {
-                    /* We found it */
-                    switch( p_item->i_type )
-                    {
-                    case CONFIG_ITEM_BOOL:
-                    case CONFIG_ITEM_INTEGER:
-                        if( !*psz_option_value )
-                            break;                    /* ignore empty option */
-                        p_item->value.i = strtol( psz_option_value, 0, 0 );
-                        p_item->saved.i = p_item->value.i;
-#if 0
-                        msg_Dbg( p_this, "option \"%s\", value %i",
-                                 p_item->psz_name, p_item->i_value );
-#endif
-                        break;
+                    /*msg_Dbg (p_this, "option \"%s\", value %i",
+                             psz_option_name, p_item->value.i);*/
+                    break;
 
-                    case CONFIG_ITEM_FLOAT:
-                        if( !*psz_option_value )
-                            break;                    /* ignore empty option */
-                        p_item->value.f = (float)i18n_atof( psz_option_value);
-                        p_item->saved.f = p_item->value.f;
-#if 0
-                        msg_Dbg( p_this, "option \"%s\", value %f",
-                                 p_item->psz_name, (double)p_item->f_value );
-#endif
-                        break;
-                    case CONFIG_ITEM_KEY:
-                        if( !*psz_option_value )
-                            break;                    /* ignore empty option */
-                        p_item->value.i = ConfigStringToKey(psz_option_value);
-                        p_item->saved.i = p_item->value.i;
-                        break;
+                case CONFIG_ITEM_FLOAT:
+                    if( !*psz_option_value )
+                        break;                    /* ignore empty option */
+                    p_item->value.f = (float)i18n_atof( psz_option_value);
+                    p_item->saved.f = p_item->value.f;
 
-                    default:
-                        vlc_mutex_lock( p_item->p_lock );
+                    /*msg_Dbg (p_this, "option \"%s\", value %f",
+                             psz_option_name, (double)p_item->value.f);*/
+                    break;
 
-                        /* free old string */
-                        freenull (p_item->value.psz);
+                case CONFIG_ITEM_KEY:
+                    if( !*psz_option_value )
+                        break;                    /* ignore empty option */
+                    p_item->value.i = ConfigStringToKey(psz_option_value);
+                    p_item->saved.i = p_item->value.i;
+                    break;
 
-                        p_item->value.psz = convert (psz_option_value);
+                default:
+                    vlc_mutex_lock( p_item->p_lock );
 
-                        freenull (p_item->saved.psz);
-                        p_item->saved.psz = NULL;
+                    /* free old string */
+                    free( (char*) p_item->value.psz );
+                    free( (char*) p_item->saved.psz );
 
-                        if( !p_item->value.psz || !p_item->orig.psz ||
-                            (p_item->value.psz && p_item->orig.psz &&
-                             strcmp(p_item->value.psz, p_item->orig.psz)))
-                            p_item->saved.psz = convert (p_item->value.psz);
+                    p_item->value.psz = convert (psz_option_value);
+                    p_item->saved.psz = strdupnull (p_item->value.psz);
 
-                        vlc_mutex_unlock( p_item->p_lock );
+                    vlc_mutex_unlock( p_item->p_lock );
 
-#if 0
-                        msg_Dbg( p_this, "option \"%s\", value \"%s\"",
-                                 p_item->psz_name,
-                                 p_item->psz_value ? p_item->psz_value : "" );
-#endif
-                        break;
-                    }
-                }
+                    /*msg_Dbg (p_this, "option \"%s\", value \"%s\"",
+                             psz_option_name, psz_option_value ?: "");*/
+                    break;
             }
-        }
 
+            break;
+        }
     }
 
     vlc_list_release( p_list );
@@ -971,7 +954,6 @@ int __config_LoadConfigFile( vlc_object_t *p_this, const char *psz_module_name )
     fclose( file );
 
     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
-
     return 0;
 }
 
@@ -1021,58 +1003,28 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
     int i_sizebuf = 0;
     char *p_bigbuffer, *p_index;
     vlc_bool_t b_backup;
-    char *psz_filename, *psz_homedir, *psz_configfile;
     int i_index;
 
     /* Acquire config file lock */
     vlc_mutex_lock( &p_this->p_libvlc->config_lock );
 
-    psz_configfile = p_this->p_libvlc->psz_configfile;
-    if( !psz_configfile || !psz_configfile )
+    if (p_this->p_libvlc->psz_configfile == NULL)
     {
-        psz_homedir = p_this->p_libvlc->psz_homedir;
+        const char *psz_homedir = p_this->p_libvlc->psz_homedir;
         if( !psz_homedir )
         {
-            msg_Err( p_this, "psz_homedir is null" );
-            vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
-            return -1;
-        }
-        psz_filename = (char *)malloc( sizeof("/" CONFIG_DIR "/" CONFIG_FILE) +
-                                       strlen(psz_homedir) );
-
-        if( psz_filename )
-            sprintf( psz_filename, "%s" DIR_SEP CONFIG_DIR, psz_homedir );
-
-        if( !psz_filename )
-        {
-            msg_Err( p_this, "out of memory" );
+            msg_Err( p_this, "no home directory defined" );
             vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
             return -1;
         }
 
-        config_CreateDir( p_this, psz_filename );
-
-        strcat( psz_filename, DIR_SEP CONFIG_FILE );
-    }
-    else
-    {
-        psz_filename = strdup( psz_configfile );
-        if( !psz_filename )
-        {
-            msg_Err( p_this, "out of memory" );
-            vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
-            return -1;
-        }
+        char dirname[strlen (psz_homedir) + sizeof (DIR_SEP CONFIG_DIR)];
+        sprintf (dirname, "%s" DIR_SEP CONFIG_DIR, psz_homedir);
+        config_CreateDir (p_this, dirname);
     }
 
-    msg_Dbg( p_this, "opening config file %s", psz_filename );
-
-    file = utf8_fopen( psz_filename, "rt" );
-    if( !file )
-    {
-        msg_Warn( p_this, "config file %s does not exist yet", psz_filename );
-    }
-    else
+    file = config_OpenConfigFile (p_this, "rt");
+    if (file != NULL)
     {
         /* look for file size */
         fseek( file, 0L, SEEK_END );
@@ -1085,7 +1037,6 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
     {
         msg_Err( p_this, "out of memory" );
         if( file ) fclose( file );
-        free( psz_filename );
         vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
         return -1;
     }
@@ -1155,12 +1106,9 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
      * Save module config in file
      */
 
-    file = utf8_fopen( psz_filename, "wt" );
+    file = config_OpenConfigFile (p_this, "wt");
     if( !file )
     {
-        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_libvlc->config_lock );
         return -1;
@@ -1298,7 +1246,6 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
     free( p_bigbuffer );
 
     fclose( file );
-    free( psz_filename );
     vlc_mutex_unlock( &p_this->p_libvlc->config_lock );
 
     return 0;
@@ -1586,7 +1533,7 @@ int __config_LoadCmdLine( vlc_object_t *p_this, int *pi_argc, char *ppsz_argv[],
                                 "You should use --%s instead.\n",
                                 p_conf->psz_name, p_conf->psz_current);
                     }
-                    psz_name=p_conf->psz_current;
+                    psz_name = p_conf->psz_current;
                     p_conf = config_FindConfig( p_this, psz_name );
                 }