trivial: add to_sting and from_string methods to DkpStatsObj

This commit is contained in:
Richard Hughes 2008-09-03 10:22:10 +01:00
parent 28f542de99
commit 6557463ba2
2 changed files with 43 additions and 0 deletions

View file

@ -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);
}

View file

@ -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