2
#include "yaml_private.h"
5
* Get the library version.
8
YAML_DECLARE(const char *)
9
yaml_get_version_string(void)
11
return YAML_VERSION_STRING;
15
* Get the library version numbers.
19
yaml_get_version(int *major, int *minor, int *patch)
21
*major = YAML_VERSION_MAJOR;
22
*minor = YAML_VERSION_MINOR;
23
*patch = YAML_VERSION_PATCH;
27
* Allocate a dynamic memory block.
31
yaml_malloc(size_t size)
33
return malloc(size ? size : 1);
37
* Reallocate a dynamic memory block.
41
yaml_realloc(void *ptr, size_t size)
43
return ptr ? realloc(ptr, size ? size : 1) : malloc(size ? size : 1);
47
* Free a dynamic memory block.
60
YAML_DECLARE(yaml_char_t *)
61
yaml_strdup(const yaml_char_t *str)
66
return (yaml_char_t *)strdup((char *)str);
74
yaml_string_extend(yaml_char_t **start,
75
yaml_char_t **pointer, yaml_char_t **end)
77
yaml_char_t *new_start = yaml_realloc(*start, (*end - *start)*2);
79
if (!new_start) return 0;
81
memset(new_start + (*end - *start), 0, *end - *start);
83
*pointer = new_start + (*pointer - *start);
84
*end = new_start + (*end - *start)*2;
91
* Append a string B to a string A.
96
yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end,
97
yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end)
99
if (*b_start == *b_pointer)
102
while (*a_end - *a_pointer <= *b_pointer - *b_start) {
103
if (!yaml_string_extend(a_start, a_pointer, a_end))
107
memcpy(*a_pointer, *b_start, *b_pointer - *b_start);
108
*a_pointer += *b_pointer - *b_start;
118
yaml_stack_extend(void **start, void **top, void **end)
120
void *new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2);
122
if (!new_start) return 0;
124
*top = (char *)new_start + ((char *)*top - (char *)*start);
125
*end = (char *)new_start + ((char *)*end - (char *)*start)*2;
132
* Extend or move a queue.
136
yaml_queue_extend(void **start, void **head, void **tail, void **end)
138
/* Check if we need to resize the queue. */
140
if (*start == *head && *tail == *end) {
141
void *new_start = yaml_realloc(*start,
142
((char *)*end - (char *)*start)*2);
144
if (!new_start) return 0;
146
*head = (char *)new_start + ((char *)*head - (char *)*start);
147
*tail = (char *)new_start + ((char *)*tail - (char *)*start);
148
*end = (char *)new_start + ((char *)*end - (char *)*start)*2;
152
/* Check if we need to move the queue at the beginning of the buffer. */
155
if (*head != *tail) {
156
memmove(*start, *head, (char *)*tail - (char *)*head);
158
*tail = (char *)*tail - (char *)*head + (char *)*start;
167
* Create a new parser object.
171
yaml_parser_initialize(yaml_parser_t *parser)
173
assert(parser); /* Non-NULL parser object expected. */
175
memset(parser, 0, sizeof(yaml_parser_t));
176
if (!BUFFER_INIT(parser, parser->raw_buffer, INPUT_RAW_BUFFER_SIZE))
178
if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE))
180
if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE))
182
if (!STACK_INIT(parser, parser->indents, INITIAL_STACK_SIZE))
184
if (!STACK_INIT(parser, parser->simple_keys, INITIAL_STACK_SIZE))
186
if (!STACK_INIT(parser, parser->states, INITIAL_STACK_SIZE))
188
if (!STACK_INIT(parser, parser->marks, INITIAL_STACK_SIZE))
190
if (!STACK_INIT(parser, parser->tag_directives, INITIAL_STACK_SIZE))
197
BUFFER_DEL(parser, parser->raw_buffer);
198
BUFFER_DEL(parser, parser->buffer);
199
QUEUE_DEL(parser, parser->tokens);
200
STACK_DEL(parser, parser->indents);
201
STACK_DEL(parser, parser->simple_keys);
202
STACK_DEL(parser, parser->states);
203
STACK_DEL(parser, parser->marks);
204
STACK_DEL(parser, parser->tag_directives);
210
* Destroy a parser object.
214
yaml_parser_delete(yaml_parser_t *parser)
216
assert(parser); /* Non-NULL parser object expected. */
218
BUFFER_DEL(parser, parser->raw_buffer);
219
BUFFER_DEL(parser, parser->buffer);
220
while (!QUEUE_EMPTY(parser, parser->tokens)) {
221
yaml_token_delete(&DEQUEUE(parser, parser->tokens));
223
QUEUE_DEL(parser, parser->tokens);
224
STACK_DEL(parser, parser->indents);
225
STACK_DEL(parser, parser->simple_keys);
226
STACK_DEL(parser, parser->states);
227
STACK_DEL(parser, parser->marks);
228
while (!STACK_EMPTY(parser, parser->tag_directives)) {
229
yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives);
230
yaml_free(tag_directive.handle);
231
yaml_free(tag_directive.prefix);
233
STACK_DEL(parser, parser->tag_directives);
235
memset(parser, 0, sizeof(yaml_parser_t));
239
* String read handler.
243
yaml_string_read_handler(void *data, unsigned char *buffer, size_t size,
246
yaml_parser_t *parser = data;
248
if (parser->input.string.current == parser->input.string.end) {
253
if (size > (size_t)(parser->input.string.end
254
- parser->input.string.current)) {
255
size = parser->input.string.end - parser->input.string.current;
258
memcpy(buffer, parser->input.string.current, size);
259
parser->input.string.current += size;
269
yaml_file_read_handler(void *data, unsigned char *buffer, size_t size,
272
yaml_parser_t *parser = data;
274
*size_read = fread(buffer, 1, size, parser->input.file);
275
return !ferror(parser->input.file);
279
* Set a string input.
283
yaml_parser_set_input_string(yaml_parser_t *parser,
284
const unsigned char *input, size_t size)
286
assert(parser); /* Non-NULL parser object expected. */
287
assert(!parser->read_handler); /* You can set the source only once. */
288
assert(input); /* Non-NULL input string expected. */
290
parser->read_handler = yaml_string_read_handler;
291
parser->read_handler_data = parser;
293
parser->input.string.start = input;
294
parser->input.string.current = input;
295
parser->input.string.end = input+size;
303
yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file)
305
assert(parser); /* Non-NULL parser object expected. */
306
assert(!parser->read_handler); /* You can set the source only once. */
307
assert(file); /* Non-NULL file object expected. */
309
parser->read_handler = yaml_file_read_handler;
310
parser->read_handler_data = parser;
312
parser->input.file = file;
316
* Set a generic input.
320
yaml_parser_set_input(yaml_parser_t *parser,
321
yaml_read_handler_t *handler, void *data)
323
assert(parser); /* Non-NULL parser object expected. */
324
assert(!parser->read_handler); /* You can set the source only once. */
325
assert(handler); /* Non-NULL read handler expected. */
327
parser->read_handler = handler;
328
parser->read_handler_data = data;
332
* Set the source encoding.
336
yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding)
338
assert(parser); /* Non-NULL parser object expected. */
339
assert(!parser->encoding); /* Encoding is already set or detected. */
341
parser->encoding = encoding;
345
* Create a new emitter object.
349
yaml_emitter_initialize(yaml_emitter_t *emitter)
351
assert(emitter); /* Non-NULL emitter object expected. */
353
memset(emitter, 0, sizeof(yaml_emitter_t));
354
if (!BUFFER_INIT(emitter, emitter->buffer, OUTPUT_BUFFER_SIZE))
356
if (!BUFFER_INIT(emitter, emitter->raw_buffer, OUTPUT_RAW_BUFFER_SIZE))
358
if (!STACK_INIT(emitter, emitter->states, INITIAL_STACK_SIZE))
360
if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE))
362
if (!STACK_INIT(emitter, emitter->indents, INITIAL_STACK_SIZE))
364
if (!STACK_INIT(emitter, emitter->tag_directives, INITIAL_STACK_SIZE))
371
BUFFER_DEL(emitter, emitter->buffer);
372
BUFFER_DEL(emitter, emitter->raw_buffer);
373
STACK_DEL(emitter, emitter->states);
374
QUEUE_DEL(emitter, emitter->events);
375
STACK_DEL(emitter, emitter->indents);
376
STACK_DEL(emitter, emitter->tag_directives);
382
* Destroy an emitter object.
386
yaml_emitter_delete(yaml_emitter_t *emitter)
388
assert(emitter); /* Non-NULL emitter object expected. */
390
BUFFER_DEL(emitter, emitter->buffer);
391
BUFFER_DEL(emitter, emitter->raw_buffer);
392
STACK_DEL(emitter, emitter->states);
393
while (!QUEUE_EMPTY(emitter, emitter->events)) {
394
yaml_event_delete(&DEQUEUE(emitter, emitter->events));
396
QUEUE_DEL(emitter, emitter->events);
397
STACK_DEL(emitter, emitter->indents);
398
while (!STACK_EMPTY(empty, emitter->tag_directives)) {
399
yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives);
400
yaml_free(tag_directive.handle);
401
yaml_free(tag_directive.prefix);
403
STACK_DEL(emitter, emitter->tag_directives);
404
yaml_free(emitter->anchors);
406
memset(emitter, 0, sizeof(yaml_emitter_t));
410
* String write handler.
414
yaml_string_write_handler(void *data, unsigned char *buffer, size_t size)
416
yaml_emitter_t *emitter = data;
418
if (emitter->output.string.size + *emitter->output.string.size_written
420
memcpy(emitter->output.string.buffer
421
+ *emitter->output.string.size_written,
423
emitter->output.string.size
424
- *emitter->output.string.size_written);
425
*emitter->output.string.size_written = emitter->output.string.size;
429
memcpy(emitter->output.string.buffer
430
+ *emitter->output.string.size_written, buffer, size);
431
*emitter->output.string.size_written += size;
436
* File write handler.
440
yaml_file_write_handler(void *data, unsigned char *buffer, size_t size)
442
yaml_emitter_t *emitter = data;
444
return (fwrite(buffer, 1, size, emitter->output.file) == size);
447
* Set a string output.
451
yaml_emitter_set_output_string(yaml_emitter_t *emitter,
452
unsigned char *output, size_t size, size_t *size_written)
454
assert(emitter); /* Non-NULL emitter object expected. */
455
assert(!emitter->write_handler); /* You can set the output only once. */
456
assert(output); /* Non-NULL output string expected. */
458
emitter->write_handler = yaml_string_write_handler;
459
emitter->write_handler_data = emitter;
461
emitter->output.string.buffer = output;
462
emitter->output.string.size = size;
463
emitter->output.string.size_written = size_written;
472
yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file)
474
assert(emitter); /* Non-NULL emitter object expected. */
475
assert(!emitter->write_handler); /* You can set the output only once. */
476
assert(file); /* Non-NULL file object expected. */
478
emitter->write_handler = yaml_file_write_handler;
479
emitter->write_handler_data = emitter;
481
emitter->output.file = file;
485
* Set a generic output handler.
489
yaml_emitter_set_output(yaml_emitter_t *emitter,
490
yaml_write_handler_t *handler, void *data)
492
assert(emitter); /* Non-NULL emitter object expected. */
493
assert(!emitter->write_handler); /* You can set the output only once. */
494
assert(handler); /* Non-NULL handler object expected. */
496
emitter->write_handler = handler;
497
emitter->write_handler_data = data;
501
* Set the output encoding.
505
yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding)
507
assert(emitter); /* Non-NULL emitter object expected. */
508
assert(!emitter->encoding); /* You can set encoding only once. */
510
emitter->encoding = encoding;
514
* Set the canonical output style.
518
yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical)
520
assert(emitter); /* Non-NULL emitter object expected. */
522
emitter->canonical = (canonical != 0);
526
* Set the indentation increment.
530
yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent)
532
assert(emitter); /* Non-NULL emitter object expected. */
534
emitter->best_indent = (1 < indent && indent < 10) ? indent : 2;
538
* Set the preferred line width.
542
yaml_emitter_set_width(yaml_emitter_t *emitter, int width)
544
assert(emitter); /* Non-NULL emitter object expected. */
546
emitter->best_width = (width >= 0) ? width : -1;
550
* Set if unescaped non-ASCII characters are allowed.
554
yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode)
556
assert(emitter); /* Non-NULL emitter object expected. */
558
emitter->unicode = (unicode != 0);
562
* Set the preferred line break character.
566
yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break)
568
assert(emitter); /* Non-NULL emitter object expected. */
570
emitter->line_break = line_break;
574
* Destroy a token object.
578
yaml_token_delete(yaml_token_t *token)
580
assert(token); /* Non-NULL token object expected. */
584
case YAML_TAG_DIRECTIVE_TOKEN:
585
yaml_free(token->data.tag_directive.handle);
586
yaml_free(token->data.tag_directive.prefix);
589
case YAML_ALIAS_TOKEN:
590
yaml_free(token->data.alias.value);
593
case YAML_ANCHOR_TOKEN:
594
yaml_free(token->data.anchor.value);
598
yaml_free(token->data.tag.handle);
599
yaml_free(token->data.tag.suffix);
602
case YAML_SCALAR_TOKEN:
603
yaml_free(token->data.scalar.value);
610
memset(token, 0, sizeof(yaml_token_t));
614
* Check if a string is a valid UTF-8 sequence.
616
* Check 'reader.c' for more details on UTF-8 encoding.
620
yaml_check_utf8(yaml_char_t *start, size_t length)
622
yaml_char_t *end = start+length;
623
yaml_char_t *pointer = start;
625
while (pointer < end) {
632
width = (octet & 0x80) == 0x00 ? 1 :
633
(octet & 0xE0) == 0xC0 ? 2 :
634
(octet & 0xF0) == 0xE0 ? 3 :
635
(octet & 0xF8) == 0xF0 ? 4 : 0;
636
value = (octet & 0x80) == 0x00 ? octet & 0x7F :
637
(octet & 0xE0) == 0xC0 ? octet & 0x1F :
638
(octet & 0xF0) == 0xE0 ? octet & 0x0F :
639
(octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
640
if (!width) return 0;
641
if (pointer+width > end) return 0;
642
for (k = 1; k < width; k ++) {
644
if ((octet & 0xC0) != 0x80) return 0;
645
value = (value << 6) + (octet & 0x3F);
647
if (!((width == 1) ||
648
(width == 2 && value >= 0x80) ||
649
(width == 3 && value >= 0x800) ||
650
(width == 4 && value >= 0x10000))) return 0;
659
* Create STREAM-START.
663
yaml_stream_start_event_initialize(yaml_event_t *event,
664
yaml_encoding_t encoding)
666
yaml_mark_t mark = { 0, 0, 0 };
668
assert(event); /* Non-NULL event object is expected. */
670
STREAM_START_EVENT_INIT(*event, encoding, mark, mark);
680
yaml_stream_end_event_initialize(yaml_event_t *event)
682
yaml_mark_t mark = { 0, 0, 0 };
684
assert(event); /* Non-NULL event object is expected. */
686
STREAM_END_EVENT_INIT(*event, mark, mark);
692
* Create DOCUMENT-START.
696
yaml_document_start_event_initialize(yaml_event_t *event,
697
yaml_version_directive_t *version_directive,
698
yaml_tag_directive_t *tag_directives_start,
699
yaml_tag_directive_t *tag_directives_end,
703
yaml_error_type_t error;
705
yaml_mark_t mark = { 0, 0, 0 };
706
yaml_version_directive_t *version_directive_copy = NULL;
708
yaml_tag_directive_t *start;
709
yaml_tag_directive_t *end;
710
yaml_tag_directive_t *top;
711
} tag_directives_copy = { NULL, NULL, NULL };
712
yaml_tag_directive_t value = { NULL, NULL };
714
assert(event); /* Non-NULL event object is expected. */
715
assert((tag_directives_start && tag_directives_end) ||
716
(tag_directives_start == tag_directives_end));
717
/* Valid tag directives are expected. */
719
if (version_directive) {
720
version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t));
721
if (!version_directive_copy) goto error;
722
version_directive_copy->major = version_directive->major;
723
version_directive_copy->minor = version_directive->minor;
726
if (tag_directives_start != tag_directives_end) {
727
yaml_tag_directive_t *tag_directive;
728
if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE))
730
for (tag_directive = tag_directives_start;
731
tag_directive != tag_directives_end; tag_directive ++) {
732
assert(tag_directive->handle);
733
assert(tag_directive->prefix);
734
if (!yaml_check_utf8(tag_directive->handle,
735
strlen((char *)tag_directive->handle)))
737
if (!yaml_check_utf8(tag_directive->prefix,
738
strlen((char *)tag_directive->prefix)))
740
value.handle = yaml_strdup(tag_directive->handle);
741
value.prefix = yaml_strdup(tag_directive->prefix);
742
if (!value.handle || !value.prefix) goto error;
743
if (!PUSH(&context, tag_directives_copy, value))
750
DOCUMENT_START_EVENT_INIT(*event, version_directive_copy,
751
tag_directives_copy.start, tag_directives_copy.top,
752
implicit, mark, mark);
757
yaml_free(version_directive_copy);
758
while (!STACK_EMPTY(context, tag_directives_copy)) {
759
yaml_tag_directive_t value = POP(context, tag_directives_copy);
760
yaml_free(value.handle);
761
yaml_free(value.prefix);
763
STACK_DEL(context, tag_directives_copy);
764
yaml_free(value.handle);
765
yaml_free(value.prefix);
771
* Create DOCUMENT-END.
775
yaml_document_end_event_initialize(yaml_event_t *event, int implicit)
777
yaml_mark_t mark = { 0, 0, 0 };
779
assert(event); /* Non-NULL emitter object is expected. */
781
DOCUMENT_END_EVENT_INIT(*event, implicit, mark, mark);
791
yaml_alias_event_initialize(yaml_event_t *event, yaml_char_t *anchor)
793
yaml_mark_t mark = { 0, 0, 0 };
794
yaml_char_t *anchor_copy = NULL;
796
assert(event); /* Non-NULL event object is expected. */
797
assert(anchor); /* Non-NULL anchor is expected. */
799
if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0;
801
anchor_copy = yaml_strdup(anchor);
805
ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark);
815
yaml_scalar_event_initialize(yaml_event_t *event,
816
yaml_char_t *anchor, yaml_char_t *tag,
817
yaml_char_t *value, int length,
818
int plain_implicit, int quoted_implicit,
819
yaml_scalar_style_t style)
821
yaml_mark_t mark = { 0, 0, 0 };
822
yaml_char_t *anchor_copy = NULL;
823
yaml_char_t *tag_copy = NULL;
824
yaml_char_t *value_copy = NULL;
826
assert(event); /* Non-NULL event object is expected. */
827
assert(value); /* Non-NULL anchor is expected. */
830
if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error;
831
anchor_copy = yaml_strdup(anchor);
832
if (!anchor_copy) goto error;
836
if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
837
tag_copy = yaml_strdup(tag);
838
if (!tag_copy) goto error;
842
length = strlen((char *)value);
845
if (!yaml_check_utf8(value, length)) goto error;
846
value_copy = yaml_malloc(length+1);
847
if (!value_copy) goto error;
848
memcpy(value_copy, value, length);
849
value_copy[length] = '\0';
851
SCALAR_EVENT_INIT(*event, anchor_copy, tag_copy, value_copy, length,
852
plain_implicit, quoted_implicit, style, mark, mark);
857
yaml_free(anchor_copy);
859
yaml_free(value_copy);
865
* Create SEQUENCE-START.
869
yaml_sequence_start_event_initialize(yaml_event_t *event,
870
yaml_char_t *anchor, yaml_char_t *tag, int implicit,
871
yaml_sequence_style_t style)
873
yaml_mark_t mark = { 0, 0, 0 };
874
yaml_char_t *anchor_copy = NULL;
875
yaml_char_t *tag_copy = NULL;
877
assert(event); /* Non-NULL event object is expected. */
880
if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error;
881
anchor_copy = yaml_strdup(anchor);
882
if (!anchor_copy) goto error;
886
if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
887
tag_copy = yaml_strdup(tag);
888
if (!tag_copy) goto error;
891
SEQUENCE_START_EVENT_INIT(*event, anchor_copy, tag_copy,
892
implicit, style, mark, mark);
897
yaml_free(anchor_copy);
904
* Create SEQUENCE-END.
908
yaml_sequence_end_event_initialize(yaml_event_t *event)
910
yaml_mark_t mark = { 0, 0, 0 };
912
assert(event); /* Non-NULL event object is expected. */
914
SEQUENCE_END_EVENT_INIT(*event, mark, mark);
920
* Create MAPPING-START.
924
yaml_mapping_start_event_initialize(yaml_event_t *event,
925
yaml_char_t *anchor, yaml_char_t *tag, int implicit,
926
yaml_mapping_style_t style)
928
yaml_mark_t mark = { 0, 0, 0 };
929
yaml_char_t *anchor_copy = NULL;
930
yaml_char_t *tag_copy = NULL;
932
assert(event); /* Non-NULL event object is expected. */
935
if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error;
936
anchor_copy = yaml_strdup(anchor);
937
if (!anchor_copy) goto error;
941
if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
942
tag_copy = yaml_strdup(tag);
943
if (!tag_copy) goto error;
946
MAPPING_START_EVENT_INIT(*event, anchor_copy, tag_copy,
947
implicit, style, mark, mark);
952
yaml_free(anchor_copy);
959
* Create MAPPING-END.
963
yaml_mapping_end_event_initialize(yaml_event_t *event)
965
yaml_mark_t mark = { 0, 0, 0 };
967
assert(event); /* Non-NULL event object is expected. */
969
MAPPING_END_EVENT_INIT(*event, mark, mark);
975
* Destroy an event object.
979
yaml_event_delete(yaml_event_t *event)
981
yaml_tag_directive_t *tag_directive;
983
assert(event); /* Non-NULL event object expected. */
987
case YAML_DOCUMENT_START_EVENT:
988
yaml_free(event->data.document_start.version_directive);
989
for (tag_directive = event->data.document_start.tag_directives.start;
990
tag_directive != event->data.document_start.tag_directives.end;
992
yaml_free(tag_directive->handle);
993
yaml_free(tag_directive->prefix);
995
yaml_free(event->data.document_start.tag_directives.start);
998
case YAML_ALIAS_EVENT:
999
yaml_free(event->data.alias.anchor);
1002
case YAML_SCALAR_EVENT:
1003
yaml_free(event->data.scalar.anchor);
1004
yaml_free(event->data.scalar.tag);
1005
yaml_free(event->data.scalar.value);
1008
case YAML_SEQUENCE_START_EVENT:
1009
yaml_free(event->data.sequence_start.anchor);
1010
yaml_free(event->data.sequence_start.tag);
1013
case YAML_MAPPING_START_EVENT:
1014
yaml_free(event->data.mapping_start.anchor);
1015
yaml_free(event->data.mapping_start.tag);
1022
memset(event, 0, sizeof(yaml_event_t));
1026
* Create a document object.
1030
yaml_document_initialize(yaml_document_t *document,
1031
yaml_version_directive_t *version_directive,
1032
yaml_tag_directive_t *tag_directives_start,
1033
yaml_tag_directive_t *tag_directives_end,
1034
int start_implicit, int end_implicit)
1037
yaml_error_type_t error;
1043
} nodes = { NULL, NULL, NULL };
1044
yaml_version_directive_t *version_directive_copy = NULL;
1046
yaml_tag_directive_t *start;
1047
yaml_tag_directive_t *end;
1048
yaml_tag_directive_t *top;
1049
} tag_directives_copy = { NULL, NULL, NULL };
1050
yaml_tag_directive_t value = { NULL, NULL };
1051
yaml_mark_t mark = { 0, 0, 0 };
1053
assert(document); /* Non-NULL document object is expected. */
1054
assert((tag_directives_start && tag_directives_end) ||
1055
(tag_directives_start == tag_directives_end));
1056
/* Valid tag directives are expected. */
1058
if (!STACK_INIT(&context, nodes, INITIAL_STACK_SIZE)) goto error;
1060
if (version_directive) {
1061
version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t));
1062
if (!version_directive_copy) goto error;
1063
version_directive_copy->major = version_directive->major;
1064
version_directive_copy->minor = version_directive->minor;
1067
if (tag_directives_start != tag_directives_end) {
1068
yaml_tag_directive_t *tag_directive;
1069
if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE))
1071
for (tag_directive = tag_directives_start;
1072
tag_directive != tag_directives_end; tag_directive ++) {
1073
assert(tag_directive->handle);
1074
assert(tag_directive->prefix);
1075
if (!yaml_check_utf8(tag_directive->handle,
1076
strlen((char *)tag_directive->handle)))
1078
if (!yaml_check_utf8(tag_directive->prefix,
1079
strlen((char *)tag_directive->prefix)))
1081
value.handle = yaml_strdup(tag_directive->handle);
1082
value.prefix = yaml_strdup(tag_directive->prefix);
1083
if (!value.handle || !value.prefix) goto error;
1084
if (!PUSH(&context, tag_directives_copy, value))
1086
value.handle = NULL;
1087
value.prefix = NULL;
1091
DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy,
1092
tag_directives_copy.start, tag_directives_copy.top,
1093
start_implicit, end_implicit, mark, mark);
1098
STACK_DEL(&context, nodes);
1099
yaml_free(version_directive_copy);
1100
while (!STACK_EMPTY(&context, tag_directives_copy)) {
1101
yaml_tag_directive_t value = POP(&context, tag_directives_copy);
1102
yaml_free(value.handle);
1103
yaml_free(value.prefix);
1105
STACK_DEL(&context, tag_directives_copy);
1106
yaml_free(value.handle);
1107
yaml_free(value.prefix);
1113
* Destroy a document object.
1117
yaml_document_delete(yaml_document_t *document)
1120
yaml_error_type_t error;
1122
yaml_tag_directive_t *tag_directive;
1124
context.error = YAML_NO_ERROR; /* Eliminate a compliler warning. */
1126
assert(document); /* Non-NULL document object is expected. */
1128
while (!STACK_EMPTY(&context, document->nodes)) {
1129
yaml_node_t node = POP(&context, document->nodes);
1130
yaml_free(node.tag);
1131
switch (node.type) {
1132
case YAML_SCALAR_NODE:
1133
yaml_free(node.data.scalar.value);
1135
case YAML_SEQUENCE_NODE:
1136
STACK_DEL(&context, node.data.sequence.items);
1138
case YAML_MAPPING_NODE:
1139
STACK_DEL(&context, node.data.mapping.pairs);
1142
assert(0); /* Should not happen. */
1145
STACK_DEL(&context, document->nodes);
1147
yaml_free(document->version_directive);
1148
for (tag_directive = document->tag_directives.start;
1149
tag_directive != document->tag_directives.end;
1151
yaml_free(tag_directive->handle);
1152
yaml_free(tag_directive->prefix);
1154
yaml_free(document->tag_directives.start);
1156
memset(document, 0, sizeof(yaml_document_t));
1160
* Get a document node.
1163
YAML_DECLARE(yaml_node_t *)
1164
yaml_document_get_node(yaml_document_t *document, int index)
1166
assert(document); /* Non-NULL document object is expected. */
1168
if (index > 0 && document->nodes.start + index <= document->nodes.top) {
1169
return document->nodes.start + index - 1;
1175
* Get the root object.
1178
YAML_DECLARE(yaml_node_t *)
1179
yaml_document_get_root_node(yaml_document_t *document)
1181
assert(document); /* Non-NULL document object is expected. */
1183
if (document->nodes.top != document->nodes.start) {
1184
return document->nodes.start;
1190
* Add a scalar node to a document.
1194
yaml_document_add_scalar(yaml_document_t *document,
1195
yaml_char_t *tag, yaml_char_t *value, int length,
1196
yaml_scalar_style_t style)
1199
yaml_error_type_t error;
1201
yaml_mark_t mark = { 0, 0, 0 };
1202
yaml_char_t *tag_copy = NULL;
1203
yaml_char_t *value_copy = NULL;
1206
assert(document); /* Non-NULL document object is expected. */
1207
assert(value); /* Non-NULL value is expected. */
1210
tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG;
1213
if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
1214
tag_copy = yaml_strdup(tag);
1215
if (!tag_copy) goto error;
1218
length = strlen((char *)value);
1221
if (!yaml_check_utf8(value, length)) goto error;
1222
value_copy = yaml_malloc(length+1);
1223
if (!value_copy) goto error;
1224
memcpy(value_copy, value, length);
1225
value_copy[length] = '\0';
1227
SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark);
1228
if (!PUSH(&context, document->nodes, node)) goto error;
1230
return document->nodes.top - document->nodes.start;
1233
yaml_free(tag_copy);
1234
yaml_free(value_copy);
1240
* Add a sequence node to a document.
1244
yaml_document_add_sequence(yaml_document_t *document,
1245
yaml_char_t *tag, yaml_sequence_style_t style)
1248
yaml_error_type_t error;
1250
yaml_mark_t mark = { 0, 0, 0 };
1251
yaml_char_t *tag_copy = NULL;
1253
yaml_node_item_t *start;
1254
yaml_node_item_t *end;
1255
yaml_node_item_t *top;
1256
} items = { NULL, NULL, NULL };
1259
assert(document); /* Non-NULL document object is expected. */
1262
tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG;
1265
if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
1266
tag_copy = yaml_strdup(tag);
1267
if (!tag_copy) goto error;
1269
if (!STACK_INIT(&context, items, INITIAL_STACK_SIZE)) goto error;
1271
SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end,
1273
if (!PUSH(&context, document->nodes, node)) goto error;
1275
return document->nodes.top - document->nodes.start;
1278
STACK_DEL(&context, items);
1279
yaml_free(tag_copy);
1285
* Add a mapping node to a document.
1289
yaml_document_add_mapping(yaml_document_t *document,
1290
yaml_char_t *tag, yaml_mapping_style_t style)
1293
yaml_error_type_t error;
1295
yaml_mark_t mark = { 0, 0, 0 };
1296
yaml_char_t *tag_copy = NULL;
1298
yaml_node_pair_t *start;
1299
yaml_node_pair_t *end;
1300
yaml_node_pair_t *top;
1301
} pairs = { NULL, NULL, NULL };
1304
assert(document); /* Non-NULL document object is expected. */
1307
tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG;
1310
if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
1311
tag_copy = yaml_strdup(tag);
1312
if (!tag_copy) goto error;
1314
if (!STACK_INIT(&context, pairs, INITIAL_STACK_SIZE)) goto error;
1316
MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end,
1318
if (!PUSH(&context, document->nodes, node)) goto error;
1320
return document->nodes.top - document->nodes.start;
1323
STACK_DEL(&context, pairs);
1324
yaml_free(tag_copy);
1330
* Append an item to a sequence node.
1334
yaml_document_append_sequence_item(yaml_document_t *document,
1335
int sequence, int item)
1338
yaml_error_type_t error;
1341
assert(document); /* Non-NULL document is required. */
1343
&& document->nodes.start + sequence <= document->nodes.top);
1344
/* Valid sequence id is required. */
1345
assert(document->nodes.start[sequence-1].type == YAML_SEQUENCE_NODE);
1346
/* A sequence node is required. */
1347
assert(item > 0 && document->nodes.start + item <= document->nodes.top);
1348
/* Valid item id is required. */
1351
document->nodes.start[sequence-1].data.sequence.items, item))
1358
* Append a pair of a key and a value to a mapping node.
1362
yaml_document_append_mapping_pair(yaml_document_t *document,
1363
int mapping, int key, int value)
1366
yaml_error_type_t error;
1369
yaml_node_pair_t pair;
1371
assert(document); /* Non-NULL document is required. */
1373
&& document->nodes.start + mapping <= document->nodes.top);
1374
/* Valid mapping id is required. */
1375
assert(document->nodes.start[mapping-1].type == YAML_MAPPING_NODE);
1376
/* A mapping node is required. */
1377
assert(key > 0 && document->nodes.start + key <= document->nodes.top);
1378
/* Valid key id is required. */
1379
assert(value > 0 && document->nodes.start + value <= document->nodes.top);
1380
/* Valid value id is required. */
1386
document->nodes.start[mapping-1].data.mapping.pairs, pair))