cyaml · Section 3
cyaml_clone
NAME
cyaml_clone - produce a deep independent copy of a DOM subtree
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cyaml.h> cyaml cyaml_clone(cyaml node);
DESCRIPTION
Copies node and all of its descendants. The clone uses the same allocator as the source tree, keeps the tag of every node that has one (see cyaml_node_tag(3)), and every dictionary of the clone keeps the member order of its source (see cyaml_dictionary_first(3)). Changing the clone doesn't affect the original, and changing the original doesn't affect the clone. node doesn't have to come from cyaml_parse*(). A tree that the caller builds directly with cyaml_create_list_mp(3) and cyaml_dictionary_set(3) has the same nesting limit that cyaml_parse*(3) applies at parse time, and cyaml_serialize(3), cyaml_serialize_flow(3) and cyaml_serialize_stream(3) apply exactly that limit as well: A tree may nest at most 500 containers (lists and dictionaries) on any path, counting the root when it is a container, and scalars and tags take no level. So every tree that a parse gives can be cloned. A tree that nests more containers than that is refused and the function returns NULL for it. The source is walked with an explicit worklist instead of by recursion, so the call stack it needs is constant and doesn't grow with the nesting depth. The limit bounds the work of one call, not the stack the caller has to provide.
PARAMETERS
node Root of the subtree to copy. NULL is accepted and gives
NULL.
RETURN VALUE
The root of the new tree, which the caller owns, or NULL when the function runs out of memory or node nests more than 500 containers. This function has no error string, so the caller cannot tell the two causes apart.
SEE ALSO
cyaml_destroy(3), cyaml_dictionary_first(3), cyaml_node_tag(3)