mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-27 07:56:14 +01:00
Support 64bit integers in GKeyFile
This commit is contained in:
parent
08b5f86697
commit
6bea235c8b
@ -1609,6 +1609,8 @@ g_key_file_get_string
|
||||
g_key_file_get_locale_string
|
||||
g_key_file_get_boolean
|
||||
g_key_file_get_integer
|
||||
g_key_file_get_int64
|
||||
g_key_file_get_uint64
|
||||
g_key_file_get_double
|
||||
g_key_file_get_string_list
|
||||
g_key_file_get_locale_string_list
|
||||
@ -1623,6 +1625,8 @@ g_key_file_set_string
|
||||
g_key_file_set_locale_string
|
||||
g_key_file_set_boolean
|
||||
g_key_file_set_integer
|
||||
g_key_file_set_int64
|
||||
g_key_file_set_uint64
|
||||
g_key_file_set_double
|
||||
g_key_file_set_string_list
|
||||
g_key_file_set_locale_string_list
|
||||
|
152
glib/gkeyfile.c
152
glib/gkeyfile.c
@ -1,6 +1,8 @@
|
||||
/* gkeyfile.c - key file parser
|
||||
*
|
||||
* Copyright 2004 Red Hat, Inc.
|
||||
* Copyright 2009-2010 Collabora Ltd.
|
||||
* Copyright 2009 Nokia Corporation
|
||||
*
|
||||
* Written by Ray Strode <rstrode@redhat.com>
|
||||
* Matthias Clasen <mclasen@redhat.com>
|
||||
@ -2160,6 +2162,156 @@ g_key_file_set_integer (GKeyFile *key_file,
|
||||
g_free (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_key_file_get_int64:
|
||||
* @key_file: a non-%NULL #GKeyFile
|
||||
* @group_name: a non-%NULL group name
|
||||
* @key: a non-%NULL key
|
||||
* @error: return location for a #GError
|
||||
*
|
||||
* Returns the value associated with @key under @group_name as a signed
|
||||
* 64-bit integer. This is similar to g_key_file_get_integer() but can return
|
||||
* 64-bit results without truncation.
|
||||
*
|
||||
* Returns: the value associated with the key as a signed 64-bit integer, or
|
||||
* 0 if the key was not found or could not be parsed.
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
gint64
|
||||
g_key_file_get_int64 (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
GError **error)
|
||||
{
|
||||
gchar *s, *end;
|
||||
gint64 v;
|
||||
|
||||
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);
|
||||
|
||||
s = g_key_file_get_value (key_file, group_name, key, error);
|
||||
|
||||
if (s == NULL)
|
||||
return 0;
|
||||
|
||||
v = g_ascii_strtoll (s, &end, 10);
|
||||
|
||||
if (*s == '\0' || *end != '\0')
|
||||
{
|
||||
g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
|
||||
"Key '%s' in group '%s' has value '%s' where int64 was expected",
|
||||
key, group_name, s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
g_free (s);
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_key_file_set_int64:
|
||||
* @key_file: a #GKeyFile
|
||||
* @group_name: a group name
|
||||
* @key: a key
|
||||
* @value: an integer value
|
||||
*
|
||||
* Associates a new integer value with @key under @group_name.
|
||||
* If @key cannot be found then it is created.
|
||||
*
|
||||
* Since: 2.26
|
||||
**/
|
||||
void
|
||||
g_key_file_set_int64 (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gint64 value)
|
||||
{
|
||||
gchar *result;
|
||||
|
||||
g_return_if_fail (key_file != NULL);
|
||||
|
||||
result = g_strdup_printf ("%" G_GINT64_FORMAT, value);
|
||||
g_key_file_set_value (key_file, group_name, key, result);
|
||||
g_free (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_key_file_get_uint64:
|
||||
* @key_file: a non-%NULL #GKeyFile
|
||||
* @group_name: a non-%NULL group name
|
||||
* @key: a non-%NULL key
|
||||
* @error: return location for a #GError
|
||||
*
|
||||
* Returns the value associated with @key under @group_name as an unsigned
|
||||
* 64-bit integer. This is similar to g_key_file_get_integer() but can return
|
||||
* large positive results without truncation.
|
||||
*
|
||||
* Returns: the value associated with the key as an unsigned 64-bit integer,
|
||||
* or 0 if the key was not found or could not be parsed.
|
||||
*
|
||||
* Since: 2.26
|
||||
*/
|
||||
guint64
|
||||
g_key_file_get_uint64 (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
GError **error)
|
||||
{
|
||||
gchar *s, *end;
|
||||
guint64 v;
|
||||
|
||||
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);
|
||||
|
||||
s = g_key_file_get_value (key_file, group_name, key, error);
|
||||
|
||||
if (s == NULL)
|
||||
return 0;
|
||||
|
||||
v = g_ascii_strtoull (s, &end, 10);
|
||||
|
||||
if (*s == '\0' || *end != '\0')
|
||||
{
|
||||
g_set_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE,
|
||||
"Key '%s' in group '%s' has value '%s' where uint64 was expected",
|
||||
key, group_name, s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
g_free (s);
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_key_file_set_uint64:
|
||||
* @key_file: a #GKeyFile
|
||||
* @group_name: a group name
|
||||
* @key: a key
|
||||
* @value: an integer value
|
||||
*
|
||||
* Associates a new integer value with @key under @group_name.
|
||||
* If @key cannot be found then it is created.
|
||||
*
|
||||
* Since: 2.26
|
||||
**/
|
||||
void
|
||||
g_key_file_set_uint64 (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
guint64 value)
|
||||
{
|
||||
gchar *result;
|
||||
|
||||
g_return_if_fail (key_file != NULL);
|
||||
|
||||
result = g_strdup_printf ("%" G_GUINT64_FORMAT, value);
|
||||
g_key_file_set_value (key_file, group_name, key, result);
|
||||
g_free (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_key_file_get_integer_list:
|
||||
* @key_file: a #GKeyFile
|
||||
|
@ -136,6 +136,22 @@ void g_key_file_set_integer (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gint value);
|
||||
gint64 g_key_file_get_int64 (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
GError **error);
|
||||
void g_key_file_set_int64 (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
gint64 value);
|
||||
guint64 g_key_file_get_uint64 (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
GError **error);
|
||||
void g_key_file_set_uint64 (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
guint64 value);
|
||||
gdouble g_key_file_get_double (GKeyFile *key_file,
|
||||
const gchar *group_name,
|
||||
const gchar *key,
|
||||
|
@ -533,6 +533,8 @@ 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_int64
|
||||
g_key_file_get_uint64
|
||||
g_key_file_get_integer_list G_GNUC_MALLOC
|
||||
g_key_file_get_keys G_GNUC_MALLOC
|
||||
g_key_file_get_locale_string G_GNUC_MALLOC
|
||||
@ -557,6 +559,8 @@ 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_int64
|
||||
g_key_file_set_uint64
|
||||
g_key_file_set_integer_list
|
||||
g_key_file_set_list_separator
|
||||
g_key_file_set_locale_string
|
||||
|
@ -1233,6 +1233,51 @@ test_reload_idempotency (void)
|
||||
g_free (data1);
|
||||
}
|
||||
|
||||
static const char int64_data[] =
|
||||
"[bees]\n"
|
||||
"a=1\n"
|
||||
"b=2\n"
|
||||
"c=123456789123456789\n"
|
||||
"d=-123456789123456789\n";
|
||||
|
||||
static void
|
||||
test_int64 (void)
|
||||
{
|
||||
GKeyFile *file;
|
||||
gboolean ok;
|
||||
guint64 c;
|
||||
gint64 d;
|
||||
gchar *value;
|
||||
|
||||
g_test_bug ("614864");
|
||||
|
||||
file = g_key_file_new ();
|
||||
|
||||
ok = g_key_file_load_from_data (file, int64_data, strlen (int64_data),
|
||||
0, NULL);
|
||||
g_assert (ok);
|
||||
|
||||
c = g_key_file_get_uint64 (file, "bees", "c", NULL);
|
||||
g_assert (c == G_GUINT64_CONSTANT (123456789123456789));
|
||||
|
||||
d = g_key_file_get_int64 (file, "bees", "d", NULL);
|
||||
g_assert (d == G_GINT64_CONSTANT (-123456789123456789));
|
||||
|
||||
g_key_file_set_uint64 (file, "bees", "c",
|
||||
G_GUINT64_CONSTANT (987654321987654321));
|
||||
value = g_key_file_get_value (file, "bees", "c", NULL);
|
||||
g_assert_cmpstr (value, ==, "987654321987654321");
|
||||
g_free (value);
|
||||
|
||||
g_key_file_set_int64 (file, "bees", "d",
|
||||
G_GINT64_CONSTANT (-987654321987654321));
|
||||
value = g_key_file_get_value (file, "bees", "d", NULL);
|
||||
g_assert_cmpstr (value, ==, "-987654321987654321");
|
||||
g_free (value);
|
||||
|
||||
g_key_file_free (file);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
@ -1258,6 +1303,7 @@ main (int argc, char *argv[])
|
||||
g_test_add_func ("/keyfile/group-names", test_group_names);
|
||||
g_test_add_func ("/keyfile/key-names", test_key_names);
|
||||
g_test_add_func ("/keyfile/reload", test_reload_idempotency);
|
||||
g_test_add_func ("/keyfile/int64", test_int64);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user