mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-20 15:48:54 +02:00
Add api to get and set doubles and lists of doubles. (#164719, Maurizio
2006-04-18 Matthias Clasen <mclasen@redhat.com> * glib/gkeyfile.h: * glib/glib.symbols: * glib/gkeyfile.c: Add api to get and set doubles and lists of doubles. (#164719, Maurizio Monge, Dom Lachowicz) * tests/keyfile-test.c: Add tests for new api.
This commit is contained in:
committed by
Matthias Clasen
parent
64434acfe1
commit
22ec64756c
233
glib/gkeyfile.c
233
glib/gkeyfile.c
@@ -152,6 +152,9 @@ static gint g_key_file_parse_value_as_integer (GKeyFile
|
||||
GError **error);
|
||||
static gchar *g_key_file_parse_integer_as_value (GKeyFile *key_file,
|
||||
gint value);
|
||||
static gdouble g_key_file_parse_value_as_double (GKeyFile *key_file,
|
||||
const gchar *value,
|
||||
GError **error);
|
||||
static gboolean g_key_file_parse_value_as_boolean (GKeyFile *key_file,
|
||||
const gchar *value,
|
||||
GError **error);
|
||||
@@ -2153,6 +2156,218 @@ g_key_file_set_integer_list (GKeyFile *key_file,
|
||||
g_string_free (values, TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_key_file_get_double:
|
||||
* @key_file: a #GKeyFile
|
||||
* @group_name: a group name
|
||||
* @key: a key
|
||||
* @error: return location for a #GError
|
||||
*
|
||||
* Returns the value associated with @key under @group_name as an
|
||||
* integer. If @group_name is %NULL, the start_group is used.
|
||||
*
|
||||
* If @key cannot be found then the return value is undefined and
|
||||
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
|
||||
* the value associated with @key cannot be interpreted as a double
|
||||
* then the return value is also undefined and @error is set to
|
||||
* #G_KEY_FILE_ERROR_INVALID_VALUE.
|
||||
*
|
||||
* Return value: the value associated with the key as a double.
|
||||
*
|
||||
* Since: 2.12
|
||||
**/
|
||||
gdouble
|
||||
g_key_file_get_double (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
GError **error)
|
||||
{
|
||||
GError *key_file_error;
|
||||
gchar *value;
|
||||
gdouble double_value;
|
||||
|
||||
g_return_val_if_fail (key_file != NULL, -1);
|
||||
g_return_val_if_fail (group_name != NULL, -1);
|
||||
g_return_val_if_fail (key != NULL, -1);
|
||||
|
||||
key_file_error = NULL;
|
||||
|
||||
value = g_key_file_get_value (key_file, group_name, key, &key_file_error);
|
||||
|
||||
if (key_file_error)
|
||||
{
|
||||
g_propagate_error (error, key_file_error);
|
||||
return 0;
|
||||
}
|
||||
|
||||
double_value = g_key_file_parse_value_as_double (key_file, value,
|
||||
&key_file_error);
|
||||
g_free (value);
|
||||
|
||||
if (key_file_error)
|
||||
{
|
||||
if (g_error_matches (key_file_error,
|
||||
G_KEY_FILE_ERROR,
|
||||
G_KEY_FILE_ERROR_INVALID_VALUE))
|
||||
{
|
||||
g_set_error (error, G_KEY_FILE_ERROR,
|
||||
G_KEY_FILE_ERROR_INVALID_VALUE,
|
||||
_("Key file contains key '%s' in group '%s' "
|
||||
"which has value that cannot be interpreted."), key,
|
||||
group_name);
|
||||
g_error_free (key_file_error);
|
||||
}
|
||||
else
|
||||
g_propagate_error (error, key_file_error);
|
||||
}
|
||||
|
||||
return double_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_key_file_set_double:
|
||||
* @key_file: a #GKeyFile
|
||||
* @group_name: a group name
|
||||
* @key: a key
|
||||
* @value: an double value
|
||||
*
|
||||
* Associates a new double value with @key under @group_name.
|
||||
* If @key cannot be found then it is created. If @group_name
|
||||
* is %NULL, the start group is used.
|
||||
*
|
||||
* Since: 2.12
|
||||
**/
|
||||
void
|
||||
g_key_file_set_double (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gdouble value)
|
||||
{
|
||||
gchar result[G_ASCII_DTOSTR_BUF_SIZE];
|
||||
|
||||
g_return_if_fail (key_file != NULL);
|
||||
g_return_if_fail (group_name != NULL);
|
||||
g_return_if_fail (key != NULL);
|
||||
|
||||
g_ascii_dtostr ( result, sizeof (result), value );
|
||||
g_key_file_set_value (key_file, group_name, key, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_key_file_get_double_list:
|
||||
* @key_file: a #GKeyFile
|
||||
* @group_name: a group name
|
||||
* @key: a key
|
||||
* @length: the number of doubles returned
|
||||
* @error: return location for a #GError
|
||||
*
|
||||
* Returns the values associated with @key under @group_name as
|
||||
* doubles. If @group_name is %NULL, the start group is used.
|
||||
*
|
||||
* If @key cannot be found then the return value is undefined and
|
||||
* @error is set to #G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if
|
||||
* the values associated with @key cannot be interpreted as doubles
|
||||
* then the return value is also undefined and @error is set to
|
||||
* #G_KEY_FILE_ERROR_INVALID_VALUE.
|
||||
*
|
||||
* Return value: the values associated with the key as a double
|
||||
*
|
||||
* Since: 2.12
|
||||
**/
|
||||
gdouble *
|
||||
g_key_file_get_double_list (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gsize *length,
|
||||
GError **error)
|
||||
{
|
||||
GError *key_file_error = NULL;
|
||||
gchar **values;
|
||||
gdouble *double_values;
|
||||
gsize i, num_doubles;
|
||||
|
||||
g_return_val_if_fail (key_file != NULL, NULL);
|
||||
g_return_val_if_fail (group_name != NULL, NULL);
|
||||
g_return_val_if_fail (key != NULL, NULL);
|
||||
|
||||
values = g_key_file_get_string_list (key_file, group_name, key,
|
||||
&num_doubles, &key_file_error);
|
||||
|
||||
if (key_file_error)
|
||||
g_propagate_error (error, key_file_error);
|
||||
|
||||
if (!values)
|
||||
return NULL;
|
||||
|
||||
double_values = g_new0 (gdouble, num_doubles);
|
||||
|
||||
for (i = 0; i < num_doubles; i++)
|
||||
{
|
||||
double_values[i] = g_key_file_parse_value_as_double (key_file,
|
||||
values[i],
|
||||
&key_file_error);
|
||||
|
||||
if (key_file_error)
|
||||
{
|
||||
g_propagate_error (error, key_file_error);
|
||||
g_strfreev (values);
|
||||
g_free (double_values);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
g_strfreev (values);
|
||||
|
||||
if (length)
|
||||
*length = num_doubles;
|
||||
|
||||
return double_values;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_key_file_set_double_list:
|
||||
* @key_file: a #GKeyFile
|
||||
* @group_name: a group name
|
||||
* @key: a key
|
||||
* @list: an array of double values
|
||||
* @length: number of double values in @list
|
||||
*
|
||||
* Associates a list of double values with @key under
|
||||
* @group_name. If @key cannot be found then it is created.
|
||||
* If @group_name is %NULL the start group is used.
|
||||
*
|
||||
* Since: 2.21
|
||||
**/
|
||||
void
|
||||
g_key_file_set_double_list (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gdouble list[],
|
||||
gsize length)
|
||||
{
|
||||
GString *values;
|
||||
gsize i;
|
||||
|
||||
g_return_if_fail (key_file != NULL);
|
||||
g_return_if_fail (group_name != NULL);
|
||||
g_return_if_fail (key != NULL);
|
||||
g_return_if_fail (list != NULL);
|
||||
|
||||
values = g_string_sized_new (length * 16);
|
||||
for (i = 0; i < length; i++)
|
||||
{
|
||||
gchar result[G_ASCII_DTOSTR_BUF_SIZE];
|
||||
|
||||
g_ascii_dtostr( result, sizeof (result), list[i] );
|
||||
|
||||
g_string_append (values, result);
|
||||
g_string_append_c (values, ';');
|
||||
}
|
||||
|
||||
g_key_file_set_value (key_file, group_name, key, values->str);
|
||||
g_string_free (values, TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
g_key_file_set_key_comment (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
@@ -3249,6 +3464,24 @@ g_key_file_parse_integer_as_value (GKeyFile *key_file,
|
||||
return g_strdup_printf ("%d", value);
|
||||
}
|
||||
|
||||
static gdouble
|
||||
g_key_file_parse_value_as_double (GKeyFile *key_file,
|
||||
const gchar *value,
|
||||
GError **error)
|
||||
{
|
||||
gchar *end_of_valid_d;
|
||||
gdouble double_value = 0;
|
||||
|
||||
double_value = g_ascii_strtod (value, &end_of_valid_d);
|
||||
|
||||
if (*end_of_valid_d != '\0')
|
||||
g_set_error (error, G_KEY_FILE_ERROR,
|
||||
G_KEY_FILE_ERROR_INVALID_VALUE,
|
||||
_("Value '%s' cannot be interpreted as a float number."), value);
|
||||
|
||||
return double_value;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
g_key_file_parse_value_as_boolean (GKeyFile *key_file,
|
||||
const gchar *value,
|
||||
|
@@ -126,6 +126,14 @@ void g_key_file_set_integer (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gint value);
|
||||
gdouble g_key_file_get_double (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
GError **error);
|
||||
void g_key_file_set_double (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gdouble value);
|
||||
gchar **g_key_file_get_string_list (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
@@ -163,6 +171,16 @@ gint *g_key_file_get_integer_list (GKeyFile *key_file,
|
||||
const gchar *key,
|
||||
gsize *length,
|
||||
GError **error) G_GNUC_MALLOC;
|
||||
void g_key_file_set_double_list (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gdouble list[],
|
||||
gsize length);
|
||||
gdouble *g_key_file_get_double_list (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gsize *length,
|
||||
GError **error) G_GNUC_MALLOC;
|
||||
void g_key_file_set_integer_list (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
|
@@ -475,6 +475,8 @@ g_key_file_get_boolean
|
||||
g_key_file_get_boolean_list G_GNUC_MALLOC
|
||||
g_key_file_get_comment G_GNUC_MALLOC
|
||||
g_key_file_get_groups G_GNUC_MALLOC
|
||||
g_key_file_get_double
|
||||
g_key_file_get_double_list G_GNUC_MALLOC
|
||||
g_key_file_get_integer
|
||||
g_key_file_get_integer_list G_GNUC_MALLOC
|
||||
g_key_file_get_keys G_GNUC_MALLOC
|
||||
@@ -496,6 +498,8 @@ g_key_file_remove_key
|
||||
g_key_file_set_boolean
|
||||
g_key_file_set_boolean_list
|
||||
g_key_file_set_comment
|
||||
g_key_file_set_double
|
||||
g_key_file_set_double_list
|
||||
g_key_file_set_integer
|
||||
g_key_file_set_integer_list
|
||||
g_key_file_set_list_separator
|
||||
|
Reference in New Issue
Block a user