Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[settings] Add fetchf_setting_copy()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Jul 19, 2013
1 parent 72fb55e commit a5be7c4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/core/settings.c
Expand Up @@ -726,7 +726,7 @@ int fetch_setting_len ( struct settings *settings, struct setting *setting ) {
int fetch_setting_copy ( struct settings *settings, struct setting *setting,
void **data ) {
int len;
int check_len = 0;
int check_len;

/* Avoid returning uninitialised data on error */
*data = NULL;
Expand Down Expand Up @@ -1028,7 +1028,7 @@ int setting_cmp ( struct setting *a, struct setting *b ) {
*/

/**
* Fetch and format value of setting
* Fetch formatted value of setting
*
* @v settings Settings block, or NULL to search all blocks
* @v setting Setting to fetch
Expand Down Expand Up @@ -1064,6 +1064,43 @@ int fetchf_setting ( struct settings *settings, struct setting *setting,
return ret;
}

/**
* Fetch copy of formatted value of setting
*
* @v settings Settings block, or NULL to search all blocks
* @v setting Setting to fetch
* @v type Settings type
* @v value Buffer to allocate and fill with formatted value
* @ret len Length of formatted value, or negative error
*
* The caller is responsible for eventually freeing the allocated
* buffer.
*/
int fetchf_setting_copy ( struct settings *settings, struct setting *setting,
char **value ) {
int len;
int check_len;

/* Avoid returning uninitialised data on error */
*value = NULL;

/* Check existence, and fetch formatted value length */
len = fetchf_setting ( settings, setting, NULL, 0 );
if ( len < 0 )
return len;

/* Allocate buffer */
*value = zalloc ( len + 1 /* NUL */ );
if ( ! *value )
return -ENOMEM;

/* Fetch formatted value */
check_len = fetchf_setting ( settings, setting, *value,
( len + 1 /* NUL */ ) );
assert ( check_len == len );
return len;
}

/**
* Store formatted value of setting
*
Expand Down
2 changes: 2 additions & 0 deletions src/include/ipxe/settings.h
Expand Up @@ -310,6 +310,8 @@ extern int setting_name ( struct settings *settings, struct setting *setting,
char *buf, size_t len );
extern int fetchf_setting ( struct settings *settings, struct setting *setting,
char *buf, size_t len );
extern int fetchf_setting_copy ( struct settings *settings,
struct setting *setting, char **value );
extern int storef_setting ( struct settings *settings,
struct setting *setting,
const char *value );
Expand Down

0 comments on commit a5be7c4

Please sign in to comment.