Changeset 944a9914b3dc1d93e8c37ceaaae1ee281cf03f26

Show
Ignore:
Timestamp:
16/12/07 11:33:04 (10 months ago)
Author:
Rémi Denis-Courmont <rem@videolan.org>
git-committer:
Rémi Denis-Courmont <rem@videolan.org> 1197801184 +0000
git-parent:

[eb2e4add4f3ed2f94fbb2ac3c81b67a8322a85c1]

git-author:
Rémi Denis-Courmont <rem@videolan.org> 1197801184 +0000
Message:

Code factorization

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/config/file.c

    r2d9c9ee r944a991  
    2828 
    2929#include <errno.h>                                                  /* errno */ 
     30#include <stdbool.h> 
    3031 
    3132#ifdef HAVE_LIMITS_H 
     
    343344} 
    344345 
     346static int 
     347config_Write (FILE *file, const char *type, const char *desc, 
     348              bool comment, const char *name, const char *fmt, ...) 
     349{ 
     350    va_list ap; 
     351    int ret; 
     352 
     353    if (desc == NULL) 
     354        desc = "?"; 
     355 
     356    if (fprintf (file, "# %s (%s)\n%s%s=", desc, gettext (type), 
     357                 comment ? "#" : "", name) < 0) 
     358        return -1; 
     359 
     360    va_start (ap, fmt); 
     361    ret = vfprintf (file, fmt, ap); 
     362    va_end (ap); 
     363    if (ret < 0) 
     364        return -1; 
     365 
     366    if (fputs ("\n\n", file) == EOF) 
     367        return -1; 
     368    return 0; 
     369} 
     370 
     371 
    345372/***************************************************************************** 
    346373 * config_SaveConfigFile: Save a module's config options. 
     
    512539             p_item++ ) 
    513540        { 
    514             char  *psz_key; 
    515             int   i_value = p_item->value.i; 
    516             float f_value = p_item->value.f
    517             const char  *psz_value = p_item->value.psz; 
    518  
    519             if( p_item->i_type & CONFIG_HINT ) 
    520                 /* ignore hints */ 
     541            /* Do not save the new value in the configuration file 
     542            * if doing an autosave, and the item is not an "autosaved" one. */ 
     543            vlc_bool_t b_retain = b_autosave && !p_item->b_autosave
     544 
     545            if ((p_item->i_type & CONFIG_HINT) /* ignore hint */ 
     546            || p_item->psz_current            /* ignore deprecated option */ 
     547             || p_item->b_unsaveable)          /* ignore volatile option */ 
    521548                continue; 
    522             /* Ignore deprecated options */ 
    523             if( p_item->psz_current ) 
    524                 continue; 
    525             if( p_item->b_unsaveable ) 
    526                 /*obvious*/ 
    527                 continue; 
    528  
    529             if( b_autosave && !p_item->b_autosave ) 
    530             { 
    531                 i_value = p_item->saved.i; 
    532                 f_value = p_item->saved.f; 
    533                 psz_value = p_item->saved.psz; 
    534                 if( !psz_value ) psz_value = p_item->orig.psz; 
     549 
     550            if (IsConfigIntegerType (p_item->i_type)) 
     551            { 
     552                int val = b_retain ? p_item->saved.i : p_item->value.i; 
     553                if (p_item->i_type == CONFIG_ITEM_KEY) 
     554                { 
     555                    char *psz_key = ConfigKeyToString (val); 
     556                    config_Write (file, p_item->psz_text, N_("key"), 
     557                                  val == p_item->orig.i, 
     558                                  p_item->psz_name, "%s", 
     559                                  psz_key ? psz_key : ""); 
     560                    free (psz_key); 
     561                } 
     562                else 
     563                    config_Write (file, p_item->psz_text, 
     564                                  (p_item->i_type == CONFIG_ITEM_BOOL) 
     565                                      ? N_("boolean") : N_("integer"), 
     566                                  val == p_item->orig.i, 
     567                                  p_item->psz_name, "%d", val); 
     568                p_item->saved.i = val; 
    535569            } 
    536570            else 
    537             { 
    538                 p_item->b_dirty = VLC_FALSE; 
    539             } 
    540  
    541             switch( p_item->i_type ) 
    542             { 
    543             case CONFIG_ITEM_BOOL: 
    544             case CONFIG_ITEM_INTEGER: 
    545                 if( p_item->psz_text ) 
    546                     fprintf( file, "# %s (%s)\n", p_item->psz_text, 
    547                              (p_item->i_type == CONFIG_ITEM_BOOL) ? 
    548                              _("boolean") : _("integer") ); 
    549                 if( i_value == p_item->orig.i ) 
    550                     fputc ('#', file); 
    551                 fprintf( file, "%s=%i\n", p_item->psz_name, i_value ); 
    552  
    553                 p_item->saved.i = i_value; 
    554                 break; 
    555  
    556             case CONFIG_ITEM_KEY: 
    557                 if( p_item->psz_text ) 
    558                     fprintf( file, "# %s (%s)\n", p_item->psz_text, 
    559                              _("key") ); 
    560                 if( i_value == p_item->orig.i ) 
    561                     fputc ('#', file); 
    562                 psz_key = ConfigKeyToString( i_value ); 
    563                 fprintf( file, "%s=%s\n", p_item->psz_name, 
    564                          psz_key ? psz_key : "" ); 
    565                 free (psz_key); 
    566  
    567                 p_item->saved.i = i_value; 
    568                 break; 
    569  
    570             case CONFIG_ITEM_FLOAT: 
    571                 if( p_item->psz_text ) 
    572                     fprintf( file, "# %s (%s)\n", p_item->psz_text, 
    573                              _("float") ); 
    574                 if( f_value == p_item->orig.f ) 
    575                     fputc ('#', file); 
    576                 fprintf( file, "%s=%f\n", p_item->psz_name, (double)f_value ); 
    577  
    578                 p_item->saved.f = f_value; 
    579                 break; 
    580  
    581             default: 
    582                 if( p_item->psz_text ) 
    583                     fprintf( file, "# %s (%s)\n", p_item->psz_text, 
    584                              _("string") ); 
    585                 if( (!psz_value && !p_item->orig.psz) || 
    586                     (psz_value && p_item->orig.psz && 
    587                      !strcmp( psz_value, p_item->orig.psz )) ) 
    588                     fputc ('#', file); 
    589                 fprintf( file, "%s=%s\n", p_item->psz_name, 
    590                          psz_value ?: "" ); 
    591  
    592                 if( b_autosave && !p_item->b_autosave ) break; 
     571            if (IsConfigFloatType (p_item->i_type)) 
     572            { 
     573                float val = b_retain ? p_item->saved.f : p_item->value.f; 
     574                config_Write (file, p_item->psz_text, N_("float"), 
     575                              val == p_item->orig.f, 
     576                              p_item->psz_name, "%f", val); 
     577                p_item->saved.f = val; 
     578            } 
     579            else 
     580            { 
     581                const char *psz_value = b_retain ? p_item->saved.psz 
     582                                                 : p_item->value.psz; 
     583                bool modified; 
     584 
     585                assert (IsConfigStringType (p_item->i_type)); 
     586 
     587                if (b_retain && (psz_value == NULL)) /* FIXME: hack */ 
     588                    psz_value = p_item->orig.psz; 
     589 
     590                modified = 
     591                    (psz_value != NULL) 
     592                        ? ((p_item->orig.psz != NULL) 
     593                            ? (strcmp (psz_value, p_item->orig.psz) != 0) 
     594                            : true) 
     595                        : (p_item->orig.psz != NULL); 
     596 
     597                config_Write (file, p_item->psz_text, N_("string"), 
     598                              modified, p_item->psz_name, "%s", 
     599                              psz_value ? psz_value : ""); 
     600 
     601                if (b_retain) 
     602                    break; 
    593603 
    594604                free ((char *)p_item->saved.psz); 
     
    600610                    p_item->saved.psz = NULL; 
    601611            } 
    602         } 
    603  
    604         fputc ('\n', file); 
     612 
     613            if (!b_retain) 
     614                p_item->b_dirty = VLC_FALSE; 
     615        } 
    605616    } 
    606617