C Collections

cyaml · Section 3

cyaml_set

cyaml_set - write a C scalar to the DOM leaf addressed by a path

C Collections (libccollections, -lccollections)

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

Works out the compile-time type of val with _Generic and writes val
as a scalar to the leaf node that path addresses.
 
These are the C types that val may have, with the CYAML node type
that each one gives:
  bool                      -> CYAML_BOOL
  any integer type          -> CYAML_INTEGER (stored as long long),
                               or CYAML_FLOAT above LLONG_MAX
  float / double            -> CYAML_FLOAT (stored as double)
  char * / const char *     -> CYAML_STRING (heap copy made)
  char array (char buf[N])  -> CYAML_STRING
  string literal ("...")    -> CYAML_STRING
  NULL, C23 nullptr         -> CYAML_NULL
 
Before C23 the macro true is the int 1, so cyaml_set(doc, k, true)
stores the CYAML_INTEGER 1, and you write (bool)true to get a
CYAML_BOOL.  In C23 true has the type bool and needs no cast.  The
library headers don't include <stdbool.h> under C23, but a C library
whose <stdbool.h> defines true as 1 even there (FreeBSD 14) brings
the int back into every file that includes it.
 
Every other C type is refused, such as long double, a struct, or a
pointer type other than char *, const char * and void *.  cyaml_set
then returns ccol_invalid_args and leaves the target leaf untouched:
an unsupported type leaves no new leaf behind as CYAML_NULL, and it
doesn't overwrite the value of an existing leaf either.
 
A void * value that is not NULL is refused too, with
ccol_invalid_args.  Only a NULL value writes CYAML_NULL, although
void * is also the compile-time type of the bare NULL literal.
 
val cannot be a bit-field, because C forbids __typeof__ on one and
the macro doesn't compile for it.  Cast the bit-field to its integer
type, for example (unsigned)cfg.port, or to bool.
 
The macro copies val into a local whose type is the type of val after
array-to-pointer conversion, without its top-level qualifiers.  So a
char array, such as a buffer that snprintf(3) filled, arrives as a
pointer to its first character, and a pointer that is itself const,
such as a static const char *const, arrives as an ordinary pointer.
Both give the text of the string.
 
Signed integer types are sign-extended to long long, and unsigned
integers in the range [0, LLONG_MAX] round-trip exactly.  An
unsigned value above LLONG_MAX doesn't fit a long long, so the leaf
becomes a CYAML_FLOAT that holds the nearest double.  That is the
same node that cyaml_parse(3) gives for the value's decimal literal,
which is too large for the integer grammar of the core schema and
resolves as a float.  cyaml_double_val(3) can read it, but
cyaml_int_val(3) can't.
 
A string value, and a leaf component that addresses a dictionary
key, must be well-formed UTF-8.  For one that is not, the macro
returns ccol_invalid_args and changes nothing.  Every Unicode
character is accepted, and the serializer escapes any that a YAML
document may not hold raw.
 
For a dictionary leaf, the key is created when it is absent, but the
dictionary that holds it must exist.  For a list leaf ("#N"), the
index must be in range, because a list has no way to grow
to fit an index that the caller picks.  Either way an existing leaf
always takes the new type, and an existing list or dictionary
subtree is deep-freed before the new value is written.
 
A double that is not finite (Inf, -Inf and NaN) is accepted and
stored, and it serializes as .inf, -.inf and .nan.  cjson_set is
different here, since it refuses these values.
 
A custom tag on an existing leaf survives the call.  A core-schema
tag survives only as long as it names the type of the new value, and
the macro drops a core-schema tag that the new value doesn't match.
For example, on a node parsed from "port: !!int 8080",
cyaml_set(doc, "port", "auto") drops the !!int tag, and
cyaml_set(doc, "port", 9090) keeps it.  See cyaml_node_set_tag(3).
 
You must write (bool)true or (bool)false to get a CYAML_BOOL node.
Without that cast the integer literal 1 or 0 gives a CYAML_INTEGER
node.

root   Root cyaml handle.
path   Dot-separated path string.
       Escapes: '\.' gives a literal '.', and '\\' gives a literal
       '\'.  A '\' before any other character is kept as it is.
val    C value, whose type is resolved at compile time.

ccol_retval: ccol_success on success.
ccol_invalid_args; an empty path, a malformed path component at any
                      position (an empty component: Nothing between
                      two dots, a leading dot, or a trailing dot; a
                      bad "#N" list-index syntax; a component that is
                      not well-formed UTF-8), a path whose shape
                      cannot apply to the tree at any position (a
                      scalar in the middle of the path, a name on a
                      list), a C type for val that is not one of the
                      accepted types above, a void * value that is
                      not NULL, or a string value that is not
                      well-formed UTF-8.
ccol_key_not_found; only for a well-formed name or index that is
                      absent: A component of the path above the leaf
                      does not exist, or a leaf "#N" list index has
                      correct syntax but is out of range, however
                      many digits it has.
ccol_not_enough_memory; an allocation failure.
ccol_container_full; a new leaf dictionary key would take the parent
                      past the largest element count that it can
                      represent, which a real program does not reach.

cyaml_set(doc, "server.port",  8080);
cyaml_set(doc, "server.name",  "prod");
cyaml_set(doc, "server.tls",   (bool)true);
cyaml_set(doc, "server.ratio", 0.95);
 
/* Replace a dictionary subtree with a scalar */
cyaml_set(doc, "server.options", 0);   /* deep-frees old subtree */
 
/* Set to null */
cyaml_set(doc, "server.deprecated", NULL);

cyaml_get(3), cyaml_node_set_tag(3)