mirror of
https://gitlab.freedesktop.org/upower/upower.git
synced 2026-05-09 03:38:05 +02:00
trivial: add to_sting and from_string methods to DkpStatsObj
This commit is contained in:
parent
28f542de99
commit
6557463ba2
2 changed files with 43 additions and 0 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue