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

Use module_get_object() where appropriate and improve error handling

Use NULL rather than a static string as the default.
module_get_object() deals with this situation.
parent 190fa71e
......@@ -356,7 +356,6 @@ static int config_PrepareDir (vlc_object_t *obj)
*****************************************************************************/
static int SaveConfigFile (vlc_object_t *p_this)
{
module_t *p_parser;
char *permanent = NULL, *temporary = NULL;
if( config_PrepareDir( p_this ) )
......@@ -404,12 +403,15 @@ static int SaveConfigFile (vlc_object_t *p_this)
if ((line[0] == '[') && (p_index2 = strchr(line,']')))
{
module_t *module;
/* we found a new section, check if we need to do a backup */
backup = true;
for (int i = 0; (p_parser = list[i]) != NULL; i++)
for (int i = 0; (module = list[i]) != NULL; i++)
{
if (!strncmp (line + 1, p_parser->psz_object_name,
strlen (p_parser->psz_object_name)))
const char *objname = module_get_object (module);
if (!strncmp (line + 1, objname, strlen (objname)))
{
backup = false; /* no, we will rewrite it! */
break;
......@@ -495,6 +497,7 @@ static int SaveConfigFile (vlc_object_t *p_this)
vlc_rwlock_rdlock (&config_lock);*/
/* Look for the selected module, if NULL then save everything */
module_t *p_parser;
for (int i = 0; (p_parser = list[i]) != NULL; i++)
{
module_config_t *p_item, *p_end;
......@@ -502,7 +505,7 @@ static int SaveConfigFile (vlc_object_t *p_this)
if( !p_parser->i_config_items )
continue;
fprintf( file, "[%s]", p_parser->psz_object_name );
fprintf( file, "[%s]", module_get_object (p_parser) );
if( p_parser->psz_longname )
fprintf( file, " # %s\n\n", p_parser->psz_longname );
else
......
......@@ -1387,10 +1387,11 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
module_config_t *p_item = NULL;
module_config_t *p_section = NULL;
module_config_t *p_end = p_parser->p_config + p_parser->confsize;
const char *objname = module_get_object (p_parser);
if( psz_search &&
( b_strict ? strcmp( psz_search, p_parser->psz_object_name )
: !strstr( p_parser->psz_object_name, psz_search ) ) )
( b_strict ? strcmp( objname, psz_search )
: !strstr( objname, psz_search ) ) )
{
char *const *pp_shortcuts = p_parser->pp_shortcuts;
unsigned i;
......@@ -1410,7 +1411,7 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
continue;
}
b_help_module = !strcmp( "help", p_parser->psz_object_name );
b_help_module = !strcmp( "help", objname );
/* Ugly hack to make sure that the help options always come first
* (part 2) */
if( !psz_search && b_help_module )
......@@ -1437,12 +1438,12 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
b_found = true;
/* Print name of module */
if( strcmp( "main", p_parser->psz_object_name ) )
if( strcmp( "main", objname ) )
{
if( b_color )
utf8_fprintf( stdout, "\n " GREEN "%s" GRAY " (%s)\n",
module_gettext( p_parser, p_parser->psz_longname ),
p_parser->psz_object_name );
objname );
else
utf8_fprintf( stdout, "\n %s\n",
module_gettext(p_parser, p_parser->psz_longname ) );
......@@ -1487,7 +1488,7 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
{
case CONFIG_HINT_CATEGORY:
case CONFIG_HINT_USAGE:
if( !strcmp( "main", p_parser->psz_object_name ) )
if( !strcmp( "main", objname ) )
{
if( b_color )
utf8_fprintf( stdout, GREEN "\n %s\n" GRAY,
......@@ -1508,7 +1509,7 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
break;
case CONFIG_HINT_SUBCATEGORY:
if( strcmp( "main", p_parser->psz_object_name ) )
if( strcmp( "main", objname ) )
break;
case CONFIG_SECTION:
p_section = p_item;
......@@ -1816,13 +1817,12 @@ static void ListModules( libvlc_int_t *p_this, bool b_verbose )
/* Enumerate each module */
for (size_t j = 0; (p_parser = list[j]) != NULL; j++)
{
const char *objname = module_get_object (p_parser);
if( b_color )
utf8_fprintf( stdout, GREEN" %-22s "WHITE"%s\n"GRAY,
p_parser->psz_object_name,
utf8_fprintf( stdout, GREEN" %-22s "WHITE"%s\n"GRAY, objname,
module_gettext( p_parser, p_parser->psz_longname ) );
else
utf8_fprintf( stdout, " %-22s %s\n",
p_parser->psz_object_name,
utf8_fprintf( stdout, " %-22s %s\n", objname,
module_gettext( p_parser, p_parser->psz_longname ) );
if( b_verbose )
......@@ -1830,7 +1830,7 @@ static void ListModules( libvlc_int_t *p_this, bool b_verbose )
char *const *pp_shortcuts = p_parser->pp_shortcuts;
for( unsigned i = 0; i < p_parser->i_shortcuts; i++ )
{
if( strcmp( pp_shortcuts[i], p_parser->psz_object_name ) )
if( strcmp( pp_shortcuts[i], objname ) )
{
if( b_color )
utf8_fprintf( stdout, CYAN" s %s\n"GRAY,
......
......@@ -202,8 +202,7 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t **r )
module = vlc_module_create();
/* Load additional infos */
free (module->psz_object_name);
LOAD_STRING(module->psz_object_name);
LOAD_STRING(module->object_name);
LOAD_STRING(module->psz_shortname);
LOAD_STRING(module->psz_longname);
LOAD_STRING(module->psz_help);
......@@ -238,9 +237,9 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t **r )
while( i_submodules-- )
{
module_t *submodule = vlc_submodule_create (module);
free (submodule->psz_object_name);
free (submodule->object_name);
free (submodule->pp_shortcuts);
LOAD_STRING(submodule->psz_object_name);
LOAD_STRING(submodule->object_name);
LOAD_STRING(submodule->psz_shortname);
LOAD_STRING(submodule->psz_longname);
LOAD_STRING(submodule->psz_help);
......@@ -498,7 +497,7 @@ static int CacheSaveBank (FILE *file, const module_cache_t *cache,
uint32_t i_submodule;
/* Save additional infos */
SAVE_STRING(module->psz_object_name);
SAVE_STRING(module->object_name);
SAVE_STRING(module->psz_shortname);
SAVE_STRING(module->psz_longname);
SAVE_STRING(module->psz_help);
......@@ -542,7 +541,7 @@ static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
if( CacheSaveSubmodule( file, p_module->next ) )
goto error;
SAVE_STRING( p_module->psz_object_name );
SAVE_STRING( p_module->object_name );
SAVE_STRING( p_module->psz_shortname );
SAVE_STRING( p_module->psz_longname );
SAVE_STRING( p_module->psz_help );
......
......@@ -39,19 +39,17 @@ static void vlc_module_destruct (gc_object_t *obj)
module_t *module = vlc_priv (obj, module_t);
free (module->pp_shortcuts);
free (module->psz_object_name);
free (module->object_name);
free (module);
}
static const char default_name[] = "unnamed";
module_t *vlc_module_create (void)
{
module_t *module = malloc (sizeof (*module));
if (module == NULL)
return NULL;
module->psz_object_name = strdup( default_name );
module->object_name = NULL;
module->next = NULL;
module->submodule = NULL;
module->parent = NULL;
......@@ -59,7 +57,7 @@ module_t *vlc_module_create (void)
vlc_gc_init (module, vlc_module_destruct);
module->psz_shortname = NULL;
module->psz_longname = (char*)default_name;
module->psz_longname = NULL;
module->psz_help = NULL;
module->pp_shortcuts = NULL;
module->i_shortcuts = 0;
......@@ -85,7 +83,7 @@ static void vlc_submodule_destruct (gc_object_t *obj)
{
module_t *module = vlc_priv (obj, module_t);
free (module->pp_shortcuts);
free (module->psz_object_name);
free (module->object_name);
free (module);
}
......@@ -109,7 +107,7 @@ module_t *vlc_submodule_create (module_t *module)
submodule->pp_shortcuts[0] = module->pp_shortcuts[0]; /* object name */
submodule->i_shortcuts = 1;
submodule->psz_object_name = strdup( module->psz_object_name );
submodule->object_name = strdup (module->object_name);
submodule->psz_shortname = module->psz_shortname;
submodule->psz_longname = module->psz_longname;
submodule->psz_capability = module->psz_capability;
......@@ -223,13 +221,14 @@ int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
case VLC_MODULE_NAME:
{
const char *value = va_arg (ap, const char *);
free( module->psz_object_name );
module->psz_object_name = strdup( value );
assert (module->object_name == NULL);
module->object_name = strdup (value);
module->pp_shortcuts = malloc( sizeof( char ** ) );
module->pp_shortcuts[0] = (char*)value; /* dooh! */
module->i_shortcuts = 1;
if (module->psz_longname == default_name)
if (module->psz_longname == NULL)
module->psz_longname = (char*)value; /* dooh! */
break;
}
......
......@@ -203,7 +203,9 @@ bool module_provides( const module_t *m, const char *cap )
*/
const char *module_get_object( const module_t *m )
{
return m->psz_object_name;
if (unlikely(m->object_name == NULL))
return "unnamed";
return m->object_name;
}
/**
......@@ -218,7 +220,9 @@ const char *module_get_name( const module_t *m, bool long_name )
if( long_name && ( m->psz_longname != NULL) )
return m->psz_longname;
return m->psz_shortname ? m->psz_shortname : m->psz_object_name;
if (m->psz_shortname != NULL)
return m->psz_shortname;
return module_get_object (m);
}
/**
......@@ -587,9 +591,9 @@ found_shortcut:
if( p_module != NULL )
{
msg_Dbg( p_this, "using %s module \"%s\"",
psz_capability, p_module->psz_object_name );
psz_capability, module_get_object(p_module) );
vlc_object_set_name( p_this, psz_alias ? psz_alias
: p_module->psz_object_name );
: module_get_object(p_module) );
}
else if( count == 0 )
msg_Dbg( p_this, "no %s module matched \"%s\"",
......@@ -655,29 +659,32 @@ module_t *module_need(vlc_object_t *obj, const char *cap, const char *name,
#undef module_unneed
void module_unneed(vlc_object_t *obj, module_t *module)
{
msg_Dbg(obj, "removing module \"%s\"", module->psz_object_name);
msg_Dbg(obj, "removing module \"%s\"", module_get_object(module));
vlc_module_unload(module, generic_stop, obj);
}
/**
* Get a pointer to a module_t given it's name.
*
* \param psz_name the name of the module
* \param name the name of the module
* \return a pointer to the module or NULL in case of a failure
*/
module_t *module_find( const char * psz_name )
module_t *module_find (const char *name)
{
module_t **list, *module;
assert (name != NULL);
list = module_list_get (NULL);
if (!list)
return NULL;
for (size_t i = 0; (module = list[i]) != NULL; i++)
{
const char *psz_module_name = module->psz_object_name;
const char *objname = module->object_name;
if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
if (unlikely(objname == NULL))
continue;
if (!strcmp (objname, name))
{
module_hold (module);
break;
......@@ -951,8 +958,6 @@ static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
{
module_t * p_module = NULL;
/* msg_Dbg( p_this, "plugin \"%s\", %s",
p_module->psz_object_name, p_module->psz_longname ); */
/* Check our plugins cache first then load plugin if needed */
if( mode == CACHE_USE )
p_module = CacheFind (p_bank->loaded_cache, p_bank->i_loaded_cache,
......@@ -1128,7 +1133,7 @@ static int AllocateBuiltinModule( vlc_object_t * p_this,
if( p_module == NULL )
return -1;
/* Initialize the module : fill p_module->psz_object_name, etc. */
/* Initialize the module : fill *p_module structure */
if( pf_entry( p_module ) != 0 )
{
/* With a well-written module we shouldn't have to print an
......@@ -1145,9 +1150,6 @@ static int AllocateBuiltinModule( vlc_object_t * p_this,
modules.head = p_module;
/* UNLOCK */
/* msg_Dbg( p_this, "builtin \"%s\", %s",
p_module->psz_object_name, p_module->psz_longname ); */
return 0;
}
......
......@@ -62,7 +62,7 @@ typedef void * module_handle_t;
*/
struct module_t
{
char *psz_object_name;
char *object_name;
gc_object_t vlc_gc_data;
module_t *next;
......
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