2 #include "yaml_private.h"
5 * Flush the buffer if needed.
8 #define FLUSH(emitter) \
9 ((emitter->buffer.pointer+5 < emitter->buffer.end) \
10 || yaml_emitter_flush(emitter))
13 * Put a character to the output buffer.
16 #define PUT(emitter,value) \
18 && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \
23 * Put a line break to the output buffer.
26 #define PUT_BREAK(emitter) \
28 && ((emitter->line_break == YAML_CR_BREAK ? \
29 (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \
30 emitter->line_break == YAML_LN_BREAK ? \
31 (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \
32 emitter->line_break == YAML_CRLN_BREAK ? \
33 (*(emitter->buffer.pointer++) = (yaml_char_t) '\r', \
34 *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0), \
35 emitter->column = 0, \
40 * Copy a character from a string into buffer.
43 #define WRITE(emitter,string) \
45 && (COPY(emitter->buffer,string), \
50 * Copy a line break character from a string into buffer.
53 #define WRITE_BREAK(emitter,string) \
55 && (CHECK(string,'\n') ? \
56 (PUT_BREAK(emitter), \
59 (COPY(emitter->buffer,string), \
60 emitter->column = 0, \
69 yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event);
76 yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem);
79 yaml_emitter_need_more_events(yaml_emitter_t *emitter);
82 yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
83 yaml_tag_directive_t value, int allow_duplicates);
86 yaml_emitter_increase_indent(yaml_emitter_t *emitter,
87 int flow, int indentless);
94 yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event);
97 yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
101 yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
102 yaml_event_t *event, int first);
105 yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
106 yaml_event_t *event);
109 yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
110 yaml_event_t *event);
113 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
114 yaml_event_t *event, int first);
117 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
118 yaml_event_t *event, int first);
121 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
122 yaml_event_t *event, int simple);
125 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
126 yaml_event_t *event, int first);
129 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
130 yaml_event_t *event, int first);
133 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
134 yaml_event_t *event, int simple);
137 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
138 int root, int sequence, int mapping, int simple_key);
141 yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event);
144 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event);
147 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event);
150 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event);
157 yaml_emitter_check_empty_document(yaml_emitter_t *emitter);
160 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter);
163 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter);
166 yaml_emitter_check_simple_key(yaml_emitter_t *emitter);
169 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event);
176 yaml_emitter_process_anchor(yaml_emitter_t *emitter);
179 yaml_emitter_process_tag(yaml_emitter_t *emitter);
182 yaml_emitter_process_scalar(yaml_emitter_t *emitter);
189 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
190 yaml_version_directive_t version_directive);
193 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
194 yaml_tag_directive_t tag_directive);
197 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
198 yaml_char_t *anchor, int alias);
201 yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
205 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
206 yaml_char_t *value, size_t length);
209 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
210 yaml_event_t *event);
217 yaml_emitter_write_bom(yaml_emitter_t *emitter);
220 yaml_emitter_write_indent(yaml_emitter_t *emitter);
223 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
224 char *indicator, int need_whitespace,
225 int is_whitespace, int is_indention);
228 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
229 yaml_char_t *value, size_t length);
232 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
233 yaml_char_t *value, size_t length);
236 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
237 yaml_char_t *value, size_t length, int need_whitespace);
240 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
241 yaml_char_t *value, size_t length, int allow_breaks);
244 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
245 yaml_char_t *value, size_t length, int allow_breaks);
248 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
249 yaml_char_t *value, size_t length, int allow_breaks);
252 yaml_emitter_determine_chomping(yaml_emitter_t *emitter,
253 yaml_string_t string);
256 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
257 yaml_char_t *value, size_t length);
260 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
261 yaml_char_t *value, size_t length);
264 * Set an emitter error and return 0.
268 yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem)
270 emitter->error = YAML_EMITTER_ERROR;
271 emitter->problem = problem;
281 yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event)
283 if (!ENQUEUE(emitter, emitter->events, *event)) {
284 yaml_event_delete(event);
288 while (!yaml_emitter_need_more_events(emitter)) {
289 if (!yaml_emitter_analyze_event(emitter, emitter->events.head))
291 if (!yaml_emitter_state_machine(emitter, emitter->events.head))
293 yaml_event_delete(&DEQUEUE(emitter, emitter->events));
300 * Check if we need to accumulate more events before emitting.
302 * We accumulate extra
303 * - 1 event for DOCUMENT-START
304 * - 2 events for SEQUENCE-START
305 * - 3 events for MAPPING-START
309 yaml_emitter_need_more_events(yaml_emitter_t *emitter)
315 if (QUEUE_EMPTY(emitter, emitter->events))
318 switch (emitter->events.head->type) {
319 case YAML_DOCUMENT_START_EVENT:
322 case YAML_SEQUENCE_START_EVENT:
325 case YAML_MAPPING_START_EVENT:
332 if (emitter->events.tail - emitter->events.head > accumulate)
335 for (event = emitter->events.head; event != emitter->events.tail; event ++) {
336 switch (event->type) {
337 case YAML_STREAM_START_EVENT:
338 case YAML_DOCUMENT_START_EVENT:
339 case YAML_SEQUENCE_START_EVENT:
340 case YAML_MAPPING_START_EVENT:
343 case YAML_STREAM_END_EVENT:
344 case YAML_DOCUMENT_END_EVENT:
345 case YAML_SEQUENCE_END_EVENT:
346 case YAML_MAPPING_END_EVENT:
360 * Append a directive to the directives stack.
364 yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
365 yaml_tag_directive_t value, int allow_duplicates)
367 yaml_tag_directive_t *tag_directive;
368 yaml_tag_directive_t copy = { NULL, NULL };
370 for (tag_directive = emitter->tag_directives.start;
371 tag_directive != emitter->tag_directives.top; tag_directive ++) {
372 if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) {
373 if (allow_duplicates)
375 return yaml_emitter_set_emitter_error(emitter,
376 "duplicate %TAG directive");
380 copy.handle = yaml_strdup(value.handle);
381 copy.prefix = yaml_strdup(value.prefix);
382 if (!copy.handle || !copy.prefix) {
383 emitter->error = YAML_MEMORY_ERROR;
387 if (!PUSH(emitter, emitter->tag_directives, copy))
393 yaml_free(copy.handle);
394 yaml_free(copy.prefix);
399 * Increase the indentation level.
403 yaml_emitter_increase_indent(yaml_emitter_t *emitter,
404 int flow, int indentless)
406 if (!PUSH(emitter, emitter->indents, emitter->indent))
409 if (emitter->indent < 0) {
410 emitter->indent = flow ? emitter->best_indent : 0;
412 else if (!indentless) {
413 emitter->indent += emitter->best_indent;
424 yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event)
426 switch (emitter->state)
428 case YAML_EMIT_STREAM_START_STATE:
429 return yaml_emitter_emit_stream_start(emitter, event);
431 case YAML_EMIT_FIRST_DOCUMENT_START_STATE:
432 return yaml_emitter_emit_document_start(emitter, event, 1);
434 case YAML_EMIT_DOCUMENT_START_STATE:
435 return yaml_emitter_emit_document_start(emitter, event, 0);
437 case YAML_EMIT_DOCUMENT_CONTENT_STATE:
438 return yaml_emitter_emit_document_content(emitter, event);
440 case YAML_EMIT_DOCUMENT_END_STATE:
441 return yaml_emitter_emit_document_end(emitter, event);
443 case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE:
444 return yaml_emitter_emit_flow_sequence_item(emitter, event, 1);
446 case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE:
447 return yaml_emitter_emit_flow_sequence_item(emitter, event, 0);
449 case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE:
450 return yaml_emitter_emit_flow_mapping_key(emitter, event, 1);
452 case YAML_EMIT_FLOW_MAPPING_KEY_STATE:
453 return yaml_emitter_emit_flow_mapping_key(emitter, event, 0);
455 case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE:
456 return yaml_emitter_emit_flow_mapping_value(emitter, event, 1);
458 case YAML_EMIT_FLOW_MAPPING_VALUE_STATE:
459 return yaml_emitter_emit_flow_mapping_value(emitter, event, 0);
461 case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE:
462 return yaml_emitter_emit_block_sequence_item(emitter, event, 1);
464 case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE:
465 return yaml_emitter_emit_block_sequence_item(emitter, event, 0);
467 case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE:
468 return yaml_emitter_emit_block_mapping_key(emitter, event, 1);
470 case YAML_EMIT_BLOCK_MAPPING_KEY_STATE:
471 return yaml_emitter_emit_block_mapping_key(emitter, event, 0);
473 case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE:
474 return yaml_emitter_emit_block_mapping_value(emitter, event, 1);
476 case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE:
477 return yaml_emitter_emit_block_mapping_value(emitter, event, 0);
479 case YAML_EMIT_END_STATE:
480 return yaml_emitter_set_emitter_error(emitter,
481 "expected nothing after STREAM-END");
484 assert(1); /* Invalid state. */
491 * Expect STREAM-START.
495 yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
498 if (event->type == YAML_STREAM_START_EVENT)
500 if (!emitter->encoding) {
501 emitter->encoding = event->data.stream_start.encoding;
504 if (!emitter->encoding) {
505 emitter->encoding = YAML_UTF8_ENCODING;
508 if (emitter->best_indent < 2 || emitter->best_indent > 9) {
509 emitter->best_indent = 2;
512 if (emitter->best_width >= 0
513 && emitter->best_width <= emitter->best_indent*2) {
514 emitter->best_width = 80;
517 if (emitter->best_width < 0) {
518 emitter->best_width = INT_MAX;
521 if (!emitter->line_break) {
522 emitter->line_break = YAML_LN_BREAK;
525 emitter->indent = -1;
529 emitter->whitespace = 1;
530 emitter->indention = 1;
532 if (emitter->encoding != YAML_UTF8_ENCODING) {
533 if (!yaml_emitter_write_bom(emitter))
537 emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE;
542 return yaml_emitter_set_emitter_error(emitter,
543 "expected STREAM-START");
547 * Expect DOCUMENT-START or STREAM-END.
551 yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
552 yaml_event_t *event, int first)
554 if (event->type == YAML_DOCUMENT_START_EVENT)
556 yaml_tag_directive_t default_tag_directives[] = {
557 {(yaml_char_t *)"!", (yaml_char_t *)"!"},
558 {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"},
561 yaml_tag_directive_t *tag_directive;
564 if (event->data.document_start.version_directive) {
565 if (!yaml_emitter_analyze_version_directive(emitter,
566 *event->data.document_start.version_directive))
570 for (tag_directive = event->data.document_start.tag_directives.start;
571 tag_directive != event->data.document_start.tag_directives.end;
573 if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive))
575 if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0))
579 for (tag_directive = default_tag_directives;
580 tag_directive->handle; tag_directive ++) {
581 if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1))
585 implicit = event->data.document_start.implicit;
586 if (!first || emitter->canonical) {
590 if (event->data.document_start.version_directive) {
592 if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0))
594 if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0))
596 if (!yaml_emitter_write_indent(emitter))
600 if (event->data.document_start.tag_directives.start
601 != event->data.document_start.tag_directives.end) {
603 for (tag_directive = event->data.document_start.tag_directives.start;
604 tag_directive != event->data.document_start.tag_directives.end;
606 if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0))
608 if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle,
609 strlen((char *)tag_directive->handle)))
611 if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix,
612 strlen((char *)tag_directive->prefix), 1))
614 if (!yaml_emitter_write_indent(emitter))
619 if (yaml_emitter_check_empty_document(emitter)) {
624 if (!yaml_emitter_write_indent(emitter))
626 if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0))
628 if (emitter->canonical) {
629 if (!yaml_emitter_write_indent(emitter))
634 emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE;
639 else if (event->type == YAML_STREAM_END_EVENT)
641 if (!yaml_emitter_flush(emitter))
644 emitter->state = YAML_EMIT_END_STATE;
649 return yaml_emitter_set_emitter_error(emitter,
650 "expected DOCUMENT-START or STREAM-END");
654 * Expect the root node.
658 yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
661 if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE))
664 return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0);
668 * Expect DOCUMENT-END.
672 yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
675 if (event->type == YAML_DOCUMENT_END_EVENT)
677 if (!yaml_emitter_write_indent(emitter))
679 if (!event->data.document_end.implicit) {
680 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
682 if (!yaml_emitter_write_indent(emitter))
685 if (!yaml_emitter_flush(emitter))
688 emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
690 while (!STACK_EMPTY(emitter, emitter->tag_directives)) {
691 yaml_tag_directive_t tag_directive = POP(emitter,
692 emitter->tag_directives);
693 yaml_free(tag_directive.handle);
694 yaml_free(tag_directive.prefix);
700 return yaml_emitter_set_emitter_error(emitter,
701 "expected DOCUMENT-END");
706 * Expect a flow item node.
710 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
711 yaml_event_t *event, int first)
715 if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0))
717 if (!yaml_emitter_increase_indent(emitter, 1, 0))
719 emitter->flow_level ++;
722 if (event->type == YAML_SEQUENCE_END_EVENT)
724 emitter->flow_level --;
725 emitter->indent = POP(emitter, emitter->indents);
726 if (emitter->canonical && !first) {
727 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
729 if (!yaml_emitter_write_indent(emitter))
732 if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0))
734 emitter->state = POP(emitter, emitter->states);
740 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
744 if (emitter->canonical || emitter->column > emitter->best_width) {
745 if (!yaml_emitter_write_indent(emitter))
748 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
751 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
755 * Expect a flow key node.
759 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
760 yaml_event_t *event, int first)
764 if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0))
766 if (!yaml_emitter_increase_indent(emitter, 1, 0))
768 emitter->flow_level ++;
771 if (event->type == YAML_MAPPING_END_EVENT)
773 emitter->flow_level --;
774 emitter->indent = POP(emitter, emitter->indents);
775 if (emitter->canonical && !first) {
776 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
778 if (!yaml_emitter_write_indent(emitter))
781 if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0))
783 emitter->state = POP(emitter, emitter->states);
789 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
792 if (emitter->canonical || emitter->column > emitter->best_width) {
793 if (!yaml_emitter_write_indent(emitter))
797 if (!emitter->canonical && yaml_emitter_check_simple_key(emitter))
799 if (!PUSH(emitter, emitter->states,
800 YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE))
803 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
807 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0))
809 if (!PUSH(emitter, emitter->states,
810 YAML_EMIT_FLOW_MAPPING_VALUE_STATE))
813 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
818 * Expect a flow value node.
822 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
823 yaml_event_t *event, int simple)
826 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
830 if (emitter->canonical || emitter->column > emitter->best_width) {
831 if (!yaml_emitter_write_indent(emitter))
834 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0))
837 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE))
839 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
843 * Expect a block item node.
847 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
848 yaml_event_t *event, int first)
852 if (!yaml_emitter_increase_indent(emitter, 0,
853 (emitter->mapping_context && !emitter->indention)))
857 if (event->type == YAML_SEQUENCE_END_EVENT)
859 emitter->indent = POP(emitter, emitter->indents);
860 emitter->state = POP(emitter, emitter->states);
865 if (!yaml_emitter_write_indent(emitter))
867 if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1))
869 if (!PUSH(emitter, emitter->states,
870 YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE))
873 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
877 * Expect a block key node.
881 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
882 yaml_event_t *event, int first)
886 if (!yaml_emitter_increase_indent(emitter, 0, 0))
890 if (event->type == YAML_MAPPING_END_EVENT)
892 emitter->indent = POP(emitter, emitter->indents);
893 emitter->state = POP(emitter, emitter->states);
898 if (!yaml_emitter_write_indent(emitter))
901 if (yaml_emitter_check_simple_key(emitter))
903 if (!PUSH(emitter, emitter->states,
904 YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE))
907 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
911 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1))
913 if (!PUSH(emitter, emitter->states,
914 YAML_EMIT_BLOCK_MAPPING_VALUE_STATE))
917 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
922 * Expect a block value node.
926 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
927 yaml_event_t *event, int simple)
930 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
934 if (!yaml_emitter_write_indent(emitter))
936 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1))
939 if (!PUSH(emitter, emitter->states,
940 YAML_EMIT_BLOCK_MAPPING_KEY_STATE))
943 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
951 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
952 int root, int sequence, int mapping, int simple_key)
954 emitter->root_context = root;
955 emitter->sequence_context = sequence;
956 emitter->mapping_context = mapping;
957 emitter->simple_key_context = simple_key;
961 case YAML_ALIAS_EVENT:
962 return yaml_emitter_emit_alias(emitter, event);
964 case YAML_SCALAR_EVENT:
965 return yaml_emitter_emit_scalar(emitter, event);
967 case YAML_SEQUENCE_START_EVENT:
968 return yaml_emitter_emit_sequence_start(emitter, event);
970 case YAML_MAPPING_START_EVENT:
971 return yaml_emitter_emit_mapping_start(emitter, event);
974 return yaml_emitter_set_emitter_error(emitter,
975 "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS");
986 yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event)
988 if (!yaml_emitter_process_anchor(emitter))
990 emitter->state = POP(emitter, emitter->states);
1000 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event)
1002 if (!yaml_emitter_select_scalar_style(emitter, event))
1004 if (!yaml_emitter_process_anchor(emitter))
1006 if (!yaml_emitter_process_tag(emitter))
1008 if (!yaml_emitter_increase_indent(emitter, 1, 0))
1010 if (!yaml_emitter_process_scalar(emitter))
1012 emitter->indent = POP(emitter, emitter->indents);
1013 emitter->state = POP(emitter, emitter->states);
1019 * Expect SEQUENCE-START.
1023 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event)
1025 if (!yaml_emitter_process_anchor(emitter))
1027 if (!yaml_emitter_process_tag(emitter))
1030 if (emitter->flow_level || emitter->canonical
1031 || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE
1032 || yaml_emitter_check_empty_sequence(emitter)) {
1033 emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
1036 emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
1043 * Expect MAPPING-START.
1047 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event)
1049 if (!yaml_emitter_process_anchor(emitter))
1051 if (!yaml_emitter_process_tag(emitter))
1054 if (emitter->flow_level || emitter->canonical
1055 || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE
1056 || yaml_emitter_check_empty_mapping(emitter)) {
1057 emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
1060 emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
1067 * Check if the document content is an empty scalar.
1071 yaml_emitter_check_empty_document(yaml_emitter_t *emitter)
1077 * Check if the next events represent an empty sequence.
1081 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter)
1083 if (emitter->events.tail - emitter->events.head < 2)
1086 return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT
1087 && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT);
1091 * Check if the next events represent an empty mapping.
1095 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter)
1097 if (emitter->events.tail - emitter->events.head < 2)
1100 return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT
1101 && emitter->events.head[1].type == YAML_MAPPING_END_EVENT);
1105 * Check if the next node can be expressed as a simple key.
1109 yaml_emitter_check_simple_key(yaml_emitter_t *emitter)
1111 yaml_event_t *event = emitter->events.head;
1114 switch (event->type)
1116 case YAML_ALIAS_EVENT:
1117 length += emitter->anchor_data.anchor_length;
1120 case YAML_SCALAR_EVENT:
1121 if (emitter->scalar_data.multiline)
1123 length += emitter->anchor_data.anchor_length
1124 + emitter->tag_data.handle_length
1125 + emitter->tag_data.suffix_length
1126 + emitter->scalar_data.length;
1129 case YAML_SEQUENCE_START_EVENT:
1130 if (!yaml_emitter_check_empty_sequence(emitter))
1132 length += emitter->anchor_data.anchor_length
1133 + emitter->tag_data.handle_length
1134 + emitter->tag_data.suffix_length;
1137 case YAML_MAPPING_START_EVENT:
1138 if (!yaml_emitter_check_empty_sequence(emitter))
1140 length += emitter->anchor_data.anchor_length
1141 + emitter->tag_data.handle_length
1142 + emitter->tag_data.suffix_length;
1156 * Determine an acceptable scalar style.
1160 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
1162 yaml_scalar_style_t style = event->data.scalar.style;
1164 if (style == YAML_ANY_SCALAR_STYLE)
1165 style = YAML_PLAIN_SCALAR_STYLE;
1167 if (emitter->canonical)
1168 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1170 if (emitter->simple_key_context && emitter->scalar_data.multiline)
1171 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1173 if (style == YAML_PLAIN_SCALAR_STYLE)
1175 if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
1176 || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
1177 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1178 if (!emitter->scalar_data.length
1179 && (emitter->flow_level || emitter->simple_key_context))
1180 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1181 if (!event->data.scalar.plain_implicit
1182 && !emitter->tag_data.handle && !emitter->tag_data.suffix)
1183 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1186 if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
1188 if (!emitter->scalar_data.single_quoted_allowed)
1189 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1192 if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
1194 if (!emitter->scalar_data.block_allowed
1195 || emitter->flow_level || emitter->simple_key_context)
1196 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1199 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1201 if (!event->data.scalar.plain_implicit
1202 && !event->data.scalar.quoted_implicit) {
1203 return yaml_emitter_set_emitter_error(emitter,
1204 "neither tag nor implicit flags are specified");
1207 if (event->data.scalar.plain_implicit
1208 && style != YAML_PLAIN_SCALAR_STYLE) {
1209 emitter->tag_data.handle = (yaml_char_t *)"!";
1210 emitter->tag_data.handle_length = 1;
1214 emitter->scalar_data.style = style;
1224 yaml_emitter_process_anchor(yaml_emitter_t *emitter)
1226 if (!emitter->anchor_data.anchor)
1229 if (!yaml_emitter_write_indicator(emitter,
1230 (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
1233 return yaml_emitter_write_anchor(emitter,
1234 emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
1242 yaml_emitter_process_tag(yaml_emitter_t *emitter)
1244 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1247 if (emitter->tag_data.handle)
1249 if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
1250 emitter->tag_data.handle_length))
1252 if (emitter->tag_data.suffix) {
1253 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1254 emitter->tag_data.suffix_length, 0))
1260 if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
1262 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1263 emitter->tag_data.suffix_length, 0))
1265 if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
1277 yaml_emitter_process_scalar(yaml_emitter_t *emitter)
1279 switch (emitter->scalar_data.style)
1281 case YAML_PLAIN_SCALAR_STYLE:
1282 return yaml_emitter_write_plain_scalar(emitter,
1283 emitter->scalar_data.value, emitter->scalar_data.length,
1284 !emitter->simple_key_context);
1286 case YAML_SINGLE_QUOTED_SCALAR_STYLE:
1287 return yaml_emitter_write_single_quoted_scalar(emitter,
1288 emitter->scalar_data.value, emitter->scalar_data.length,
1289 !emitter->simple_key_context);
1291 case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
1292 return yaml_emitter_write_double_quoted_scalar(emitter,
1293 emitter->scalar_data.value, emitter->scalar_data.length,
1294 !emitter->simple_key_context);
1296 case YAML_LITERAL_SCALAR_STYLE:
1297 return yaml_emitter_write_literal_scalar(emitter,
1298 emitter->scalar_data.value, emitter->scalar_data.length);
1300 case YAML_FOLDED_SCALAR_STYLE:
1301 return yaml_emitter_write_folded_scalar(emitter,
1302 emitter->scalar_data.value, emitter->scalar_data.length);
1305 assert(1); /* Impossible. */
1312 * Check if a %YAML directive is valid.
1316 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
1317 yaml_version_directive_t version_directive)
1319 if (version_directive.major != 1 || version_directive.minor != 1) {
1320 return yaml_emitter_set_emitter_error(emitter,
1321 "incompatible %YAML directive");
1328 * Check if a %TAG directive is valid.
1332 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
1333 yaml_tag_directive_t tag_directive)
1335 yaml_string_t handle = STRING(tag_directive.handle,
1336 strlen((char *)tag_directive.handle));
1337 yaml_string_t prefix = STRING(tag_directive.prefix,
1338 strlen((char *)tag_directive.prefix));
1340 if (handle.start == handle.end) {
1341 return yaml_emitter_set_emitter_error(emitter,
1342 "tag handle must not be empty");
1345 if (handle.start[0] != '!') {
1346 return yaml_emitter_set_emitter_error(emitter,
1347 "tag handle must start with '!'");
1350 if (handle.end[-1] != '!') {
1351 return yaml_emitter_set_emitter_error(emitter,
1352 "tag handle must end with '!'");
1357 while (handle.pointer < handle.end-1) {
1358 if (!IS_ALPHA(handle)) {
1359 return yaml_emitter_set_emitter_error(emitter,
1360 "tag handle must contain alphanumerical characters only");
1365 if (prefix.start == prefix.end) {
1366 return yaml_emitter_set_emitter_error(emitter,
1367 "tag prefix must not be empty");
1374 * Check if an anchor is valid.
1378 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
1379 yaml_char_t *anchor, int alias)
1381 yaml_string_t string = STRING(anchor, strlen((char *)anchor));
1383 if (string.start == string.end) {
1384 return yaml_emitter_set_emitter_error(emitter, alias ?
1385 "alias value must not be empty" :
1386 "anchor value must not be empty");
1389 while (string.pointer != string.end) {
1390 if (!IS_ALPHA(string)) {
1391 return yaml_emitter_set_emitter_error(emitter, alias ?
1392 "alias value must contain alphanumerical characters only" :
1393 "anchor value must contain alphanumerical characters only");
1398 emitter->anchor_data.anchor = string.start;
1399 emitter->anchor_data.anchor_length = string.end - string.start;
1400 emitter->anchor_data.alias = alias;
1406 * Check if a tag is valid.
1410 yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
1413 yaml_string_t string = STRING(tag, strlen((char *)tag));
1414 yaml_tag_directive_t *tag_directive;
1416 if (string.start == string.end) {
1417 return yaml_emitter_set_emitter_error(emitter,
1418 "tag value must not be empty");
1421 for (tag_directive = emitter->tag_directives.start;
1422 tag_directive != emitter->tag_directives.top; tag_directive ++) {
1423 size_t prefix_length = strlen((char *)tag_directive->prefix);
1424 if (prefix_length < (string.end - string.start)
1425 && strncmp((char *)tag_directive->prefix, (char *)string.start,
1426 prefix_length) == 0)
1428 emitter->tag_data.handle = tag_directive->handle;
1429 emitter->tag_data.handle_length =
1430 strlen((char *)tag_directive->handle);
1431 emitter->tag_data.suffix = string.start + prefix_length;
1432 emitter->tag_data.suffix_length =
1433 (string.end - string.start) - prefix_length;
1438 emitter->tag_data.suffix = string.start;
1439 emitter->tag_data.suffix_length = string.end - string.start;
1445 * Check if a scalar is valid.
1449 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
1450 yaml_char_t *value, size_t length)
1452 yaml_string_t string = STRING(value, length);
1454 int block_indicators = 0;
1455 int flow_indicators = 0;
1456 int line_breaks = 0;
1457 int special_characters = 0;
1459 int inline_spaces = 0;
1460 int inline_breaks = 0;
1461 int leading_spaces = 0;
1462 int leading_breaks = 0;
1463 int trailing_spaces = 0;
1464 int trailing_breaks = 0;
1465 int inline_breaks_spaces = 0;
1466 int mixed_breaks_spaces = 0;
1468 int preceeded_by_space = 0;
1469 int followed_by_space = 0;
1475 emitter->scalar_data.value = value;
1476 emitter->scalar_data.length = length;
1478 if (string.start == string.end)
1480 emitter->scalar_data.multiline = 0;
1481 emitter->scalar_data.flow_plain_allowed = 0;
1482 emitter->scalar_data.block_plain_allowed = 1;
1483 emitter->scalar_data.single_quoted_allowed = 1;
1484 emitter->scalar_data.block_allowed = 0;
1489 if ((CHECK_AT(string, '-', 0)
1490 && CHECK_AT(string, '-', 1)
1491 && CHECK_AT(string, '-', 2))
1492 || (CHECK_AT(string, '.', 0)
1493 && CHECK_AT(string, '.', 1)
1494 && CHECK_AT(string, '.', 2))) {
1495 block_indicators = 1;
1496 flow_indicators = 1;
1499 preceeded_by_space = 1;
1500 followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
1502 while (string.pointer != string.end)
1504 if (string.start == string.pointer)
1506 if (CHECK(string, '#') || CHECK(string, ',')
1507 || CHECK(string, '[') || CHECK(string, ']')
1508 || CHECK(string, '{') || CHECK(string, '}')
1509 || CHECK(string, '&') || CHECK(string, '*')
1510 || CHECK(string, '!') || CHECK(string, '|')
1511 || CHECK(string, '>') || CHECK(string, '\'')
1512 || CHECK(string, '"') || CHECK(string, '%')
1513 || CHECK(string, '@') || CHECK(string, '`')) {
1514 flow_indicators = 1;
1515 block_indicators = 1;
1518 if (CHECK(string, '?') || CHECK(string, ':')) {
1519 flow_indicators = 1;
1520 if (followed_by_space) {
1521 block_indicators = 1;
1525 if (CHECK(string, '-') && followed_by_space) {
1526 flow_indicators = 1;
1527 block_indicators = 1;
1532 if (CHECK(string, ',') || CHECK(string, '?')
1533 || CHECK(string, '[') || CHECK(string, ']')
1534 || CHECK(string, '{') || CHECK(string, '}')) {
1535 flow_indicators = 1;
1538 if (CHECK(string, ':')) {
1539 flow_indicators = 1;
1540 if (followed_by_space) {
1541 block_indicators = 1;
1545 if (CHECK(string, '#') && preceeded_by_space) {
1546 flow_indicators = 1;
1547 block_indicators = 1;
1551 if (!IS_PRINTABLE(string)
1552 || (!IS_ASCII(string) && !emitter->unicode)) {
1553 special_characters = 1;
1556 if (IS_BREAK(string)) {
1560 if (IS_SPACE(string))
1563 if (string.start == string.pointer) {
1568 else if (IS_BREAK(string))
1574 if (string.start == string.pointer) {
1579 else if (spaces || breaks)
1582 if (spaces && breaks) {
1583 mixed_breaks_spaces = 1;
1594 mixed_breaks_spaces = 1;
1596 else if (spaces && breaks) {
1597 inline_breaks_spaces = 1;
1606 spaces = breaks = mixed = leading = 0;
1609 if ((spaces || breaks) && string.pointer == string.end-1)
1611 if (spaces && breaks) {
1612 mixed_breaks_spaces = 1;
1618 trailing_spaces = 1;
1624 trailing_breaks = 1;
1628 preceeded_by_space = IS_BLANKZ(string);
1630 if (string.pointer != string.end) {
1631 followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
1635 emitter->scalar_data.multiline = line_breaks;
1637 emitter->scalar_data.flow_plain_allowed = 1;
1638 emitter->scalar_data.block_plain_allowed = 1;
1639 emitter->scalar_data.single_quoted_allowed = 1;
1640 emitter->scalar_data.block_allowed = 1;
1642 if (leading_spaces || leading_breaks || trailing_spaces) {
1643 emitter->scalar_data.flow_plain_allowed = 0;
1644 emitter->scalar_data.block_plain_allowed = 0;
1645 emitter->scalar_data.block_allowed = 0;
1648 if (trailing_breaks) {
1649 emitter->scalar_data.flow_plain_allowed = 0;
1650 emitter->scalar_data.block_plain_allowed = 0;
1653 if (inline_breaks_spaces) {
1654 emitter->scalar_data.flow_plain_allowed = 0;
1655 emitter->scalar_data.block_plain_allowed = 0;
1656 emitter->scalar_data.single_quoted_allowed = 0;
1659 if (mixed_breaks_spaces || special_characters) {
1660 emitter->scalar_data.flow_plain_allowed = 0;
1661 emitter->scalar_data.block_plain_allowed = 0;
1662 emitter->scalar_data.single_quoted_allowed = 0;
1663 emitter->scalar_data.block_allowed = 0;
1667 emitter->scalar_data.flow_plain_allowed = 0;
1668 emitter->scalar_data.block_plain_allowed = 0;
1671 if (flow_indicators) {
1672 emitter->scalar_data.flow_plain_allowed = 0;
1675 if (block_indicators) {
1676 emitter->scalar_data.block_plain_allowed = 0;
1683 * Check if the event data is valid.
1687 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
1688 yaml_event_t *event)
1690 emitter->anchor_data.anchor = NULL;
1691 emitter->anchor_data.anchor_length = 0;
1692 emitter->tag_data.handle = NULL;
1693 emitter->tag_data.handle_length = 0;
1694 emitter->tag_data.suffix = NULL;
1695 emitter->tag_data.suffix_length = 0;
1696 emitter->scalar_data.value = NULL;
1697 emitter->scalar_data.length = 0;
1699 switch (event->type)
1701 case YAML_ALIAS_EVENT:
1702 if (!yaml_emitter_analyze_anchor(emitter,
1703 event->data.alias.anchor, 1))
1707 case YAML_SCALAR_EVENT:
1708 if (event->data.scalar.anchor) {
1709 if (!yaml_emitter_analyze_anchor(emitter,
1710 event->data.scalar.anchor, 0))
1713 if (event->data.scalar.tag && (emitter->canonical ||
1714 (!event->data.scalar.plain_implicit
1715 && !event->data.scalar.quoted_implicit))) {
1716 if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
1719 if (!yaml_emitter_analyze_scalar(emitter,
1720 event->data.scalar.value, event->data.scalar.length))
1724 case YAML_SEQUENCE_START_EVENT:
1725 if (event->data.sequence_start.anchor) {
1726 if (!yaml_emitter_analyze_anchor(emitter,
1727 event->data.sequence_start.anchor, 0))
1730 if (event->data.sequence_start.tag && (emitter->canonical ||
1731 !event->data.sequence_start.implicit)) {
1732 if (!yaml_emitter_analyze_tag(emitter,
1733 event->data.sequence_start.tag))
1738 case YAML_MAPPING_START_EVENT:
1739 if (event->data.mapping_start.anchor) {
1740 if (!yaml_emitter_analyze_anchor(emitter,
1741 event->data.mapping_start.anchor, 0))
1744 if (event->data.mapping_start.tag && (emitter->canonical ||
1745 !event->data.mapping_start.implicit)) {
1746 if (!yaml_emitter_analyze_tag(emitter,
1747 event->data.mapping_start.tag))
1758 * Write the BOM character.
1762 yaml_emitter_write_bom(yaml_emitter_t *emitter)
1764 if (!FLUSH(emitter)) return 0;
1766 *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
1767 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
1768 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
1774 yaml_emitter_write_indent(yaml_emitter_t *emitter)
1776 int indent = (emitter->indent >= 0) ? emitter->indent : 0;
1778 if (!emitter->indention || emitter->column > indent
1779 || (emitter->column == indent && !emitter->whitespace)) {
1780 if (!PUT_BREAK(emitter)) return 0;
1783 while (emitter->column < indent) {
1784 if (!PUT(emitter, ' ')) return 0;
1787 emitter->whitespace = 1;
1788 emitter->indention = 1;
1794 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
1795 char *indicator, int need_whitespace,
1796 int is_whitespace, int is_indention)
1798 yaml_string_t string = STRING((yaml_char_t *)indicator, strlen(indicator));
1800 if (need_whitespace && !emitter->whitespace) {
1801 if (!PUT(emitter, ' ')) return 0;
1804 while (string.pointer != string.end) {
1805 if (!WRITE(emitter, string)) return 0;
1808 emitter->whitespace = is_whitespace;
1809 emitter->indention = (emitter->indention && is_indention);
1815 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
1816 yaml_char_t *value, size_t length)
1818 yaml_string_t string = STRING(value, length);
1820 while (string.pointer != string.end) {
1821 if (!WRITE(emitter, string)) return 0;
1824 emitter->whitespace = 0;
1825 emitter->indention = 0;
1831 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
1832 yaml_char_t *value, size_t length)
1834 yaml_string_t string = STRING(value, length);
1836 if (!emitter->whitespace) {
1837 if (!PUT(emitter, ' ')) return 0;
1840 while (string.pointer != string.end) {
1841 if (!WRITE(emitter, string)) return 0;
1844 emitter->whitespace = 0;
1845 emitter->indention = 0;
1851 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
1852 yaml_char_t *value, size_t length,
1853 int need_whitespace)
1855 yaml_string_t string = STRING(value, length);
1857 if (need_whitespace && !emitter->whitespace) {
1858 if (!PUT(emitter, ' ')) return 0;
1861 while (string.pointer != string.end) {
1862 if (IS_ALPHA(string)
1863 || CHECK(string, ';') || CHECK(string, '/')
1864 || CHECK(string, '?') || CHECK(string, ':')
1865 || CHECK(string, '@') || CHECK(string, '&')
1866 || CHECK(string, '=') || CHECK(string, '+')
1867 || CHECK(string, '$') || CHECK(string, ',')
1868 || CHECK(string, '_') || CHECK(string, '.')
1869 || CHECK(string, '~') || CHECK(string, '*')
1870 || CHECK(string, '\'') || CHECK(string, '(')
1871 || CHECK(string, ')') || CHECK(string, '[')
1872 || CHECK(string, ']')) {
1873 if (!WRITE(emitter, string)) return 0;
1876 int width = WIDTH(string);
1879 value = *(string.pointer++);
1880 if (!PUT(emitter, '%')) return 0;
1881 if (!PUT(emitter, (value >> 4)
1882 + ((value >> 4) < 10 ? '0' : 'A' - 10)))
1884 if (!PUT(emitter, (value & 0x0F)
1885 + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
1891 emitter->whitespace = 0;
1892 emitter->indention = 0;
1898 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
1899 yaml_char_t *value, size_t length, int allow_breaks)
1901 yaml_string_t string = STRING(value, length);
1905 if (!emitter->whitespace) {
1906 if (!PUT(emitter, ' ')) return 0;
1909 while (string.pointer != string.end)
1911 if (IS_SPACE(string))
1913 if (allow_breaks && !spaces
1914 && emitter->column > emitter->best_width
1915 && !IS_SPACE_AT(string, 1)) {
1916 if (!yaml_emitter_write_indent(emitter)) return 0;
1920 if (!WRITE(emitter, string)) return 0;
1924 else if (IS_BREAK(string))
1926 if (!breaks && CHECK(string, '\n')) {
1927 if (!PUT_BREAK(emitter)) return 0;
1929 if (!WRITE_BREAK(emitter, string)) return 0;
1930 emitter->indention = 1;
1936 if (!yaml_emitter_write_indent(emitter)) return 0;
1938 if (!WRITE(emitter, string)) return 0;
1939 emitter->indention = 0;
1945 emitter->whitespace = 0;
1946 emitter->indention = 0;
1952 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
1953 yaml_char_t *value, size_t length, int allow_breaks)
1955 yaml_string_t string = STRING(value, length);
1959 if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
1962 while (string.pointer != string.end)
1964 if (IS_SPACE(string))
1966 if (allow_breaks && !spaces
1967 && emitter->column > emitter->best_width
1968 && string.pointer != string.start
1969 && string.pointer != string.end - 1
1970 && !IS_SPACE_AT(string, 1)) {
1971 if (!yaml_emitter_write_indent(emitter)) return 0;
1975 if (!WRITE(emitter, string)) return 0;
1979 else if (IS_BREAK(string))
1981 if (!breaks && CHECK(string, '\n')) {
1982 if (!PUT_BREAK(emitter)) return 0;
1984 if (!WRITE_BREAK(emitter, string)) return 0;
1985 emitter->indention = 1;
1991 if (!yaml_emitter_write_indent(emitter)) return 0;
1993 if (CHECK(string, '\'')) {
1994 if (!PUT(emitter, '\'')) return 0;
1996 if (!WRITE(emitter, string)) return 0;
1997 emitter->indention = 0;
2003 if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
2006 emitter->whitespace = 0;
2007 emitter->indention = 0;
2013 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
2014 yaml_char_t *value, size_t length, int allow_breaks)
2016 yaml_string_t string = STRING(value, length);
2019 if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
2022 while (string.pointer != string.end)
2024 if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
2025 || IS_BOM(string) || IS_BREAK(string)
2026 || CHECK(string, '"') || CHECK(string, '\\'))
2028 unsigned char octet;
2033 octet = string.pointer[0];
2034 width = (octet & 0x80) == 0x00 ? 1 :
2035 (octet & 0xE0) == 0xC0 ? 2 :
2036 (octet & 0xF0) == 0xE0 ? 3 :
2037 (octet & 0xF8) == 0xF0 ? 4 : 0;
2038 value = (octet & 0x80) == 0x00 ? octet & 0x7F :
2039 (octet & 0xE0) == 0xC0 ? octet & 0x1F :
2040 (octet & 0xF0) == 0xE0 ? octet & 0x0F :
2041 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
2042 for (k = 1; k < width; k ++) {
2043 octet = string.pointer[k];
2044 value = (value << 6) + (octet & 0x3F);
2046 string.pointer += width;
2048 if (!PUT(emitter, '\\')) return 0;
2053 if (!PUT(emitter, '0')) return 0;
2057 if (!PUT(emitter, 'a')) return 0;
2061 if (!PUT(emitter, 'b')) return 0;
2065 if (!PUT(emitter, 't')) return 0;
2069 if (!PUT(emitter, 'n')) return 0;
2073 if (!PUT(emitter, 'v')) return 0;
2077 if (!PUT(emitter, 'f')) return 0;
2081 if (!PUT(emitter, 'r')) return 0;
2085 if (!PUT(emitter, 'e')) return 0;
2089 if (!PUT(emitter, '\"')) return 0;
2093 if (!PUT(emitter, '\\')) return 0;
2097 if (!PUT(emitter, 'N')) return 0;
2101 if (!PUT(emitter, '_')) return 0;
2105 if (!PUT(emitter, 'L')) return 0;
2109 if (!PUT(emitter, 'P')) return 0;
2113 if (value <= 0xFF) {
2114 if (!PUT(emitter, 'x')) return 0;
2117 else if (value <= 0xFFFF) {
2118 if (!PUT(emitter, 'u')) return 0;
2122 if (!PUT(emitter, 'U')) return 0;
2125 for (k = (width-1)*4; k >= 0; k -= 4) {
2126 int digit = (value >> k) & 0x0F;
2127 if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
2133 else if (IS_SPACE(string))
2135 if (allow_breaks && !spaces
2136 && emitter->column > emitter->best_width
2137 && string.pointer != string.start
2138 && string.pointer != string.end - 1) {
2139 if (!yaml_emitter_write_indent(emitter)) return 0;
2140 if (IS_SPACE_AT(string, 1)) {
2141 if (!PUT(emitter, '\\')) return 0;
2146 if (!WRITE(emitter, string)) return 0;
2152 if (!WRITE(emitter, string)) return 0;
2157 if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
2160 emitter->whitespace = 0;
2161 emitter->indention = 0;
2167 yaml_emitter_determine_chomping(yaml_emitter_t *emitter,
2168 yaml_string_t string)
2170 string.pointer = string.end;
2171 if (string.start == string.pointer)
2175 } while ((*string.pointer & 0xC0) == 0x80);
2176 if (!IS_BREAK(string))
2178 if (string.start == string.pointer)
2182 } while ((*string.pointer & 0xC0) == 0x80);
2183 if (!IS_BREAK(string))
2190 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
2191 yaml_char_t *value, size_t length)
2193 yaml_string_t string = STRING(value, length);
2194 int chomp = yaml_emitter_determine_chomping(emitter, string);
2197 if (!yaml_emitter_write_indicator(emitter,
2198 chomp == -1 ? "|-" : chomp == +1 ? "|+" : "|", 1, 0, 0))
2200 if (!yaml_emitter_write_indent(emitter))
2203 while (string.pointer != string.end)
2205 if (IS_BREAK(string))
2207 if (!WRITE_BREAK(emitter, string)) return 0;
2208 emitter->indention = 1;
2214 if (!yaml_emitter_write_indent(emitter)) return 0;
2216 if (!WRITE(emitter, string)) return 0;
2217 emitter->indention = 0;
2226 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
2227 yaml_char_t *value, size_t length)
2229 yaml_string_t string = STRING(value, length);
2230 int chomp = yaml_emitter_determine_chomping(emitter, string);
2232 int leading_spaces = 0;
2234 if (!yaml_emitter_write_indicator(emitter,
2235 chomp == -1 ? ">-" : chomp == +1 ? ">+" : ">", 1, 0, 0))
2237 if (!yaml_emitter_write_indent(emitter))
2240 while (string.pointer != string.end)
2242 if (IS_BREAK(string))
2244 if (!breaks && !leading_spaces && CHECK(string, '\n')) {
2246 while (IS_BREAK_AT(string, k)) {
2247 k += WIDTH_AT(string, k);
2249 if (!IS_BLANK_AT(string, k)) {
2250 if (!PUT_BREAK(emitter)) return 0;
2253 if (!WRITE_BREAK(emitter, string)) return 0;
2254 emitter->indention = 1;
2260 if (!yaml_emitter_write_indent(emitter)) return 0;
2261 leading_spaces = IS_BLANK(string);
2263 if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
2264 && emitter->column > emitter->best_width) {
2265 if (!yaml_emitter_write_indent(emitter)) return 0;
2269 if (!WRITE(emitter, string)) return 0;
2271 emitter->indention = 0;