Simplify _set_string functions

This commit is contained in:
Willem Toorop 2015-12-29 15:57:55 +01:00
parent ad23c446b6
commit f91e263f09
3 changed files with 11 additions and 49 deletions

View File

@ -578,7 +578,7 @@ getdns_dict_set_list(
/*---------------------------------------- getdns_dict_set_bindata */
getdns_return_t
_getdns_dict_set_const_bindata(
getdns_dict *dict, const char *name, size_t size, const uint8_t *data)
getdns_dict *dict, const char *name, size_t size, const void *data)
{
getdns_item *item;
getdns_bindata *newbindata;
@ -612,28 +612,9 @@ getdns_dict_set_bindata(
getdns_return_t
getdns_dict_util_set_string(getdns_dict *dict, char *name, const char *value)
{
getdns_item *item;
getdns_bindata *newbindata;
getdns_return_t r;
if (!dict || !name || !value)
return GETDNS_RETURN_INVALID_PARAMETER;
if (!(newbindata = GETDNS_MALLOC(dict->mf, getdns_bindata)))
return GETDNS_RETURN_MEMORY_ERROR;
newbindata->size = strlen(value);
if (!(newbindata->data = (void *)_getdns_strdup(&dict->mf, value))) {
GETDNS_FREE(dict->mf, newbindata);
return GETDNS_RETURN_MEMORY_ERROR;
}
if ((r = _getdns_dict_find_and_add(dict, name, &item))) {
_getdns_bindata_destroy(&dict->mf, newbindata);
return r;
}
item->dtype = t_bindata;
item->data.bindata = newbindata;
return GETDNS_RETURN_GOOD;
return value
? _getdns_dict_set_const_bindata(dict, name, strlen(value), value)
: GETDNS_RETURN_INVALID_PARAMETER;
} /* getdns_dict_util_set_dict */
/*---------------------------------------- getdns_dict_set_int */

View File

@ -554,7 +554,7 @@ getdns_list_set_list(
/*---------------------------------------- getdns_list_set_bindata */
static getdns_return_t
_getdns_list_set_const_bindata(
getdns_list *list, size_t index, size_t size, const uint8_t *data)
getdns_list *list, size_t index, size_t size, const void *data)
{
getdns_bindata *newbindata;
getdns_return_t r;
@ -587,28 +587,9 @@ getdns_list_set_bindata(
static getdns_return_t
getdns_list_set_string(getdns_list *list, size_t index, const char *value)
{
getdns_bindata *newbindata;
getdns_return_t r;
if (!list || !value)
return GETDNS_RETURN_INVALID_PARAMETER;
if (!(newbindata = GETDNS_MALLOC(list->mf, getdns_bindata)))
return GETDNS_RETURN_MEMORY_ERROR;
newbindata->size = strlen(value);
if (!(newbindata->data = (void *)_getdns_strdup(&list->mf, value))) {
GETDNS_FREE(list->mf, newbindata);
return GETDNS_RETURN_MEMORY_ERROR;
}
if ((r = _getdns_list_request_index(list, index))) {
GETDNS_FREE(list->mf, newbindata->data);
GETDNS_FREE(list->mf, newbindata);
return r;
}
list->items[index].dtype = t_bindata;
list->items[index].data.bindata = newbindata;
return GETDNS_RETURN_GOOD;
return value
? _getdns_list_set_const_bindata(list, index, strlen(value), value)
: GETDNS_RETURN_INVALID_PARAMETER;
} /* getdns_list_set_string */
/*---------------------------------------- getdns_list_set_int */
@ -648,7 +629,7 @@ _getdns_list_append_bindata(getdns_list *list, const getdns_bindata *child_binda
}
getdns_return_t
_getdns_list_append_const_bindata(
getdns_list *list, size_t size, const uint8_t *data)
getdns_list *list, size_t size, const void *data)
{
if (!list) return GETDNS_RETURN_INVALID_PARAMETER;
return _getdns_list_set_const_bindata(list, list->numinuse, size, data);

View File

@ -83,10 +83,10 @@ getdns_return_t _getdns_list_append_int(getdns_list *list,
uint32_t child_uint32);
getdns_return_t _getdns_list_append_const_bindata(getdns_list *list,
size_t size, const uint8_t *data);
size_t size, const void *data);
getdns_return_t _getdns_dict_set_const_bindata(getdns_dict *dict,
const char *name, size_t size, const uint8_t *data);
const char *name, size_t size, const void *data);
/**
* private function (API users should not be calling this), this uses library