Merge branch 'is_modified' into 'master'

config: add is-modified helper

See merge request videolan/vlc!1365
This commit is contained in:
Lyndon Brown 2024-04-28 07:11:03 +00:00
commit 1e76e4966f
3 changed files with 52 additions and 0 deletions

View File

@ -251,6 +251,26 @@ VLC_API void config_ResetAll(void);
*/
VLC_API module_config_t *config_FindConfig(const char *name) VLC_USED;
/**
* Check item's modified state.
*
* This function checks whether or not an item's saved state is considered to
* be default or modified. Currently we consider state to be modified only if
* the value is different from it's default value.
*
* \warning The check is performed without locking. Callers are responsibile for
* ensuring thread safety. Note that whilst most item attributes remain constant
* throughout the lifetime of the core config database, the `value` attribute
* stands out as mutable and therefore is a thread safety concern. Safe use of
* this function thus requires either holding the config lock (applicable within
* the core only), or otherwise having suitably exclusive access to the item's
* value.
*
* \param item Configuration item
* \retval true if modified, false if unmodified (default).
*/
VLC_API bool vlc_config_ItemIsModified(const module_config_t *item);
/**
* System directory identifiers
*/

View File

@ -470,6 +470,37 @@ module_config_t *config_FindConfig(const char *name)
return (param != NULL) ? &param->item : NULL;
}
bool vlc_config_ItemIsModified(const module_config_t *item)
{
bool is_modified;
switch (CONFIG_CLASS(item->i_type))
{
case CONFIG_ITEM_FLOAT:
is_modified = (item->value.f != item->orig.f);
break;
case CONFIG_ITEM_BOOL:
case CONFIG_ITEM_INTEGER:
is_modified = (item->value.i != item->orig.i);
break;
case CONFIG_ITEM_STRING:
{
bool orig_is_empty = (item->orig.psz == NULL || item->orig.psz[0] == '\0');
bool curr_is_empty = (item->value.psz == NULL || item->value.psz[0] == '\0');
if (orig_is_empty)
is_modified = !curr_is_empty;
else if (curr_is_empty)
is_modified = true;
else
is_modified = (strcmp(item->value.psz, item->orig.psz) != 0);
break;
}
default:
vlc_assert_unreachable();
break;
}
return is_modified;
}
/**
* Destroys an array of configuration items.
* \param tab start of array of items

View File

@ -74,6 +74,7 @@ config_GetIntChoices
config_GetPsz
config_GetPszChoices
config_GetType
vlc_config_ItemIsModified
config_PutFloat
config_PutInt
config_PutPsz