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_write_block_scalar_hints(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;
1163 int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix);
1165 if (no_tag && !event->data.scalar.plain_implicit
1166 && !event->data.scalar.quoted_implicit) {
1167 return yaml_emitter_set_emitter_error(emitter,
1168 "neither tag nor implicit flags are specified");
1171 if (style == YAML_ANY_SCALAR_STYLE)
1172 style = YAML_PLAIN_SCALAR_STYLE;
1174 if (emitter->canonical)
1175 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1177 if (emitter->simple_key_context && emitter->scalar_data.multiline)
1178 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1180 if (style == YAML_PLAIN_SCALAR_STYLE)
1182 if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
1183 || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
1184 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1185 if (!emitter->scalar_data.length
1186 && (emitter->flow_level || emitter->simple_key_context))
1187 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1188 if (no_tag && !event->data.scalar.plain_implicit)
1189 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1192 if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
1194 if (!emitter->scalar_data.single_quoted_allowed)
1195 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1198 if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
1200 if (!emitter->scalar_data.block_allowed
1201 || emitter->flow_level || emitter->simple_key_context)
1202 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1205 if (no_tag && !event->data.scalar.quoted_implicit
1206 && style != YAML_PLAIN_SCALAR_STYLE)
1208 emitter->tag_data.handle = (yaml_char_t *)"!";
1209 emitter->tag_data.handle_length = 1;
1212 emitter->scalar_data.style = style;
1222 yaml_emitter_process_anchor(yaml_emitter_t *emitter)
1224 if (!emitter->anchor_data.anchor)
1227 if (!yaml_emitter_write_indicator(emitter,
1228 (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
1231 return yaml_emitter_write_anchor(emitter,
1232 emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
1240 yaml_emitter_process_tag(yaml_emitter_t *emitter)
1242 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1245 if (emitter->tag_data.handle)
1247 if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
1248 emitter->tag_data.handle_length))
1250 if (emitter->tag_data.suffix) {
1251 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1252 emitter->tag_data.suffix_length, 0))
1258 if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
1260 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1261 emitter->tag_data.suffix_length, 0))
1263 if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
1275 yaml_emitter_process_scalar(yaml_emitter_t *emitter)
1277 switch (emitter->scalar_data.style)
1279 case YAML_PLAIN_SCALAR_STYLE:
1280 return yaml_emitter_write_plain_scalar(emitter,
1281 emitter->scalar_data.value, emitter->scalar_data.length,
1282 !emitter->simple_key_context);
1284 case YAML_SINGLE_QUOTED_SCALAR_STYLE:
1285 return yaml_emitter_write_single_quoted_scalar(emitter,
1286 emitter->scalar_data.value, emitter->scalar_data.length,
1287 !emitter->simple_key_context);
1289 case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
1290 return yaml_emitter_write_double_quoted_scalar(emitter,
1291 emitter->scalar_data.value, emitter->scalar_data.length,
1292 !emitter->simple_key_context);
1294 case YAML_LITERAL_SCALAR_STYLE:
1295 return yaml_emitter_write_literal_scalar(emitter,
1296 emitter->scalar_data.value, emitter->scalar_data.length);
1298 case YAML_FOLDED_SCALAR_STYLE:
1299 return yaml_emitter_write_folded_scalar(emitter,
1300 emitter->scalar_data.value, emitter->scalar_data.length);
1303 assert(1); /* Impossible. */
1310 * Check if a %YAML directive is valid.
1314 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
1315 yaml_version_directive_t version_directive)
1317 if (version_directive.major != 1 || version_directive.minor != 1) {
1318 return yaml_emitter_set_emitter_error(emitter,
1319 "incompatible %YAML directive");
1326 * Check if a %TAG directive is valid.
1330 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
1331 yaml_tag_directive_t tag_directive)
1333 yaml_string_t handle = STRING(tag_directive.handle,
1334 strlen((char *)tag_directive.handle));
1335 yaml_string_t prefix = STRING(tag_directive.prefix,
1336 strlen((char *)tag_directive.prefix));
1338 if (handle.start == handle.end) {
1339 return yaml_emitter_set_emitter_error(emitter,
1340 "tag handle must not be empty");
1343 if (handle.start[0] != '!') {
1344 return yaml_emitter_set_emitter_error(emitter,
1345 "tag handle must start with '!'");
1348 if (handle.end[-1] != '!') {
1349 return yaml_emitter_set_emitter_error(emitter,
1350 "tag handle must end with '!'");
1355 while (handle.pointer < handle.end-1) {
1356 if (!IS_ALPHA(handle)) {
1357 return yaml_emitter_set_emitter_error(emitter,
1358 "tag handle must contain alphanumerical characters only");
1363 if (prefix.start == prefix.end) {
1364 return yaml_emitter_set_emitter_error(emitter,
1365 "tag prefix must not be empty");
1372 * Check if an anchor is valid.
1376 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
1377 yaml_char_t *anchor, int alias)
1379 yaml_string_t string = STRING(anchor, strlen((char *)anchor));
1381 if (string.start == string.end) {
1382 return yaml_emitter_set_emitter_error(emitter, alias ?
1383 "alias value must not be empty" :
1384 "anchor value must not be empty");
1387 while (string.pointer != string.end) {
1388 if (!IS_ALPHA(string)) {
1389 return yaml_emitter_set_emitter_error(emitter, alias ?
1390 "alias value must contain alphanumerical characters only" :
1391 "anchor value must contain alphanumerical characters only");
1396 emitter->anchor_data.anchor = string.start;
1397 emitter->anchor_data.anchor_length = string.end - string.start;
1398 emitter->anchor_data.alias = alias;
1404 * Check if a tag is valid.
1408 yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
1411 yaml_string_t string = STRING(tag, strlen((char *)tag));
1412 yaml_tag_directive_t *tag_directive;
1414 if (string.start == string.end) {
1415 return yaml_emitter_set_emitter_error(emitter,
1416 "tag value must not be empty");
1419 for (tag_directive = emitter->tag_directives.start;
1420 tag_directive != emitter->tag_directives.top; tag_directive ++) {
1421 size_t prefix_length = strlen((char *)tag_directive->prefix);
1422 if (prefix_length < (size_t)(string.end - string.start)
1423 && strncmp((char *)tag_directive->prefix, (char *)string.start,
1424 prefix_length) == 0)
1426 emitter->tag_data.handle = tag_directive->handle;
1427 emitter->tag_data.handle_length =
1428 strlen((char *)tag_directive->handle);
1429 emitter->tag_data.suffix = string.start + prefix_length;
1430 emitter->tag_data.suffix_length =
1431 (string.end - string.start) - prefix_length;
1436 emitter->tag_data.suffix = string.start;
1437 emitter->tag_data.suffix_length = string.end - string.start;
1443 * Check if a scalar is valid.
1447 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
1448 yaml_char_t *value, size_t length)
1450 yaml_string_t string = STRING(value, length);
1452 int block_indicators = 0;
1453 int flow_indicators = 0;
1454 int line_breaks = 0;
1455 int special_characters = 0;
1457 int leading_space = 0;
1458 int leading_break = 0;
1459 int trailing_space = 0;
1460 int trailing_break = 0;
1461 int break_space = 0;
1462 int space_break = 0;
1464 int preceeded_by_whitespace = 0;
1465 int followed_by_whitespace = 0;
1466 int previous_space = 0;
1467 int previous_break = 0;
1469 emitter->scalar_data.value = value;
1470 emitter->scalar_data.length = length;
1472 if (string.start == string.end)
1474 emitter->scalar_data.multiline = 0;
1475 emitter->scalar_data.flow_plain_allowed = 0;
1476 emitter->scalar_data.block_plain_allowed = 1;
1477 emitter->scalar_data.single_quoted_allowed = 1;
1478 emitter->scalar_data.block_allowed = 0;
1483 if ((CHECK_AT(string, '-', 0)
1484 && CHECK_AT(string, '-', 1)
1485 && CHECK_AT(string, '-', 2))
1486 || (CHECK_AT(string, '.', 0)
1487 && CHECK_AT(string, '.', 1)
1488 && CHECK_AT(string, '.', 2))) {
1489 block_indicators = 1;
1490 flow_indicators = 1;
1493 preceeded_by_whitespace = 1;
1494 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
1496 while (string.pointer != string.end)
1498 if (string.start == string.pointer)
1500 if (CHECK(string, '#') || CHECK(string, ',')
1501 || CHECK(string, '[') || CHECK(string, ']')
1502 || CHECK(string, '{') || CHECK(string, '}')
1503 || CHECK(string, '&') || CHECK(string, '*')
1504 || CHECK(string, '!') || CHECK(string, '|')
1505 || CHECK(string, '>') || CHECK(string, '\'')
1506 || CHECK(string, '"') || CHECK(string, '%')
1507 || CHECK(string, '@') || CHECK(string, '`')) {
1508 flow_indicators = 1;
1509 block_indicators = 1;
1512 if (CHECK(string, '?') || CHECK(string, ':')) {
1513 flow_indicators = 1;
1514 if (followed_by_whitespace) {
1515 block_indicators = 1;
1519 if (CHECK(string, '-') && followed_by_whitespace) {
1520 flow_indicators = 1;
1521 block_indicators = 1;
1526 if (CHECK(string, ',') || CHECK(string, '?')
1527 || CHECK(string, '[') || CHECK(string, ']')
1528 || CHECK(string, '{') || CHECK(string, '}')) {
1529 flow_indicators = 1;
1532 if (CHECK(string, ':')) {
1533 flow_indicators = 1;
1534 if (followed_by_whitespace) {
1535 block_indicators = 1;
1539 if (CHECK(string, '#') && preceeded_by_whitespace) {
1540 flow_indicators = 1;
1541 block_indicators = 1;
1545 if (!IS_PRINTABLE(string)
1546 || (!IS_ASCII(string) && !emitter->unicode)) {
1547 special_characters = 1;
1550 if (IS_BREAK(string)) {
1554 if (IS_SPACE(string))
1556 if (string.start == string.pointer) {
1559 if (string.pointer+WIDTH(string) == string.end) {
1562 if (previous_break) {
1568 else if (IS_BREAK(string))
1570 if (string.start == string.pointer) {
1573 if (string.pointer+WIDTH(string) == string.end) {
1576 if (previous_space) {
1588 preceeded_by_whitespace = IS_BLANKZ(string);
1590 if (string.pointer != string.end) {
1591 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
1595 emitter->scalar_data.multiline = line_breaks;
1597 emitter->scalar_data.flow_plain_allowed = 1;
1598 emitter->scalar_data.block_plain_allowed = 1;
1599 emitter->scalar_data.single_quoted_allowed = 1;
1600 emitter->scalar_data.block_allowed = 1;
1602 if (leading_space || leading_break || trailing_space || trailing_break) {
1603 emitter->scalar_data.flow_plain_allowed = 0;
1604 emitter->scalar_data.block_plain_allowed = 0;
1607 if (trailing_space) {
1608 emitter->scalar_data.block_allowed = 0;
1612 emitter->scalar_data.flow_plain_allowed = 0;
1613 emitter->scalar_data.block_plain_allowed = 0;
1614 emitter->scalar_data.single_quoted_allowed = 0;
1617 if (space_break || special_characters) {
1618 emitter->scalar_data.flow_plain_allowed = 0;
1619 emitter->scalar_data.block_plain_allowed = 0;
1620 emitter->scalar_data.single_quoted_allowed = 0;
1621 emitter->scalar_data.block_allowed = 0;
1625 emitter->scalar_data.flow_plain_allowed = 0;
1626 emitter->scalar_data.block_plain_allowed = 0;
1629 if (flow_indicators) {
1630 emitter->scalar_data.flow_plain_allowed = 0;
1633 if (block_indicators) {
1634 emitter->scalar_data.block_plain_allowed = 0;
1641 * Check if the event data is valid.
1645 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
1646 yaml_event_t *event)
1648 emitter->anchor_data.anchor = NULL;
1649 emitter->anchor_data.anchor_length = 0;
1650 emitter->tag_data.handle = NULL;
1651 emitter->tag_data.handle_length = 0;
1652 emitter->tag_data.suffix = NULL;
1653 emitter->tag_data.suffix_length = 0;
1654 emitter->scalar_data.value = NULL;
1655 emitter->scalar_data.length = 0;
1657 switch (event->type)
1659 case YAML_ALIAS_EVENT:
1660 if (!yaml_emitter_analyze_anchor(emitter,
1661 event->data.alias.anchor, 1))
1665 case YAML_SCALAR_EVENT:
1666 if (event->data.scalar.anchor) {
1667 if (!yaml_emitter_analyze_anchor(emitter,
1668 event->data.scalar.anchor, 0))
1671 if (event->data.scalar.tag && (emitter->canonical ||
1672 (!event->data.scalar.plain_implicit
1673 && !event->data.scalar.quoted_implicit))) {
1674 if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
1677 if (!yaml_emitter_analyze_scalar(emitter,
1678 event->data.scalar.value, event->data.scalar.length))
1682 case YAML_SEQUENCE_START_EVENT:
1683 if (event->data.sequence_start.anchor) {
1684 if (!yaml_emitter_analyze_anchor(emitter,
1685 event->data.sequence_start.anchor, 0))
1688 if (event->data.sequence_start.tag && (emitter->canonical ||
1689 !event->data.sequence_start.implicit)) {
1690 if (!yaml_emitter_analyze_tag(emitter,
1691 event->data.sequence_start.tag))
1696 case YAML_MAPPING_START_EVENT:
1697 if (event->data.mapping_start.anchor) {
1698 if (!yaml_emitter_analyze_anchor(emitter,
1699 event->data.mapping_start.anchor, 0))
1702 if (event->data.mapping_start.tag && (emitter->canonical ||
1703 !event->data.mapping_start.implicit)) {
1704 if (!yaml_emitter_analyze_tag(emitter,
1705 event->data.mapping_start.tag))
1716 * Write the BOM character.
1720 yaml_emitter_write_bom(yaml_emitter_t *emitter)
1722 if (!FLUSH(emitter)) return 0;
1724 *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
1725 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
1726 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
1732 yaml_emitter_write_indent(yaml_emitter_t *emitter)
1734 int indent = (emitter->indent >= 0) ? emitter->indent : 0;
1736 if (!emitter->indention || emitter->column > indent
1737 || (emitter->column == indent && !emitter->whitespace)) {
1738 if (!PUT_BREAK(emitter)) return 0;
1741 while (emitter->column < indent) {
1742 if (!PUT(emitter, ' ')) return 0;
1745 emitter->whitespace = 1;
1746 emitter->indention = 1;
1752 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
1753 char *indicator, int need_whitespace,
1754 int is_whitespace, int is_indention)
1756 yaml_string_t string = STRING((yaml_char_t *)indicator, strlen(indicator));
1758 if (need_whitespace && !emitter->whitespace) {
1759 if (!PUT(emitter, ' ')) return 0;
1762 while (string.pointer != string.end) {
1763 if (!WRITE(emitter, string)) return 0;
1766 emitter->whitespace = is_whitespace;
1767 emitter->indention = (emitter->indention && is_indention);
1773 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
1774 yaml_char_t *value, size_t length)
1776 yaml_string_t string = STRING(value, length);
1778 while (string.pointer != string.end) {
1779 if (!WRITE(emitter, string)) return 0;
1782 emitter->whitespace = 0;
1783 emitter->indention = 0;
1789 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
1790 yaml_char_t *value, size_t length)
1792 yaml_string_t string = STRING(value, length);
1794 if (!emitter->whitespace) {
1795 if (!PUT(emitter, ' ')) return 0;
1798 while (string.pointer != string.end) {
1799 if (!WRITE(emitter, string)) return 0;
1802 emitter->whitespace = 0;
1803 emitter->indention = 0;
1809 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
1810 yaml_char_t *value, size_t length,
1811 int need_whitespace)
1813 yaml_string_t string = STRING(value, length);
1815 if (need_whitespace && !emitter->whitespace) {
1816 if (!PUT(emitter, ' ')) return 0;
1819 while (string.pointer != string.end) {
1820 if (IS_ALPHA(string)
1821 || CHECK(string, ';') || CHECK(string, '/')
1822 || CHECK(string, '?') || CHECK(string, ':')
1823 || CHECK(string, '@') || CHECK(string, '&')
1824 || CHECK(string, '=') || CHECK(string, '+')
1825 || CHECK(string, '$') || CHECK(string, ',')
1826 || CHECK(string, '_') || CHECK(string, '.')
1827 || CHECK(string, '~') || CHECK(string, '*')
1828 || CHECK(string, '\'') || CHECK(string, '(')
1829 || CHECK(string, ')') || CHECK(string, '[')
1830 || CHECK(string, ']')) {
1831 if (!WRITE(emitter, string)) return 0;
1834 int width = WIDTH(string);
1837 value = *(string.pointer++);
1838 if (!PUT(emitter, '%')) return 0;
1839 if (!PUT(emitter, (value >> 4)
1840 + ((value >> 4) < 10 ? '0' : 'A' - 10)))
1842 if (!PUT(emitter, (value & 0x0F)
1843 + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
1849 emitter->whitespace = 0;
1850 emitter->indention = 0;
1856 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
1857 yaml_char_t *value, size_t length, int allow_breaks)
1859 yaml_string_t string = STRING(value, length);
1863 if (!emitter->whitespace) {
1864 if (!PUT(emitter, ' ')) return 0;
1867 while (string.pointer != string.end)
1869 if (IS_SPACE(string))
1871 if (allow_breaks && !spaces
1872 && emitter->column > emitter->best_width
1873 && !IS_SPACE_AT(string, 1)) {
1874 if (!yaml_emitter_write_indent(emitter)) return 0;
1878 if (!WRITE(emitter, string)) return 0;
1882 else if (IS_BREAK(string))
1884 if (!breaks && CHECK(string, '\n')) {
1885 if (!PUT_BREAK(emitter)) return 0;
1887 if (!WRITE_BREAK(emitter, string)) return 0;
1888 emitter->indention = 1;
1894 if (!yaml_emitter_write_indent(emitter)) return 0;
1896 if (!WRITE(emitter, string)) return 0;
1897 emitter->indention = 0;
1903 emitter->whitespace = 0;
1904 emitter->indention = 0;
1910 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
1911 yaml_char_t *value, size_t length, int allow_breaks)
1913 yaml_string_t string = STRING(value, length);
1917 if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
1920 while (string.pointer != string.end)
1922 if (IS_SPACE(string))
1924 if (allow_breaks && !spaces
1925 && emitter->column > emitter->best_width
1926 && string.pointer != string.start
1927 && string.pointer != string.end - 1
1928 && !IS_SPACE_AT(string, 1)) {
1929 if (!yaml_emitter_write_indent(emitter)) return 0;
1933 if (!WRITE(emitter, string)) return 0;
1937 else if (IS_BREAK(string))
1939 if (!breaks && CHECK(string, '\n')) {
1940 if (!PUT_BREAK(emitter)) return 0;
1942 if (!WRITE_BREAK(emitter, string)) return 0;
1943 emitter->indention = 1;
1949 if (!yaml_emitter_write_indent(emitter)) return 0;
1951 if (CHECK(string, '\'')) {
1952 if (!PUT(emitter, '\'')) return 0;
1954 if (!WRITE(emitter, string)) return 0;
1955 emitter->indention = 0;
1961 if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
1964 emitter->whitespace = 0;
1965 emitter->indention = 0;
1971 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
1972 yaml_char_t *value, size_t length, int allow_breaks)
1974 yaml_string_t string = STRING(value, length);
1977 if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
1980 while (string.pointer != string.end)
1982 if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
1983 || IS_BOM(string) || IS_BREAK(string)
1984 || CHECK(string, '"') || CHECK(string, '\\'))
1986 unsigned char octet;
1991 octet = string.pointer[0];
1992 width = (octet & 0x80) == 0x00 ? 1 :
1993 (octet & 0xE0) == 0xC0 ? 2 :
1994 (octet & 0xF0) == 0xE0 ? 3 :
1995 (octet & 0xF8) == 0xF0 ? 4 : 0;
1996 value = (octet & 0x80) == 0x00 ? octet & 0x7F :
1997 (octet & 0xE0) == 0xC0 ? octet & 0x1F :
1998 (octet & 0xF0) == 0xE0 ? octet & 0x0F :
1999 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
2000 for (k = 1; k < (int)width; k ++) {
2001 octet = string.pointer[k];
2002 value = (value << 6) + (octet & 0x3F);
2004 string.pointer += width;
2006 if (!PUT(emitter, '\\')) return 0;
2011 if (!PUT(emitter, '0')) return 0;
2015 if (!PUT(emitter, 'a')) return 0;
2019 if (!PUT(emitter, 'b')) return 0;
2023 if (!PUT(emitter, 't')) return 0;
2027 if (!PUT(emitter, 'n')) return 0;
2031 if (!PUT(emitter, 'v')) return 0;
2035 if (!PUT(emitter, 'f')) return 0;
2039 if (!PUT(emitter, 'r')) return 0;
2043 if (!PUT(emitter, 'e')) return 0;
2047 if (!PUT(emitter, '\"')) return 0;
2051 if (!PUT(emitter, '\\')) return 0;
2055 if (!PUT(emitter, 'N')) return 0;
2059 if (!PUT(emitter, '_')) return 0;
2063 if (!PUT(emitter, 'L')) return 0;
2067 if (!PUT(emitter, 'P')) return 0;
2071 if (value <= 0xFF) {
2072 if (!PUT(emitter, 'x')) return 0;
2075 else if (value <= 0xFFFF) {
2076 if (!PUT(emitter, 'u')) return 0;
2080 if (!PUT(emitter, 'U')) return 0;
2083 for (k = (width-1)*4; k >= 0; k -= 4) {
2084 int digit = (value >> k) & 0x0F;
2085 if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
2091 else if (IS_SPACE(string))
2093 if (allow_breaks && !spaces
2094 && emitter->column > emitter->best_width
2095 && string.pointer != string.start
2096 && string.pointer != string.end - 1) {
2097 if (!yaml_emitter_write_indent(emitter)) return 0;
2098 if (IS_SPACE_AT(string, 1)) {
2099 if (!PUT(emitter, '\\')) return 0;
2104 if (!WRITE(emitter, string)) return 0;
2110 if (!WRITE(emitter, string)) return 0;
2115 if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
2118 emitter->whitespace = 0;
2119 emitter->indention = 0;
2125 yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
2126 yaml_string_t string)
2128 char indent_hint[2];
2129 char *chomp_hint = NULL;
2131 if (IS_SPACE(string) || IS_BREAK(string))
2133 indent_hint[0] = '0' + (char)emitter->best_indent;
2134 indent_hint[1] = '\0';
2135 if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0))
2139 string.pointer = string.end;
2140 if (string.start == string.pointer)
2148 } while ((*string.pointer & 0xC0) == 0x80);
2149 if (!IS_BREAK(string))
2153 else if (string.start == string.pointer)
2161 } while ((*string.pointer & 0xC0) == 0x80);
2162 if (IS_BREAK(string))
2171 if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0))
2179 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
2180 yaml_char_t *value, size_t length)
2182 yaml_string_t string = STRING(value, length);
2185 if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0))
2187 if (!yaml_emitter_write_block_scalar_hints(emitter, string))
2189 if (!PUT_BREAK(emitter)) return 0;
2190 emitter->indention = 1;
2191 emitter->whitespace = 1;
2193 while (string.pointer != string.end)
2195 if (IS_BREAK(string))
2197 if (!WRITE_BREAK(emitter, string)) return 0;
2198 emitter->indention = 1;
2204 if (!yaml_emitter_write_indent(emitter)) return 0;
2206 if (!WRITE(emitter, string)) return 0;
2207 emitter->indention = 0;
2216 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
2217 yaml_char_t *value, size_t length)
2219 yaml_string_t string = STRING(value, length);
2221 int leading_spaces = 1;
2223 if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0))
2225 if (!yaml_emitter_write_block_scalar_hints(emitter, string))
2227 if (!PUT_BREAK(emitter)) return 0;
2228 emitter->indention = 1;
2229 emitter->whitespace = 1;
2231 while (string.pointer != string.end)
2233 if (IS_BREAK(string))
2235 if (!breaks && !leading_spaces && CHECK(string, '\n')) {
2237 while (IS_BREAK_AT(string, k)) {
2238 k += WIDTH_AT(string, k);
2240 if (!IS_BLANK_AT(string, k)) {
2241 if (!PUT_BREAK(emitter)) return 0;
2244 if (!WRITE_BREAK(emitter, string)) return 0;
2245 emitter->indention = 1;
2251 if (!yaml_emitter_write_indent(emitter)) return 0;
2252 leading_spaces = IS_BLANK(string);
2254 if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
2255 && emitter->column > emitter->best_width) {
2256 if (!yaml_emitter_write_indent(emitter)) return 0;
2260 if (!WRITE(emitter, string)) return 0;
2262 emitter->indention = 0;