📄 configuration.c
字号:
{ vlc_list_t *p_list; module_t *p_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, *psz_configfile; int i_index; psz_configfile = p_this->p_vlc->psz_configfile; if( !psz_configfile || !psz_configfile ) { psz_homedir = p_this->p_vlc->psz_homedir; if( !psz_homedir ) { msg_Err( p_this, "psz_homedir is null" ); 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 DIR_SEP CONFIG_FILE, psz_homedir ); } else { psz_filename = strdup( psz_configfile ); } if( !psz_filename ) { msg_Err( p_this, "out of memory" ); return -1; } msg_Dbg( p_this, "opening config file %s", psz_filename ); /* Acquire config file lock */ vlc_mutex_lock( &p_this->p_vlc->config_lock ); file = utf8_fopen( psz_filename, "rt" ); if( !file ) { msg_Warn( p_this, "config file %s does not exist yet", psz_filename ); free( psz_filename ); vlc_mutex_unlock( &p_this->p_vlc->config_lock ); return -1; } /* 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++ ) { char * (*convert) (const char *) = FromLocaleDup; char bom[3]; p_parser = (module_t *)p_list->p_values[i_index].p_object ; if( psz_module_name && strcmp( psz_module_name, p_parser->psz_object_name ) ) { 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 ); /* Look for UTF-8 Byte Order Mark */ if ((fread (bom, 1, 3, file) == 3) && (memcmp (bom, "\xEF\xBB\xBF", 3) == 0)) convert = strdup; 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 0 msg_Dbg( p_this, "loading config for module \"%s\"", p_parser->psz_object_name );#endif break; } } /* 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 ) ) { if( line[0] == '[' ) break; /* end of section */ /* ignore comments or empty lines */ if( (line[0] == '#') || (line[0] == '\n') || (line[0] == (char)0) ) continue; /* get rid of line feed */ if( line[strlen(line)-1] == '\n' ) line[strlen(line)-1] = (char)0; /* 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!!! */ *p_index = (char)0; psz_option_value = p_index + 1; if( !p_parser->i_config_items ) { continue; } /* try to match this option with one of the module's options */ for( p_item = p_parser->p_config; p_item->i_type != CONFIG_HINT_END; p_item++ ) { if( p_item->i_type & CONFIG_HINT ) /* ignore hints */ continue; 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->i_value = strtol( psz_option_value, 0, 0 ); p_item->i_value_saved = p_item->i_value;#if 0 msg_Dbg( p_this, "option \"%s\", value %i", p_item->psz_name, p_item->i_value );#endif break; case CONFIG_ITEM_FLOAT: if( !*psz_option_value ) break; /* ignore empty option */ p_item->f_value = (float)i18n_atof( psz_option_value); p_item->f_value_saved = p_item->f_value;#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->i_value = ConfigStringToKey(psz_option_value); p_item->i_value_saved = p_item->i_value; break; default: vlc_mutex_lock( p_item->p_lock ); /* free old string */ if( p_item->psz_value ) free( p_item->psz_value ); p_item->psz_value = *psz_option_value ? convert( psz_option_value ) : NULL; if( p_item->psz_value_saved ) free( p_item->psz_value_saved ); p_item->psz_value_saved = 0; if( !p_item->psz_value || !p_item->psz_value_orig || (p_item->psz_value && p_item->psz_value_orig && strcmp(p_item->psz_value,p_item->psz_value_orig))) p_item->psz_value_saved = p_item->psz_value ? strdup( p_item->psz_value ) : 0; 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; } } } } } vlc_list_release( p_list ); fclose( file ); free( psz_filename ); vlc_mutex_unlock( &p_this->p_vlc->config_lock ); return 0;}/***************************************************************************** * config_CreateDir: Create configuration directory if it doesn't exist. *****************************************************************************/int config_CreateDir( vlc_object_t *p_this, const char *psz_dirname ){ if( !psz_dirname && !*psz_dirname ) return -1; if( utf8_mkdir( psz_dirname ) && ( errno != EEXIST ) ) { msg_Err( p_this, "could not create %s (%s)", psz_dirname, strerror(errno) ); return -1; } return 0;}/***************************************************************************** * config_SaveConfigFile: Save a module's config options. ***************************************************************************** * This will save the specified module's config options to the config file. * If psz_module_name is NULL then we save all the modules config options. * It's no use to save the config options that kept their default values, so * we'll try to be a bit clever here. * * When we save we mustn't delete the config options of the modules that * haven't been loaded. So we cannot just create a new config file with the * config structures we've got in memory. * I don't really know how to deal with this nicely, so I will use a completly * dumb method ;-) * I will load the config file in memory, but skipping all the sections of the * modules we want to save. Then I will create a brand new file, dump the file * loaded in memory and then append the sections of the modules we want to * save. * Really stupid no ? *****************************************************************************/static int SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name, vlc_bool_t b_autosave ){ module_t *p_parser; vlc_list_t *p_list; module_config_t *p_item; FILE *file; char p_line[1024], *p_index2; 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_vlc->config_lock ); psz_configfile = p_this->p_vlc->psz_configfile; if( !psz_configfile || !psz_configfile ) { 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( 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" ); vlc_mutex_unlock( &p_this->p_vlc->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_vlc->config_lock ); return -1; } } 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 { /* look for file size */ fseek( file, 0L, SEEK_END ); i_sizebuf = ftell( file ); fseek( file, 0L, SEEK_SET ); } p_bigbuffer = p_index = malloc( i_sizebuf+1 ); if( !p_bigbuffer ) { msg_Err( p_this, "out of memory" ); if( file ) fclose( file ); free( psz_filename ); vlc_mutex_unlock( &p_this->p_vlc->config_lock ); return -1; } 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; while( file && fgets( p_line, 1024, file ) ) { if( (p_line[0] == '[') && (p_index2 = strchr(p_line,']'))) { /* we found a section, check if we need to do a backup */ for( i_index = 0; i_index < p_list->i_count; i_index++ ) { p_parser = (module_t *)p_list->p_values[i_index].p_object ; if( ((p_index2 - &p_line[1]) == (int)strlen(p_parser->psz_object_name) ) && !memcmp( &p_line[1], p_parser->psz_object_name, strlen(p_parser->psz_object_name) ) ) { if( !psz_module_name ) break; else if( !strcmp( psz_module_name, p_parser->psz_object_name ) ) break; } } if( i_index == p_list->i_count ) { /* we don't have this section in our list so we need to back * it up */ *p_index2 = 0;#if 0 msg_Dbg( p_this, "backing up config for unknown module \"%s\"", &p_line[1] );#endif *p_index2 = ']'; b_backup = 1; } else { b_backup = 0; } } /* save line if requested and line is valid (doesn't begin with a * space, tab, or eol) */ if( b_backup && (p_line[0] != '\n') && (p_line[0] != ' ')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -