C Collections

cyaml · Section 3

cyaml_get

cyaml_get - navigate to a DOM node with a dot-separated path

C Collections (libccollections, -lccollections)

#include <ccollections/cyaml.h>
 
cyaml cyaml_get(cyaml root, const char *path);

Walks from root through a dot-separated path and returns the node it
ends at.
 
Path syntax:
  - A dot ('.') separates the components.
  - A component made of '#' and one or more decimal digits indexes a
    CYAML_LIST by position, so "#0" is the first element.  An index
    with any number of digits is well formed, so an index that the
    list doesn't reach is absent however large it is, and the macro
    returns NULL for it.
  - Every other component is a literal CYAML_DICTIONARY key.
  - A bare '#' with no digits is not a valid index.  When the current
    node is a CYAML_DICTIONARY, the macro uses it as a literal key,
    like any other component that is not "#N", so the path addresses
    a key named "#".  A CYAML_LIST has no literal-key fallback at
    all, so on a list the navigation fails and the macro returns
    NULL.  cjson refuses a bare '#' in that case the same way.
  - Escapes: '\.' gives a literal '.' in the key instead of a
    separator, and '\\' gives a literal '\'.  A '\' before any
    other character is kept as it is.
  - An empty component (nothing between two dots, a leading dot, or a
    trailing dot) means nothing, neither an empty-string key nor a
    "#N" index.  That holds at every position in the path, and the
    navigation fails.
  - A NULL or empty path names root itself, and the macro gives root.
  - A NULL root names no node, and the macro gives NULL.
 
The returned reference is NON-OWNING and becomes invalid when the
caller changes or destroys the node that holds it.

root  Root cyaml handle, normally a CYAML_DICTIONARY or a CYAML_LIST.
path  Dot-separated path, given as a string literal or a char *.

A non-owning cyaml handle to the target, or NULL when root is NULL,
when a component along the path is missing, when the node type
doesn't fit, or when the path holds a malformed component (an empty
one, or one that is not well-formed UTF-8, which no key of a tree can
be).  A path of 256 bytes or more is first copied with root's
allocator, and the macro also returns NULL when that copy fails.  A
shorter path needs no allocation.

cyaml port = cyaml_get(doc, "server.ports.#0");
if (cyaml_type(port) == CYAML_INTEGER)
    printf("%lld\n", cyaml_int_val(port));
 
/* An empty path gives root itself */
cyaml same = cyaml_get(doc, "");

cyaml_set(3)