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:
Colin Walters 2011-05-16 15:30:31 -04:00
parent b539004d13
commit 9966fe4493
5 changed files with 74 additions and 3 deletions

View File

@ -1746,6 +1746,7 @@ g_key_file_get_groups
g_key_file_get_keys
g_key_file_has_group
g_key_file_has_key
g_key_file_has_key_full
<SUBSECTION>
g_key_file_get_value

View File

@ -3130,14 +3130,22 @@ g_key_file_has_group (GKeyFile *key_file,
}
/**
* g_key_file_has_key:
* g_key_file_has_key: (skip)
* @key_file: a #GKeyFile
* @group_name: a group name
* @key: a key name
* @error: return location for a #GError
*
* 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
* otherwise.
@ -3149,6 +3157,44 @@ g_key_file_has_key (GKeyFile *key_file,
const gchar *group_name,
const gchar *key,
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;
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);
return pair != NULL;
if (has_key)
*has_key = pair != NULL;
return TRUE;
}
static void

View File

@ -94,6 +94,11 @@ gboolean g_key_file_has_key (GKeyFile *key_file,
const gchar *group_name,
const gchar *key,
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,
const gchar *group_name,
const gchar *key,

View File

@ -611,6 +611,7 @@ g_key_file_get_string_list G_GNUC_MALLOC
g_key_file_get_value G_GNUC_MALLOC
g_key_file_has_group
g_key_file_has_key
g_key_file_has_key_full
g_key_file_load_from_dirs
g_key_file_load_from_data
g_key_file_load_from_data_dirs

View File

@ -444,6 +444,7 @@ test_listing (void)
gsize len;
gchar *start;
GError *error = NULL;
gboolean has_key;
const gchar *data =
"[group1]\n"
@ -496,6 +497,21 @@ test_listing (void)
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);
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);
}