-
Notifications
You must be signed in to change notification settings - Fork 58
/
cyaml.h
2038 lines (1960 loc) · 70.8 KB
/
cyaml.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2017-2023 Michael Drake <tlsa@netsurf-browser.org>
*/
/**
* \file
* \brief CYAML library public header.
*
* CYAML is a C library for parsing and serialising structured YAML documents.
*/
#ifndef CYAML_H
#define CYAML_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "cyaml/private.h"
/**
* CYAML library version string.
*/
extern const char *cyaml_version_str;
/**
* CYAML library version number suitable for comparisons.
*
* Version number binary composition is `0bRUUUUUUUJJJJJJJJNNNNNNNNPPPPPPPP`.
*
* | Character | Meaning |
* | --------- | -------------------------------------- |
* | `R` | Release flag. If set, it's a release. |
* | `U` | Unused / reserved. |
* | `J` | Major component of version. |
* | `N` | Minor component of version. |
* | `P` | Patch component of version. |
*/
extern const uint32_t cyaml_version;
typedef struct cyaml_schema_value cyaml_schema_value_t;
/**
* Data loaded or saved by CYAML has this type. CYAML schemas are used
* to describe the data contained.
*/
typedef void cyaml_data_t;
/**
* CYAML value types.
*
* These are the fundamental data types that apply to "values" in CYAML.
*
* CYAML "values" are represented in by \ref cyaml_schema_value.
*/
typedef enum cyaml_type {
CYAML_INT, /**< Value is a signed integer. */
CYAML_UINT, /**< Value is an unsigned signed integer. */
CYAML_BOOL, /**< Value is a boolean. */
/**
* Value is an enum. Values of this type require a string / value
* mapping array in the schema entry, to define the list of valid
* enum values.
*/
CYAML_ENUM,
/**
* Value is a flags bit field. Values of this type require a string /
* value list in the schema entry, to define the list of valid flag
* values. Each bit is a boolean flag. To store values of various
* bit sizes, use a \ref CYAML_BITFIELD instead.
*
* In the YAML, a \ref CYAML_FLAGS value must be presented as a
* sequence of strings.
*/
CYAML_FLAGS,
CYAML_FLOAT, /**< Value is floating point. */
CYAML_STRING, /**< Value is a string. */
/**
* Value is a mapping. Values of this type require mapping schema
* array in the schema entry.
*/
CYAML_MAPPING,
/**
* Value is a bit field. Values of this type require an array of value
* definitions in the schema entry. If the bit field is used to store
* only single-bit flags, it may be better to use \ref CYAML_FLAGS
* instead.
*
* In the YAML, a \ref CYAML_FLAGS value must be presented as a
* mapping of bit field entry names to their numerical values.
*/
CYAML_BITFIELD,
/**
* Value is a sequence. Values of this type must be the direct
* children of a mapping. They require:
*
* - A schema describing the type of the sequence entries.
* - Offset to the array entry count member in the mapping structure.
* - Size in bytes of the count member in the mapping structure.
* - The minimum and maximum number of sequence count entries.
* Set `max` to \ref CYAML_UNLIMITED to make array count
* unconstrained.
*/
CYAML_SEQUENCE,
/**
* Value is a **fixed length** sequence. It is similar to \ref
* CYAML_SEQUENCE, however:
*
* - Values of this type do not need to be direct children of a mapping.
* - The minimum and maximum entry count must be the same. If not
* \ref CYAML_ERR_SEQUENCE_FIXED_COUNT will be returned.
* - Unlike with \ref CYAML_SEQUENCE, where the min and max are limits,
* here they are the actual entry count.
* - Since values of this type represent a constant size array, the
* given entry count must not be \ref CYAML_UNLIMITED, because that
* would cause it to attempt a massive allocation, which on most
* systems would fail.
* - The offset and size of the count structure member is unused.
* Because the count is a schema-defined constant, it does not need
* to be recorded.
*/
CYAML_SEQUENCE_FIXED,
/**
* Value of this type is completely ignored. This is most useful for
* ignoring particular keys in a mapping, when the LivCYAML client has
* not set a configuration of \ref CYAML_CFG_IGNORE_UNKNOWN_KEYS.
*/
CYAML_IGNORE,
/**
* Count of the valid CYAML types. This value is **not a valid type**
* itself.
*/
CYAML__TYPE_COUNT,
} cyaml_type_e;
/**
* CYAML value flags.
*
* These may be bitwise-ORed together.
*/
typedef enum cyaml_flag {
CYAML_FLAG_DEFAULT = 0, /**< Default value flags (none set). */
CYAML_FLAG_OPTIONAL = (1 << 0), /**< Mapping field is optional. */
/**
* Value is a pointer to its type.
*
* With this there must be a non-NULL value. Consider using
* \ref CYAML_FLAG_POINTER_NULL or \ref CYAML_FLAG_POINTER_NULL_STR
* if you want to allow NULL values.
*/
CYAML_FLAG_POINTER = (1 << 1),
/**
* Permit `NULL` values for \ref CYAML_FLAG_POINTER types.
*
* An empty value in the YAML is loaded as a NULL pointer, and NULL
* pointers are saved in YAML as empty values.
*
* Note, when you set \ref CYAML_FLAG_POINTER_NULL, then
* \ref CYAML_FLAG_POINTER is set automatically.
*/
CYAML_FLAG_POINTER_NULL = (1 << 2) | CYAML_FLAG_POINTER,
/**
* Permit storage of `NULL` values as special NULL strings in YAML.
*
* This extends \ref CYAML_FLAG_POINTER_NULL, but in addition to
* treating empty values as NULL, any of the following are also treated
* as NULL:
*
* * `null`,
* * `Null`,
* * `NULL`,
* * `~`,
*
* Note that as a side effect, loading a \ref CYAML_STRING field with
* one of these values will not store the literal string, it will store
* NULL.
*
* When saving, a NULL value will be recorded in the YAML as `null`.
*
* Note, when you set \ref CYAML_FLAG_POINTER_NULL_STR, then both
* \ref CYAML_FLAG_POINTER and \ref CYAML_FLAG_POINTER_NULL are set
* automatically.
*/
CYAML_FLAG_POINTER_NULL_STR = (1 << 3) | CYAML_FLAG_POINTER_NULL,
/**
* Make value handling strict.
*
* For \ref CYAML_ENUM and \ref CYAML_FLAGS types, in strict mode
* the YAML must contain a matching string. Without strict, numerical
* values are also permitted.
*
* * For \ref CYAML_ENUM, the value becomes the value of the enum.
* The numerical value is treated as signed.
* * For \ref CYAML_FLAGS, the values are bitwise ORed together.
* The numerical values are treated as unsigned.
*
* For \ref CYAML_FLOAT types, in strict mode floating point values
* that would cause overflow or underflow are not permitted.
*/
CYAML_FLAG_STRICT = (1 << 4),
/**
* When saving, emit mapping / sequence value in block style.
*
* This can be used to override, for this value, any default style set
* in the \ref cyaml_cfg_flags CYAML behavioural configuration flags.
*
* \note This is ignored unless the value's type is \ref CYAML_MAPPING,
* \ref CYAML_SEQUENCE, or \ref CYAML_SEQUENCE_FIXED.
*
* \note If both \ref CYAML_FLAG_BLOCK and \ref CYAML_FLAG_FLOW are set,
* then block style takes precedence.
*
* \note If neither block nor flow style set either here, or in the
* \ref cyaml_cfg_flags CYAML behavioural configuration flags,
* then libyaml's default behaviour is used.
*/
CYAML_FLAG_BLOCK = (1 << 5),
/**
* When saving, emit mapping / sequence value in flow style.
*
* This can be used to override, for this value, any default style set
* in the \ref cyaml_cfg_flags CYAML behavioural configuration flags.
*
* \note This is ignored unless the value's type is \ref CYAML_MAPPING,
* \ref CYAML_SEQUENCE, or \ref CYAML_SEQUENCE_FIXED.
*
* \note If both \ref CYAML_FLAG_BLOCK and \ref CYAML_FLAG_FLOW are set,
* then block style takes precedence.
*
* \note If neither block nor flow style set either here, or in the
* \ref cyaml_cfg_flags CYAML behavioural configuration flags,
* then libyaml's default behaviour is used.
*/
CYAML_FLAG_FLOW = (1 << 6),
/**
* When comparing strings for this value, compare with case sensitivity.
*
* By default, strings are compared with case sensitivity.
* If \ref CYAML_CFG_CASE_INSENSITIVE is set, this can override
* the configured behaviour for this specific value.
*
* \note If both \ref CYAML_FLAG_CASE_SENSITIVE and
* \ref CYAML_FLAG_CASE_INSENSITIVE are set,
* then case insensitive takes precedence.
*
* \note This applies to values of types \ref CYAML_MAPPING,
* \ref CYAML_ENUM, and \ref CYAML_FLAGS. For mappings,
* it applies to matching of the mappings' keys. For
* enums and flags it applies to the comparison of
* \ref cyaml_strval strings.
*/
CYAML_FLAG_CASE_SENSITIVE = (1 << 7),
/**
* When comparing strings for this value, compare with case sensitivity.
*
* By default, strings are compared with case sensitivity.
* If \ref CYAML_CFG_CASE_INSENSITIVE is set, this can override
* the configured behaviour for this specific value.
*
* \note If both \ref CYAML_FLAG_CASE_SENSITIVE and
* \ref CYAML_FLAG_CASE_INSENSITIVE are set,
* then case insensitive takes precedence.
*
* \note This applies to values of types \ref CYAML_MAPPING,
* \ref CYAML_ENUM, and \ref CYAML_FLAGS. For mappings,
* it applies to matching of the mappings' keys. For
* enums and flags it applies to the comparison of
* \ref cyaml_strval strings.
*/
CYAML_FLAG_CASE_INSENSITIVE = (1 << 8),
/**
* When saving, emit scalar value with plain style (no quotes).
*
* \note This is ignored if the value is non-scaler.
*
* \note In cases where conflicting scalar style flags are set, the
* the one with the highest precedence is used. From lowest to
* highest precedence:
* \ref CYAML_FLAG_SCALAR_PLAIN,
* \ref CYAML_FLAG_SCALAR_FOLDED,
* \ref CYAML_FLAG_SCALAR_LITERAL,
* \ref CYAML_FLAG_SCALAR_QUOTE_SINGLE,
* \ref CYAML_FLAG_SCALAR_QUOTE_DOUBLE,
*
* If none of these are set, libyaml's default behaviour is used.
*/
CYAML_FLAG_SCALAR_PLAIN = (1 << 9),
/**
* When saving, emit scalar value with folded style:
*
* ```yaml
* string: >
* This string
* really has no line breaks!
* ```
*
* See the notes for \ref CYAML_FLAG_SCALAR_PLAIN for applicability
* and precedence.
*/
CYAML_FLAG_SCALAR_FOLDED = (1 << 10),
/**
* When saving, emit scalar value with literal style:
*
* ```yaml
* string: |
* This is a
* multi-line string!
* ```
*
* See the notes for \ref CYAML_FLAG_SCALAR_PLAIN for applicability
* and precedence.
*/
CYAML_FLAG_SCALAR_LITERAL = (1 << 11),
/**
* When saving, emit scalar value with single quotes (`'`).
*
* See the notes for \ref CYAML_FLAG_SCALAR_PLAIN for applicability
* and precedence.
*/
CYAML_FLAG_SCALAR_QUOTE_SINGLE = (1 << 12),
/**
* When saving, emit scalar value with double quotes (`"`).
*
* See the notes for \ref CYAML_FLAG_SCALAR_PLAIN for applicability
* and precedence.
*/
CYAML_FLAG_SCALAR_QUOTE_DOUBLE = (1 << 13),
} cyaml_flag_e;
/**
* Mapping between a string and a signed value.
*
* Used for \ref CYAML_ENUM and \ref CYAML_FLAGS types.
*/
typedef struct cyaml_strval {
const char *str; /**< String representing enum or flag value. */
int64_t val; /**< Value of given string. */
} cyaml_strval_t;
/**
* Bitfield value info.
*
* Used for \ref CYAML_BITFIELD type.
*/
typedef struct cyaml_bitdef {
const char *name; /**< String representing the value's name. */
uint8_t offset; /**< Bit offset to value in bit field. */
uint8_t bits; /**< Maximum bits available for value. */
} cyaml_bitdef_t;
/**
* Value validation callback function for \ref CYAML_INT.
*
* Clients provide this for values of type \ref CYAML_INT.
* When called, return `false` to reject the value as invalid or true to
* accept it as valid.
*
* \param[in] ctx Client's private validation context.
* \param[in] schema The schema for the value.
* \param[in] value The value to be validated.
* \return `true` if values is valid, `false` otherwise.
*/
typedef bool (*cyaml_validate_int_fn_t)(
void *ctx,
const cyaml_schema_value_t *schema,
int64_t value);
/**
* Value validation callback function for \ref CYAML_UINT.
*
* Clients provide this for values of type \ref CYAML_UINT.
* When called, return `false` to reject the value as invalid or true to
* accept it as valid.
*
* \param[in] ctx Client's private validation context.
* \param[in] schema The schema for the value.
* \param[in] value The value to be validated.
* \return `true` if values is valid, `false` otherwise.
*/
typedef bool (*cyaml_validate_uint_fn_t)(
void *ctx,
const cyaml_schema_value_t *schema,
uint64_t value);
/**
* Value validation callback function for \ref CYAML_FLOAT.
*
* Clients provide this for values of type \ref CYAML_FLOAT.
* When called, return `false` to reject the value as invalid or true to
* accept it as valid.
*
* \param[in] ctx Client's private validation context.
* \param[in] schema The schema for the value.
* \param[in] value The value to be validated.
* \return `true` if values is valid, `false` otherwise.
*/
typedef bool (*cyaml_validate_float_fn_t)(
void *ctx,
const cyaml_schema_value_t *schema,
double value);
/**
* Value validation callback function for \ref CYAML_STRING.
*
* Clients provide this for values of type \ref CYAML_STRING.
* When called, return `false` to reject the value as invalid or true to
* accept it as valid.
*
* \param[in] ctx Client's private validation context.
* \param[in] schema The schema for the value.
* \param[in] value The value to be validated.
* \return `true` if values is valid, `false` otherwise.
*/
typedef bool (*cyaml_validate_string_fn_t)(
void *ctx,
const cyaml_schema_value_t *schema,
const char *value);
/**
* Value validation callback function for \ref CYAML_MAPPING.
*
* Clients provide this for values of type \ref CYAML_MAPPING.
* When called, return `false` to reject the value as invalid or true to
* accept it as valid.
*
* \param[in] ctx Client's private validation context.
* \param[in] schema The schema for the value.
* \param[in] value The value to be validated.
* \return `true` if values is valid, `false` otherwise.
*/
typedef bool (*cyaml_validate_mapping_fn_t)(
void *ctx,
const cyaml_schema_value_t *schema,
const cyaml_data_t *value);
/**
* Value validation callback function for \ref CYAML_SEQUENCE and
* \ref CYAML_SEQUENCE_FIXED.
*
* Clients provide this for values of type \ref CYAML_SEQUENCE or
* \ref CYAML_SEQUENCE_FIXED.
* When called, return `false` to reject the value as invalid or true to
* accept it as valid.
*
* \param[in] ctx Client's private validation context.
* \param[in] schema The schema for the value.
* \param[in] seq The value to be validated.
* \param[in] seq_count Number of entries in seq.
* \return `true` if values is valid, `false` otherwise.
*/
typedef bool (*cyaml_validate_sequence_fn_t)(
void *ctx,
const cyaml_schema_value_t *schema,
const cyaml_data_t *seq,
size_t seq_count);
/**
* Schema definition for a value.
*
* \note There are convenience macros for each of the types to assist in
* building a CYAML schema data structure for your YAML documents.
*
* This is the fundamental building block of CYAML schemas. The load, save and
* free functions take parameters of this type to explain what the top-level
* type of the YAML document should be.
*
* Values of type \ref CYAML_SEQUENCE and \ref CYAML_SEQUENCE_FIXED
* contain a reference to another \ref cyaml_schema_value representing
* the type of the entries of the sequence. For example, if you want
* a sequence of integers, you'd have a \ref cyaml_schema_value for the
* for the sequence value type, and another for the integer value type.
*
* Values of type \ref CYAML_MAPPING contain an array of \ref cyaml_schema_field
* entries, defining the YAML keys allowed by the mapping. Each field contains
* a \ref cyaml_schema_value representing the schema for the value.
*/
typedef struct cyaml_schema_value {
/**
* The type of the value defined by this schema entry.
*/
enum cyaml_type type;
/** Flags indicating value's characteristics. */
enum cyaml_flag flags;
/**
* Size of the value's client data type in bytes.
*
* For example, `short` `int`, `long`, `int8_t`, etc are all signed
* integer types, so they would have the type \ref CYAML_INT,
* however, they have different sizes.
*/
uint32_t data_size;
/** Anonymous union containing type-specific attributes. */
union {
/** \ref CYAML_INT type-specific schema data. */
struct {
/**
* Minimum allowed value.
*
* Setting `INT64_MIN` is equivalent to having no
* minimum constraint.
*
* \note If both `min` and `max` are set to zero,
* no range constraint is applied.
*/
int64_t min;
/**
* Maximum allowed value.
*
* Setting `INT64_MAX` is equivalent to having no
* maximum constraint.
*
* \note If both `min` and `max` are set to zero,
* no range constraint is applied.
*/
int64_t max;
/**
* Optional client value validation callback.
*
* May be NULL.
*/
cyaml_validate_int_fn_t validation_cb;
/**
* Value to use for missing YAML field.
*
* This is only used when the value is used for a
* mapping field with the \ref CYAML_FLAG_OPTIONAL flag
* set.
*/
int64_t missing;
} integer;
/** \ref CYAML_UINT type-specific schema data. */
struct {
/**
* Minimum allowed value.
*
* Setting `0` is equivalent to having no minimum
* constraint.
*
* \note If both `min` and `max` are set to zero,
* no range constraint is applied.
*/
uint64_t min;
/**
* Maximum allowed value.
*
* Setting `UINT64_MAX` is equivalent to having no
* maximum constraint.
*
* \note If both `min` and `max` are set to zero,
* no range constraint is applied.
*/
uint64_t max;
/**
* Optional client value validation callback.
*
* May be NULL.
*/
cyaml_validate_uint_fn_t validation_cb;
/**
* Value to use for missing YAML field.
*
* This is only used when the value is used for a
* mapping field with the \ref CYAML_FLAG_OPTIONAL flag
* set.
*/
uint64_t missing;
} unsigned_integer;
/** \ref CYAML_BOOL type-specific schema data. */
struct {
/**
* Value to use for missing YAML field.
*
* This is only used when the value is used for a
* mapping field with the \ref CYAML_FLAG_OPTIONAL flag
* set.
*/
bool missing;
} boolean;
/** \ref CYAML_FLOAT type-specific schema data. */
struct {
/**
* Optional client value validation callback.
*
* May be NULL.
*/
cyaml_validate_float_fn_t validation_cb;
/**
* Value to use for missing YAML field.
*
* This is only used when the value is used for a
* mapping field with the \ref CYAML_FLAG_OPTIONAL flag
* set.
*/
double missing;
} floating_point;
/** \ref CYAML_STRING type-specific schema data. */
struct {
/**
* Minimum string length (bytes).
*
* \note Excludes trailing NUL.
*/
uint32_t min;
/**
* Maximum string length (bytes).
*
* \note Excludes trailing NULL, so for character array
* strings (rather than pointer strings), this
* must be no more than `data_size - 1`.
*/
uint32_t max;
/**
* Optional client value validation callback.
*
* May be NULL.
*/
cyaml_validate_string_fn_t validation_cb;
/**
* Value to use for missing YAML field.
*
* This is only used when the value is used for a
* mapping field with the \ref CYAML_FLAG_OPTIONAL flag
* set.
*
* \note This is may be NULL, if no default sequence is
* wanted for a missing field in the YAML.
*/
const char *missing;
} string;
/** \ref CYAML_MAPPING type-specific schema data. */
struct {
/**
* Array of cyaml mapping field schema definitions.
*
* The array must be terminated by an entry with a
* NULL key. See \ref cyaml_schema_field_t
* and \ref CYAML_FIELD_END for more info.
*/
const struct cyaml_schema_field *fields;
/**
* Optional client value validation callback.
*
* May be NULL.
*/
cyaml_validate_mapping_fn_t validation_cb;
/**
* Value to use for missing YAML field.
*
* This is only used when the value is used for a
* mapping field with the \ref CYAML_FLAG_OPTIONAL flag
* set.
*
* \note This is may be NULL, if no default sequence is
* wanted for a missing field in the YAML.
*/
const void *missing;
} mapping;
/** \ref CYAML_BITFIELD type-specific schema data. */
struct {
/** Array of bit definitions for the bit field. */
const struct cyaml_bitdef *bitdefs;
/** Entry count for bitdefs array. */
uint32_t count;
/**
* Optional client value validation callback.
*
* May be NULL.
*/
cyaml_validate_uint_fn_t validation_cb;
/**
* Value to use for missing YAML field.
*
* This is only used when the value is used for a
* mapping field with the \ref CYAML_FLAG_OPTIONAL flag
* set.
*/
uint64_t missing;
} bitfield;
/**
* \ref CYAML_SEQUENCE and \ref CYAML_SEQUENCE_FIXED
* type-specific schema data.
*/
struct {
/**
* Schema definition for the type of the entries in the
* sequence.
*
* All of a sequence's entries must be of the same
* type, and a sequence can not have an entry type of
* type \ref CYAML_SEQUENCE (although \ref
* CYAML_SEQUENCE_FIXED is allowed). That is, you
* can't have a sequence of variable-length sequences.
*/
const struct cyaml_schema_value *entry;
/**
* Minimum number of sequence entries.
*
* \note min and max must be the same for \ref
* CYAML_SEQUENCE_FIXED.
*/
uint32_t min;
/**
* Maximum number of sequence entries.
*
* \note min and max must be the same for \ref
* CYAML_SEQUENCE_FIXED.
*/
uint32_t max;
/**
* Optional client value validation callback.
*
* May be NULL.
*/
cyaml_validate_sequence_fn_t validation_cb;
/**
* Value to use for missing YAML field.
*
* This is only used when the value is used for a
* mapping field with the \ref CYAML_FLAG_OPTIONAL flag
* set.
*
* \note This is may be NULL, if no default sequence is
* wanted for a missing field in the YAML.
*/
const void *missing;
/**
* Number of entries in missing array.
*/
uint32_t missing_count;
} sequence;
/**
* \ref CYAML_ENUM and \ref CYAML_FLAGS type-specific schema
* data.
*/
struct {
/** Array of string / value mappings defining enum. */
const cyaml_strval_t *strings;
/** Entry count for strings array. */
uint32_t count;
/**
* Optional client value validation callback.
*
* May be NULL.
*/
cyaml_validate_int_fn_t validation_cb;
/**
* Value to use for missing YAML field.
*
* This is only used when the value is used for a
* mapping field with the \ref CYAML_FLAG_OPTIONAL flag
* set.
*/
int64_t missing;
} enumeration;
};
} cyaml_schema_value_t;
/**
* Schema definition entry for mapping fields.
*
* YAML mappings are key:value pairs. CYAML only supports scalar mapping keys,
* i.e. the keys must be strings. Each mapping field schema contains a
* \ref cyaml_schema_value to define field's value.
*
* The schema for mappings is composed of an array of entries of this
* data structure. It specifies the name of the key, and the type of
* the value. It also specifies the offset into the data at which value
* data should be placed. The array is terminated by an entry with a NULL key.
*/
typedef struct cyaml_schema_field {
/**
* String for YAML mapping key that his schema entry describes,
* or NULL to indicated the end of an array of \ref cyaml_schema_field
* entries.
*/
const char *key;
/**
* Offset in data structure at which the value for this key should
* be placed / read from.
*/
uint32_t data_offset;
/**
* \ref CYAML_SEQUENCE only: Offset to sequence
* entry count member in mapping's data structure.
*/
uint32_t count_offset;
/**
* \ref CYAML_SEQUENCE only: Size in bytes of sequence
* entry count member in mapping's data structure.
*/
uint8_t count_size;
/**
* Defines the schema for the mapping field's value.
*/
struct cyaml_schema_value value;
} cyaml_schema_field_t;
/**
* CYAML behavioural configuration flags for clients
*
* These may be bitwise-ORed together.
*/
typedef enum cyaml_cfg_flags {
/**
* This indicates CYAML's default behaviour.
*/
CYAML_CFG_DEFAULT = 0,
/**
* When set, unknown mapping keys are ignored when loading YAML.
* Without this flag set, CYAML's default behaviour is to return
* with the error \ref CYAML_ERR_INVALID_KEY.
*/
CYAML_CFG_IGNORE_UNKNOWN_KEYS = (1 << 0),
/**
* When saving, emit mapping / sequence values in block style.
*
* This setting can be overridden for specific values using schema
* value flags (\ref cyaml_flag).
*
* \note This only applies to values of type \ref CYAML_MAPPING,
* \ref CYAML_SEQUENCE, or \ref CYAML_SEQUENCE_FIXED.
*
* \note If both \ref CYAML_CFG_STYLE_BLOCK and
* \ref CYAML_CFG_STYLE_FLOW are set, then block style takes
* precedence.
*/
CYAML_CFG_STYLE_BLOCK = (1 << 1),
/**
* When saving, emit mapping / sequence values in flow style.
*
* This setting can be overridden for specific values using schema
* value flags (\ref cyaml_flag).
*
* \note This only applies to values of type \ref CYAML_MAPPING,
* \ref CYAML_SEQUENCE, or \ref CYAML_SEQUENCE_FIXED.
*
* \note If both \ref CYAML_CFG_STYLE_BLOCK and
* \ref CYAML_CFG_STYLE_FLOW are set, then block style takes
* precedence.
*/
CYAML_CFG_STYLE_FLOW = (1 << 2),
/**
* When saving, emit "---" at document start and "..." at document end.
*
* If this flag isn't set, these document delimiting marks will not
* be emitted.
*/
CYAML_CFG_DOCUMENT_DELIM = (1 << 3),
/**
* When comparing strings, compare without case sensitivity.
*
* By default, strings are compared with case sensitivity.
*/
CYAML_CFG_CASE_INSENSITIVE = (1 << 4),
/**
* When loading, don't allow YAML aliases in the document.
*
* If this option is enabled, anchors will be ignored, and the
* error code \ref CYAML_ERR_ALIAS will be returned if an alias
* is encountered.
*
* Setting this removes the overhead of recording anchors, so
* it may be worth setting if aliases are not required, and
* memory is constrained.
*/
CYAML_CFG_NO_ALIAS = (1 << 5),
/**
* Log any ignored mapping keys at \ref CYAML_LOG_WARNING level.
*/
CYAML_CFG_IGNORED_KEY_WARNING = (1 << 6),
} cyaml_cfg_flags_t;
/**
* CYAML function return codes indicating success or reason for failure.
*
* Use \ref cyaml_strerror() to convert an error code to a human-readable
* string.
*/
typedef enum cyaml_err {
CYAML_OK, /**< Success. */
CYAML_ERR_OOM, /**< Memory allocation failed. */
CYAML_ERR_ALIAS, /**< See \ref CYAML_CFG_NO_ALIAS. */
CYAML_ERR_FILE_OPEN, /**< Failed to open file. */
CYAML_ERR_INVALID_KEY, /**< Mapping key rejected by schema. */
CYAML_ERR_INVALID_VALUE, /**< Value rejected by schema. */
CYAML_ERR_INVALID_ALIAS, /**< No anchor found for alias. */
CYAML_ERR_INTERNAL_ERROR, /**< Internal error in LibCYAML. */
CYAML_ERR_UNEXPECTED_EVENT, /**< YAML event rejected by schema. */
CYAML_ERR_STRING_LENGTH_MIN, /**< String length too short. */
CYAML_ERR_STRING_LENGTH_MAX, /**< String length too long. */
CYAML_ERR_INVALID_DATA_SIZE, /**< Value's data size unsupported. */
CYAML_ERR_TOP_LEVEL_NON_PTR, /**< Top level type must be pointer. */
CYAML_ERR_BAD_TYPE_IN_SCHEMA, /**< Schema contains invalid type. */
CYAML_ERR_BAD_MIN_MAX_SCHEMA, /**< Schema minimum exceeds maximum. */
CYAML_ERR_BAD_PARAM_SEQ_COUNT, /**< Bad seq_count param for schema. */
CYAML_ERR_BAD_PARAM_NULL_DATA, /**< Client gave NULL data argument. */
CYAML_ERR_BAD_BITVAL_IN_SCHEMA, /**< Bit value beyond bit field size. */
CYAML_ERR_SEQUENCE_ENTRIES_MIN, /**< Too few sequence entries. */
CYAML_ERR_SEQUENCE_ENTRIES_MAX, /**< Too many sequence entries. */
CYAML_ERR_SEQUENCE_FIXED_COUNT, /**< Mismatch between min and max. */
CYAML_ERR_SEQUENCE_IN_SEQUENCE, /**< Non-fixed sequence in sequence. */
CYAML_ERR_MAPPING_FIELD_MISSING, /**< Required mapping field missing. */
CYAML_ERR_BAD_CONFIG_NULL_MEMFN, /**< Client gave NULL mem function. */
CYAML_ERR_BAD_PARAM_NULL_CONFIG, /**< Client gave NULL config arg. */
CYAML_ERR_BAD_PARAM_NULL_SCHEMA, /**< Client gave NULL schema arg. */
CYAML_ERR_DATA_TARGET_NON_NULL, /**< Data target must be NULL ptr */
CYAML_ERR_LIBYAML_EMITTER_INIT, /**< Failed to initialise libyaml. */
CYAML_ERR_LIBYAML_PARSER_INIT, /**< Failed to initialise libyaml. */
CYAML_ERR_LIBYAML_EVENT_INIT, /**< Failed to initialise libyaml. */
CYAML_ERR_LIBYAML_EMITTER, /**< Error inside libyaml emitter. */
CYAML_ERR_LIBYAML_PARSER, /**< Error inside libyaml parser. */
CYAML_ERR__COUNT, /**< Count of CYAML return codes.
* This is **not a valid return
* code** itself.
*/
} cyaml_err_t;
/**
* Value schema helper macro for values with \ref CYAML_INT type.
*
* \param[in] _TYPE The type-specific suffix of a \ref cyaml_type.
* For example, `INT` for \ref CYAML_INT.
* \param[in] _flags Any behavioural flags relevant to this value.
* \param[in] _type The C type for this value.
* \param[in] ... An initialiser appropriate for the type-specific
* \ref cyaml_schema_value anonymous union member.
*/
#define CYAML_VALUE( \
_TYPE, _flags, _type, ...) \
.type = CYAML_ ## _TYPE, \
.flags = (enum cyaml_flag)(_flags), \
.data_size = sizeof(_type), \
CYAML__UNION_MEMBER_ ## _TYPE = __VA_ARGS__
/**
* Mapping schema building helper macro for non-pointer fields.
*
* Use this for any structure members that are not pointers.
*
* \param[in] _TYPE The CYAML type for the field. It must be one of:
* * `INT` for \ref CYAML_INT
* * `UINT` for \ref CYAML_UINT
* * `BOOL` for \ref CYAML_BOOL
* * `ENUM` for \ref CYAML_ENUM
* * `FLAGS` for \ref CYAML_FLAGS
* * `FLOAT` for \ref CYAML_FLOAT
* * `STRING` for \ref CYAML_STRING
* * `MAPPING` for \ref CYAML_MAPPING
* * `BITFIELD` for \ref CYAML_BITFIELD
* * `SEQUENCE` for \ref CYAML_SEQUENCE
* * `SEQUENCE_FIXED` for \ref CYAML_SEQUENCE_FIXED
* \param[in] _key String defining the YAML mapping key for this value.
* \param[in] _flags Any behavioural flags relevant to this value.
* \param[in] _structure The structure corresponding to the mapping.
* \param[in] _member The member in _structure for this mapping value.
* \param[in] ... An initialiser appropriate for the type-specific
* \ref cyaml_schema_value anonymous union member.
*/
#define CYAML_FIELD( \
_TYPE, _key, _flags, _structure, _member, ...) \
{ \
.key = _key, \
.data_offset = offsetof(_structure, _member), \
.value = { \
CYAML_VALUE(_TYPE, \
CYAML__POINTER_REMOVE(_flags), \
CYAML__MEMBER(_structure, _member), \
__VA_ARGS__), \
}, \
}
/**
* Mapping schema building helper macro for fields with a sequence type.
*
* Use this only for structure members that are arrays.
*
* \note The universal \ref CYAML_FIELD_PTR macro may also be used for
* sequences. The only difference is that \ref CYAML_FIELD_PTR assumes
* that for fields of type \ref CYAML_SEQUENCE there will be a pair of
* `<_member>` for the array and `<_member>_count` for storing the number
* of entries in the array, This macro allows an arbitrary name for the
* entry count variable, rather than assuming a `_count` postfix.
*
* \param[in] _TYPE The CYAML type for the field. It must be one of:
* * `INT` for \ref CYAML_INT
* * `UINT` for \ref CYAML_UINT
* * `BOOL` for \ref CYAML_BOOL
* * `ENUM` for \ref CYAML_ENUM
* * `FLAGS` for \ref CYAML_FLAGS
* * `FLOAT` for \ref CYAML_FLOAT
* * `STRING` for \ref CYAML_STRING
* * `MAPPING` for \ref CYAML_MAPPING