Commit 944a9914 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Code factorization

parent eb2e4add
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "vlc_keys.h" #include "vlc_keys.h"
#include <errno.h> /* errno */ #include <errno.h> /* errno */
#include <stdbool.h>
#ifdef HAVE_LIMITS_H #ifdef HAVE_LIMITS_H
# include <limits.h> # include <limits.h>
...@@ -342,6 +343,32 @@ int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname ) ...@@ -342,6 +343,32 @@ int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname )
return -1; return -1;
} }
static int
config_Write (FILE *file, const char *type, const char *desc,
bool comment, const char *name, const char *fmt, ...)
{
va_list ap;
int ret;
if (desc == NULL)
desc = "?";
if (fprintf (file, "# %s (%s)\n%s%s=", desc, gettext (type),
comment ? "#" : "", name) < 0)
return -1;
va_start (ap, fmt);
ret = vfprintf (file, fmt, ap);
va_end (ap);
if (ret < 0)
return -1;
if (fputs ("\n\n", file) == EOF)
return -1;
return 0;
}
/***************************************************************************** /*****************************************************************************
* config_SaveConfigFile: Save a module's config options. * config_SaveConfigFile: Save a module's config options.
***************************************************************************** *****************************************************************************
...@@ -511,85 +538,68 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name, ...@@ -511,85 +538,68 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
p_item < p_end; p_item < p_end;
p_item++ ) p_item++ )
{ {
char *psz_key; /* Do not save the new value in the configuration file
int i_value = p_item->value.i; * if doing an autosave, and the item is not an "autosaved" one. */
float f_value = p_item->value.f; vlc_bool_t b_retain = b_autosave && !p_item->b_autosave;
const char *psz_value = p_item->value.psz;
if( p_item->i_type & CONFIG_HINT ) if ((p_item->i_type & CONFIG_HINT) /* ignore hint */
/* ignore hints */ || p_item->psz_current /* ignore deprecated option */
continue; || p_item->b_unsaveable) /* ignore volatile option */
/* Ignore deprecated options */
if( p_item->psz_current )
continue;
if( p_item->b_unsaveable )
/*obvious*/
continue; continue;
if( b_autosave && !p_item->b_autosave ) if (IsConfigIntegerType (p_item->i_type))
{ {
i_value = p_item->saved.i; int val = b_retain ? p_item->saved.i : p_item->value.i;
f_value = p_item->saved.f; if (p_item->i_type == CONFIG_ITEM_KEY)
psz_value = p_item->saved.psz; {
if( !psz_value ) psz_value = p_item->orig.psz; char *psz_key = ConfigKeyToString (val);
config_Write (file, p_item->psz_text, N_("key"),
val == p_item->orig.i,
p_item->psz_name, "%s",
psz_key ? psz_key : "");
free (psz_key);
}
else
config_Write (file, p_item->psz_text,
(p_item->i_type == CONFIG_ITEM_BOOL)
? N_("boolean") : N_("integer"),
val == p_item->orig.i,
p_item->psz_name, "%d", val);
p_item->saved.i = val;
} }
else else
if (IsConfigFloatType (p_item->i_type))
{ {
p_item->b_dirty = VLC_FALSE; float val = b_retain ? p_item->saved.f : p_item->value.f;
config_Write (file, p_item->psz_text, N_("float"),
val == p_item->orig.f,
p_item->psz_name, "%f", val);
p_item->saved.f = val;
} }
else
switch( p_item->i_type )
{ {
case CONFIG_ITEM_BOOL: const char *psz_value = b_retain ? p_item->saved.psz
case CONFIG_ITEM_INTEGER: : p_item->value.psz;
if( p_item->psz_text ) bool modified;
fprintf( file, "# %s (%s)\n", p_item->psz_text,
(p_item->i_type == CONFIG_ITEM_BOOL) ?
_("boolean") : _("integer") );
if( i_value == p_item->orig.i )
fputc ('#', file);
fprintf( file, "%s=%i\n", p_item->psz_name, i_value );
p_item->saved.i = i_value;
break;
case CONFIG_ITEM_KEY: assert (IsConfigStringType (p_item->i_type));
if( p_item->psz_text )
fprintf( file, "# %s (%s)\n", p_item->psz_text,
_("key") );
if( i_value == p_item->orig.i )
fputc ('#', file);
psz_key = ConfigKeyToString( i_value );
fprintf( file, "%s=%s\n", p_item->psz_name,
psz_key ? psz_key : "" );
free (psz_key);
p_item->saved.i = i_value; if (b_retain && (psz_value == NULL)) /* FIXME: hack */
break; psz_value = p_item->orig.psz;
case CONFIG_ITEM_FLOAT: modified =
if( p_item->psz_text ) (psz_value != NULL)
fprintf( file, "# %s (%s)\n", p_item->psz_text, ? ((p_item->orig.psz != NULL)
_("float") ); ? (strcmp (psz_value, p_item->orig.psz) != 0)
if( f_value == p_item->orig.f ) : true)
fputc ('#', file); : (p_item->orig.psz != NULL);
fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value );
p_item->saved.f = f_value;
break;
default: config_Write (file, p_item->psz_text, N_("string"),
if( p_item->psz_text ) modified, p_item->psz_name, "%s",
fprintf( file, "# %s (%s)\n", p_item->psz_text, psz_value ? psz_value : "");
_("string") );
if( (!psz_value && !p_item->orig.psz) || if (b_retain)
(psz_value && p_item->orig.psz && break;
!strcmp( psz_value, p_item->orig.psz )) )
fputc ('#', file);
fprintf( file, "%s=%s\n", p_item->psz_name,
psz_value ?: "" );
if( b_autosave && !p_item->b_autosave ) break;
free ((char *)p_item->saved.psz); free ((char *)p_item->saved.psz);
if( (psz_value && p_item->orig.psz && if( (psz_value && p_item->orig.psz &&
...@@ -599,9 +609,10 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name, ...@@ -599,9 +609,10 @@ static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name,
else else
p_item->saved.psz = NULL; p_item->saved.psz = NULL;
} }
}
fputc ('\n', file); if (!b_retain)
p_item->b_dirty = VLC_FALSE;
}
} }
vlc_list_release( p_list ); vlc_list_release( p_list );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment