Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
cn-create: add cn_cbor_array_remove function
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Dupont committed Apr 10, 2018
1 parent 9acd21b commit c6bcf1e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/cn-cbor/cn-cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,22 @@ bool cn_cbor_array_append(cn_cbor* cb_array,
cn_cbor* cb_value,
cn_cbor_errback *errp);

/**
* Remove an item from a CBOR array.
*
* The removed node is automatically freed on succes.
*
* @param[in] cb_array The array from which to remove
* @param[in] cb_value The value to remove
* @param[in] CBOR_CONTEXT Allocation context (only if USE_CBOR_CONTEXT is defined)
* @param[out] errp Error
* @return True on success
*/
bool cn_cbor_array_remove(cn_cbor* cb_array,
cn_cbor* cb_value,
CBOR_CONTEXT,
cn_cbor_errback *errp);

#ifdef __cplusplus
}
#endif
Expand Down
41 changes: 41 additions & 0 deletions src/cn-create.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,47 @@ bool cn_cbor_array_append(cn_cbor* cb_array,
return true;
}

bool cn_cbor_array_remove(cn_cbor* cb_array,
cn_cbor* cb_value,
CBOR_CONTEXT,
cn_cbor_errback *errp)
{
//Make sure input is an array.
if(!cb_array || !cb_value || cb_array->type != CN_CBOR_ARRAY ||
cb_value->parent != cb_array)
{
if (errp) {errp->err = CN_CBOR_ERR_INVALID_PARAMETER;}
return false;
}

cn_cbor *p = cb_array->first_child;
if (p == cb_value) {
cb_array->first_child = cb_value->next;
p = NULL;
} else {
while (p) {
if (p->next == cb_value) {
break;
}
p = p->next;
}
}
if (cb_array->last_child == cb_value) {
cb_array->last_child = p;
if (p) {
p->next = NULL;
}
}
if (p && p->next) {
p->next = p->next->next;
}
cb_array->length--;
cb_value->next = NULL;
cb_value->parent = NULL;
cn_cbor_free(cb_value CBOR_CONTEXT_PARAM);
return true;
}

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit c6bcf1e

Please sign in to comment.