]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libucl/doc/libucl.3
ident(1): Normalizing date format
[FreeBSD/FreeBSD.git] / contrib / libucl / doc / libucl.3
1 .TH "LIBUCL" "3" "27 December, 2014" "Libucl manual" ""
2 .SH NAME
3 .PP
4 \f[B]ucl_parser_new\f[], \f[B]ucl_parser_register_macro\f[],
5 \f[B]ucl_parser_register_variable\f[], \f[B]ucl_parser_add_chunk\f[],
6 \f[B]ucl_parser_add_string\f[], \f[B]ucl_parser_add_file\f[],
7 \f[B]ucl_parser_get_object\f[], \f[B]ucl_parser_get_error\f[],
8 \f[B]ucl_parser_free\f[], \f[B]ucl_pubkey_add\f[],
9 \f[B]ucl_parser_set_filevars\f[] \- universal configuration library
10 parser and utility functions
11 .SH LIBRARY
12 .PP
13 UCL library (libucl, \-lucl)
14 .SH SYNOPSIS
15 .PP
16 \f[C]#include\ <ucl.h>\f[]
17 .SH DESCRIPTION
18 .PP
19 Libucl is a parser and \f[C]C\f[] API to parse and generate \f[C]ucl\f[]
20 objects.
21 Libucl consist of several groups of functions:
22 .SS Parser functions
23 .PP
24 Used to parse \f[C]ucl\f[] files and provide interface to extract
25 \f[C]ucl\f[] object.
26 Currently, \f[C]libucl\f[] can parse only full \f[C]ucl\f[] documents,
27 for instance, it is impossible to parse a part of document and therefore
28 it is impossible to use \f[C]libucl\f[] as a streaming parser.
29 In future, this limitation can be removed.
30 .SS Emitting functions
31 .PP
32 Convert \f[C]ucl\f[] objects to some textual or binary representation.
33 Currently, libucl supports the following exports:
34 .IP \[bu] 2
35 \f[C]JSON\f[] \- valid json format (can possibly lose some original
36 data, such as implicit arrays)
37 .IP \[bu] 2
38 \f[C]Config\f[] \- human\-readable configuration format (lossless)
39 .IP \[bu] 2
40 \f[C]YAML\f[] \- embedded yaml format (has the same limitations as
41 \f[C]json\f[] output)
42 .SS Conversion functions
43 .PP
44 Help to convert \f[C]ucl\f[] objects to C types.
45 These functions are used to convert \f[C]ucl_object_t\f[] to C primitive
46 types, such as numbers, strings or boolean values.
47 .SS Generation functions
48 .PP
49 Allow creation of \f[C]ucl\f[] objects from C types and creating of
50 complex \f[C]ucl\f[] objects, such as hashes or arrays from primitive
51 \f[C]ucl\f[] objects, such as numbers or strings.
52 .SS Iteration functions
53 .PP
54 Iterate over \f[C]ucl\f[] complex objects or over a chain of values, for
55 example when a key in an object has multiple values (that can be treated
56 as implicit array or implicit consolidation).
57 .SS Validation functions
58 .PP
59 Validation functions are used to validate some object \f[C]obj\f[] using
60 json\-schema compatible object \f[C]schema\f[].
61 Both input and schema must be UCL objects to perform validation.
62 .SS Utility functions
63 .PP
64 Provide basic utilities to manage \f[C]ucl\f[] objects: creating,
65 removing, retaining and releasing reference count and so on.
66 .SH PARSER FUNCTIONS
67 .PP
68 Parser functions operates with \f[C]struct\ ucl_parser\f[].
69 .SS ucl_parser_new
70 .IP
71 .nf
72 \f[C]
73 struct\ ucl_parser*\ ucl_parser_new\ (int\ flags);
74 \f[]
75 .fi
76 .PP
77 Creates new parser with the specified flags:
78 .IP \[bu] 2
79 \f[C]UCL_PARSER_KEY_LOWERCASE\f[] \- lowercase keys parsed
80 .IP \[bu] 2
81 \f[C]UCL_PARSER_ZEROCOPY\f[] \- try to use zero\-copy mode when reading
82 files (in zero\-copy mode text chunk being parsed without copying
83 strings so it should exist till any object parsed is used)
84 .IP \[bu] 2
85 \f[C]UCL_PARSER_NO_TIME\f[] \- treat time values as strings without
86 parsing them as floats
87 .SS ucl_parser_register_macro
88 .IP
89 .nf
90 \f[C]
91 void\ ucl_parser_register_macro\ (struct\ ucl_parser\ *parser,
92 \ \ \ \ const\ char\ *macro,\ ucl_macro_handler\ handler,\ void*\ ud);
93 \f[]
94 .fi
95 .PP
96 Register new macro with name .\f[C]macro\f[] parsed by handler
97 \f[C]handler\f[] that accepts opaque data pointer \f[C]ud\f[].
98 Macro handler should be of the following type:
99 .IP
100 .nf
101 \f[C]
102 bool\ (*ucl_macro_handler)\ (const\ unsigned\ char\ *data,
103 \ \ \ \ size_t\ len,\ void*\ ud);`
104 \f[]
105 .fi
106 .PP
107 Handler function accepts macro text \f[C]data\f[] of length \f[C]len\f[]
108 and the opaque pointer \f[C]ud\f[].
109 If macro is parsed successfully the handler should return \f[C]true\f[].
110 \f[C]false\f[] indicates parsing failure and the parser can be
111 terminated.
112 .SS ucl_parser_register_variable
113 .IP
114 .nf
115 \f[C]
116 void\ ucl_parser_register_variable\ (struct\ ucl_parser\ *parser,
117 \ \ \ \ const\ char\ *var,\ const\ char\ *value);
118 \f[]
119 .fi
120 .PP
121 Register new variable $\f[C]var\f[] that should be replaced by the
122 parser to the \f[C]value\f[] string.
123 .SS ucl_parser_add_chunk
124 .IP
125 .nf
126 \f[C]
127 bool\ ucl_parser_add_chunk\ (struct\ ucl_parser\ *parser,\ 
128 \ \ \ \ const\ unsigned\ char\ *data,\ size_t\ len);
129 \f[]
130 .fi
131 .PP
132 Add new text chunk with \f[C]data\f[] of length \f[C]len\f[] to the
133 parser.
134 At the moment, \f[C]libucl\f[] parser is not a streamlined parser and
135 chunk \f[I]must\f[] contain the \f[I]valid\f[] ucl object.
136 For example, this object should be valid:
137 .IP
138 .nf
139 \f[C]
140 {\ "var":\ "value"\ }
141 \f[]
142 .fi
143 .PP
144 while this one won\[aq]t be parsed correctly:
145 .IP
146 .nf
147 \f[C]
148 {\ "var":\ 
149 \f[]
150 .fi
151 .PP
152 This limitation may possible be removed in future.
153 .SS ucl_parser_add_string
154 .IP
155 .nf
156 \f[C]
157 bool\ ucl_parser_add_string\ (struct\ ucl_parser\ *parser,\ 
158 \ \ \ \ const\ char\ *data,\ size_t\ len);
159 \f[]
160 .fi
161 .PP
162 This function acts exactly like \f[C]ucl_parser_add_chunk\f[] does but
163 if \f[C]len\f[] argument is zero, then the string \f[C]data\f[] must be
164 zero\-terminated and the actual length is calculated up to \f[C]\\0\f[]
165 character.
166 .SS ucl_parser_add_file
167 .IP
168 .nf
169 \f[C]
170 bool\ ucl_parser_add_file\ (struct\ ucl_parser\ *parser,\ 
171 \ \ \ \ const\ char\ *filename);
172 \f[]
173 .fi
174 .PP
175 Load file \f[C]filename\f[] and parse it with the specified
176 \f[C]parser\f[].
177 This function uses \f[C]mmap\f[] call to load file, therefore, it should
178 not be \f[C]shrunk\f[] during parsing.
179 Otherwise, \f[C]libucl\f[] can cause memory corruption and terminate the
180 calling application.
181 This function is also used by the internal handler of \f[C]include\f[]
182 macro, hence, this macro has the same limitation.
183 .SS ucl_parser_get_object
184 .IP
185 .nf
186 \f[C]
187 ucl_object_t*\ ucl_parser_get_object\ (struct\ ucl_parser\ *parser);
188 \f[]
189 .fi
190 .PP
191 If the \f[C]ucl\f[] data has been parsed correctly this function returns
192 the top object for the parser.
193 Otherwise, this function returns the \f[C]NULL\f[] pointer.
194 The reference count for \f[C]ucl\f[] object returned is increased by
195 one, therefore, a caller should decrease reference by using
196 \f[C]ucl_object_unref\f[] to free object after usage.
197 .SS ucl_parser_get_error
198 .IP
199 .nf
200 \f[C]
201 const\ char\ *ucl_parser_get_error(struct\ ucl_parser\ *parser);
202 \f[]
203 .fi
204 .PP
205 Returns the constant error string for the parser object.
206 If no error occurred during parsing a \f[C]NULL\f[] object is returned.
207 A caller should not try to free or modify this string.
208 .SS ucl_parser_free
209 .IP
210 .nf
211 \f[C]
212 void\ ucl_parser_free\ (struct\ ucl_parser\ *parser);
213 \f[]
214 .fi
215 .PP
216 Frees memory occupied by the parser object.
217 The reference count for top object is decreased as well, however if the
218 function \f[C]ucl_parser_get_object\f[] was called previously then the
219 top object won\[aq]t be freed.
220 .SS ucl_pubkey_add
221 .IP
222 .nf
223 \f[C]
224 bool\ ucl_pubkey_add\ (struct\ ucl_parser\ *parser,\ 
225 \ \ \ \ const\ unsigned\ char\ *key,\ size_t\ len);
226 \f[]
227 .fi
228 .PP
229 This function adds a public key from text blob \f[C]key\f[] of length
230 \f[C]len\f[] to the \f[C]parser\f[] object.
231 This public key should be in the \f[C]PEM\f[] format and can be used by
232 \f[C]\&.includes\f[] macro for checking signatures of files included.
233 \f[C]Openssl\f[] support should be enabled to make this function
234 working.
235 If a key cannot be added (e.g.
236 due to format error) or \f[C]openssl\f[] was not linked to
237 \f[C]libucl\f[] then this function returns \f[C]false\f[].
238 .SS ucl_parser_set_filevars
239 .IP
240 .nf
241 \f[C]
242 bool\ ucl_parser_set_filevars\ (struct\ ucl_parser\ *parser,\ 
243 \ \ \ \ const\ char\ *filename,\ bool\ need_expand);
244 \f[]
245 .fi
246 .PP
247 Add the standard file variables to the \f[C]parser\f[] based on the
248 \f[C]filename\f[] specified:
249 .IP \[bu] 2
250 \f[C]$FILENAME\f[] \- a filename of \f[C]ucl\f[] input
251 .IP \[bu] 2
252 \f[C]$CURDIR\f[] \- a current directory of the input
253 .PP
254 For example, if a \f[C]filename\f[] param is
255 \f[C]\&../something.conf\f[] then the variables will have the following
256 values:
257 .IP \[bu] 2
258 \f[C]$FILENAME\f[] \- "../something.conf"
259 .IP \[bu] 2
260 \f[C]$CURDIR\f[] \- ".."
261 .PP
262 if \f[C]need_expand\f[] parameter is \f[C]true\f[] then all relative
263 paths are expanded using \f[C]realpath\f[] call.
264 In this example if \f[C]\&..\f[] is \f[C]/etc/dir\f[] then variables
265 will have these values:
266 .IP \[bu] 2
267 \f[C]$FILENAME\f[] \- "/etc/something.conf"
268 .IP \[bu] 2
269 \f[C]$CURDIR\f[] \- "/etc"
270 .SS Parser usage example
271 .PP
272 The following example loads, parses and extracts \f[C]ucl\f[] object
273 from stdin using \f[C]libucl\f[] parser functions (the length of input
274 is limited to 8K):
275 .IP
276 .nf
277 \f[C]
278 char\ inbuf[8192];
279 struct\ ucl_parser\ *parser\ =\ NULL;
280 int\ ret\ =\ 0,\ r\ =\ 0;
281 ucl_object_t\ *obj\ =\ NULL;
282 FILE\ *in;
283
284 in\ =\ stdin;
285 parser\ =\ ucl_parser_new\ (0);
286 while\ (!feof\ (in)\ &&\ r\ <\ (int)sizeof\ (inbuf))\ {
287 \ \ \ \ r\ +=\ fread\ (inbuf\ +\ r,\ 1,\ sizeof\ (inbuf)\ \-\ r,\ in);
288 }
289 ucl_parser_add_chunk\ (parser,\ inbuf,\ r);
290 fclose\ (in);
291
292 if\ (ucl_parser_get_error\ (parser))\ {
293 \ \ \ \ printf\ ("Error\ occurred:\ %s\\n",\ ucl_parser_get_error\ (parser));
294 \ \ \ \ ret\ =\ 1;
295 }
296 else\ {
297 \ \ \ \ obj\ =\ ucl_parser_get_object\ (parser);
298 }
299
300 if\ (parser\ !=\ NULL)\ {
301 \ \ \ \ ucl_parser_free\ (parser);
302 }
303 if\ (obj\ !=\ NULL)\ {
304 \ \ \ \ ucl_object_unref\ (obj);
305 }
306 return\ ret;
307 \f[]
308 .fi
309 .SH EMITTING FUNCTIONS
310 .PP
311 Libucl can transform UCL objects to a number of tectual formats:
312 .IP \[bu] 2
313 configuration (\f[C]UCL_EMIT_CONFIG\f[]) \- nginx like human readable
314 configuration file where implicit arrays are transformed to the
315 duplicate keys
316 .IP \[bu] 2
317 compact json: \f[C]UCL_EMIT_JSON_COMPACT\f[] \- single line valid json
318 without spaces
319 .IP \[bu] 2
320 formatted json: \f[C]UCL_EMIT_JSON\f[] \- pretty formatted JSON with
321 newlines and spaces
322 .IP \[bu] 2
323 compact yaml: \f[C]UCL_EMIT_YAML\f[] \- compact YAML output
324 .PP
325 Moreover, libucl API allows to select a custom set of emitting functions
326 allowing efficient and zero\-copy output of libucl objects.
327 Libucl uses the following structure to support this feature:
328 .IP
329 .nf
330 \f[C]
331 struct\ ucl_emitter_functions\ {
332 \ \ \ \ /**\ Append\ a\ single\ character\ */
333 \ \ \ \ int\ (*ucl_emitter_append_character)\ (unsigned\ char\ c,\ size_t\ nchars,\ void\ *ud);
334 \ \ \ \ /**\ Append\ a\ string\ of\ a\ specified\ length\ */
335 \ \ \ \ int\ (*ucl_emitter_append_len)\ (unsigned\ const\ char\ *str,\ size_t\ len,\ void\ *ud);
336 \ \ \ \ /**\ Append\ a\ 64\ bit\ integer\ */
337 \ \ \ \ int\ (*ucl_emitter_append_int)\ (int64_t\ elt,\ void\ *ud);
338 \ \ \ \ /**\ Append\ floating\ point\ element\ */
339 \ \ \ \ int\ (*ucl_emitter_append_double)\ (double\ elt,\ void\ *ud);
340 \ \ \ \ /**\ Opaque\ userdata\ pointer\ */
341 \ \ \ \ void\ *ud;
342 };
343 \f[]
344 .fi
345 .PP
346 This structure defines the following callbacks:
347 .IP \[bu] 2
348 \f[C]ucl_emitter_append_character\f[] \- a function that is called to
349 append \f[C]nchars\f[] characters equal to \f[C]c\f[]
350 .IP \[bu] 2
351 \f[C]ucl_emitter_append_len\f[] \- used to append a string of length
352 \f[C]len\f[] starting from pointer \f[C]str\f[]
353 .IP \[bu] 2
354 \f[C]ucl_emitter_append_int\f[] \- this function applies to integer
355 numbers
356 .IP \[bu] 2
357 \f[C]ucl_emitter_append_double\f[] \- this function is intended to
358 output floating point variable
359 .PP
360 The set of these functions could be used to output text formats of
361 \f[C]UCL\f[] objects to different structures or streams.
362 .PP
363 Libucl provides the following functions for emitting UCL objects:
364 .SS ucl_object_emit
365 .IP
366 .nf
367 \f[C]
368 unsigned\ char\ *ucl_object_emit\ (const\ ucl_object_t\ *obj,\ enum\ ucl_emitter\ emit_type);
369 \f[]
370 .fi
371 .PP
372 Allocate a string that is suitable to fit the underlying UCL object
373 \f[C]obj\f[] and fill it with the textual representation of the object
374 \f[C]obj\f[] according to style \f[C]emit_type\f[].
375 The caller should free the returned string after using.
376 .SS ucl_object_emit_full
377 .IP
378 .nf
379 \f[C]
380 bool\ ucl_object_emit_full\ (const\ ucl_object_t\ *obj,\ enum\ ucl_emitter\ emit_type,
381 \ \ \ \ \ \ \ \ struct\ ucl_emitter_functions\ *emitter);
382 \f[]
383 .fi
384 .PP
385 This function is similar to the previous with the exception that it
386 accepts the additional argument \f[C]emitter\f[] that defines the
387 concrete set of output functions.
388 This emit function could be useful for custom structures or streams
389 emitters (including C++ ones, for example).
390 .SH CONVERSION FUNCTIONS
391 .PP
392 Conversion functions are used to convert UCL objects to primitive types,
393 such as strings, numbers, or boolean values.
394 There are two types of conversion functions:
395 .IP \[bu] 2
396 safe: try to convert an ucl object to a primitive type and fail if such
397 a conversion is not possible
398 .IP \[bu] 2
399 unsafe: return primitive type without additional checks, if the object
400 cannot be converted then some reasonable default is returned (NULL for
401 strings and 0 for numbers)
402 .PP
403 Also there is a single \f[C]ucl_object_tostring_forced\f[] function that
404 converts any UCL object (including compound types \- arrays and objects)
405 to a string representation.
406 For objects, arrays, booleans and numeric types this function performs
407 emitting to a compact json format actually.
408 .PP
409 Here is a list of all conversion functions:
410 .IP \[bu] 2
411 \f[C]ucl_object_toint\f[] \- returns \f[C]int64_t\f[] of UCL object
412 .IP \[bu] 2
413 \f[C]ucl_object_todouble\f[] \- returns \f[C]double\f[] of UCL object
414 .IP \[bu] 2
415 \f[C]ucl_object_toboolean\f[] \- returns \f[C]bool\f[] of UCL object
416 .IP \[bu] 2
417 \f[C]ucl_object_tostring\f[] \- returns \f[C]const\ char\ *\f[] of UCL
418 object (this string is NULL terminated)
419 .IP \[bu] 2
420 \f[C]ucl_object_tolstring\f[] \- returns \f[C]const\ char\ *\f[] and
421 \f[C]size_t\f[] len of UCL object (string does not need to be NULL
422 terminated)
423 .IP \[bu] 2
424 \f[C]ucl_object_tostring_forced\f[] \- returns string representation of
425 any UCL object
426 .PP
427 Strings returned by these pointers are associated with the UCL object
428 and exist over its lifetime.
429 A caller should not free this memory.
430 .SH GENERATION FUNCTIONS
431 .PP
432 It is possible to generate UCL objects from C primitive types.
433 Moreover, libucl allows creation and modifying complex UCL objects, such
434 as arrays or associative objects.
435 .SS ucl_object_new
436 .IP
437 .nf
438 \f[C]
439 ucl_object_t\ *\ ucl_object_new\ (void)
440 \f[]
441 .fi
442 .PP
443 Creates new object of type \f[C]UCL_NULL\f[].
444 This object should be released by caller.
445 .SS ucl_object_typed_new
446 .IP
447 .nf
448 \f[C]
449 ucl_object_t\ *\ ucl_object_typed_new\ (unsigned\ int\ type)
450 \f[]
451 .fi
452 .PP
453 Create an object of a specified type: \- \f[C]UCL_OBJECT\f[] \- UCL
454 object \- key/value pairs \- \f[C]UCL_ARRAY\f[] \- UCL array \-
455 \f[C]UCL_INT\f[] \- integer number \- \f[C]UCL_FLOAT\f[] \- floating
456 point number \- \f[C]UCL_STRING\f[] \- NULL terminated string \-
457 \f[C]UCL_BOOLEAN\f[] \- boolean value \- \f[C]UCL_TIME\f[] \- time value
458 (floating point number of seconds) \- \f[C]UCL_USERDATA\f[] \- opaque
459 userdata pointer (may be used in macros) \- \f[C]UCL_NULL\f[] \- null
460 value
461 .PP
462 This object should be released by caller.
463 .SS Primitive objects generation
464 .PP
465 Libucl provides the functions similar to inverse conversion functions
466 called with the specific C type: \- \f[C]ucl_object_fromint\f[] \-
467 converts \f[C]int64_t\f[] to UCL object \-
468 \f[C]ucl_object_fromdouble\f[] \- converts \f[C]double\f[] to UCL object
469 \- \f[C]ucl_object_fromboolean\f[] \- converts \f[C]bool\f[] to UCL
470 object \- \f[C]ucl_object_fromstring\f[] \- converts
471 \f[C]const\ char\ *\f[] to UCL object (this string should be NULL
472 terminated) \- \f[C]ucl_object_fromlstring\f[] \- converts
473 \f[C]const\ char\ *\f[] and \f[C]size_t\f[] len to UCL object (string
474 does not need to be NULL terminated)
475 .PP
476 Also there is a function to generate UCL object from a string performing
477 various parsing or conversion operations called
478 \f[C]ucl_object_fromstring_common\f[].
479 .SS ucl_object_fromstring_common
480 .IP
481 .nf
482 \f[C]
483 ucl_object_t\ *\ ucl_object_fromstring_common\ (const\ char\ *str,\ 
484 \ \ \ \ size_t\ len,\ enum\ ucl_string_flags\ flags)
485 \f[]
486 .fi
487 .PP
488 This function is used to convert a string \f[C]str\f[] of size
489 \f[C]len\f[] to a UCL object applying \f[C]flags\f[] conversions.
490 If \f[C]len\f[] is equal to zero then a \f[C]str\f[] is assumed as
491 NULL\-terminated.
492 This function supports the following flags (a set of flags can be
493 specified using logical \f[C]OR\f[] operation):
494 .IP \[bu] 2
495 \f[C]UCL_STRING_ESCAPE\f[] \- perform JSON escape
496 .IP \[bu] 2
497 \f[C]UCL_STRING_TRIM\f[] \- trim leading and trailing whitespaces
498 .IP \[bu] 2
499 \f[C]UCL_STRING_PARSE_BOOLEAN\f[] \- parse passed string and detect
500 boolean
501 .IP \[bu] 2
502 \f[C]UCL_STRING_PARSE_INT\f[] \- parse passed string and detect integer
503 number
504 .IP \[bu] 2
505 \f[C]UCL_STRING_PARSE_DOUBLE\f[] \- parse passed string and detect
506 integer or float number
507 .IP \[bu] 2
508 \f[C]UCL_STRING_PARSE_TIME\f[] \- parse time values as floating point
509 numbers
510 .IP \[bu] 2
511 \f[C]UCL_STRING_PARSE_NUMBER\f[] \- parse passed string and detect
512 number (both float, integer and time types)
513 .IP \[bu] 2
514 \f[C]UCL_STRING_PARSE\f[] \- parse passed string (and detect booleans,
515 numbers and time values)
516 .IP \[bu] 2
517 \f[C]UCL_STRING_PARSE_BYTES\f[] \- assume that numeric multipliers are
518 in bytes notation, for example \f[C]10k\f[] means \f[C]10*1024\f[] and
519 not \f[C]10*1000\f[] as assumed without this flag
520 .PP
521 If parsing operations fail then the resulting UCL object will be a
522 \f[C]UCL_STRING\f[].
523 A caller should always check the type of the returned object and release
524 it after using.
525 .SH ITERATION FUNCTIONS
526 .PP
527 Iteration are used to iterate over UCL compound types: arrays and
528 objects.
529 Moreover, iterations could be performed over the keys with multiple
530 values (implicit arrays).
531 There are two types of iterators API: old and unsafe one via
532 \f[C]ucl_iterate_object\f[] and the proposed interface of safe
533 iterators.
534 .SS ucl_iterate_object
535 .IP
536 .nf
537 \f[C]
538 const\ ucl_object_t*\ ucl_iterate_object\ (const\ ucl_object_t\ *obj,\ 
539 \ \ \ \ ucl_object_iter_t\ *iter,\ bool\ expand_values);
540 \f[]
541 .fi
542 .PP
543 This function accepts opaque iterator pointer \f[C]iter\f[].
544 In the first call this iterator \f[I]must\f[] be initialized to
545 \f[C]NULL\f[].
546 Iterator is changed by this function call.
547 \f[C]ucl_iterate_object\f[] returns the next UCL object in the compound
548 object \f[C]obj\f[] or \f[C]NULL\f[] if all objects have been iterated.
549 The reference count of the object returned is not increased, so a caller
550 should not unref the object or modify its content (e.g.
551 by inserting to another compound object).
552 The object \f[C]obj\f[] should not be changed during the iteration
553 process as well.
554 \f[C]expand_values\f[] flag speicifies whether
555 \f[C]ucl_iterate_object\f[] should expand keys with multiple values.
556 The general rule is that if you need to iterate through the
557 \f[I]object\f[] or \f[I]explicit array\f[], then you always need to set
558 this flag to \f[C]true\f[].
559 However, if you get some key in the object and want to extract all its
560 values then you should set \f[C]expand_values\f[] to \f[C]false\f[].
561 Mixing of iteration types is not permitted since the iterator is set
562 according to the iteration type and cannot be reused.
563 Here is an example of iteration over the objects using libucl API
564 (assuming that \f[C]top\f[] is \f[C]UCL_OBJECT\f[] in this example):
565 .IP
566 .nf
567 \f[C]
568 ucl_object_iter_t\ it\ =\ NULL,\ it_obj\ =\ NULL;
569 const\ ucl_object_t\ *cur,\ *tmp;
570
571 /*\ Iterate\ over\ the\ object\ */
572 while\ ((obj\ =\ ucl_iterate_object\ (top,\ &it,\ true)))\ {
573 \ \ \ \ printf\ ("key:\ \\"%s\\"\\n",\ ucl_object_key\ (obj));
574 \ \ \ \ /*\ Iterate\ over\ the\ values\ of\ a\ key\ */
575 \ \ \ \ while\ ((cur\ =\ ucl_iterate_object\ (obj,\ &it_obj,\ false)))\ {
576 \ \ \ \ \ \ \ \ printf\ ("value:\ \\"%s\\"\\n",\ 
577 \ \ \ \ \ \ \ \ \ \ \ \ ucl_object_tostring_forced\ (cur));
578 \ \ \ \ }
579 }
580 \f[]
581 .fi
582 .SS Safe iterators API
583 .PP
584 Safe iterators are defined to clarify iterating over UCL objects and
585 simplify flattening of UCL objects in non\-trivial cases.
586 For example, if there is an implicit array that contains another array
587 and a boolean value it is extremely unclear how to iterate over such an
588 object.
589 Safe iterators are desinged to define two sorts of iteration:
590 .IP "1." 3
591 Iteration over complex objects with expanding all values
592 .IP "2." 3
593 Iteration over complex objects without expanding of values
594 .PP
595 The following example demonstrates the difference between these two
596 types of iteration:
597 .IP
598 .nf
599 \f[C]
600 key\ =\ 1;
601 key\ =\ [2,\ 3,\ 4];
602
603 Iteration\ with\ expansion:
604
605 1,\ 2,\ 3,\ 4
606
607 Iteration\ without\ expansion:
608
609 1,\ [2,\ 3,\ 4]
610 \f[]
611 .fi
612 .PP
613 UCL defines the following functions to manage safe iterators:
614 .IP \[bu] 2
615 \f[C]ucl_object_iterate_new\f[] \- creates new safe iterator
616 .IP \[bu] 2
617 \f[C]ucl_object_iterate_reset\f[] \- resets iterator to a new object
618 .IP \[bu] 2
619 \f[C]ucl_object_iterate_safe\f[] \- safely iterate the object inside
620 iterator
621 .IP \[bu] 2
622 \f[C]ucl_object_iterate_free\f[] \- free memory associated with the safe
623 iterator
624 .PP
625 Please note that unlike unsafe iterators, safe iterators \f[I]must\f[]
626 be explicitly initialized and freed.
627 An assert is likely generated if you use uninitialized or \f[C]NULL\f[]
628 iterator in all safe iterators functions.
629 .IP
630 .nf
631 \f[C]
632 ucl_object_iter_t\ it;
633 const\ ucl_object_t\ *cur;
634
635 it\ =\ ucl_object_iterate_new\ (obj);
636
637 while\ ((cur\ =\ ucl_object_iterate_safe\ (it,\ true))\ !=\ NULL)\ {
638 \ \ \ \ /*\ Do\ something\ */
639 }
640
641 /*\ Switch\ to\ another\ object\ */
642 it\ =\ ucl_object_iterate_reset\ (it,\ another_obj);
643
644 while\ ((cur\ =\ ucl_object_iterate_safe\ (it,\ true))\ !=\ NULL)\ {
645 \ \ \ \ /*\ Do\ something\ else\ */
646 }
647
648 ucl_object_iterate_free\ (it);
649 \f[]
650 .fi
651 .SH VALIDATION FUNCTIONS
652 .PP
653 Currently, there is only one validation function called
654 \f[C]ucl_object_validate\f[].
655 It performs validation of object using the specified schema.
656 This function is defined as following:
657 .SS ucl_object_validate
658 .IP
659 .nf
660 \f[C]
661 bool\ ucl_object_validate\ (const\ ucl_object_t\ *schema,
662 \ \ \ \ const\ ucl_object_t\ *obj,\ struct\ ucl_schema_error\ *err);
663 \f[]
664 .fi
665 .PP
666 This function uses ucl object \f[C]schema\f[], that must be valid in
667 terms of \f[C]json\-schema\f[] draft v4, to validate input object
668 \f[C]obj\f[].
669 If this function returns \f[C]true\f[] then validation procedure has
670 been succeed.
671 Otherwise, \f[C]false\f[] is returned and \f[C]err\f[] is set to a
672 specific value.
673 If a caller sets \f[C]err\f[] to NULL then this function does not set
674 any error just returning \f[C]false\f[].
675 Error is the structure defined as following:
676 .IP
677 .nf
678 \f[C]
679 struct\ ucl_schema_error\ {
680 \ \ \ \ enum\ ucl_schema_error_code\ code;\ \ \ \ /*\ error\ code\ */
681 \ \ \ \ char\ msg[128];\ \ \ \ \ \ \ \ \ \ \ \ \ \ /*\ error\ message\ */
682 \ \ \ \ ucl_object_t\ *obj;\ \ \ \ \ \ \ \ \ \ /*\ object\ where\ error\ occurred\ */
683 };
684 \f[]
685 .fi
686 .PP
687 Caller may use \f[C]code\f[] field to get a numeric error code:
688 .IP
689 .nf
690 \f[C]
691 enum\ ucl_schema_error_code\ {
692 \ \ \ \ UCL_SCHEMA_OK\ =\ 0,\ \ \ \ \ \ \ \ \ \ /*\ no\ error\ */
693 \ \ \ \ UCL_SCHEMA_TYPE_MISMATCH,\ \ \ /*\ type\ of\ object\ is\ incorrect\ */
694 \ \ \ \ UCL_SCHEMA_INVALID_SCHEMA,\ \ /*\ schema\ is\ invalid\ */
695 \ \ \ \ UCL_SCHEMA_MISSING_PROPERTY,/*\ missing\ properties\ */
696 \ \ \ \ UCL_SCHEMA_CONSTRAINT,\ \ \ \ \ \ /*\ constraint\ found\ */
697 \ \ \ \ UCL_SCHEMA_MISSING_DEPENDENCY,\ /*\ missing\ dependency\ */
698 \ \ \ \ UCL_SCHEMA_UNKNOWN\ \ \ \ \ \ \ \ \ \ /*\ generic\ error\ */
699 };
700 \f[]
701 .fi
702 .PP
703 \f[C]msg\f[] is a string description of an error and \f[C]obj\f[] is an
704 object where error has occurred.
705 Error object is not allocated by libucl, so there is no need to free it
706 after validation (a static object should thus be used).
707 .SH AUTHORS
708 Vsevolod Stakhov <vsevolod@highsecure.ru>.