diff options
author | Richard Hughes <richard@hughsie.com> | 2008-09-03 10:22:10 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2008-09-03 10:22:10 +0100 |
commit | 6557463ba25dd51630bd09e2f7c30186e296bb9e (patch) | |
tree | f72f1c0c7fffec30944a5ec8fe711f1cb694fe22 | |
parent | 28f542de990b18f0eb9df1780baf42c2d0607041 (diff) |
trivial: add to_sting and from_string methods to DkpStatsObj
-rw-r--r-- | libdevkit-power/dkp-stats-obj.c | 41 | ||||
-rw-r--r-- | libdevkit-power/dkp-stats-obj.h | 2 |
2 files changed, 43 insertions, 0 deletions
diff --git a/libdevkit-power/dkp-stats-obj.c b/libdevkit-power/dkp-stats-obj.c index fcc70ca..9b76a84 100644 --- a/libdevkit-power/dkp-stats-obj.c +++ b/libdevkit-power/dkp-stats-obj.c @@ -77,3 +77,44 @@ dkp_stats_obj_create (gdouble value, gdouble state) return obj; } +/** + * dkp_stats_obj_from_string: + **/ +DkpStatsObj * +dkp_stats_obj_from_string (const gchar *text) +{ + DkpStatsObj *obj = NULL; + gchar **parts = NULL; + guint length; + + if (text == NULL) + goto out; + + /* split by tab */ + parts = g_strsplit (text, "\t", 0); + length = g_strv_length (parts); + if (length != 2) { + egg_warning ("invalid string: '%s'", text); + goto out; + } + + /* parse and create */ + obj = dkp_stats_obj_new (); + obj->value = atoi (parts[0]); + obj->accuracy = atof (parts[1]); +out: + g_strfreev (parts); + return obj; +} + +/** + * dkp_stats_obj_to_string: + **/ +gchar * +dkp_stats_obj_to_string (const DkpStatsObj *obj) +{ + if (obj == NULL) + return NULL; + return g_strdup_printf ("%.2f\t%.2f", obj->value, obj->accuracy); +} + diff --git a/libdevkit-power/dkp-stats-obj.h b/libdevkit-power/dkp-stats-obj.h index 2334f73..42ba951 100644 --- a/libdevkit-power/dkp-stats-obj.h +++ b/libdevkit-power/dkp-stats-obj.h @@ -37,6 +37,8 @@ gboolean dkp_stats_obj_free (DkpStatsObj *obj); DkpStatsObj *dkp_stats_obj_copy (const DkpStatsObj *cobj); DkpStatsObj *dkp_stats_obj_create (gdouble value, gdouble state); +DkpStatsObj *dkp_stats_obj_from_string (const gchar *text); +gchar *dkp_stats_obj_to_string (const DkpStatsObj *obj); G_END_DECLS |