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;
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 inline_spaces = 0;
1458 int inline_breaks = 0;
1459 int leading_spaces = 0;
1460 int leading_breaks = 0;
1461 int trailing_spaces = 0;
1462 int trailing_breaks = 0;
1463 int inline_breaks_spaces = 0;
1464 int mixed_breaks_spaces = 0;
1466 int preceeded_by_space = 0;
1467 int followed_by_space = 0;
1473 emitter->scalar_data.value = value;
1474 emitter->scalar_data.length = length;
1476 if (string.start == string.end)
1478 emitter->scalar_data.multiline = 0;
1479 emitter->scalar_data.flow_plain_allowed = 0;
1480 emitter->scalar_data.block_plain_allowed = 1;
1481 emitter->scalar_data.single_quoted_allowed = 1;
1482 emitter->scalar_data.block_allowed = 0;
1487 if ((CHECK_AT(string, '-', 0)
1488 && CHECK_AT(string, '-', 1)
1489 && CHECK_AT(string, '-', 2))
1490 || (CHECK_AT(string, '.', 0)
1491 && CHECK_AT(string, '.', 1)
1492 && CHECK_AT(string, '.', 2))) {
1493 block_indicators = 1;
1494 flow_indicators = 1;
1497 preceeded_by_space = 1;
1498 followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
1500 while (string.pointer != string.end)
1502 if (string.start == string.pointer)
1504 if (CHECK(string, '#') || CHECK(string, ',')
1505 || CHECK(string, '[') || CHECK(string, ']')
1506 || 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 flow_indicators = 1;
1513 block_indicators = 1;
1516 if (CHECK(string, '?') || CHECK(string, ':')) {
1517 flow_indicators = 1;
1518 if (followed_by_space) {
1519 block_indicators = 1;
1523 if (CHECK(string, '-') && followed_by_space) {
1524 flow_indicators = 1;
1525 block_indicators = 1;
1530 if (CHECK(string, ',') || CHECK(string, '?')
1531 || CHECK(string, '[') || CHECK(string, ']')
1532 || CHECK(string, '{') || CHECK(string, '}')) {
1533 flow_indicators = 1;
1536 if (CHECK(string, ':')) {
1537 flow_indicators = 1;
1538 if (followed_by_space) {
1539 block_indicators = 1;
1543 if (CHECK(string, '#') && preceeded_by_space) {
1544 flow_indicators = 1;
1545 block_indicators = 1;
1549 if (!IS_PRINTABLE(string)
1550 || (!IS_ASCII(string) && !emitter->unicode)) {
1551 special_characters = 1;
1554 if (IS_BREAK(string)) {
1558 if (IS_SPACE(string))
1561 if (string.start == string.pointer) {
1566 else if (IS_BREAK(string))
1572 if (string.start == string.pointer) {
1577 else if (spaces || breaks)
1580 if (spaces && breaks) {
1581 mixed_breaks_spaces = 1;
1592 mixed_breaks_spaces = 1;
1594 else if (spaces && breaks) {
1595 inline_breaks_spaces = 1;
1604 spaces = breaks = mixed = leading = 0;
1607 if ((spaces || breaks) && string.pointer == string.end-1)
1609 if (spaces && breaks) {
1610 mixed_breaks_spaces = 1;
1616 trailing_spaces = 1;
1622 trailing_breaks = 1;
1626 preceeded_by_space = IS_BLANKZ(string);
1628 if (string.pointer != string.end) {
1629 followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
1633 emitter->scalar_data.multiline = line_breaks;
1635 emitter->scalar_data.flow_plain_allowed = 1;
1636 emitter->scalar_data.block_plain_allowed = 1;
1637 emitter->scalar_data.single_quoted_allowed = 1;
1638 emitter->scalar_data.block_allowed = 1;
1640 if (leading_spaces || leading_breaks || trailing_spaces) {
1641 emitter->scalar_data.flow_plain_allowed = 0;
1642 emitter->scalar_data.block_plain_allowed = 0;
1643 emitter->scalar_data.block_allowed = 0;
1646 if (trailing_breaks) {
1647 emitter->scalar_data.flow_plain_allowed = 0;
1648 emitter->scalar_data.block_plain_allowed = 0;
1651 if (inline_breaks_spaces) {
1652 emitter->scalar_data.flow_plain_allowed = 0;
1653 emitter->scalar_data.block_plain_allowed = 0;
1654 emitter->scalar_data.single_quoted_allowed = 0;
1657 if (mixed_breaks_spaces || special_characters) {
1658 emitter->scalar_data.flow_plain_allowed = 0;
1659 emitter->scalar_data.block_plain_allowed = 0;
1660 emitter->scalar_data.single_quoted_allowed = 0;
1661 emitter->scalar_data.block_allowed = 0;
1665 emitter->scalar_data.flow_plain_allowed = 0;
1666 emitter->scalar_data.block_plain_allowed = 0;
1669 if (flow_indicators) {
1670 emitter->scalar_data.flow_plain_allowed = 0;
1673 if (block_indicators) {
1674 emitter->scalar_data.block_plain_allowed = 0;
1681 * Check if the event data is valid.
1685 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
1686 yaml_event_t *event)
1688 emitter->anchor_data.anchor = NULL;
1689 emitter->anchor_data.anchor_length = 0;
1690 emitter->tag_data.handle = NULL;
1691 emitter->tag_data.handle_length = 0;
1692 emitter->tag_data.suffix = NULL;
1693 emitter->tag_data.suffix_length = 0;
1694 emitter->scalar_data.value = NULL;
1695 emitter->scalar_data.length = 0;
1697 switch (event->type)
1699 case YAML_ALIAS_EVENT:
1700 if (!yaml_emitter_analyze_anchor(emitter,
1701 event->data.alias.anchor, 1))
1705 case YAML_SCALAR_EVENT:
1706 if (event->data.scalar.anchor) {
1707 if (!yaml_emitter_analyze_anchor(emitter,
1708 event->data.scalar.anchor, 0))
1711 if (event->data.scalar.tag && (emitter->canonical ||
1712 (!event->data.scalar.plain_implicit
1713 && !event->data.scalar.quoted_implicit))) {
1714 if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
1717 if (!yaml_emitter_analyze_scalar(emitter,
1718 event->data.scalar.value, event->data.scalar.length))
1722 case YAML_SEQUENCE_START_EVENT:
1723 if (event->data.sequence_start.anchor) {
1724 if (!yaml_emitter_analyze_anchor(emitter,
1725 event->data.sequence_start.anchor, 0))
1728 if (event->data.sequence_start.tag && (emitter->canonical ||
1729 !event->data.sequence_start.implicit)) {
1730 if (!yaml_emitter_analyze_tag(emitter,
1731 event->data.sequence_start.tag))
1736 case YAML_MAPPING_START_EVENT:
1737 if (event->data.mapping_start.anchor) {
1738 if (!yaml_emitter_analyze_anchor(emitter,
1739 event->data.mapping_start.anchor, 0))
1742 if (event->data.mapping_start.tag && (emitter->canonical ||
1743 !event->data.mapping_start.implicit)) {
1744 if (!yaml_emitter_analyze_tag(emitter,
1745 event->data.mapping_start.tag))
1756 * Write the BOM character.
1760 yaml_emitter_write_bom(yaml_emitter_t *emitter)
1762 if (!FLUSH(emitter)) return 0;
1764 *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
1765 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
1766 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
1772 yaml_emitter_write_indent(yaml_emitter_t *emitter)
1774 int indent = (emitter->indent >= 0) ? emitter->indent : 0;
1776 if (!emitter->indention || emitter->column > indent
1777 || (emitter->column == indent && !emitter->whitespace)) {
1778 if (!PUT_BREAK(emitter)) return 0;
1781 while (emitter->column < indent) {
1782 if (!PUT(emitter, ' ')) return 0;
1785 emitter->whitespace = 1;
1786 emitter->indention = 1;
1792 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
1793 char *indicator, int need_whitespace,
1794 int is_whitespace, int is_indention)
1796 yaml_string_t string = STRING((yaml_char_t *)indicator, strlen(indicator));
1798 if (need_whitespace && !emitter->whitespace) {
1799 if (!PUT(emitter, ' ')) return 0;
1802 while (string.pointer != string.end) {
1803 if (!WRITE(emitter, string)) return 0;
1806 emitter->whitespace = is_whitespace;
1807 emitter->indention = (emitter->indention && is_indention);
1813 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
1814 yaml_char_t *value, size_t length)
1816 yaml_string_t string = STRING(value, length);
1818 while (string.pointer != string.end) {
1819 if (!WRITE(emitter, string)) return 0;
1822 emitter->whitespace = 0;
1823 emitter->indention = 0;
1829 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
1830 yaml_char_t *value, size_t length)
1832 yaml_string_t string = STRING(value, length);
1834 if (!emitter->whitespace) {
1835 if (!PUT(emitter, ' ')) return 0;
1838 while (string.pointer != string.end) {
1839 if (!WRITE(emitter, string)) return 0;
1842 emitter->whitespace = 0;
1843 emitter->indention = 0;
1849 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
1850 yaml_char_t *value, size_t length,
1851 int need_whitespace)
1853 yaml_string_t string = STRING(value, length);
1855 if (need_whitespace && !emitter->whitespace) {
1856 if (!PUT(emitter, ' ')) return 0;
1859 while (string.pointer != string.end) {
1860 if (IS_ALPHA(string)
1861 || CHECK(string, ';') || CHECK(string, '/')
1862 || CHECK(string, '?') || CHECK(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, ']')) {
1871 if (!WRITE(emitter, string)) return 0;
1874 int width = WIDTH(string);
1877 value = *(string.pointer++);
1878 if (!PUT(emitter, '%')) return 0;
1879 if (!PUT(emitter, (value >> 4)
1880 + ((value >> 4) < 10 ? '0' : 'A' - 10)))
1882 if (!PUT(emitter, (value & 0x0F)
1883 + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
1889 emitter->whitespace = 0;
1890 emitter->indention = 0;
1896 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
1897 yaml_char_t *value, size_t length, int allow_breaks)
1899 yaml_string_t string = STRING(value, length);
1903 if (!emitter->whitespace) {
1904 if (!PUT(emitter, ' ')) return 0;
1907 while (string.pointer != string.end)
1909 if (IS_SPACE(string))
1911 if (allow_breaks && !spaces
1912 && emitter->column > emitter->best_width
1913 && !IS_SPACE_AT(string, 1)) {
1914 if (!yaml_emitter_write_indent(emitter)) return 0;
1918 if (!WRITE(emitter, string)) return 0;
1922 else if (IS_BREAK(string))
1924 if (!breaks && CHECK(string, '\n')) {
1925 if (!PUT_BREAK(emitter)) return 0;
1927 if (!WRITE_BREAK(emitter, string)) return 0;
1928 emitter->indention = 1;
1934 if (!yaml_emitter_write_indent(emitter)) return 0;
1936 if (!WRITE(emitter, string)) return 0;
1937 emitter->indention = 0;
1943 emitter->whitespace = 0;
1944 emitter->indention = 0;
1950 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
1951 yaml_char_t *value, size_t length, int allow_breaks)
1953 yaml_string_t string = STRING(value, length);
1957 if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
1960 while (string.pointer != string.end)
1962 if (IS_SPACE(string))
1964 if (allow_breaks && !spaces
1965 && emitter->column > emitter->best_width
1966 && string.pointer != string.start
1967 && string.pointer != string.end - 1
1968 && !IS_SPACE_AT(string, 1)) {
1969 if (!yaml_emitter_write_indent(emitter)) return 0;
1973 if (!WRITE(emitter, string)) return 0;
1977 else if (IS_BREAK(string))
1979 if (!breaks && CHECK(string, '\n')) {
1980 if (!PUT_BREAK(emitter)) return 0;
1982 if (!WRITE_BREAK(emitter, string)) return 0;
1983 emitter->indention = 1;
1989 if (!yaml_emitter_write_indent(emitter)) return 0;
1991 if (CHECK(string, '\'')) {
1992 if (!PUT(emitter, '\'')) return 0;
1994 if (!WRITE(emitter, string)) return 0;
1995 emitter->indention = 0;
2001 if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
2004 emitter->whitespace = 0;
2005 emitter->indention = 0;
2011 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
2012 yaml_char_t *value, size_t length, int allow_breaks)
2014 yaml_string_t string = STRING(value, length);
2017 if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
2020 while (string.pointer != string.end)
2022 if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
2023 || IS_BOM(string) || IS_BREAK(string)
2024 || CHECK(string, '"') || CHECK(string, '\\'))
2026 unsigned char octet;
2031 octet = string.pointer[0];
2032 width = (octet & 0x80) == 0x00 ? 1 :
2033 (octet & 0xE0) == 0xC0 ? 2 :
2034 (octet & 0xF0) == 0xE0 ? 3 :
2035 (octet & 0xF8) == 0xF0 ? 4 : 0;
2036 value = (octet & 0x80) == 0x00 ? octet & 0x7F :
2037 (octet & 0xE0) == 0xC0 ? octet & 0x1F :
2038 (octet & 0xF0) == 0xE0 ? octet & 0x0F :
2039 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
2040 for (k = 1; k < width; k ++) {
2041 octet = string.pointer[k];
2042 value = (value << 6) + (octet & 0x3F);
2044 string.pointer += width;
2046 if (!PUT(emitter, '\\')) return 0;
2051 if (!PUT(emitter, '0')) return 0;
2055 if (!PUT(emitter, 'a')) return 0;
2059 if (!PUT(emitter, 'b')) return 0;
2063 if (!PUT(emitter, 't')) return 0;
2067 if (!PUT(emitter, 'n')) return 0;
2071 if (!PUT(emitter, 'v')) return 0;
2075 if (!PUT(emitter, 'f')) return 0;
2079 if (!PUT(emitter, 'r')) return 0;
2083 if (!PUT(emitter, 'e')) return 0;
2087 if (!PUT(emitter, '\"')) return 0;
2091 if (!PUT(emitter, '\\')) return 0;
2095 if (!PUT(emitter, 'N')) return 0;
2099 if (!PUT(emitter, '_')) return 0;
2103 if (!PUT(emitter, 'L')) return 0;
2107 if (!PUT(emitter, 'P')) return 0;
2111 if (value <= 0xFF) {
2112 if (!PUT(emitter, 'x')) return 0;
2115 else if (value <= 0xFFFF) {
2116 if (!PUT(emitter, 'u')) return 0;
2120 if (!PUT(emitter, 'U')) return 0;
2123 for (k = (width-1)*4; k >= 0; k -= 4) {
2124 int digit = (value >> k) & 0x0F;
2125 if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
2131 else if (IS_SPACE(string))
2133 if (allow_breaks && !spaces
2134 && emitter->column > emitter->best_width
2135 && string.pointer != string.start
2136 && string.pointer != string.end - 1) {
2137 if (!yaml_emitter_write_indent(emitter)) return 0;
2138 if (IS_SPACE_AT(string, 1)) {
2139 if (!PUT(emitter, '\\')) return 0;
2144 if (!WRITE(emitter, string)) return 0;
2150 if (!WRITE(emitter, string)) return 0;
2155 if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
2158 emitter->whitespace = 0;
2159 emitter->indention = 0;
2165 yaml_emitter_determine_chomping(yaml_emitter_t *emitter,
2166 yaml_string_t string)
2168 string.pointer = string.end;
2169 if (string.start == string.pointer)
2173 } while ((*string.pointer & 0xC0) == 0x80);
2174 if (!IS_BREAK(string))
2176 if (string.start == string.pointer)
2180 } while ((*string.pointer & 0xC0) == 0x80);
2181 if (!IS_BREAK(string))
2188 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
2189 yaml_char_t *value, size_t length)
2191 yaml_string_t string = STRING(value, length);
2192 int chomp = yaml_emitter_determine_chomping(emitter, string);
2195 if (!yaml_emitter_write_indicator(emitter,
2196 chomp == -1 ? "|-" : chomp == +1 ? "|+" : "|", 1, 0, 0))
2198 if (!yaml_emitter_write_indent(emitter))
2201 while (string.pointer != string.end)
2203 if (IS_BREAK(string))
2205 if (!WRITE_BREAK(emitter, string)) return 0;
2206 emitter->indention = 1;
2212 if (!yaml_emitter_write_indent(emitter)) return 0;
2214 if (!WRITE(emitter, string)) return 0;
2215 emitter->indention = 0;
2224 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
2225 yaml_char_t *value, size_t length)
2227 yaml_string_t string = STRING(value, length);
2228 int chomp = yaml_emitter_determine_chomping(emitter, string);
2230 int leading_spaces = 0;
2232 if (!yaml_emitter_write_indicator(emitter,
2233 chomp == -1 ? ">-" : chomp == +1 ? ">+" : ">", 1, 0, 0))
2235 if (!yaml_emitter_write_indent(emitter))
2238 while (string.pointer != string.end)
2240 if (IS_BREAK(string))
2242 if (!breaks && !leading_spaces && CHECK(string, '\n')) {
2244 while (IS_BREAK_AT(string, k)) {
2245 k += WIDTH_AT(string, k);
2247 if (!IS_BLANK_AT(string, k)) {
2248 if (!PUT_BREAK(emitter)) return 0;
2251 if (!WRITE_BREAK(emitter, string)) return 0;
2252 emitter->indention = 1;
2258 if (!yaml_emitter_write_indent(emitter)) return 0;
2259 leading_spaces = IS_BLANK(string);
2261 if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
2262 && emitter->column > emitter->best_width) {
2263 if (!yaml_emitter_write_indent(emitter)) return 0;
2267 if (!WRITE(emitter, string)) return 0;
2269 emitter->indention = 0;