2
0
mirror of https://github.com/xcat2/xNBA.git synced 2024-11-23 01:51:58 +00:00

[settings] Add fetch_setting_copy()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2012-04-19 16:09:31 +01:00
parent 62eb2294f0
commit 31e60de676
2 changed files with 42 additions and 0 deletions

View File

@ -676,6 +676,46 @@ int fetch_setting_len ( struct settings *settings, struct setting *setting ) {
return fetch_setting ( settings, setting, NULL, 0 );
}
/**
* Fetch copy of setting
*
* @v settings Settings block, or NULL to search all blocks
* @v setting Setting to fetch
* @v data Buffer to allocate and fill with setting data
* @ret len Length of setting, or negative error
*
* The caller is responsible for eventually freeing the allocated
* buffer.
*
* To allow the caller to distinguish between a non-existent setting
* and an error in allocating memory for the copy, this function will
* return success (and a NULL buffer pointer) for a non-existent
* setting.
*/
int fetch_setting_copy ( struct settings *settings, struct setting *setting,
void **data ) {
int len;
int check_len = 0;
/* Avoid returning uninitialised data on error */
*data = NULL;
/* Fetch setting length, and return success if non-existent */
len = fetch_setting_len ( settings, setting );
if ( len < 0 )
return 0;
/* Allocate buffer */
*data = malloc ( len );
if ( ! *data )
return -ENOMEM;
/* Fetch setting */
check_len = fetch_setting ( settings, setting, *data, len );
assert ( check_len == len );
return len;
}
/**
* Fetch value of string setting
*

View File

@ -240,6 +240,8 @@ extern struct settings * fetch_setting_origin ( struct settings *settings,
struct setting *setting );
extern int fetch_setting_len ( struct settings *settings,
struct setting *setting );
extern int fetch_setting_copy ( struct settings *settings,
struct setting *setting, void **data );
extern int fetch_string_setting ( struct settings *settings,
struct setting *setting,
char *data, size_t len );