[virt-tools-list] [libosinfo] More type-specific entity value getters/setters

Zeeshan Ali (Khattak) zeeshanak at gnome.org
Tue Sep 25 14:37:57 UTC 2012


From: "Zeeshan Ali (Khattak)" <zeeshanak at gnome.org>

Add:

* osinfo_entity_get_param_value_boolean_with_default
* osinfo_entity_get_param_value_int64
* osinfo_entity_set_param_int64

These methods already existed as private functions in media and
resources subclasses. This patch puts them where they belong, exposes
them in public API and renames them appropriately.
---
 osinfo/libosinfo.syms     |  7 +++++++
 osinfo/osinfo_entity.c    | 40 ++++++++++++++++++++++++++++++++++++-
 osinfo/osinfo_entity.h    |  5 +++++
 osinfo/osinfo_media.c     | 20 ++++---------------
 osinfo/osinfo_resources.c | 51 +++++++++++++++++++----------------------------
 5 files changed, 75 insertions(+), 48 deletions(-)

diff --git a/osinfo/libosinfo.syms b/osinfo/libosinfo.syms
index 6060b1d..5f7bf21 100644
--- a/osinfo/libosinfo.syms
+++ b/osinfo/libosinfo.syms
@@ -301,6 +301,13 @@ LIBOSINFO_0.2.0 {
 	osinfo_os_find_install_script;
 } LIBOSINFO_0.1.0;
 
