[Libosinfo] [libosinfo PATCH 16/18] os: Add add_entity_to_list_check()

Fabiano Fidêncio fidencio at redhat.com
Thu Nov 8 06:55:35 UTC 2018


The addition of this new function is basically preparing the ground for
the changes that are coming in the next commits where we'll need to do
the very same check in both _get_device_links_internal() and
_get_devices_internal(). With that in mind, let's avoid code
duplication and have a single function that can be used in both methods.

https://gitlab.com/libosinfo/osinfo-db/issues/13

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 osinfo/osinfo_os.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index 26fbab4..8762f1a 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -204,6 +204,21 @@ OsinfoOs *osinfo_os_new(const gchar *id)
 }
 
 
+static gboolean
+add_entity_to_list_check(OsinfoEntity *ent1, /* OsinfoDeviceLink */
+                         OsinfoEntity *ent2, /* OsinfoDevice or OsinfoDevice Link */
+                         OsinfoFilter *filter,
+                         gboolean include_removed)
+{
+    gboolean ret = FALSE;
+
+    if (filter == NULL || osinfo_filter_matches(filter, ent2))
+        ret = TRUE;
+
+    return ret;
+}
+
+
 static OsinfoDeviceList *
 osinfo_os_get_devices_internal(OsinfoOs *os,
                                OsinfoFilter *filter,
@@ -220,7 +235,9 @@ osinfo_os_get_devices_internal(OsinfoOs *os,
     while (tmp) {
         OsinfoDeviceLink *devlink = OSINFO_DEVICELINK(tmp->data);
         OsinfoDevice *dev = osinfo_devicelink_get_target(devlink);
-        if (!filter || osinfo_filter_matches(filter, OSINFO_ENTITY(dev)))
+
+        if (add_entity_to_list_check
+                (OSINFO_ENTITY(devlink), OSINFO_ENTITY(dev), filter, include_removed))
             osinfo_list_add(OSINFO_LIST(newList), OSINFO_ENTITY(dev));
 
         tmp = tmp->next;
@@ -345,7 +362,8 @@ osinfo_os_get_device_links_internal(OsinfoOs *os,
     while (tmp) {
         OsinfoDeviceLink *devlink = OSINFO_DEVICELINK(tmp->data);
 
-        if (!filter || osinfo_filter_matches(filter, OSINFO_ENTITY(devlink)))
+        if (add_entity_to_list_check
+                (OSINFO_ENTITY(devlink), OSINFO_ENTITY(devlink), filter, include_removed))
             osinfo_list_add(OSINFO_LIST(newList), OSINFO_ENTITY(devlink));
 
         tmp = tmp->next;
-- 
2.19.1




More information about the Libosinfo mailing list