config_odev_add*_attribute: Don't add the attribute to the list twice

Don't do the xorg_list_append a 2nd time when updating existing attributes,
while at it add a helper function for this to avoid code duplication.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Hans de Goede 2014-03-03 17:00:57 +01:00
parent 2f2967173b
commit c6dea929a4

View File

@ -159,23 +159,38 @@ config_odev_find_attribute(struct OdevAttributes *attribs, int attrib_id)
return NULL; return NULL;
} }
static struct OdevAttribute *
config_odev_find_or_add_attribute(struct OdevAttributes *attribs, int attrib)
{
struct OdevAttribute *oa;
oa = config_odev_find_attribute(attribs, attrib);
if (oa)
return oa;
oa = calloc(1, sizeof(struct OdevAttribute));
if (!oa)
return oa;
oa->attrib_id = attrib;
xorg_list_append(&oa->member, &attribs->list);
return oa;
}
Bool Bool
config_odev_add_attribute(struct OdevAttributes *attribs, int attrib, config_odev_add_attribute(struct OdevAttributes *attribs, int attrib,
const char *attrib_name) const char *attrib_name)
{ {
struct OdevAttribute *oa; struct OdevAttribute *oa;
oa = config_odev_find_attribute(attribs, attrib); oa = config_odev_find_or_add_attribute(attribs, attrib);
if (!oa)
oa = calloc(1, sizeof(struct OdevAttribute));
if (!oa) if (!oa)
return FALSE; return FALSE;
oa->attrib_id = attrib;
free(oa->attrib_name); free(oa->attrib_name);
oa->attrib_name = strdup(attrib_name); oa->attrib_name = strdup(attrib_name);
oa->attrib_type = ODEV_ATTRIB_STRING; oa->attrib_type = ODEV_ATTRIB_STRING;
xorg_list_append(&oa->member, &attribs->list);
return TRUE; return TRUE;
} }
@ -185,16 +200,12 @@ config_odev_add_int_attribute(struct OdevAttributes *attribs, int attrib,
{ {
struct OdevAttribute *oa; struct OdevAttribute *oa;
oa = config_odev_find_attribute(attribs, attrib); oa = config_odev_find_or_add_attribute(attribs, attrib);
if (!oa)
oa = calloc(1, sizeof(struct OdevAttribute));
if (!oa) if (!oa)
return FALSE; return FALSE;
oa->attrib_id = attrib;
oa->attrib_value = attrib_value; oa->attrib_value = attrib_value;
oa->attrib_type = ODEV_ATTRIB_INT; oa->attrib_type = ODEV_ATTRIB_INT;
xorg_list_append(&oa->member, &attribs->list);
return TRUE; return TRUE;
} }