Line data Source code
1 : /*
2 : MIT License
3 :
4 : Copyright (c) 2026 - A bunch of nerds
5 :
6 : Permission is hereby granted, free of charge, to any person obtaining a copy
7 : of this software and associated documentation files (the "Software"), to deal
8 : in the Software without restriction, including without limitation the rights
9 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : copies of the Software, and to permit persons to whom the Software is
11 : furnished to do so, subject to the following conditions:
12 :
13 : The above copyright notice and this permission notice shall be included in all
14 : copies or substantial portions of the Software.
15 :
16 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 : SOFTWARE.
23 : */
24 :
25 : #pragma once
26 :
27 : #include "common.h"
28 :
29 : // Everything from here to the matching pop is the public ABI and the shared
30 : // library exports it. The library builds with -fvisibility=hidden, so anything
31 : // outside these blocks stays internal.
32 : #pragma GCC visibility push(default)
33 :
34 : // A YAML 1.2 parser, serializer and mutable DOM. A list keeps its children in a
35 : // cvector and a dictionary maps char * keys to children in a chashmap; the man
36 : // pages (cyaml(7) and one page per call) hold the full rules. The main ones:
37 : //
38 : // The input must be well-formed UTF-8 made of YAML 1.2 c-printable characters
39 : // only, and anything else is refused with an error that gives the line and the
40 : // column; the parser never repairs input. A string value can't hold a NUL, so
41 : // every escape that decodes to U+0000 is a parse error. Plain scalars get the
42 : // core-schema types (null, bool, int with strict 0x/0o forms, float with .inf
43 : // and .nan). A tag forces or checks a type as cyaml(7) describes, and a custom
44 : // tag is kept as metadata. A collection used as a key is stored as its
45 : // canonical flow text, with mapping entries sorted, and a scalar key is stored
46 : // as its canonical text ("0x10" becomes "16"). Merge keys (<<) expand in place
47 : // with PyYAML's precedence. A stream of several documents parses to a list of
48 : // their roots.
49 : //
50 : // Dictionaries keep insertion order; a repeated key keeps its first place and
51 : // its last value. A tree nests at most 500 containers, and a parse is bounded
52 : // by a node budget, a byte budget and a fixed budget for what aliases, merges
53 : // and collection keys copy. Numbers are parsed and written in the C locale for
54 : // the length of each call. Every node carries the allocator it was made with,
55 : // interned so the caller's procs struct may go away, and each thread pools up
56 : // to 512 default-allocator nodes.
57 : //
58 : // The kind of a node.
59 : typedef enum cyaml_node_type {
60 : CYAML_NULL = 0,
61 : CYAML_BOOL,
62 : CYAML_INTEGER,
63 : CYAML_FLOAT,
64 : CYAML_STRING,
65 : CYAML_LIST,
66 : CYAML_DICTIONARY,
67 : } cyaml_node_type;
68 :
69 : // The DOM node. The struct is opaque and cyaml is the handle.
70 : typedef struct cyaml_node cyaml_node;
71 :
72 : typedef cyaml_node *cyaml;
73 :
74 : // Creates a CYAML_NULL node, for building or extending a tree by hand. mp is
75 : // your allocator, or NULL for the default one. Gives the new node, which you
76 : // own: Free it with cyaml_destroy() or hand it to a parent with
77 : // cyaml_list_push() or cyaml_dictionary_set(). NULL when the allocation fails
78 : // or a 65th distinct allocator would be needed.
79 : cyaml cyaml_create_null_mp(ccol_memmgmt_procs *mp);
80 :
81 : // Creates a CYAML_NULL node with the default allocator, for building a tree by
82 : // hand. Gives the new node, which you own: Free it with cyaml_destroy() or hand
83 : // it to a parent with cyaml_list_push() or cyaml_dictionary_set(). NULL when
84 : // the allocation fails.
85 3677 : static inline cyaml cyaml_create_null(void) {
86 3677 : return cyaml_create_null_mp(NULL);
87 : }
88 :
89 : // Creates a CYAML_BOOL node that holds val. mp is your allocator, or NULL for
90 : // the default one. You own the new node and free it with cyaml_destroy(),
91 : // unless a parent takes it through cyaml_list_push() or cyaml_dictionary_set().
92 : // NULL when the allocation fails or a 65th distinct allocator would be needed.
93 : cyaml cyaml_create_bool_mp(bool val, ccol_memmgmt_procs *mp);
94 :
95 : // Creates a CYAML_BOOL node that holds val, with the default allocator. You own
96 : // the new node and free it with cyaml_destroy(), unless a parent takes it
97 : // through cyaml_list_push() or cyaml_dictionary_set(). NULL when the
98 : // allocation fails.
99 14 : static inline cyaml cyaml_create_bool(bool val) {
100 14 : return cyaml_create_bool_mp(val, NULL);
101 : }
102 :
103 : // Creates a CYAML_INTEGER node that holds val. mp is your allocator, or NULL
104 : // for the default one. The new node is yours: Free it with cyaml_destroy() or
105 : // attach it to a list or dictionary. NULL when the allocation fails or a 65th
106 : // distinct allocator would be needed.
107 : cyaml cyaml_create_int_mp(long long val, ccol_memmgmt_procs *mp);
108 :
109 : // Creates a CYAML_INTEGER node that holds val, with the default allocator. The
110 : // new node is yours: Free it with cyaml_destroy() or attach it to a list or
111 : // dictionary. NULL when the allocation fails.
112 128 : static inline cyaml cyaml_create_int(long long val) {
113 127 : return cyaml_create_int_mp(val, NULL);
114 : }
115 :
116 : // Creates a CYAML_FLOAT node that holds val. A non-finite value is stored, and
117 : // serialized as .inf, -.inf or .nan. mp is your allocator, or NULL for the
118 : // default one. Gives the new node, which you free with cyaml_destroy() or
119 : // attach to a parent; NULL when the allocation fails or a 65th distinct
120 : // allocator would be needed.
121 : cyaml cyaml_create_double_mp(double val, ccol_memmgmt_procs *mp);
122 :
123 : // Creates a CYAML_FLOAT node that holds val, with the default allocator. A
124 : // non-finite value is stored, and serialized as .inf, -.inf or .nan. Gives the
125 : // new node, which you free with cyaml_destroy() or attach to a parent; NULL
126 : // when the allocation fails.
127 71 : static inline cyaml cyaml_create_double(double val) {
128 71 : return cyaml_create_double_mp(val, NULL);
129 : }
130 :
131 : // Creates a CYAML_STRING node holding a copy of val, which must be well-formed
132 : // UTF-8 (NULL gives a CYAML_NULL node). mp is your allocator, or NULL for the
133 : // default one. You own the new node and free it with cyaml_destroy(), unless a
134 : // parent takes it. Gives NULL for a string that isn't well-formed UTF-8, when
135 : // memory runs out, or when a 65th distinct allocator would be needed.
136 : cyaml cyaml_create_string_mp(const char *val, ccol_memmgmt_procs *mp);
137 :
138 : // Creates a CYAML_STRING node holding a copy of val, with the default
139 : // allocator; val must be well-formed UTF-8, and NULL gives a CYAML_NULL node.
140 : // You own the new node and free it with cyaml_destroy(), unless a parent takes
141 : // it. Gives NULL for a string that isn't well-formed UTF-8 or when memory runs
142 : // out.
143 363 : static inline cyaml cyaml_create_string(const char *val) {
144 362 : return cyaml_create_string_mp(val, NULL);
145 : }
146 :
147 : // Creates an empty CYAML_LIST node; fill it with cyaml_list_push(). mp is the
148 : // allocator of the list and of what it stores, or NULL for the default one.
149 : // You own the list and free it, children included, with cyaml_destroy(). NULL
150 : // when the allocation fails or a 65th distinct allocator would be needed.
151 : cyaml cyaml_create_list_mp(ccol_memmgmt_procs *mp);
152 :
153 : // Creates an empty CYAML_LIST node with the default allocator; fill it with
154 : // cyaml_list_push(). You own the list and free it, children included, with
155 : // cyaml_destroy(). NULL when the allocation fails.
156 2165 : static inline cyaml cyaml_create_list(void) {
157 2163 : return cyaml_create_list_mp(NULL);
158 : }
159 :
160 : // Creates an empty CYAML_DICTIONARY node; fill it with cyaml_dictionary_set().
161 : // mp is the allocator of the dictionary and of what it stores, or NULL for the
162 : // default one. You own it and free it, values included, with cyaml_destroy().
163 : // NULL when the allocation fails or a 65th distinct allocator would be needed.
164 : cyaml cyaml_create_dictionary_mp(ccol_memmgmt_procs *mp);
165 :
166 : // Creates an empty CYAML_DICTIONARY node with the default allocator; fill it
167 : // with cyaml_dictionary_set(). You own it and free it, values included, with
168 : // cyaml_destroy(). NULL when the allocation fails.
169 1560161 : static inline cyaml cyaml_create_dictionary(void) {
170 1560160 : return cyaml_create_dictionary_mp(NULL);
171 : }
172 :
173 : // Parses a NUL-terminated YAML text into a tree that you own and free with
174 : // cyaml_destroy(); a stream of several documents gives a CYAML_LIST of their
175 : // roots. NULL on a parse error or when memory runs out. On failure *err_str
176 : // (when err_str isn't NULL) points at a printable-ASCII message that the
177 : // library owns: Don't free it. It's per thread and stays valid until the next
178 : // failing parse on the same thread. On success *err_str is NULL. mp is the
179 : // allocator of every node the parse builds, or NULL for the default one.
180 : cyaml cyaml_parse_mp(const char *yaml_str, char **err_str,
181 : ccol_memmgmt_procs *mp);
182 :
183 : // Parses a NUL-terminated YAML text into a tree, with the default allocator.
184 : // You own the tree and free it with cyaml_destroy(); a stream of several
185 : // documents gives a CYAML_LIST of their roots. NULL on a parse error or when
186 : // memory runs out. On failure *err_str (when err_str isn't NULL) points at a
187 : // printable-ASCII message that the library owns: Don't free it. It's per
188 : // thread and stays valid until the next failing parse on the same thread. On
189 : // success *err_str is NULL.
190 2082 : static inline cyaml cyaml_parse(const char *yaml_str, char **err_str) {
191 2066 : return cyaml_parse_mp(yaml_str, err_str, NULL);
192 : }
193 :
194 : // Parses exactly len bytes of YAML text at yaml_str, which needn't end with a
195 : // NUL, and reads nothing past them. A NUL byte inside them, or a UTF-8
196 : // sequence that len cuts short, is a parse error. Gives a tree that you own and
197 : // free with cyaml_destroy() (a CYAML_LIST of the roots for several documents),
198 : // or NULL on a parse error or when memory runs out; *err_str then points at a
199 : // per-thread message that the library owns and that stays valid until the
200 : // next failing parse on the thread, and it's NULL on success. err_str may be
201 : // NULL. mp is the allocator of every node, or NULL for the default one.
202 : cyaml cyaml_parse_n_mp(const char *yaml_str, size_t len, char **err_str,
203 : ccol_memmgmt_procs *mp);
204 :
205 : // Parses exactly len bytes of YAML text at yaml_str with the default allocator;
206 : // the bytes needn't end with a NUL. A NUL byte inside them, or a UTF-8 sequence
207 : // that len cuts short, is a parse error. Gives a tree that you own and free
208 : // with cyaml_destroy() (a CYAML_LIST of the roots for several documents), or
209 : // NULL on a parse error or when memory runs out; *err_str then points at a
210 : // per-thread message that the library owns and that stays valid until the
211 : // next failing parse on the thread, and it's NULL on success. err_str may be
212 : // NULL.
213 9306 : static inline cyaml cyaml_parse_n(const char *yaml_str, size_t len,
214 : char **err_str) {
215 9306 : return cyaml_parse_n_mp(yaml_str, len, err_str, NULL);
216 : }
217 :
218 : // Writes the tree as block-style YAML. A scalar is plain when YAML 1.1 readers
219 : // would read the same text, and double-quoted otherwise. A float always holds a
220 : // '.', with the shortest of 15, 16 or 17 digits that reads back the same. A
221 : // NULL node gives "~\n". The buffer comes from the allocator of the root; free
222 : // it with cyaml_serialize_free() for the default allocator, or with
223 : // cyaml_serialize_free_mp() and the tree's allocator. NULL when memory runs out
224 : // or the tree nests more than 500 containers; the two can't be told apart.
225 : char *cyaml_serialize(cyaml node);
226 :
227 : // Writes the tree as compact one-line flow YAML ([a, b], {k: v}), for logs and
228 : // one-line output; a NULL node gives "~". The buffer comes from the allocator
229 : // of the root: Free it with cyaml_serialize_free() for the default allocator,
230 : // or with cyaml_serialize_free_mp() and the tree's allocator. NULL when memory
231 : // runs out or the tree nests more than 500 containers.
232 : char *cyaml_serialize_flow(cyaml node);
233 :
234 : // Writes each element of a list as its own "---" document, the counterpart of
235 : // parsing a stream, so a multi-document file can be edited and written back. An
236 : // empty list gives "", and a node that isn't a list is written as one document.
237 : // The tag of the list isn't written. Each element may nest at most 499
238 : // containers. The buffer comes from the allocator of node: Free it with
239 : // cyaml_serialize_free() for the default allocator, or with
240 : // cyaml_serialize_free_mp() and the tree's allocator. NULL when memory runs out
241 : // or the nesting is too deep.
242 : char *cyaml_serialize_stream(cyaml node);
243 :
244 : // Frees a string that cyaml_serialize(), cyaml_serialize_flow() or
245 : // cyaml_serialize_stream() gave. mp must be the allocator of the tree (NULL for
246 : // the default one); another allocator is undefined behaviour. s may be NULL,
247 : // and the call then does nothing.
248 : void cyaml_serialize_free_mp(char *s, ccol_memmgmt_procs *mp);
249 :
250 : // Frees a string that cyaml_serialize(), cyaml_serialize_flow() or
251 : // cyaml_serialize_stream() gave for a tree built with the default allocator; a
252 : // tree with its own allocator needs cyaml_serialize_free_mp(). s may be NULL,
253 : // and the call then does nothing.
254 873 : static inline void cyaml_serialize_free(char *s) {
255 809 : cyaml_serialize_free_mp(s, NULL);
256 163 : }
257 :
258 : // Gives the kind of node, so you can check it before an accessor that is fatal
259 : // on the wrong kind; CYAML_NULL for a NULL node.
260 : cyaml_node_type cyaml_type(cyaml node);
261 :
262 : // Gives the name of the kind of node as a string literal, such as "CYAML_LIST",
263 : // for messages and debugging; a NULL node gives "CYAML_NULL". Never free or
264 : // change the string.
265 8 : static inline const char *cyaml_type_str(cyaml node) {
266 8 : switch (cyaml_type(node)) {
267 : case CYAML_NULL:
268 : return "CYAML_NULL";
269 1 : case CYAML_BOOL:
270 1 : return "CYAML_BOOL";
271 1 : case CYAML_INTEGER:
272 1 : return "CYAML_INTEGER";
273 1 : case CYAML_FLOAT:
274 1 : return "CYAML_FLOAT";
275 1 : case CYAML_STRING:
276 1 : return "CYAML_STRING";
277 1 : case CYAML_LIST:
278 1 : return "CYAML_LIST";
279 1 : case CYAML_DICTIONARY:
280 1 : return "CYAML_DICTIONARY";
281 0 : default:
282 0 : return "CYAML_UNKNOWN";
283 : }
284 : }
285 :
286 : // The core-schema tag of a null node (!!null), to compare with cyaml_node_tag()
287 : // or pass to cyaml_node_set_tag().
288 : #define CYAML_TAG_NULL "tag:yaml.org,2002:null"
289 : // The core-schema tag of a bool node (!!bool), to compare with cyaml_node_tag()
290 : // or pass to cyaml_node_set_tag().
291 : #define CYAML_TAG_BOOL "tag:yaml.org,2002:bool"
292 : // The core-schema tag of an integer node (!!int), as cyaml_node_tag() gives it
293 : // and cyaml_node_set_tag() takes it.
294 : #define CYAML_TAG_INT "tag:yaml.org,2002:int"
295 : // The core-schema tag of a float node (!!float), as cyaml_node_tag() gives it
296 : // and cyaml_node_set_tag() takes it.
297 : #define CYAML_TAG_FLOAT "tag:yaml.org,2002:float"
298 : // The core-schema tag of a string node (!!str), to compare with
299 : // cyaml_node_tag() or pass to cyaml_node_set_tag().
300 : #define CYAML_TAG_STR "tag:yaml.org,2002:str"
301 : // The core-schema tag of a list node (!!seq), to compare with cyaml_node_tag()
302 : // or pass to cyaml_node_set_tag().
303 : #define CYAML_TAG_SEQ "tag:yaml.org,2002:seq"
304 : // The core-schema tag of a dictionary node (!!map), to compare with
305 : // cyaml_node_tag() or pass to cyaml_node_set_tag().
306 : #define CYAML_TAG_MAP "tag:yaml.org,2002:map"
307 :
308 : // Gives the resolved tag of node, so you can read the tags a document carried,
309 : // or NULL when it has none (or node is NULL). The node owns the string. It
310 : // stays valid until the node's tag or value is set again or the node is removed
311 : // or destroyed.
312 : const char *cyaml_node_tag(cyaml node);
313 :
314 : // Sets, replaces or (with a NULL tag) clears the tag of node, so a tree you
315 : // build can carry tags. It never changes the type or the value. A core-schema
316 : // tag must name the node's own type, and "", "!" and a string that isn't
317 : // well-formed UTF-8 are refused; each refusal, and a NULL node, gives
318 : // ccol_invalid_args. ccol_not_enough_memory when the copy fails. On every
319 : // failure the old tag stays.
320 : ccol_retval cyaml_node_set_tag(cyaml node, const char *tag);
321 :
322 : // Gives the value of a CYAML_BOOL node. Ends the program with a fatal error
323 : // when node is NULL or of another type, so check cyaml_type() first when you
324 : // aren't sure.
325 : bool cyaml_bool_val(cyaml node);
326 :
327 : // Gives the value of a CYAML_INTEGER node. A NULL node or a node of another
328 : // type is a fatal error that ends the program; check cyaml_type() first when
329 : // you aren't sure.
330 : long long cyaml_int_val(cyaml node);
331 :
332 : // Gives the value of a CYAML_FLOAT node, which may be infinite or NaN. For a
333 : // NULL node or a node of another type it ends the program with a fatal error,
334 : // so check cyaml_type() first when you aren't sure.
335 : double cyaml_double_val(cyaml node);
336 :
337 : // Gives the text of a CYAML_STRING node, which is well-formed UTF-8. The string
338 : // belongs to the node: Don't free it. It stays valid until the node gets a new
339 : // value or is removed or destroyed. A NULL node or a node of another type is a
340 : // fatal error that ends the program; check cyaml_type() first when needed.
341 : const char *cyaml_str_val(cyaml node);
342 :
343 : // Gives the number of elements of a CYAML_LIST node, for a walk by index. Ends
344 : // the program with a fatal error when node is NULL or isn't a list.
345 : size_t cyaml_list_size(cyaml node);
346 :
347 : // Gives the number of keys of a CYAML_DICTIONARY node. Ends the program with a
348 : // fatal error when node is NULL or isn't a dictionary.
349 : size_t cyaml_dictionary_size(cyaml node);
350 :
351 : // Appends child to the list seq, which takes ownership of it. ccol_invalid_args
352 : // (a NULL argument, seq not a list, child already attached to a parent, or
353 : // child == seq) leaves child untouched and yours. Every other failure
354 : // (ccol_not_enough_memory, ccol_container_full) deep-frees child. Cycles
355 : // through an ancestor without a parent aren't checked; don't build one.
356 : ccol_retval cyaml_list_push(cyaml seq, cyaml child);
357 :
358 : // Gives a borrowed reference to the element at index, for reading a list, or
359 : // NULL when index is out of range or seq is NULL or isn't a list. The list
360 : // keeps owning the element, which stays valid until the tree changes; never
361 : // destroy it or attach it elsewhere, and cyaml_clone() it to get a copy.
362 : cyaml cyaml_list_get(cyaml seq, size_t index);
363 :
364 : // Inserts or replaces key in map, which keeps its own copy of key and takes
365 : // ownership of child. ccol_invalid_args (a NULL argument, map not a
366 : // dictionary, a key that isn't well-formed UTF-8, child already attached to a
367 : // parent, or child == map) leaves child untouched and yours. Every other
368 : // failure (ccol_not_enough_memory, ccol_container_full) deep-frees child. A
369 : // replaced child is deep-freed and the key keeps its place. Setting a key to
370 : // the value it already holds does nothing and succeeds. A cycle through child's
371 : // own subtree isn't checked; don't build one.
372 : ccol_retval cyaml_dictionary_set(cyaml map, const char *key, cyaml child);
373 :
374 : // Gives a borrowed reference to the value of key, for reading a dictionary, or
375 : // NULL when the key is absent, map or key is NULL, or map isn't a dictionary.
376 : // The dictionary keeps owning the value, which stays valid until the tree
377 : // changes; never destroy it or attach it elsewhere.
378 : cyaml cyaml_dictionary_get(cyaml map, const char *key);
379 :
380 : // Removes and deep-frees the element at index; later elements move down.
381 : // ccol_invalid_args for a NULL or non-list seq, or an index out of range.
382 : ccol_retval cyaml_list_remove(cyaml seq, size_t index);
383 :
384 : // Removes the entry of key from map and deep-frees its value; the other members
385 : // keep their order. ccol_invalid_args when map or key is NULL or map isn't a
386 : // dictionary, ccol_key_not_found when the key is absent.
387 : ccol_retval cyaml_dictionary_remove(cyaml map, const char *key);
388 :
389 : // A cursor over the members of a dictionary in insertion order. It lives in
390 : // your storage, and stepping allocates nothing. The cursor holds the member
391 : // that the next step reads, so you may replace values, remove the current
392 : // member or insert new keys while you iterate; removing that next member or
393 : // destroying the dictionary invalidates it. Replacing the current value through
394 : // cyaml_dictionary_set() frees the node that value names.
395 : typedef struct cyaml_dictionary_iter {
396 : const char *key;
397 : cyaml value;
398 : const void *cyaml__next;
399 : } cyaml_dictionary_iter;
400 :
401 : // Starts a walk over a dictionary in insertion order: Steps it onto the first
402 : // member of dict and gives true, with it->key and it->value borrowed from the
403 : // dictionary. false (with key and value NULL) when the dictionary is empty,
404 : // dict is NULL or not a dictionary, or it is NULL. The walk allocates nothing;
405 : // continue it with cyaml_dictionary_next().
406 : bool cyaml_dictionary_first(cyaml dict, cyaml_dictionary_iter *it);
407 :
408 : // Steps the cursor it, which cyaml_dictionary_first() started, onto the next
409 : // member in insertion order and gives true, with it->key and it->value
410 : // borrowed from the dictionary. false (key and value NULL) once the iteration
411 : // is over or for a NULL it. Removing the member that the cursor reads next, or
412 : // destroying the dictionary, invalidates the cursor.
413 : bool cyaml_dictionary_next(cyaml_dictionary_iter *it);
414 :
415 : // Gives a deep copy of the tree at node, for editing a tree while keeping the
416 : // original or attaching a copy of a borrowed node. You own the copy and free it
417 : // with cyaml_destroy(). It has the same allocator, tags and member order. NULL
418 : // for a NULL node, when memory runs out, or when the tree nests more than 500
419 : // containers.
420 : cyaml cyaml_clone(cyaml node);
421 :
422 : // Declares an uninitialized cyaml variable named var_name; assign it, for
423 : // example from cyaml_parse() or a create call, before you use it. It exists so
424 : // code reads like the other modules' declare macros.
425 : #define cyaml_declare(var_name) cyaml var_name
426 :
427 : // Declares a cyaml variable named var_name, set to NULL, whose tree is
428 : // destroyed when it goes out of scope on every way out, so a tree can't leak
429 : // on an early return. Store only a tree you own in it, never a borrowed node
430 : // from cyaml_get() or another getter. Needs GCC or Clang.
431 : #define cyaml_declare_scoped(var_name) \
432 : cyaml var_name ccol__destructor(cyaml__destroy_cleanup) = NULL
433 :
434 : // Internal: Frees a whole tree, the body of cyaml_destroy(). It uses a loop, so
435 : // any depth is fine. Use cyaml_destroy() instead.
436 : void cyaml__destroy(cyaml node);
437 :
438 : // Destroys *node and sets it to NULL; the cleanup function of
439 : // cyaml_declare_scoped(), also usable when you have no lvalue for
440 : // cyaml_destroy().
441 1 : static inline void cyaml__destroy_cleanup(cyaml *node) {
442 1 : if (node && *node) {
443 1 : cyaml__destroy(*node);
444 1 : *node = NULL;
445 : }
446 : }
447 :
448 : // Frees the tree and sets node to NULL, so the handle can't dangle. node must
449 : // be a modifiable lvalue and is evaluated once; NULL is fine. It's a statement
450 : // and gives no value. Destroy only a root you own, never a borrowed node or one
451 : // attached to a parent; remove a child with cyaml_list_remove() or
452 : // cyaml_dictionary_remove().
453 : #define cyaml_destroy(node) \
454 : ccol__cyaml_destroy_impl(node, \
455 : ccol__uniq(ccol__cyaml_destroy_slot, __COUNTER__))
456 :
457 : // Internal: The body of cyaml_destroy(). slot comes from ccol__uniq(), so
458 : // destroy macros can nest without -Wshadow warnings, and node is evaluated once
459 : // through it.
460 : #define ccol__cyaml_destroy_impl(node, slot) \
461 : do { \
462 : __typeof__(node) *slot = &(node); \
463 : if (*slot) { \
464 : cyaml__destroy(*slot); \
465 : *slot = NULL; \
466 : } \
467 : } while (0)
468 :
469 : // Internal: The body of cyaml_get(). A NULL or empty path gives root. NULL for
470 : // a missing component, a wrong type on the way, an empty component, or when the
471 : // copy of a path of 256 bytes or more fails.
472 : cyaml cyaml__get(cyaml root, const char *path);
473 :
474 : // Internal: The body of cyaml_remove(). ccol_invalid_args for a NULL or empty
475 : // path, a malformed component anywhere, or a parent of the wrong type;
476 : // ccol_key_not_found for a well-formed component that is absent;
477 : // ccol_not_enough_memory when the path copy fails.
478 : ccol_retval cyaml__remove(cyaml root, const char *path);
479 :
480 : // Internal: The body of cyaml_set(). raw points at the value, raw_size is its
481 : // sizeof() (a pointer to the string for CYAML_STRING) and is checked before the
482 : // value is read. The errors are those of cyaml__remove(), plus a string value
483 : // that isn't well-formed UTF-8, and ccol_container_full for a new key past the
484 : // largest count. An absent key at the leaf is created; an absent list index or
485 : // an absent key above the leaf is ccol_key_not_found.
486 : ccol_retval cyaml__set_typed(cyaml root, const char *path, cyaml_node_type type,
487 : void *raw, size_t raw_size, bool is_signed);
488 :
489 : // What cyaml__type_of() gives for a type that cyaml_set() doesn't accept, so
490 : // such a call is refused at run time instead of storing garbage. It's no real
491 : // node type, so the type check of cyaml__set_typed() refuses it.
492 : #define CYAML__TYPE_UNSUPPORTED ((cyaml_node_type)0x7f)
493 :
494 : // Maps the type of the C expression val to the node type cyaml_set() stores, so
495 : // cyaml_set() works for any supported C type. It evaluates to a
496 : // cyaml_node_type, or CYAML__TYPE_UNSUPPORTED. A bare NULL (void *) and nullptr
497 : // map to CYAML_NULL on purpose. The clang form wraps the _Generic to silence
498 : // -Wunreachable-code-generic-assoc.
499 : #if defined(CCOL__CLANG_GENERIC_ASSOC_WARNING)
500 : #define cyaml__type_of(val) \
501 : cyaml__type_of_impl((val), ccol__uniq(cyaml__type_of_tmp, __COUNTER__))
502 : #define cyaml__type_of_impl(val, cyaml__type_of_name) \
503 : ({ \
504 : _Pragma("GCC diagnostic push"); \
505 : _Pragma("GCC diagnostic ignored \"-Wunreachable-code-generic-assoc\""); \
506 : cyaml_node_type cyaml__type_of_name = _Generic((val), \
507 : bool: CYAML_BOOL, \
508 : char: CYAML_INTEGER, \
509 : signed char: CYAML_INTEGER, \
510 : short: CYAML_INTEGER, \
511 : int: CYAML_INTEGER, \
512 : long: CYAML_INTEGER, \
513 : long long: CYAML_INTEGER, \
514 : unsigned char: CYAML_INTEGER, \
515 : unsigned short: CYAML_INTEGER, \
516 : unsigned int: CYAML_INTEGER, \
517 : unsigned long: CYAML_INTEGER, \
518 : unsigned long long: CYAML_INTEGER, \
519 : float: CYAML_FLOAT, \
520 : double: CYAML_FLOAT, \
521 : char *: CYAML_STRING, \
522 : const char *: CYAML_STRING, \
523 : CCOL__NULLPTR_ASSOC(CYAML_NULL) void *: CYAML_NULL, \
524 : default: CYAML__TYPE_UNSUPPORTED); \
525 : _Pragma("GCC diagnostic pop"); \
526 : cyaml__type_of_name; \
527 : })
528 : #else
529 : #define cyaml__type_of(val) \
530 : _Generic((val), \
531 : bool: CYAML_BOOL, \
532 : char: CYAML_INTEGER, \
533 : signed char: CYAML_INTEGER, \
534 : short: CYAML_INTEGER, \
535 : int: CYAML_INTEGER, \
536 : long: CYAML_INTEGER, \
537 : long long: CYAML_INTEGER, \
538 : unsigned char: CYAML_INTEGER, \
539 : unsigned short: CYAML_INTEGER, \
540 : unsigned int: CYAML_INTEGER, \
541 : unsigned long: CYAML_INTEGER, \
542 : unsigned long long: CYAML_INTEGER, \
543 : float: CYAML_FLOAT, \
544 : double: CYAML_FLOAT, \
545 : char *: CYAML_STRING, \
546 : const char *: CYAML_STRING, \
547 : CCOL__NULLPTR_ASSOC(CYAML_NULL) void *: CYAML_NULL, \
548 : default: CYAML__TYPE_UNSUPPORTED)
549 : #endif
550 :
551 : // True when plain char is signed on this target, for cyaml__is_signed().
552 : #if defined(__CHAR_UNSIGNED__)
553 : #define CYAML__CHAR_SIGNED false
554 : #else
555 : #define CYAML__CHAR_SIGNED true
556 : #endif
557 :
558 : // Evaluates to true when val has a signed integer type, so cyaml__set_typed()
559 : // knows how to widen the raw integer. The clang form wraps the _Generic like
560 : // cyaml__type_of().
561 : #if defined(CCOL__CLANG_GENERIC_ASSOC_WARNING)
562 : #define cyaml__is_signed(val) \
563 : cyaml__is_signed_impl((val), ccol__uniq(cyaml__is, __COUNTER__))
564 : #define cyaml__is_signed_impl(val, cyaml__is_name) \
565 : ({ \
566 : _Pragma("GCC diagnostic push"); \
567 : _Pragma("GCC diagnostic ignored \"-Wunreachable-code-generic-assoc\""); \
568 : bool cyaml__is_name = _Generic((val), \
569 : char: CYAML__CHAR_SIGNED, \
570 : signed char: true, \
571 : short: true, \
572 : int: true, \
573 : long: true, \
574 : long long: true, \
575 : default: false); \
576 : _Pragma("GCC diagnostic pop"); \
577 : cyaml__is_name; \
578 : })
579 : #else
580 : #define cyaml__is_signed(val) \
581 : _Generic((val), \
582 : char: CYAML__CHAR_SIGNED, \
583 : signed char: true, \
584 : short: true, \
585 : int: true, \
586 : long: true, \
587 : long long: true, \
588 : default: false)
589 : #endif
590 :
591 : // Gives a borrowed reference to the node at a dot-separated path ("a.#0.b"), so
592 : // you can reach deep values in one call. '#N' indexes a list; '\.' and '\\'
593 : // escape a dot and a backslash inside a key. A NULL or empty path gives root.
594 : // The tree keeps owning the node, which stays valid until the tree changes.
595 : // NULL for a NULL root, an absent or malformed component, a path whose shape
596 : // doesn't fit the tree, or when the copy of a path of 256 bytes or more fails.
597 : #define cyaml_get(root, path) cyaml__get((root), (path))
598 :
599 : // Writes a C value to the leaf at path, so you can set a value of any supported
600 : // C type in one call. val may be bool, any integer type, float, double, a
601 : // string or NULL. Before C23 `true` is an int, so write (bool)true for a
602 : // CYAML_BOOL. An unsigned value above LLONG_MAX becomes the nearest float, and
603 : // a bit-field must be cast first. A missing key at the leaf is created, but
604 : // every component above it and a list index must exist. A custom tag on the
605 : // leaf survives, a core tag only while it matches the new type. Evaluates to
606 : // ccol_success; ccol_invalid_args for a NULL root, an empty or malformed path,
607 : // a path whose shape doesn't fit the tree, an unsupported type, a void * that
608 : // isn't NULL, or a string that isn't well-formed UTF-8; ccol_key_not_found for
609 : // an absent component above the leaf or an absent list index;
610 : // ccol_not_enough_memory; ccol_container_full for a new key past the largest
611 : // count a dictionary holds.
612 : #define cyaml_set(root, path, val) \
613 : cyaml__set_impl((root), (path), (val), ccol__uniq(cyaml__sv, __COUNTER__))
614 :
615 : // Internal: The body of cyaml_set(); cyaml__sv_name comes from ccol__uniq() so
616 : // nested macros don't shadow each other. __auto_type decays an array and drops
617 : // qualifiers, so a char[N] or a const pointer arrives as a plain char pointer.
618 : // It evaluates to the ccol_retval.
619 : #define cyaml__set_impl(root, path, val, cyaml__sv_name) \
620 : ({ \
621 : __auto_type cyaml__sv_name = (val); \
622 : cyaml__set_typed((root), (path), cyaml__type_of(cyaml__sv_name), \
623 : (void *)&cyaml__sv_name, sizeof(cyaml__sv_name), \
624 : cyaml__is_signed(cyaml__sv_name)); \
625 : })
626 :
627 : // Removes and deep-frees the node at path in the tree root, so you can drop a
628 : // deep value in one call. path is dot-separated, with '#N' for a list index;
629 : // under a list parent the leaf must be '#N'. Evaluates to ccol_success,
630 : // ccol_invalid_args for a NULL root or path, an empty or malformed path or one
631 : // whose shape doesn't fit the tree, ccol_key_not_found when a well-formed
632 : // component is absent, or ccol_not_enough_memory.
633 : #define cyaml_remove(root, path) cyaml__remove((root), (path))
634 :
635 : #pragma GCC visibility pop
|