mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-27 22:46:15 +01:00
g_key_file_has_key_full: New function to fix g_key_file_has_key()'s GError semantics
See https://bugzilla.gnome.org/show_bug.cgi?id=649657 for discussion of why it's bad for bindings for gerror return values to both signal errors and carry meaning. https://bugzilla.gnome.org/show_bug.cgi?id=650345
This commit is contained in:
parent
b539004d13
commit
9966fe4493
@ -1746,6 +1746,7 @@ g_key_file_get_groups
|
|||||||
g_key_file_get_keys
|
g_key_file_get_keys
|
||||||
g_key_file_has_group
|
g_key_file_has_group
|
||||||
g_key_file_has_key
|
g_key_file_has_key
|
||||||
|
g_key_file_has_key_full
|
||||||
|
|
||||||
<SUBSECTION>
|
<SUBSECTION>
|
||||||
g_key_file_get_value
|
g_key_file_get_value
|
||||||
|
@ -3130,7 +3130,7 @@ g_key_file_has_group (GKeyFile *key_file,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* g_key_file_has_key:
|
* g_key_file_has_key: (skip)
|
||||||
* @key_file: a #GKeyFile
|
* @key_file: a #GKeyFile
|
||||||
* @group_name: a group name
|
* @group_name: a group name
|
||||||
* @key: a key name
|
* @key: a key name
|
||||||
@ -3139,6 +3139,14 @@ g_key_file_has_group (GKeyFile *key_file,
|
|||||||
* Looks whether the key file has the key @key in the group
|
* Looks whether the key file has the key @key in the group
|
||||||
* @group_name.
|
* @group_name.
|
||||||
*
|
*
|
||||||
|
* <note>This function does not follow the rules for #GError strictly;
|
||||||
|
* the return value both carries meaning and signals an error. To use
|
||||||
|
* this function, you must pass a #GError pointer in @error, and check
|
||||||
|
* whether it is not %NULL to see if an error occurred.</note>
|
||||||
|
*
|
||||||
|
* See g_key_file_has_key_full() for a replacement function which does
|
||||||
|
* follow the #GError rules.
|
||||||
|
*
|
||||||
* Return value: %TRUE if @key is a part of @group_name, %FALSE
|
* Return value: %TRUE if @key is a part of @group_name, %FALSE
|
||||||
* otherwise.
|
* otherwise.
|
||||||
*
|
*
|
||||||
@ -3149,6 +3157,44 @@ g_key_file_has_key (GKeyFile *key_file,
|
|||||||
const gchar *group_name,
|
const gchar *group_name,
|
||||||
const gchar *key,
|
const gchar *key,
|
||||||
GError **error)
|
GError **error)
|
||||||
|
{
|
||||||
|
GError *temp_error = NULL;
|
||||||
|
gboolean has_key;
|
||||||
|
|
||||||
|
if (g_key_file_has_key_full (key_file, group_name, key, &has_key, &temp_error))
|
||||||
|
{
|
||||||
|
return has_key;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
g_propagate_error (error, temp_error);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_key_file_has_key_full:
|
||||||
|
* @key_file: a #GKeyFile
|
||||||
|
* @group_name: a group name
|
||||||
|
* @key: a key name
|
||||||
|
* @has_key: (out) (allow-none): Return location for whether or not key exists
|
||||||
|
* @error: return location for a #GError
|
||||||
|
*
|
||||||
|
* Looks whether the key file has the key @key in the group
|
||||||
|
* @group_name.
|
||||||
|
*
|
||||||
|
* Return value: %TRUE if a group with the name @group_name
|
||||||
|
* exists. Otherwise, @error is set and %FALSE is returned.
|
||||||
|
*
|
||||||
|
* Since: 2.30
|
||||||
|
* Rename to: g_key_file_has_key
|
||||||
|
**/
|
||||||
|
gboolean
|
||||||
|
g_key_file_has_key_full (GKeyFile *key_file,
|
||||||
|
const gchar *group_name,
|
||||||
|
const gchar *key,
|
||||||
|
gboolean *has_key,
|
||||||
|
GError **error)
|
||||||
{
|
{
|
||||||
GKeyFileKeyValuePair *pair;
|
GKeyFileKeyValuePair *pair;
|
||||||
GKeyFileGroup *group;
|
GKeyFileGroup *group;
|
||||||
@ -3171,7 +3217,9 @@ g_key_file_has_key (GKeyFile *key_file,
|
|||||||
|
|
||||||
pair = g_key_file_lookup_key_value_pair (key_file, group, key);
|
pair = g_key_file_lookup_key_value_pair (key_file, group, key);
|
||||||
|
|
||||||
return pair != NULL;
|
if (has_key)
|
||||||
|
*has_key = pair != NULL;
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -94,6 +94,11 @@ gboolean g_key_file_has_key (GKeyFile *key_file,
|
|||||||
const gchar *group_name,
|
const gchar *group_name,
|
||||||
const gchar *key,
|
const gchar *key,
|
||||||
GError **error);
|
GError **error);
|
||||||
|
gboolean g_key_file_has_key_full (GKeyFile *key_file,
|
||||||
|
const gchar *group_name,
|
||||||
|
const gchar *key,
|
||||||
|
gboolean *has_key,
|
||||||
|
GError **error);
|
||||||
gchar *g_key_file_get_value (GKeyFile *key_file,
|
gchar *g_key_file_get_value (GKeyFile *key_file,
|
||||||
const gchar *group_name,
|
const gchar *group_name,
|
||||||
const gchar *key,
|
const gchar *key,
|
||||||
|
@ -611,6 +611,7 @@ g_key_file_get_string_list G_GNUC_MALLOC
|
|||||||
g_key_file_get_value G_GNUC_MALLOC
|
g_key_file_get_value G_GNUC_MALLOC
|
||||||
g_key_file_has_group
|
g_key_file_has_group
|
||||||
g_key_file_has_key
|
g_key_file_has_key
|
||||||
|
g_key_file_has_key_full
|
||||||
g_key_file_load_from_dirs
|
g_key_file_load_from_dirs
|
||||||
g_key_file_load_from_data
|
g_key_file_load_from_data
|
||||||
g_key_file_load_from_data_dirs
|
g_key_file_load_from_data_dirs
|
||||||
|
@ -444,6 +444,7 @@ test_listing (void)
|
|||||||
gsize len;
|
gsize len;
|
||||||
gchar *start;
|
gchar *start;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
|
gboolean has_key;
|
||||||
|
|
||||||
const gchar *data =
|
const gchar *data =
|
||||||
"[group1]\n"
|
"[group1]\n"
|
||||||
@ -496,6 +497,21 @@ test_listing (void)
|
|||||||
g_key_file_has_key (keyfile, "no-such-group", "key", &error);
|
g_key_file_has_key (keyfile, "no-such-group", "key", &error);
|
||||||
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
|
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
|
||||||
|
|
||||||
|
g_assert (g_key_file_has_key_full (keyfile, "group1", "key1", &has_key, &error));
|
||||||
|
check_no_error (&error);
|
||||||
|
g_assert (has_key);
|
||||||
|
|
||||||
|
g_assert (g_key_file_has_key_full (keyfile, "group2", "key3", &has_key, &error));
|
||||||
|
check_no_error (&error);
|
||||||
|
g_assert (has_key);
|
||||||
|
|
||||||
|
g_assert (g_key_file_has_key_full (keyfile, "group2", "no-such-key", &has_key, &error));
|
||||||
|
g_assert (!has_key);
|
||||||
|
check_no_error (&error);
|
||||||
|
|
||||||
|
g_assert (!g_key_file_has_key_full (keyfile, "no-such-group", "key", &has_key, &error));
|
||||||
|
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
|
||||||
|
|
||||||
g_key_file_free (keyfile);
|
g_key_file_free (keyfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user