libcbor 0.12.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
Loading...
Searching...
No Matches
arrays.c File Reference
#include "arrays.h"
#include <string.h>
#include "internal/memory_utils.h"

Go to the source code of this file.

Functions

size_t cbor_array_size (const cbor_item_t *item)
 Get the number of members.
 
size_t cbor_array_allocated (const cbor_item_t *item)
 Get the size of the allocated storage.
 
cbor_item_tcbor_array_get (const cbor_item_t *item, size_t index)
 Get item by index.
 
bool cbor_array_set (cbor_item_t *item, size_t index, cbor_item_t *value)
 Set item by index.
 
bool cbor_array_replace (cbor_item_t *item, size_t index, cbor_item_t *value)
 Replace item at an index.
 
bool cbor_array_push (cbor_item_t *array, cbor_item_t *pushee)
 Append to the end.
 
bool cbor_array_is_definite (const cbor_item_t *item)
 Is the array definite?
 
bool cbor_array_is_indefinite (const cbor_item_t *item)
 Is the array indefinite?
 
cbor_item_t ** cbor_array_handle (const cbor_item_t *item)
 Get the array contents.
 
cbor_item_tcbor_new_definite_array (size_t size)
 Create new definite array.
 
cbor_item_tcbor_new_indefinite_array (void)
 Create new indefinite array.
 

Function Documentation

◆ cbor_array_allocated()

size_t cbor_array_allocated ( const cbor_item_t * item)

Get the size of the allocated storage.

Parameters
itemAn array
Returns
The size of the allocated storage (number of items)

Definition at line 17 of file arrays.c.

◆ cbor_array_get()

cbor_item_t * cbor_array_get ( const cbor_item_t * item,
size_t index )

Get item by index.

Parameters
itemAn array
indexThe index (zero-based)
Returns
Reference to the item, or NULL in case of boundary violation.

Increases the reference count of the underlying item. The returned reference must be released using cbor_decref.

Definition at line 22 of file arrays.c.

◆ cbor_array_handle()

cbor_item_t ** cbor_array_handle ( const cbor_item_t * item)

Get the array contents.

The items may be reordered and modified as long as references remain consistent.

Parameters
itemAn array item
Returns
An array of cbor_item_t pointers of size cbor_array_size.

Definition at line 92 of file arrays.c.

◆ cbor_array_is_definite()

bool cbor_array_is_definite ( const cbor_item_t * item)

Is the array definite?

Parameters
itemAn array
Returns
Is the array definite?

Definition at line 82 of file arrays.c.

◆ cbor_array_is_indefinite()

bool cbor_array_is_indefinite ( const cbor_item_t * item)

Is the array indefinite?

Parameters
itemAn array
Returns
Is the array indefinite?

Definition at line 87 of file arrays.c.

◆ cbor_array_push()

bool cbor_array_push ( cbor_item_t * array,
cbor_item_t * pushee )

Append to the end.

For indefinite items, storage may be reallocated. For definite items, only the preallocated capacity is available.

Parameters
arrayAn array
pusheeThe item to push. Its reference count will be increased by one.
Returns
true on success, false on failure

Definition at line 44 of file arrays.c.

◆ cbor_array_replace()

bool cbor_array_replace ( cbor_item_t * item,
size_t index,
cbor_item_t * value )

Replace item at an index.

The reference to the item being replaced will be released using cbor_decref.

Parameters
itemAn array
valueThe item to assign. Its reference count will be increased by one.
indexThe index (zero-based)
Returns
true on success, false on allocation failure.

Definition at line 36 of file arrays.c.

◆ cbor_array_set()

bool cbor_array_set ( cbor_item_t * item,
size_t index,
cbor_item_t * value )

Set item by index.

If the index is out of bounds, the array is not modified and false is returned. Creating arrays with holes is not possible.

Parameters
itemAn array
valueThe item to assign
indexThe index (zero-based)
Returns
true on success, false on allocation failure.

Definition at line 26 of file arrays.c.

◆ cbor_array_size()

size_t cbor_array_size ( const cbor_item_t * item)

Get the number of members.

Parameters
itemAn array
Returns
The number of members

Definition at line 12 of file arrays.c.

◆ cbor_new_definite_array()

cbor_item_t * cbor_new_definite_array ( size_t size)

Create new definite array.

Parameters
sizeNumber of slots to preallocate
Returns
Reference to the new array item. The item's reference count is initialized to one.
NULL if memory allocation fails

Definition at line 97 of file arrays.c.

◆ cbor_new_indefinite_array()

cbor_item_t * cbor_new_indefinite_array ( void )

Create new indefinite array.

Returns
Reference to the new array item. The item's reference count is initialized to one.
NULL if memory allocation fails

Definition at line 118 of file arrays.c.