8 main(int argc, char *argv[])
17 yaml_emitter_t emitter;
18 yaml_event_t input_event;
19 yaml_event_t output_event;
21 /* Clear the objects. */
23 memset(&parser, 0, sizeof(parser));
24 memset(&emitter, 0, sizeof(emitter));
25 memset(&input_event, 0, sizeof(input_event));
26 memset(&output_event, 0, sizeof(output_event));
28 /* Analyze command line options. */
30 for (k = 1; k < argc; k ++)
32 if (strcmp(argv[k], "-h") == 0
33 || strcmp(argv[k], "--help") == 0) {
37 else if (strcmp(argv[k], "-c") == 0
38 || strcmp(argv[k], "--canonical") == 0) {
42 else if (strcmp(argv[k], "-u") == 0
43 || strcmp(argv[k], "--unicode") == 0) {
48 fprintf(stderr, "Unrecognized option: %s\n"
49 "Try `%s --help` for more information.\n",
55 /* Display the help string. */
60 "or\n%s -h | --help\nDeconstruct a YAML stream\n\nOptions:\n"
61 "-h, --help\t\tdisplay this help and exit\n"
62 "-c, --canonical\t\toutput in the canonical YAML format\n"
63 "-u, --unicode\t\toutput unescaped non-ASCII characters\n",
68 /* Initialize the parser and emitter objects. */
70 if (!yaml_parser_initialize(&parser)) {
71 fprintf(stderr, "Could not initialize the parser object\n");
75 if (!yaml_emitter_initialize(&emitter)) {
76 yaml_parser_delete(&parser);
77 fprintf(stderr, "Could not inialize the emitter object\n");
81 /* Set the parser parameters. */
83 yaml_parser_set_input_file(&parser, stdin);
85 /* Set the emitter parameters. */
87 yaml_emitter_set_output_file(&emitter, stdout);
89 yaml_emitter_set_canonical(&emitter, canonical);
90 yaml_emitter_set_unicode(&emitter, unicode);
92 /* Create and emit the STREAM-START event. */
94 if (!yaml_stream_start_event_initialize(&output_event, YAML_UTF8_ENCODING))
96 if (!yaml_emitter_emit(&emitter, &output_event))
99 /* Create and emit the DOCUMENT-START event. */
101 if (!yaml_document_start_event_initialize(&output_event,
102 NULL, NULL, NULL, 0))
104 if (!yaml_emitter_emit(&emitter, &output_event))
107 /* Create and emit the SEQUENCE-START event. */
109 if (!yaml_sequence_start_event_initialize(&output_event,
110 NULL, "tag:yaml.org,2002:seq", 1,
111 YAML_BLOCK_SEQUENCE_STYLE))
113 if (!yaml_emitter_emit(&emitter, &output_event))
116 /* Loop through the input events. */
120 /* Get the next event. */
122 if (!yaml_parser_parse(&parser, &input_event))
125 /* Check if this is the stream end. */
127 if (input_event.type == YAML_STREAM_END_EVENT) {
131 /* Create and emit a MAPPING-START event. */
133 if (!yaml_mapping_start_event_initialize(&output_event,
134 NULL, "tag:yaml.org,2002:map", 1,
135 YAML_BLOCK_MAPPING_STYLE))
137 if (!yaml_emitter_emit(&emitter, &output_event))
140 /* Analyze the event. */
142 switch (input_event.type)
144 case YAML_STREAM_START_EVENT:
148 if (!yaml_scalar_event_initialize(&output_event,
149 NULL, "tag:yaml.org,2002:str", "type", -1,
150 1, 1, YAML_PLAIN_SCALAR_STYLE))
152 if (!yaml_emitter_emit(&emitter, &output_event))
155 /* Write 'STREAM-START'. */
157 if (!yaml_scalar_event_initialize(&output_event,
158 NULL, "tag:yaml.org,2002:str", "STREAM-START", -1,
159 1, 1, YAML_PLAIN_SCALAR_STYLE))
161 if (!yaml_emitter_emit(&emitter, &output_event))
164 /* Display encoding information. */
166 if (input_event.data.stream_start.encoding)
168 yaml_encoding_t encoding
169 = input_event.data.stream_start.encoding;
171 /* Write 'encoding'. */
173 if (!yaml_scalar_event_initialize(&output_event,
174 NULL, "tag:yaml.org,2002:str", "encoding", -1,
175 1, 1, YAML_PLAIN_SCALAR_STYLE))
177 if (!yaml_emitter_emit(&emitter, &output_event))
180 /* Write the stream encoding. */
182 if (!yaml_scalar_event_initialize(&output_event,
183 NULL, "tag:yaml.org,2002:str",
184 (encoding == YAML_UTF8_ENCODING ? "utf-8" :
185 encoding == YAML_UTF16LE_ENCODING ? "utf-16-le" :
186 encoding == YAML_UTF16BE_ENCODING ? "utf-16-be" :
188 1, 1, YAML_PLAIN_SCALAR_STYLE))
190 if (!yaml_emitter_emit(&emitter, &output_event))
196 case YAML_STREAM_END_EVENT:
200 if (!yaml_scalar_event_initialize(&output_event,
201 NULL, "tag:yaml.org,2002:str", "type", -1,
202 1, 1, YAML_PLAIN_SCALAR_STYLE))
204 if (!yaml_emitter_emit(&emitter, &output_event))
207 /* Write 'STREAM-END'. */
209 if (!yaml_scalar_event_initialize(&output_event,
210 NULL, "tag:yaml.org,2002:str", "STREAM-END", -1,
211 1, 1, YAML_PLAIN_SCALAR_STYLE))
213 if (!yaml_emitter_emit(&emitter, &output_event))
218 case YAML_DOCUMENT_START_EVENT:
222 if (!yaml_scalar_event_initialize(&output_event,
223 NULL, "tag:yaml.org,2002:str", "type", -1,
224 1, 1, YAML_PLAIN_SCALAR_STYLE))
226 if (!yaml_emitter_emit(&emitter, &output_event))
229 /* Write 'DOCUMENT-START'. */
231 if (!yaml_scalar_event_initialize(&output_event,
232 NULL, "tag:yaml.org,2002:str", "DOCUMENT-START", -1,
233 1, 1, YAML_PLAIN_SCALAR_STYLE))
235 if (!yaml_emitter_emit(&emitter, &output_event))
238 /* Display the document version numbers. */
240 if (input_event.data.document_start.version_directive)
242 yaml_version_directive_t *version
243 = input_event.data.document_start.version_directive;
246 /* Write 'version'. */
248 if (!yaml_scalar_event_initialize(&output_event,
249 NULL, "tag:yaml.org,2002:str", "version", -1,
250 1, 1, YAML_PLAIN_SCALAR_STYLE))
252 if (!yaml_emitter_emit(&emitter, &output_event))
257 if (!yaml_mapping_start_event_initialize(&output_event,
258 NULL, "tag:yaml.org,2002:map", 1,
259 YAML_FLOW_MAPPING_STYLE))
261 if (!yaml_emitter_emit(&emitter, &output_event))
266 if (!yaml_scalar_event_initialize(&output_event,
267 NULL, "tag:yaml.org,2002:str", "major", -1,
268 1, 1, YAML_PLAIN_SCALAR_STYLE))
270 if (!yaml_emitter_emit(&emitter, &output_event))
273 /* Write a number. */
275 sprintf(number, "%d", version->major);
276 if (!yaml_scalar_event_initialize(&output_event,
277 NULL, "tag:yaml.org,2002:int", number, -1,
278 1, 1, YAML_PLAIN_SCALAR_STYLE))
280 if (!yaml_emitter_emit(&emitter, &output_event))
285 if (!yaml_scalar_event_initialize(&output_event,
286 NULL, "tag:yaml.org,2002:str", "minor", -1,
287 1, 1, YAML_PLAIN_SCALAR_STYLE))
289 if (!yaml_emitter_emit(&emitter, &output_event))
292 /* Write a number. */
294 sprintf(number, "%d", version->minor);
295 if (!yaml_scalar_event_initialize(&output_event,
296 NULL, "tag:yaml.org,2002:int", number, -1,
297 1, 1, YAML_PLAIN_SCALAR_STYLE))
299 if (!yaml_emitter_emit(&emitter, &output_event))
304 if (!yaml_mapping_end_event_initialize(&output_event))
306 if (!yaml_emitter_emit(&emitter, &output_event))
310 /* Display the document tag directives. */
312 if (input_event.data.document_start.tag_directives.start
313 != input_event.data.document_start.tag_directives.end)
315 yaml_tag_directive_t *tag;
319 if (!yaml_scalar_event_initialize(&output_event,
320 NULL, "tag:yaml.org,2002:str", "tags", -1,
321 1, 1, YAML_PLAIN_SCALAR_STYLE))
323 if (!yaml_emitter_emit(&emitter, &output_event))
326 /* Start a block sequence. */
328 if (!yaml_sequence_start_event_initialize(&output_event,
329 NULL, "tag:yaml.org,2002:seq", 1,
330 YAML_BLOCK_SEQUENCE_STYLE))
332 if (!yaml_emitter_emit(&emitter, &output_event))
335 for (tag = input_event.data.document_start.tag_directives.start;
336 tag != input_event.data.document_start.tag_directives.end;
341 if (!yaml_mapping_start_event_initialize(&output_event,
342 NULL, "tag:yaml.org,2002:map", 1,
343 YAML_FLOW_MAPPING_STYLE))
345 if (!yaml_emitter_emit(&emitter, &output_event))
348 /* Write 'handle'. */
350 if (!yaml_scalar_event_initialize(&output_event,
351 NULL, "tag:yaml.org,2002:str", "handle", -1,
352 1, 1, YAML_PLAIN_SCALAR_STYLE))
354 if (!yaml_emitter_emit(&emitter, &output_event))
357 /* Write the tag directive handle. */
359 if (!yaml_scalar_event_initialize(&output_event,
360 NULL, "tag:yaml.org,2002:str",
362 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
364 if (!yaml_emitter_emit(&emitter, &output_event))
367 /* Write 'prefix'. */
369 if (!yaml_scalar_event_initialize(&output_event,
370 NULL, "tag:yaml.org,2002:str", "prefix", -1,
371 1, 1, YAML_PLAIN_SCALAR_STYLE))
373 if (!yaml_emitter_emit(&emitter, &output_event))
376 /* Write the tag directive prefix. */
378 if (!yaml_scalar_event_initialize(&output_event,
379 NULL, "tag:yaml.org,2002:str",
381 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
383 if (!yaml_emitter_emit(&emitter, &output_event))
388 if (!yaml_mapping_end_event_initialize(&output_event))
390 if (!yaml_emitter_emit(&emitter, &output_event))
394 /* End a block sequence. */
396 if (!yaml_sequence_end_event_initialize(&output_event))
398 if (!yaml_emitter_emit(&emitter, &output_event))
402 /* Write 'implicit'. */
404 if (!yaml_scalar_event_initialize(&output_event,
405 NULL, "tag:yaml.org,2002:str", "implicit", -1,
406 1, 1, YAML_PLAIN_SCALAR_STYLE))
408 if (!yaml_emitter_emit(&emitter, &output_event))
411 /* Write if the document is implicit. */
413 if (!yaml_scalar_event_initialize(&output_event,
414 NULL, "tag:yaml.org,2002:bool",
415 (input_event.data.document_start.implicit ?
416 "true" : "false"), -1,
417 1, 0, YAML_PLAIN_SCALAR_STYLE))
419 if (!yaml_emitter_emit(&emitter, &output_event))
424 case YAML_DOCUMENT_END_EVENT:
428 if (!yaml_scalar_event_initialize(&output_event,
429 NULL, "tag:yaml.org,2002:str", "type", -1,
430 1, 1, YAML_PLAIN_SCALAR_STYLE))
432 if (!yaml_emitter_emit(&emitter, &output_event))
435 /* Write 'DOCUMENT-END'. */
437 if (!yaml_scalar_event_initialize(&output_event,
438 NULL, "tag:yaml.org,2002:str", "DOCUMENT-END", -1,
439 1, 1, YAML_PLAIN_SCALAR_STYLE))
441 if (!yaml_emitter_emit(&emitter, &output_event))
444 /* Write 'implicit'. */
446 if (!yaml_scalar_event_initialize(&output_event,
447 NULL, "tag:yaml.org,2002:str", "implicit", -1,
448 1, 1, YAML_PLAIN_SCALAR_STYLE))
450 if (!yaml_emitter_emit(&emitter, &output_event))
453 /* Write if the document is implicit. */
455 if (!yaml_scalar_event_initialize(&output_event,
456 NULL, "tag:yaml.org,2002:bool",
457 (input_event.data.document_end.implicit ?
458 "true" : "false"), -1,
459 1, 0, YAML_PLAIN_SCALAR_STYLE))
461 if (!yaml_emitter_emit(&emitter, &output_event))
466 case YAML_ALIAS_EVENT:
470 if (!yaml_scalar_event_initialize(&output_event,
471 NULL, "tag:yaml.org,2002:str", "type", -1,
472 1, 1, YAML_PLAIN_SCALAR_STYLE))
474 if (!yaml_emitter_emit(&emitter, &output_event))
479 if (!yaml_scalar_event_initialize(&output_event,
480 NULL, "tag:yaml.org,2002:str", "ALIAS", -1,
481 1, 1, YAML_PLAIN_SCALAR_STYLE))
483 if (!yaml_emitter_emit(&emitter, &output_event))
486 /* Write 'anchor'. */
488 if (!yaml_scalar_event_initialize(&output_event,
489 NULL, "tag:yaml.org,2002:str", "anchor", -1,
490 1, 1, YAML_PLAIN_SCALAR_STYLE))
492 if (!yaml_emitter_emit(&emitter, &output_event))
495 /* Write the alias anchor. */
497 if (!yaml_scalar_event_initialize(&output_event,
498 NULL, "tag:yaml.org,2002:str",
499 input_event.data.alias.anchor, -1,
500 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
502 if (!yaml_emitter_emit(&emitter, &output_event))
507 case YAML_SCALAR_EVENT:
511 if (!yaml_scalar_event_initialize(&output_event,
512 NULL, "tag:yaml.org,2002:str", "type", -1,
513 1, 1, YAML_PLAIN_SCALAR_STYLE))
515 if (!yaml_emitter_emit(&emitter, &output_event))
518 /* Write 'SCALAR'. */
520 if (!yaml_scalar_event_initialize(&output_event,
521 NULL, "tag:yaml.org,2002:str", "SCALAR", -1,
522 1, 1, YAML_PLAIN_SCALAR_STYLE))
524 if (!yaml_emitter_emit(&emitter, &output_event))
527 /* Display the scalar anchor. */
529 if (input_event.data.scalar.anchor)
531 /* Write 'anchor'. */
533 if (!yaml_scalar_event_initialize(&output_event,
534 NULL, "tag:yaml.org,2002:str", "anchor", -1,
535 1, 1, YAML_PLAIN_SCALAR_STYLE))
537 if (!yaml_emitter_emit(&emitter, &output_event))
540 /* Write the scalar anchor. */
542 if (!yaml_scalar_event_initialize(&output_event,
543 NULL, "tag:yaml.org,2002:str",
544 input_event.data.scalar.anchor, -1,
545 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
547 if (!yaml_emitter_emit(&emitter, &output_event))
551 /* Display the scalar tag. */
553 if (input_event.data.scalar.tag)
557 if (!yaml_scalar_event_initialize(&output_event,
558 NULL, "tag:yaml.org,2002:str", "tag", -1,
559 1, 1, YAML_PLAIN_SCALAR_STYLE))
561 if (!yaml_emitter_emit(&emitter, &output_event))
564 /* Write the scalar tag. */
566 if (!yaml_scalar_event_initialize(&output_event,
567 NULL, "tag:yaml.org,2002:str",
568 input_event.data.scalar.tag, -1,
569 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
571 if (!yaml_emitter_emit(&emitter, &output_event))
575 /* Display the scalar value. */
579 if (!yaml_scalar_event_initialize(&output_event,
580 NULL, "tag:yaml.org,2002:str", "value", -1,
581 1, 1, YAML_PLAIN_SCALAR_STYLE))
583 if (!yaml_emitter_emit(&emitter, &output_event))
586 /* Write the scalar value. */
588 if (!yaml_scalar_event_initialize(&output_event,
589 NULL, "tag:yaml.org,2002:str",
590 input_event.data.scalar.value,
591 input_event.data.scalar.length,
592 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
594 if (!yaml_emitter_emit(&emitter, &output_event))
597 /* Display if the scalar tag is implicit. */
599 /* Write 'implicit'. */
601 if (!yaml_scalar_event_initialize(&output_event,
602 NULL, "tag:yaml.org,2002:str", "implicit", -1,
603 1, 1, YAML_PLAIN_SCALAR_STYLE))
605 if (!yaml_emitter_emit(&emitter, &output_event))
610 if (!yaml_mapping_start_event_initialize(&output_event,
611 NULL, "tag:yaml.org,2002:map", 1,
612 YAML_FLOW_MAPPING_STYLE))
614 if (!yaml_emitter_emit(&emitter, &output_event))
619 if (!yaml_scalar_event_initialize(&output_event,
620 NULL, "tag:yaml.org,2002:str", "plain", -1,
621 1, 1, YAML_PLAIN_SCALAR_STYLE))
623 if (!yaml_emitter_emit(&emitter, &output_event))
626 /* Write if the scalar is implicit in the plain style. */
628 if (!yaml_scalar_event_initialize(&output_event,
629 NULL, "tag:yaml.org,2002:bool",
630 (input_event.data.scalar.plain_implicit ?
631 "true" : "false"), -1,
632 1, 0, YAML_PLAIN_SCALAR_STYLE))
634 if (!yaml_emitter_emit(&emitter, &output_event))
637 /* Write 'quoted'. */
639 if (!yaml_scalar_event_initialize(&output_event,
640 NULL, "tag:yaml.org,2002:str", "non-plain", -1,
641 1, 1, YAML_PLAIN_SCALAR_STYLE))
643 if (!yaml_emitter_emit(&emitter, &output_event))
646 /* Write if the scalar is implicit in a non-plain style. */
648 if (!yaml_scalar_event_initialize(&output_event,
649 NULL, "tag:yaml.org,2002:bool",
650 (input_event.data.scalar.quoted_implicit ?
651 "true" : "false"), -1,
652 1, 0, YAML_PLAIN_SCALAR_STYLE))
654 if (!yaml_emitter_emit(&emitter, &output_event))
659 if (!yaml_mapping_end_event_initialize(&output_event))
661 if (!yaml_emitter_emit(&emitter, &output_event))
664 /* Display the style information. */
666 if (input_event.data.scalar.style)
668 yaml_scalar_style_t style = input_event.data.scalar.style;
672 if (!yaml_scalar_event_initialize(&output_event,
673 NULL, "tag:yaml.org,2002:str", "style", -1,
674 1, 1, YAML_PLAIN_SCALAR_STYLE))
676 if (!yaml_emitter_emit(&emitter, &output_event))
679 /* Write the scalar style. */
681 if (!yaml_scalar_event_initialize(&output_event,
682 NULL, "tag:yaml.org,2002:str",
683 (style == YAML_PLAIN_SCALAR_STYLE ? "plain" :
684 style == YAML_SINGLE_QUOTED_SCALAR_STYLE ?
686 style == YAML_DOUBLE_QUOTED_SCALAR_STYLE ?
688 style == YAML_LITERAL_SCALAR_STYLE ? "literal" :
689 style == YAML_FOLDED_SCALAR_STYLE ? "folded" :
691 1, 1, YAML_PLAIN_SCALAR_STYLE))
693 if (!yaml_emitter_emit(&emitter, &output_event))
699 case YAML_SEQUENCE_START_EVENT:
703 if (!yaml_scalar_event_initialize(&output_event,
704 NULL, "tag:yaml.org,2002:str", "type", -1,
705 1, 1, YAML_PLAIN_SCALAR_STYLE))
707 if (!yaml_emitter_emit(&emitter, &output_event))
710 /* Write 'SEQUENCE-START'. */
712 if (!yaml_scalar_event_initialize(&output_event,
713 NULL, "tag:yaml.org,2002:str", "SEQUENCE-START", -1,
714 1, 1, YAML_PLAIN_SCALAR_STYLE))
716 if (!yaml_emitter_emit(&emitter, &output_event))
719 /* Display the sequence anchor. */
721 if (input_event.data.sequence_start.anchor)
723 /* Write 'anchor'. */
725 if (!yaml_scalar_event_initialize(&output_event,
726 NULL, "tag:yaml.org,2002:str", "anchor", -1,
727 1, 1, YAML_PLAIN_SCALAR_STYLE))
729 if (!yaml_emitter_emit(&emitter, &output_event))
732 /* Write the sequence anchor. */
734 if (!yaml_scalar_event_initialize(&output_event,
735 NULL, "tag:yaml.org,2002:str",
736 input_event.data.sequence_start.anchor, -1,
737 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
739 if (!yaml_emitter_emit(&emitter, &output_event))
743 /* Display the sequence tag. */
745 if (input_event.data.sequence_start.tag)
749 if (!yaml_scalar_event_initialize(&output_event,
750 NULL, "tag:yaml.org,2002:str", "tag", -1,
751 1, 1, YAML_PLAIN_SCALAR_STYLE))
753 if (!yaml_emitter_emit(&emitter, &output_event))
756 /* Write the sequence tag. */
758 if (!yaml_scalar_event_initialize(&output_event,
759 NULL, "tag:yaml.org,2002:str",
760 input_event.data.sequence_start.tag, -1,
761 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
763 if (!yaml_emitter_emit(&emitter, &output_event))
767 /* Write 'implicit'. */
769 if (!yaml_scalar_event_initialize(&output_event,
770 NULL, "tag:yaml.org,2002:str", "implicit", -1,
771 1, 1, YAML_PLAIN_SCALAR_STYLE))
773 if (!yaml_emitter_emit(&emitter, &output_event))
776 /* Write if the sequence tag is implicit. */
778 if (!yaml_scalar_event_initialize(&output_event,
779 NULL, "tag:yaml.org,2002:bool",
780 (input_event.data.sequence_start.implicit ?
781 "true" : "false"), -1,
782 1, 0, YAML_PLAIN_SCALAR_STYLE))
784 if (!yaml_emitter_emit(&emitter, &output_event))
787 /* Display the style information. */
789 if (input_event.data.sequence_start.style)
791 yaml_sequence_style_t style
792 = input_event.data.sequence_start.style;
796 if (!yaml_scalar_event_initialize(&output_event,
797 NULL, "tag:yaml.org,2002:str", "style", -1,
798 1, 1, YAML_PLAIN_SCALAR_STYLE))
800 if (!yaml_emitter_emit(&emitter, &output_event))
803 /* Write the scalar style. */
805 if (!yaml_scalar_event_initialize(&output_event,
806 NULL, "tag:yaml.org,2002:str",
807 (style == YAML_BLOCK_SEQUENCE_STYLE ? "block" :
808 style == YAML_FLOW_SEQUENCE_STYLE ? "flow" :
810 1, 1, YAML_PLAIN_SCALAR_STYLE))
812 if (!yaml_emitter_emit(&emitter, &output_event))
818 case YAML_SEQUENCE_END_EVENT:
822 if (!yaml_scalar_event_initialize(&output_event,
823 NULL, "tag:yaml.org,2002:str", "type", -1,
824 1, 1, YAML_PLAIN_SCALAR_STYLE))
826 if (!yaml_emitter_emit(&emitter, &output_event))
829 /* Write 'SEQUENCE-END'. */
831 if (!yaml_scalar_event_initialize(&output_event,
832 NULL, "tag:yaml.org,2002:str", "SEQUENCE-END", -1,
833 1, 1, YAML_PLAIN_SCALAR_STYLE))
835 if (!yaml_emitter_emit(&emitter, &output_event))
840 case YAML_MAPPING_START_EVENT:
844 if (!yaml_scalar_event_initialize(&output_event,
845 NULL, "tag:yaml.org,2002:str", "type", -1,
846 1, 1, YAML_PLAIN_SCALAR_STYLE))
848 if (!yaml_emitter_emit(&emitter, &output_event))
851 /* Write 'MAPPING-START'. */
853 if (!yaml_scalar_event_initialize(&output_event,
854 NULL, "tag:yaml.org,2002:str", "MAPPING-START", -1,
855 1, 1, YAML_PLAIN_SCALAR_STYLE))
857 if (!yaml_emitter_emit(&emitter, &output_event))
860 /* Display the mapping anchor. */
862 if (input_event.data.mapping_start.anchor)
864 /* Write 'anchor'. */
866 if (!yaml_scalar_event_initialize(&output_event,
867 NULL, "tag:yaml.org,2002:str", "anchor", -1,
868 1, 1, YAML_PLAIN_SCALAR_STYLE))
870 if (!yaml_emitter_emit(&emitter, &output_event))
873 /* Write the mapping anchor. */
875 if (!yaml_scalar_event_initialize(&output_event,
876 NULL, "tag:yaml.org,2002:str",
877 input_event.data.mapping_start.anchor, -1,
878 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
880 if (!yaml_emitter_emit(&emitter, &output_event))
884 /* Display the mapping tag. */
886 if (input_event.data.mapping_start.tag)
890 if (!yaml_scalar_event_initialize(&output_event,
891 NULL, "tag:yaml.org,2002:str", "tag", -1,
892 1, 1, YAML_PLAIN_SCALAR_STYLE))
894 if (!yaml_emitter_emit(&emitter, &output_event))
897 /* Write the mapping tag. */
899 if (!yaml_scalar_event_initialize(&output_event,
900 NULL, "tag:yaml.org,2002:str",
901 input_event.data.mapping_start.tag, -1,
902 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
904 if (!yaml_emitter_emit(&emitter, &output_event))
908 /* Write 'implicit'. */
910 if (!yaml_scalar_event_initialize(&output_event,
911 NULL, "tag:yaml.org,2002:str", "implicit", -1,
912 1, 1, YAML_PLAIN_SCALAR_STYLE))
914 if (!yaml_emitter_emit(&emitter, &output_event))
917 /* Write if the mapping tag is implicit. */
919 if (!yaml_scalar_event_initialize(&output_event,
920 NULL, "tag:yaml.org,2002:bool",
921 (input_event.data.mapping_start.implicit ?
922 "true" : "false"), -1,
923 1, 0, YAML_PLAIN_SCALAR_STYLE))
925 if (!yaml_emitter_emit(&emitter, &output_event))
928 /* Display the style information. */
930 if (input_event.data.mapping_start.style)
932 yaml_mapping_style_t style
933 = input_event.data.mapping_start.style;
937 if (!yaml_scalar_event_initialize(&output_event,
938 NULL, "tag:yaml.org,2002:str", "style", -1,
939 1, 1, YAML_PLAIN_SCALAR_STYLE))
941 if (!yaml_emitter_emit(&emitter, &output_event))
944 /* Write the scalar style. */
946 if (!yaml_scalar_event_initialize(&output_event,
947 NULL, "tag:yaml.org,2002:str",
948 (style == YAML_BLOCK_MAPPING_STYLE ? "block" :
949 style == YAML_FLOW_MAPPING_STYLE ? "flow" :
951 1, 1, YAML_PLAIN_SCALAR_STYLE))
953 if (!yaml_emitter_emit(&emitter, &output_event))
959 case YAML_MAPPING_END_EVENT:
963 if (!yaml_scalar_event_initialize(&output_event,
964 NULL, "tag:yaml.org,2002:str", "type", -1,
965 1, 1, YAML_PLAIN_SCALAR_STYLE))
967 if (!yaml_emitter_emit(&emitter, &output_event))
970 /* Write 'MAPPING-END'. */
972 if (!yaml_scalar_event_initialize(&output_event,
973 NULL, "tag:yaml.org,2002:str", "MAPPING-END", -1,
974 1, 1, YAML_PLAIN_SCALAR_STYLE))
976 if (!yaml_emitter_emit(&emitter, &output_event))
982 /* It couldn't really happen. */
986 /* Delete the event object. */
988 yaml_event_delete(&input_event);
990 /* Create and emit a MAPPING-END event. */
992 if (!yaml_mapping_end_event_initialize(&output_event))
994 if (!yaml_emitter_emit(&emitter, &output_event))
998 /* Create and emit the SEQUENCE-END event. */
1000 if (!yaml_sequence_end_event_initialize(&output_event))
1002 if (!yaml_emitter_emit(&emitter, &output_event))
1005 /* Create and emit the DOCUMENT-END event. */
1007 if (!yaml_document_end_event_initialize(&output_event, 0))
1009 if (!yaml_emitter_emit(&emitter, &output_event))
1012 /* Create and emit the STREAM-END event. */
1014 if (!yaml_stream_end_event_initialize(&output_event))
1016 if (!yaml_emitter_emit(&emitter, &output_event))
1019 yaml_parser_delete(&parser);
1020 yaml_emitter_delete(&emitter);
1026 /* Display a parser error message. */
1028 switch (parser.error)
1030 case YAML_MEMORY_ERROR:
1031 fprintf(stderr, "Memory error: Not enough memory for parsing\n");
1034 case YAML_READER_ERROR:
1035 if (parser.problem_value != -1) {
1036 fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem,
1037 parser.problem_value, parser.problem_offset);
1040 fprintf(stderr, "Reader error: %s at %d\n", parser.problem,
1041 parser.problem_offset);
1045 case YAML_SCANNER_ERROR:
1046 if (parser.context) {
1047 fprintf(stderr, "Scanner error: %s at line %d, column %d\n"
1048 "%s at line %d, column %d\n", parser.context,
1049 parser.context_mark.line+1, parser.context_mark.column+1,
1050 parser.problem, parser.problem_mark.line+1,
1051 parser.problem_mark.column+1);
1054 fprintf(stderr, "Scanner error: %s at line %d, column %d\n",
1055 parser.problem, parser.problem_mark.line+1,
1056 parser.problem_mark.column+1);
1060 case YAML_PARSER_ERROR:
1061 if (parser.context) {
1062 fprintf(stderr, "Parser error: %s at line %d, column %d\n"
1063 "%s at line %d, column %d\n", parser.context,
1064 parser.context_mark.line+1, parser.context_mark.column+1,
1065 parser.problem, parser.problem_mark.line+1,
1066 parser.problem_mark.column+1);
1069 fprintf(stderr, "Parser error: %s at line %d, column %d\n",
1070 parser.problem, parser.problem_mark.line+1,
1071 parser.problem_mark.column+1);
1076 /* Couldn't happen. */
1077 fprintf(stderr, "Internal error\n");
1081 yaml_event_delete(&input_event);
1082 yaml_parser_delete(&parser);
1083 yaml_emitter_delete(&emitter);
1089 /* Display an emitter error message. */
1091 switch (emitter.error)
1093 case YAML_MEMORY_ERROR:
1094 fprintf(stderr, "Memory error: Not enough memory for emitting\n");
1097 case YAML_WRITER_ERROR:
1098 fprintf(stderr, "Writer error: %s\n", emitter.problem);
1101 case YAML_EMITTER_ERROR:
1102 fprintf(stderr, "Emitter error: %s\n", emitter.problem);
1106 /* Couldn't happen. */
1107 fprintf(stderr, "Internal error\n");
1111 yaml_event_delete(&input_event);
1112 yaml_parser_delete(&parser);
1113 yaml_emitter_delete(&emitter);
1119 fprintf(stderr, "Memory error: Not enough memory for creating an event\n");
1121 yaml_event_delete(&input_event);
1122 yaml_parser_delete(&parser);
1123 yaml_emitter_delete(&emitter);