Skip to content

Commit

Permalink
libotutil: Add ot_keyfile_get_value_with_default_group_optional()
Browse files Browse the repository at this point in the history
Adds ot_keyfile_get_value_with_default_group_optional() which allows
getting values from keys where the group is optional in the config
file. This is preparatory to add the sysroot.bootloader repo config
key, where the sysroot group is optional.
  • Loading branch information
rfairley committed Feb 20, 2019
1 parent 3a6f8a0 commit 5877371
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/libotutil/ot-keyfile-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,44 @@ ot_keyfile_get_value_with_default (GKeyFile *keyfile,
return ret;
}

gboolean
ot_keyfile_get_value_with_default_group_optional (GKeyFile *keyfile,
const char *section,
const char *value,
const char *default_value,
char **out_value,
GError **error)
{
gboolean ret = FALSE;
GError *temp_error = NULL;
g_autofree char *ret_value = NULL;

g_return_val_if_fail (keyfile != NULL, ret);
g_return_val_if_fail (section != NULL, ret);
g_return_val_if_fail (value != NULL, ret);

ret_value = g_key_file_get_value (keyfile, section, value, &temp_error);
if (temp_error)
{
if (g_error_matches (temp_error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)
|| g_error_matches (temp_error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND))
{
g_clear_error (&temp_error);
ret_value = g_strdup (default_value);
}
else
{
g_propagate_error (error, temp_error);
goto out;
}
}

ret = TRUE;
ot_transfer_out_value(out_value, &ret_value);
out:
return ret;
}

/* Read the value of key as a string. If the value string contains
* zero or one of the separators and none of the others, read the
* string as a NULL-terminated array out_value. If the value string
Expand Down
8 changes: 8 additions & 0 deletions src/libotutil/ot-keyfile-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ ot_keyfile_get_value_with_default (GKeyFile *keyfile,
char **out_value,
GError **error);

gboolean
ot_keyfile_get_value_with_default_group_optional (GKeyFile *keyfile,
const char *section,
const char *value,
const char *default_value,
char **out_value,
GError **error);

gboolean
ot_keyfile_get_string_list_with_separator_choice (GKeyFile *keyfile,
const char *section,
Expand Down

0 comments on commit 5877371

Please sign in to comment.