C Collections

cyaml · Section 3

cyaml_dictionary_set

cyaml_dictionary_set - insert or replace a key in a dictionary

C Collections (libccollections, -lccollections)

#include <ccollections/cyaml.h>
 
ccol_retval cyaml_dictionary_set(cyaml map, const char *key,
                                cyaml child);

Stores child under key in the CYAML_DICTIONARY node map, which makes
its own copy of key.  On success map owns child, so don't free child
after such a call.  When the key is already present, the previous
child is deep-freed and the new one goes into the same slot, and
since nothing between the two steps can fail, the slot never names
freed memory once the call returns.  An existing key keeps its place
in the dictionary's member order, and a new key goes after every
member the dictionary already holds; see cyaml_dictionary_first(3).
 
Each return code carries one rule about child, exactly as on
cyaml_list_push(3).  ccol_invalid_args always means that the function
refused the arguments and didn't touch child at all, so the caller
owns child and must destroy it itself.  Every other failure takes
child and deep-frees it before the function returns, so the caller
must not destroy child after any of them.
 
These give ccol_invalid_args: A NULL child, a NULL map, a NULL key, a
key that is not well-formed UTF-8, a map that is not a
CYAML_DICTIONARY, a child that is attached to a list or dictionary
parent, and a child that is map itself.  There is one exception to
the attachment rule: You can set key to the value it already holds.
The function then accepts the borrowed reference that this same slot
owns, does nothing and returns ccol_success.
 
child must not hold map anywhere in its own subtree either, and the
function doesn't check this.  A cycle made that way turns a later
cyaml_destroy(), cyaml_clone() or cyaml_serialize() call on the tree
into undefined behaviour.

map    The target node, a CYAML_DICTIONARY.
key    Null-terminated key in well-formed UTF-8.  Every Unicode
       character is accepted, and the serializer escapes any that a
       YAML document may not hold raw.
child  The value node.  The call takes it, except on
       ccol_invalid_args.

ccol_success, ccol_invalid_args, ccol_not_enough_memory, or
ccol_container_full.
ccol_invalid_args leaves child untouched and with the caller.  It is
returned when map, key or child is NULL, when key is not well-formed
UTF-8, when map is not a dictionary, when child is attached, or when
child is map itself.
ccol_container_full means that the dictionary holds the largest
element count it can represent, which a real program doesn't reach.
On every outcome other than ccol_invalid_args the call takes child.

cyaml_dictionary_get(3), cyaml_dictionary_size(3), cyaml_dictionary_first(3), cyaml_create_dictionary(3)