+LIBOSINFO_0.2.1 {
+    global:
+	osinfo_entity_get_param_value_boolean_with_default;
+	osinfo_entity_get_param_value_int64;
+	osinfo_entity_set_param_int64;
+} LIBOSINFO_0.2.0;
+
 /* Symbols in next release...
 
   LIBOSINFO_0.0.2 {
diff --git a/osinfo/osinfo_entity.c b/osinfo/osinfo_entity.c
index eafb4bd..21fa79d 100644
--- a/osinfo/osinfo_entity.c
+++ b/osinfo/osinfo_entity.c
@@ -201,6 +201,15 @@ void osinfo_entity_set_param_boolean(OsinfoEntity *entity, const gchar *key, gbo
     osinfo_entity_set_param(entity, key, value ? "true" : "false");
 }
 
+void osinfo_entity_set_param_int64(OsinfoEntity *entity, const gchar *key, gint64 value)
+{
+    gchar *str;
+
+    str = g_strdup_printf("%"G_GUINT64_FORMAT, value);
+    osinfo_entity_set_param(entity, key, str);
+    g_free(str);
+}
+
 /**
  * osinfo_entity_add_param:
  * @entity: OsinfoEntity containing the parameters
@@ -311,14 +320,43 @@ const gchar *osinfo_entity_get_param_value(OsinfoEntity *entity, const gchar *ke
     return NULL;
 }
 
+static gboolean str_to_bool(const char *str)
+{
+    return (g_strcmp0("true", str) == 0 || g_strcmp0("yes", str) == 0);
+}
 
 gboolean osinfo_entity_get_param_value_boolean(OsinfoEntity *entity, const gchar *key)
 {
     const gchar *value = osinfo_entity_get_param_value(entity, key);
 
-    return value && g_str_equal(value, "true");
+    return value && str_to_bool(value);
 }
 
+gboolean osinfo_entity_get_param_value_boolean_with_default(OsinfoEntity *entity,
+                                                            const char *key,
+                                                            gboolean default_value)
+{
+    const gchar *value;
+
+    value = osinfo_entity_get_param_value(entity, key);
+    if (value == NULL)
+        return default_value;
+    else
+        return str_to_bool(value);
+}
+
+gint64 osinfo_entity_get_param_value_int64(OsinfoEntity *entity,
+                                           const gchar *key)
+{
+    const gchar *str;
+
+    str = osinfo_entity_get_param_value(entity, key);
+
+    if (str == NULL)
+        return -1;
+
+    return (gint64) g_ascii_strtod(str, NULL);
+}
 
 /**
  * osinfo_entity_get_param_value_list:
diff --git a/osinfo/osinfo_entity.h b/osinfo/osinfo_entity.h
index 9a99704..e7704d6 100644
--- a/osinfo/osinfo_entity.h
+++ b/osinfo/osinfo_entity.h
@@ -71,9 +71,14 @@ const gchar *osinfo_entity_get_id(OsinfoEntity *entity);
 GList *osinfo_entity_get_param_keys(OsinfoEntity *entity);
 const gchar *osinfo_entity_get_param_value(OsinfoEntity *entity, const gchar *key);
 gboolean osinfo_entity_get_param_value_boolean(OsinfoEntity *entity, const gchar *key);
+gboolean osinfo_entity_get_param_value_boolean_with_default(OsinfoEntity *entity,
+                                                            const char *key,
+                                                            gboolean default_value);
+gint64 osinfo_entity_get_param_value_int64(OsinfoEntity *entity, const gchar *key);
 GList *osinfo_entity_get_param_value_list(OsinfoEntity *entity, const gchar *key);
 void osinfo_entity_set_param(OsinfoEntity *entity, const gchar *key, const gchar *value);
 void osinfo_entity_set_param_boolean(OsinfoEntity *entity, const gchar *key, gboolean value);
+void osinfo_entity_set_param_int64(OsinfoEntity *entity, const gchar *key, gint64 value);
 void osinfo_entity_add_param(OsinfoEntity *entity, const gchar *key, const gchar *value);
 void osinfo_entity_clear_param(OsinfoEntity *entity, const gchar *key);
 
diff --git a/osinfo/osinfo_media.c b/osinfo/osinfo_media.c
index 9cc3538..e03bfe0 100644
--- a/osinfo/osinfo_media.c
+++ b/osinfo/osinfo_media.c
@@ -988,20 +988,6 @@ const gchar *osinfo_media_get_initrd_path(OsinfoMedia *media)
                                          OSINFO_MEDIA_PROP_INITRD);
 }
 
-static gboolean get_param_as_bool (OsinfoMedia *media,
-                                   const char *key,
-                                   gboolean default_value)
-{
-    const gchar *value;
-
-    value = osinfo_entity_get_param_value(OSINFO_ENTITY(media), key);
-    if (value == NULL)
-        return default_value;
-
-    return (g_strcmp0 ("true", value) == 0 ||
-            g_strcmp0 ("yes", value) == 0);
-}
-
 /**
  * osinfo_media_get_installer:
  * @media: a #OsinfoMedia instance
@@ -1012,7 +998,8 @@ static gboolean get_param_as_bool (OsinfoMedia *media,
  */
 gboolean osinfo_media_get_installer(OsinfoMedia *media)
 {
-    return get_param_as_bool (media, OSINFO_MEDIA_PROP_INSTALLER, TRUE);
+    return osinfo_entity_get_param_value_boolean_with_default
+            (OSINFO_ENTITY(media), OSINFO_MEDIA_PROP_INSTALLER, TRUE);
 }
 
 /**
@@ -1025,7 +1012,8 @@ gboolean osinfo_media_get_installer(OsinfoMedia *media)
  */
 gboolean osinfo_media_get_live(OsinfoMedia *media)
 {
-    return get_param_as_bool (media, OSINFO_MEDIA_PROP_LIVE, FALSE);
+    return osinfo_entity_get_param_value_boolean_with_default
+            (OSINFO_ENTITY(media), OSINFO_MEDIA_PROP_LIVE, FALSE);
 }
 
 /*
diff --git a/osinfo/osinfo_resources.c b/osinfo/osinfo_resources.c
index 9f1e4e4..29f62bd 100644
--- a/osinfo/osinfo_resources.c
+++ b/osinfo/osinfo_resources.c
@@ -61,29 +61,6 @@ struct _OsinfoResourcesPrivate
     gboolean unused;
 };
 
-static gint64 get_param_as_int64(OsinfoResources *resources,
-                                 const gchar *key)
-{
-    const gchar *str;
-
-    str = osinfo_entity_get_param_value(OSINFO_ENTITY(resources), key);
-
-    if (str == NULL)
-        return -1;
-
-    return (gint64) g_ascii_strtod(str, NULL);
-}
-
-static void set_param_from_int64(OsinfoResources *resources,
-                                 const gchar *key,
-                                 gint64 value)
-{
-    gchar *str;
-
-    str = g_strdup_printf("%"G_GUINT64_FORMAT, value);
-    osinfo_entity_set_param(OSINFO_ENTITY(resources), key, str);
-}
-
 static void
 osinfo_resources_finalize (GObject *object)
 {
@@ -324,7 +301,8 @@ const gchar *osinfo_resources_get_architecture(OsinfoResources *resources)
  */
 gint osinfo_resources_get_n_cpus(OsinfoResources *resources)
 {
-    return (gint) get_param_as_int64(resources, OSINFO_RESOURCES_PROP_N_CPUS);
+    return (gint) osinfo_entity_get_param_value_int64
+            (OSINFO_ENTITY(resources), OSINFO_RESOURCES_PROP_N_CPUS);
 }
 
 /**
@@ -338,7 +316,8 @@ gint osinfo_resources_get_n_cpus(OsinfoResources *resources)
  */
 gint64 osinfo_resources_get_cpu(OsinfoResources *resources)
 {
-    return get_param_as_int64(resources, OSINFO_RESOURCES_PROP_CPU);
+    return osinfo_entity_get_param_value_int64
+            (OSINFO_ENTITY(resources), OSINFO_RESOURCES_PROP_CPU);
 }
 
 /**
@@ -352,7 +331,8 @@ gint64 osinfo_resources_get_cpu(OsinfoResources *resources)
  */
 gint64 osinfo_resources_get_ram(OsinfoResources *resources)
 {
-    return get_param_as_int64(resources, OSINFO_RESOURCES_PROP_RAM);
+    return osinfo_entity_get_param_value_int64
+            (OSINFO_ENTITY(resources), OSINFO_RESOURCES_PROP_RAM);
 }
 
 /**
@@ -366,7 +346,8 @@ gint64 osinfo_resources_get_ram(OsinfoResources *resources)
  */
 gint64 osinfo_resources_get_storage(OsinfoResources *resources)
 {
-    return get_param_as_int64(resources, OSINFO_RESOURCES_PROP_STORAGE);
+    return osinfo_entity_get_param_value_int64
+            (OSINFO_ENTITY(resources), OSINFO_RESOURCES_PROP_STORAGE);
 }
 
 /**
@@ -378,7 +359,9 @@ gint64 osinfo_resources_get_storage(OsinfoResources *resources)
  */
 void osinfo_resources_set_n_cpus(OsinfoResources *resources, gint n_cpus)
 {
-    set_param_from_int64 (resources, OSINFO_RESOURCES_PROP_N_CPUS, n_cpus);
+    osinfo_entity_set_param_int64(OSINFO_ENTITY(resources),
+                                  OSINFO_RESOURCES_PROP_N_CPUS,
+                                  n_cpus);
 }
 
 /**
@@ -390,7 +373,9 @@ void osinfo_resources_set_n_cpus(OsinfoResources *resources, gint n_cpus)
  */
 void osinfo_resources_set_cpu(OsinfoResources *resources, gint64 cpu)
 {
-    set_param_from_int64 (resources, OSINFO_RESOURCES_PROP_CPU, cpu);
+    osinfo_entity_set_param_int64(OSINFO_ENTITY(resources),
+                                  OSINFO_RESOURCES_PROP_CPU,
+                                  cpu);
 }
 
 /**
@@ -402,7 +387,9 @@ void osinfo_resources_set_cpu(OsinfoResources *resources, gint64 cpu)
  */
 void osinfo_resources_set_ram(OsinfoResources *resources, gint64 ram)
 {
-    set_param_from_int64 (resources, OSINFO_RESOURCES_PROP_RAM, ram);
+    osinfo_entity_set_param_int64(OSINFO_ENTITY(resources),
+                                  OSINFO_RESOURCES_PROP_RAM,
+                                  ram);
 }
 
 /**
@@ -414,7 +401,9 @@ void osinfo_resources_set_ram(OsinfoResources *resources, gint64 ram)
  */
 void osinfo_resources_set_storage(OsinfoResources *resources, gint64 storage)
 {
-    set_param_from_int64 (resources, OSINFO_RESOURCES_PROP_STORAGE, storage);
+    osinfo_entity_set_param_int64(OSINFO_ENTITY(resources),
+                                  OSINFO_RESOURCES_PROP_STORAGE,
+                                  storage);
 }
 
 /*
-- 
1.7.12




More information about the virt-tools-list mailing list