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 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;
693 return yaml_emitter_set_emitter_error(emitter,
694 "expected DOCUMENT-END");
698 * Expect a flow item node.
702 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
703 yaml_event_t *event, int first)
707 if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0))
709 if (!yaml_emitter_increase_indent(emitter, 1, 0))
711 emitter->flow_level ++;
714 if (event->type == YAML_SEQUENCE_END_EVENT)
716 emitter->flow_level --;
717 emitter->indent = POP(emitter, emitter->indents);
718 if (emitter->canonical && !first) {
719 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
721 if (!yaml_emitter_write_indent(emitter))
724 if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0))
726 emitter->state = POP(emitter, emitter->states);
731 if (emitter->canonical || emitter->column > emitter->best_width) {
732 if (!yaml_emitter_write_indent(emitter))
735 if (PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
738 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
742 * Expect a flow key node.
746 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
747 yaml_event_t *event, int first)
751 if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0))
753 if (!yaml_emitter_increase_indent(emitter, 1, 0))
755 emitter->flow_level ++;
758 if (event->type == YAML_MAPPING_END_EVENT)
760 emitter->flow_level --;
761 emitter->indent = POP(emitter, emitter->indents);
762 if (emitter->canonical && !first) {
763 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
765 if (!yaml_emitter_write_indent(emitter))
768 if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0))
770 emitter->state = POP(emitter, emitter->states);
776 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
779 if (emitter->canonical || emitter->column > emitter->best_width) {
780 if (!yaml_emitter_write_indent(emitter))
784 if (!emitter->canonical && yaml_emitter_check_simple_key(emitter))
786 if (!PUSH(emitter, emitter->states,
787 YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE))
790 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
794 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0))
796 if (!PUSH(emitter, emitter->states,
797 YAML_EMIT_FLOW_MAPPING_VALUE_STATE))
800 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
805 * Expect a flow value node.
809 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
810 yaml_event_t *event, int simple)
813 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
817 if (emitter->canonical || emitter->column > emitter->best_width) {
818 if (!yaml_emitter_write_indent(emitter))
821 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0))
824 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE))
826 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
830 * Expect a block item node.
834 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
835 yaml_event_t *event, int first)
839 if (!yaml_emitter_increase_indent(emitter, 0,
840 (emitter->mapping_context && !emitter->indention)))
844 if (event->type == YAML_SEQUENCE_END_EVENT)
846 emitter->indent = POP(emitter, emitter->indents);
847 emitter->state = POP(emitter, emitter->states);
852 if (!yaml_emitter_write_indent(emitter))
854 if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1))
856 if (!PUSH(emitter, emitter->states,
857 YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE))
860 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
864 * Expect a block key node.
868 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
869 yaml_event_t *event, int first)
873 if (!yaml_emitter_increase_indent(emitter, 0, 0))
877 if (event->type == YAML_MAPPING_END_EVENT)
879 emitter->indent = POP(emitter, emitter->indents);
880 emitter->state = POP(emitter, emitter->states);
885 if (!yaml_emitter_write_indent(emitter))
888 if (yaml_emitter_check_simple_key(emitter))
890 if (!PUSH(emitter, emitter->states,
891 YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE))
894 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
898 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1))
900 if (!PUSH(emitter, emitter->states,
901 YAML_EMIT_BLOCK_MAPPING_VALUE_STATE))
904 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
909 * Expect a block value node.
913 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
914 yaml_event_t *event, int simple)
917 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
921 if (!yaml_emitter_write_indent(emitter))
923 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1))
926 if (!PUSH(emitter, emitter->states,
927 YAML_EMIT_BLOCK_MAPPING_KEY_STATE))
930 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
938 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
939 int root, int sequence, int mapping, int simple_key)
941 emitter->root_context = root;
942 emitter->sequence_context = sequence;
943 emitter->mapping_context = mapping;
944 emitter->simple_key_context = simple_key;
948 case YAML_ALIAS_EVENT:
949 return yaml_emitter_emit_alias(emitter, event);
951 case YAML_SCALAR_EVENT:
952 return yaml_emitter_emit_scalar(emitter, event);
954 case YAML_SEQUENCE_START_EVENT:
955 return yaml_emitter_emit_sequence_start(emitter, event);
957 case YAML_MAPPING_START_EVENT:
958 return yaml_emitter_emit_mapping_start(emitter, event);
961 return yaml_emitter_set_emitter_error(emitter,
962 "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS");
973 yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event)
975 if (!yaml_emitter_process_anchor(emitter))
977 emitter->state = POP(emitter, emitter->states);
987 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event)
989 if (!yaml_emitter_select_scalar_style(emitter, event))
991 if (!yaml_emitter_process_anchor(emitter))
993 if (!yaml_emitter_process_tag(emitter))
995 if (!yaml_emitter_increase_indent(emitter, 1, 0))
997 if (!yaml_emitter_process_scalar(emitter))
999 emitter->indent = POP(emitter, emitter->indents);
1000 emitter->state = POP(emitter, emitter->states);
1006 * Expect SEQUENCE-START.
1010 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event)
1012 if (!yaml_emitter_process_anchor(emitter))
1014 if (!yaml_emitter_process_tag(emitter))
1017 if (emitter->flow_level || emitter->canonical
1018 || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE
1019 || yaml_emitter_check_empty_sequence(emitter)) {
1020 emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
1023 emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
1030 * Expect MAPPING-START.
1034 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event)
1036 if (!yaml_emitter_process_anchor(emitter))
1038 if (!yaml_emitter_process_tag(emitter))
1041 if (emitter->flow_level || emitter->canonical
1042 || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE
1043 || yaml_emitter_check_empty_mapping(emitter)) {
1044 emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
1047 emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
1054 * Check if the document content is an empty scalar.
1058 yaml_emitter_check_empty_document(yaml_emitter_t *emitter)
1064 * Check if the next events represent an empty sequence.
1068 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter)
1070 if (emitter->events.tail - emitter->events.head < 2)
1073 return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT
1074 && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT);
1078 * Check if the next events represent an empty mapping.
1082 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter)
1084 if (emitter->events.tail - emitter->events.head < 2)
1087 return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT
1088 && emitter->events.head[1].type == YAML_MAPPING_END_EVENT);
1092 * Check if the next node can be expressed as a simple key.
1096 yaml_emitter_check_simple_key(yaml_emitter_t *emitter)
1098 yaml_event_t *event = emitter->events.head;
1101 switch (event->type)
1103 case YAML_ALIAS_EVENT:
1104 length += emitter->anchor_data.anchor_length;
1107 case YAML_SCALAR_EVENT:
1108 if (emitter->scalar_data.multiline)
1110 length += emitter->anchor_data.anchor_length
1111 + emitter->tag_data.handle_length
1112 + emitter->tag_data.suffix_length
1113 + emitter->scalar_data.length;
1116 case YAML_SEQUENCE_START_EVENT:
1117 if (!yaml_emitter_check_empty_sequence(emitter))
1119 length += emitter->anchor_data.anchor_length
1120 + emitter->tag_data.handle_length
1121 + emitter->tag_data.suffix_length;
1124 case YAML_MAPPING_START_EVENT:
1125 if (!yaml_emitter_check_empty_sequence(emitter))
1127 length += emitter->anchor_data.anchor_length
1128 + emitter->tag_data.handle_length
1129 + emitter->tag_data.suffix_length;
1143 * Determine an acceptable scalar style.
1147 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
1149 yaml_scalar_style_t style = event->data.scalar.style;
1151 if (style == YAML_ANY_SCALAR_STYLE)
1152 style = YAML_PLAIN_SCALAR_STYLE;
1154 if (emitter->canonical)
1155 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1157 if (emitter->simple_key_context && emitter->scalar_data.multiline)
1158 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1160 if (style == YAML_PLAIN_SCALAR_STYLE)
1162 if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
1163 || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
1164 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1165 if (!emitter->scalar_data.length
1166 && (emitter->flow_level || emitter->simple_key_context))
1167 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1168 if (!event->data.scalar.plain_implicit
1169 && !emitter->tag_data.handle && !emitter->tag_data.suffix)
1170 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1173 if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
1175 if (!emitter->scalar_data.single_quoted_allowed)
1176 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1179 if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
1181 if (!emitter->scalar_data.block_allowed)
1182 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1185 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1187 if (!event->data.scalar.plain_implicit
1188 && !event->data.scalar.quoted_implicit) {
1189 return yaml_emitter_set_emitter_error(emitter,
1190 "neither tag nor implicit flags are specified");
1193 if (event->data.scalar.plain_implicit
1194 && style != YAML_PLAIN_SCALAR_STYLE) {
1195 emitter->tag_data.handle = (yaml_char_t *)"!";
1196 emitter->tag_data.handle_length = 1;
1200 emitter->scalar_data.style = style;
1210 yaml_emitter_process_anchor(yaml_emitter_t *emitter)
1212 if (!emitter->anchor_data.anchor)
1215 if (!yaml_emitter_write_indicator(emitter,
1216 (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
1219 return yaml_emitter_write_anchor(emitter,
1220 emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
1228 yaml_emitter_process_tag(yaml_emitter_t *emitter)
1230 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1233 if (emitter->tag_data.handle)
1235 if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
1236 emitter->tag_data.handle_length))
1238 if (emitter->tag_data.suffix) {
1239 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1240 emitter->tag_data.suffix_length, 0))
1246 if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
1248 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1249 emitter->tag_data.suffix_length, 0))
1251 if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
1263 yaml_emitter_process_scalar(yaml_emitter_t *emitter)
1265 switch (emitter->scalar_data.style)
1267 case YAML_PLAIN_SCALAR_STYLE:
1268 return yaml_emitter_write_plain_scalar(emitter,
1269 emitter->scalar_data.value, emitter->scalar_data.length,
1270 !emitter->simple_key_context);
1272 case YAML_SINGLE_QUOTED_SCALAR_STYLE:
1273 return yaml_emitter_write_single_quoted_scalar(emitter,
1274 emitter->scalar_data.value, emitter->scalar_data.length,
1275 !emitter->simple_key_context);
1277 case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
1278 return yaml_emitter_write_double_quoted_scalar(emitter,
1279 emitter->scalar_data.value, emitter->scalar_data.length,
1280 !emitter->simple_key_context);
1282 case YAML_LITERAL_SCALAR_STYLE:
1283 return yaml_emitter_write_literal_scalar(emitter,
1284 emitter->scalar_data.value, emitter->scalar_data.length);
1286 case YAML_FOLDED_SCALAR_STYLE:
1287 return yaml_emitter_write_folded_scalar(emitter,
1288 emitter->scalar_data.value, emitter->scalar_data.length);
1291 assert(1); /* Impossible. */
1298 * Check if a %YAML directive is valid.
1302 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
1303 yaml_version_directive_t version_directive)
1305 if (version_directive.major != 1 || version_directive.minor != 1) {
1306 return yaml_emitter_set_emitter_error(emitter,
1307 "incompatible %YAML directive");
1314 * Check if a %TAG directive is valid.
1318 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
1319 yaml_tag_directive_t tag_directive)
1321 yaml_string_t handle = STRING(tag_directive.handle,
1322 strlen((char *)tag_directive.handle));
1323 yaml_string_t prefix = STRING(tag_directive.prefix,
1324 strlen((char *)tag_directive.prefix));
1326 if (handle.start == handle.end) {
1327 return yaml_emitter_set_emitter_error(emitter,
1328 "tag handle must not be empty");
1331 if (handle.start[0] != '!') {
1332 return yaml_emitter_set_emitter_error(emitter,
1333 "tag handle must start with '!'");
1336 if (handle.end[-1] != '!') {
1337 return yaml_emitter_set_emitter_error(emitter,
1338 "tag handle must end with '!'");
1343 while (handle.pointer != handle.end-1) {
1344 if (!IS_ALPHA(handle)) {
1345 return yaml_emitter_set_emitter_error(emitter,
1346 "tag handle must contain alphanumerical characters only");
1351 if (prefix.start == prefix.end) {
1352 return yaml_emitter_set_emitter_error(emitter,
1353 "tag prefix must not be empty");
1360 * Check if an anchor is valid.
1364 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
1365 yaml_char_t *anchor, int alias)
1367 yaml_string_t string = STRING(anchor, strlen((char *)anchor));
1369 if (string.start == string.end) {
1370 return yaml_emitter_set_emitter_error(emitter, alias ?
1371 "alias value must not be empty" :
1372 "anchor value must not be empty");
1375 while (string.pointer != string.end) {
1376 if (!IS_ALPHA(string)) {
1377 return yaml_emitter_set_emitter_error(emitter, alias ?
1378 "alias value must contain alphanumerical characters only" :
1379 "anchor value must contain alphanumerical characters only");
1386 * Check if a tag is valid.
1390 yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
1393 yaml_string_t string = STRING(tag, strlen((char *)tag));
1394 yaml_tag_directive_t *tag_directive;
1396 if (string.start == string.end) {
1397 return yaml_emitter_set_emitter_error(emitter,
1398 "tag value must not be empty");
1401 for (tag_directive = emitter->tag_directives.start;
1402 tag_directive != emitter->tag_directives.end; tag_directive ++) {
1403 size_t prefix_length = strlen((char *)tag_directive->prefix);
1404 if (prefix_length < (string.end - string.start)
1405 && strncmp((char *)tag_directive->prefix, (char *)string.start,
1406 prefix_length) == 0)
1408 emitter->tag_data.handle = tag_directive->handle;
1409 emitter->tag_data.handle_length =
1410 strlen((char *)tag_directive->handle);
1411 emitter->tag_data.suffix = string.start + prefix_length;
1412 emitter->tag_data.suffix_length =
1413 (string.end - string.start) - prefix_length;
1418 emitter->tag_data.suffix = string.start;
1419 emitter->tag_data.suffix_length = string.end - string.start;
1425 * Check if a scalar is valid.
1429 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
1430 yaml_char_t *value, size_t length)
1432 yaml_string_t string = STRING(value, length);
1434 int block_indicators = 0;
1435 int flow_indicators = 0;
1436 int line_breaks = 0;
1437 int special_characters = 0;
1439 int inline_spaces = 0;
1440 int inline_breaks = 0;
1441 int leading_spaces = 0;
1442 int leading_breaks = 0;
1443 int trailing_spaces = 0;
1444 int trailing_breaks = 0;
1445 int inline_breaks_spaces = 0;
1446 int mixed_breaks_spaces = 0;
1448 int preceeded_by_space = 0;
1449 int followed_by_space = 0;
1455 emitter->scalar_data.value = value;
1456 emitter->scalar_data.length = length;
1458 if (string.start == string.end)
1460 emitter->scalar_data.multiline = 0;
1461 emitter->scalar_data.flow_plain_allowed = 0;
1462 emitter->scalar_data.block_plain_allowed = 1;
1463 emitter->scalar_data.single_quoted_allowed = 1;
1464 emitter->scalar_data.block_allowed = 0;
1469 if ((CHECK_AT(string, '-', 0)
1470 && CHECK_AT(string, '-', 1)
1471 && CHECK_AT(string, '-', 2))
1472 || (CHECK_AT(string, '.', 0)
1473 && CHECK_AT(string, '.', 1)
1474 && CHECK_AT(string, '.', 2))) {
1475 block_indicators = 1;
1476 flow_indicators = 1;
1479 preceeded_by_space = 1;
1480 followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
1482 while (string.pointer != string.end)
1484 if (string.start == string.pointer)
1486 if (CHECK(string, '#') || CHECK(string, ',')
1487 || CHECK(string, '[') || CHECK(string, ']')
1488 || CHECK(string, '{') || CHECK(string, '}')
1489 || CHECK(string, '&') || CHECK(string, '*')
1490 || CHECK(string, '!') || CHECK(string, '|')
1491 || CHECK(string, '>') || CHECK(string, '\'')
1492 || CHECK(string, '"') || CHECK(string, '%')
1493 || CHECK(string, '@') || CHECK(string, '`')) {
1494 flow_indicators = 1;
1495 block_indicators = 1;
1498 if (CHECK(string, '?') || CHECK(string, ':')) {
1499 flow_indicators = 1;
1500 if (followed_by_space) {
1501 block_indicators = 1;
1505 if (CHECK(string, '-') && followed_by_space) {
1506 flow_indicators = 1;
1507 block_indicators = 1;
1512 if (CHECK(string, ',') || CHECK(string, '?')
1513 || CHECK(string, '[') || CHECK(string, ']')
1514 || CHECK(string, '{') || CHECK(string, '}')) {
1515 flow_indicators = 1;
1518 if (CHECK(string, ':')) {
1519 flow_indicators = 1;
1520 if (followed_by_space) {
1521 block_indicators = 1;
1525 if (CHECK(string, '#') && preceeded_by_space) {
1526 flow_indicators = 1;
1527 block_indicators = 1;
1531 if (!IS_PRINTABLE(string)
1532 || (!IS_ASCII(string) && !emitter->unicode)) {
1533 special_characters = 1;
1536 if (IS_BREAK(string)) {
1540 if (IS_SPACE(string))
1543 if (string.start == string.pointer) {
1548 else if (IS_BREAK(string))
1554 if (string.start == string.pointer) {
1559 else if (spaces || breaks)
1562 if (spaces && breaks) {
1563 mixed_breaks_spaces = 1;
1574 mixed_breaks_spaces = 1;
1576 else if (spaces && breaks) {
1577 inline_breaks_spaces = 1;
1586 spaces = breaks = mixed = leading = 0;
1589 preceeded_by_space = IS_BLANKZ(string);
1591 if (string.pointer != string.end) {
1592 followed_by_space = IS_BLANKZ_AT(string, WIDTH(string));
1596 emitter->scalar_data.multiline = line_breaks;
1598 emitter->scalar_data.flow_plain_allowed = 1;
1599 emitter->scalar_data.block_plain_allowed = 1;
1600 emitter->scalar_data.single_quoted_allowed = 1;
1601 emitter->scalar_data.block_allowed = 1;
1603 if (leading_spaces || leading_breaks || trailing_spaces) {
1604 emitter->scalar_data.flow_plain_allowed = 0;
1605 emitter->scalar_data.block_plain_allowed = 0;
1606 emitter->scalar_data.block_allowed = 0;
1609 if (trailing_breaks) {
1610 emitter->scalar_data.flow_plain_allowed = 0;
1611 emitter->scalar_data.block_plain_allowed = 0;
1614 if (inline_breaks_spaces) {
1615 emitter->scalar_data.flow_plain_allowed = 0;
1616 emitter->scalar_data.block_plain_allowed = 0;
1617 emitter->scalar_data.single_quoted_allowed = 0;
1620 if (mixed_breaks_spaces || special_characters) {
1621 emitter->scalar_data.flow_plain_allowed = 0;
1622 emitter->scalar_data.block_plain_allowed = 0;
1623 emitter->scalar_data.single_quoted_allowed = 0;
1624 emitter->scalar_data.block_allowed = 0;
1628 emitter->scalar_data.flow_plain_allowed = 0;
1629 emitter->scalar_data.block_plain_allowed = 0;
1632 if (flow_indicators) {
1633 emitter->scalar_data.flow_plain_allowed = 0;
1636 if (block_indicators) {
1637 emitter->scalar_data.block_plain_allowed = 0;
1644 * Check if the event data is valid.
1648 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
1649 yaml_event_t *event)
1651 emitter->anchor_data.anchor = NULL;
1652 emitter->anchor_data.anchor_length = 0;
1653 emitter->tag_data.handle = NULL;
1654 emitter->tag_data.handle_length = 0;
1655 emitter->tag_data.suffix = NULL;
1656 emitter->tag_data.suffix_length = 0;
1657 emitter->scalar_data.value = NULL;
1658 emitter->scalar_data.length = 0;
1660 switch (event->type)
1662 case YAML_ALIAS_EVENT:
1663 if (!yaml_emitter_analyze_anchor(emitter,
1664 event->data.alias.anchor, 1))
1668 case YAML_SCALAR_EVENT:
1669 if (event->data.scalar.anchor) {
1670 if (!yaml_emitter_analyze_anchor(emitter,
1671 event->data.scalar.anchor, 0))
1674 if (event->data.scalar.tag && (emitter->canonical ||
1675 (!event->data.scalar.plain_implicit
1676 && !event->data.scalar.quoted_implicit))) {
1677 if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
1680 if (!yaml_emitter_analyze_scalar(emitter,
1681 event->data.scalar.value, event->data.scalar.length))
1685 case YAML_SEQUENCE_START_EVENT:
1686 if (event->data.sequence_start.anchor) {
1687 if (!yaml_emitter_analyze_anchor(emitter,
1688 event->data.sequence_start.anchor, 0))
1691 if (event->data.sequence_start.tag && (emitter->canonical ||
1692 !event->data.sequence_start.implicit)) {
1693 if (!yaml_emitter_analyze_tag(emitter,
1694 event->data.sequence_start.tag))
1699 case YAML_MAPPING_START_EVENT:
1700 if (event->data.mapping_start.anchor) {
1701 if (!yaml_emitter_analyze_anchor(emitter,
1702 event->data.mapping_start.anchor, 0))
1705 if (event->data.mapping_start.tag && (emitter->canonical ||
1706 !event->data.mapping_start.implicit)) {
1707 if (!yaml_emitter_analyze_tag(emitter,
1708 event->data.mapping_start.tag))
1719 * Write the BOM character.
1723 yaml_emitter_write_bom(yaml_emitter_t *emitter)
1725 if (!FLUSH(emitter)) return 0;
1727 *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
1728 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
1729 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
1735 yaml_emitter_write_indent(yaml_emitter_t *emitter)
1737 int indent = (emitter->indent >= 0) ? emitter->indent : 0;
1739 if (!emitter->indention || emitter->column > indent
1740 || (emitter->column == indent && !emitter->whitespace)) {
1741 if (!PUT_BREAK(emitter)) return 0;
1744 while (emitter->column < indent) {
1745 if (!PUT(emitter, ' ')) return 0;
1748 emitter->whitespace = 1;
1749 emitter->indention = 1;
1755 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
1756 char *indicator, int need_whitespace,
1757 int is_whitespace, int is_indention)
1759 yaml_string_t string = STRING((yaml_char_t *)indicator, strlen(indicator));
1761 if (need_whitespace && !emitter->whitespace) {
1762 if (!PUT(emitter, ' ')) return 0;
1765 while (string.pointer != string.end) {
1766 if (!WRITE(emitter, string)) return 0;
1769 emitter->whitespace = is_whitespace;
1770 emitter->indention = (emitter->indention && is_indention);
1776 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
1777 yaml_char_t *value, size_t length)
1779 yaml_string_t string = STRING(value, length);
1781 while (string.pointer != string.end) {
1782 if (!WRITE(emitter, string)) return 0;
1785 emitter->whitespace = 0;
1786 emitter->indention = 0;
1792 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
1793 yaml_char_t *value, size_t length)
1795 yaml_string_t string = STRING(value, length);
1797 if (!emitter->whitespace) {
1798 if (!PUT(emitter, ' ')) return 0;
1801 while (string.pointer != string.end) {
1802 if (!WRITE(emitter, string)) return 0;
1805 emitter->whitespace = 0;
1806 emitter->indention = 0;
1812 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
1813 yaml_char_t *value, size_t length,
1814 int need_whitespace)
1816 yaml_string_t string = STRING(value, length);
1818 if (need_whitespace && !emitter->whitespace) {
1819 if (!PUT(emitter, ' ')) return 0;
1822 while (string.pointer != string.end) {
1823 if (IS_ALPHA(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, '~') || CHECK(string, '*')
1831 || CHECK(string, '\'') || CHECK(string, '(')
1832 || CHECK(string, ')') || CHECK(string, '[')
1833 || CHECK(string, ']')) {
1834 if (!WRITE(emitter, string)) return 0;
1837 int width = WIDTH(string);
1840 value = *(string.pointer++);
1841 if (!PUT(emitter, '%')) return 0;
1842 if (!PUT(emitter, (value >> 8)
1843 + ((value >> 8) < 10 ? '0' : 'A' - 10)))
1845 if (!PUT(emitter, (value & 0x0F)
1846 + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
1852 emitter->whitespace = 0;
1853 emitter->indention = 0;
1859 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
1860 yaml_char_t *value, size_t length, int allow_breaks)
1862 yaml_string_t string = STRING(value, length);
1866 if (!emitter->whitespace) {
1867 if (!PUT(emitter, ' ')) return 0;
1870 while (string.pointer != string.end)
1872 if (IS_SPACE(string))
1874 if (allow_breaks && !spaces
1875 && emitter->column > emitter->best_width
1876 && !IS_SPACE_AT(string, 1)) {
1877 if (!yaml_emitter_write_indent(emitter)) return 0;
1881 if (!WRITE(emitter, string)) return 0;
1885 else if (IS_BREAK(string))
1887 if (!breaks && CHECK(string, '\n')) {
1888 if (!PUT_BREAK(emitter)) return 0;
1890 if (!WRITE_BREAK(emitter, string)) return 0;
1891 emitter->indention = 1;
1897 if (!yaml_emitter_write_indent(emitter)) return 0;
1899 if (!WRITE(emitter, string)) return 0;
1900 emitter->indention = 0;
1906 emitter->whitespace = 0;
1907 emitter->indention = 0;
1913 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
1914 yaml_char_t *value, size_t length, int allow_breaks)
1916 yaml_string_t string = STRING(value, length);
1920 if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
1923 while (string.pointer != string.end)
1925 if (IS_SPACE(string))
1927 if (allow_breaks && !spaces
1928 && emitter->column > emitter->best_width
1929 && string.pointer != string.start
1930 && string.pointer != string.end - 1
1931 && !IS_SPACE_AT(string, 1)) {
1932 if (!yaml_emitter_write_indent(emitter)) return 0;
1936 if (!WRITE(emitter, string)) return 0;
1940 else if (IS_BREAK(string))
1942 if (!breaks && CHECK(string, '\n')) {
1943 if (!PUT_BREAK(emitter)) return 0;
1945 if (!WRITE_BREAK(emitter, string)) return 0;
1946 emitter->indention = 1;
1952 if (!yaml_emitter_write_indent(emitter)) return 0;
1954 if (CHECK(string, '\'')) {
1955 if (!PUT(emitter, '\'')) return 0;
1957 if (!WRITE(emitter, string)) return 0;
1958 emitter->indention = 0;
1964 if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
1967 emitter->whitespace = 0;
1968 emitter->indention = 0;
1974 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
1975 yaml_char_t *value, size_t length, int allow_breaks)
1977 yaml_string_t string = STRING(value, length);
1980 if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
1983 while (string.pointer != string.end)
1985 if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
1986 || IS_BOM(string) || IS_BREAK(string)
1987 || CHECK(string, '"') || CHECK(string, '\\'))
1989 unsigned char octet;
1994 octet = string.pointer[0];
1995 width = (octet & 0x80) == 0x00 ? 1 :
1996 (octet & 0xE0) == 0xC0 ? 2 :
1997 (octet & 0xF0) == 0xE0 ? 3 :
1998 (octet & 0xF8) == 0xF0 ? 4 : 0;
1999 value = (octet & 0x80) == 0x00 ? octet & 0x7F :
2000 (octet & 0xE0) == 0xC0 ? octet & 0x1F :
2001 (octet & 0xF0) == 0xE0 ? octet & 0x0F :
2002 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
2003 for (k = 1; k < width; k ++) {
2004 octet = string.pointer[k];
2005 value = (value << 6) + (octet & 0x3F);
2007 string.pointer += width;
2009 if (!PUT(emitter, '\\')) return 0;
2014 if (!PUT(emitter, '0')) return 0;
2018 if (!PUT(emitter, 'a')) return 0;
2022 if (!PUT(emitter, 'b')) return 0;
2026 if (!PUT(emitter, 't')) return 0;
2030 if (!PUT(emitter, 'n')) return 0;
2034 if (!PUT(emitter, 'v')) return 0;
2038 if (!PUT(emitter, 'f')) return 0;
2042 if (!PUT(emitter, 'r')) return 0;
2046 if (!PUT(emitter, 'e')) return 0;
2050 if (!PUT(emitter, '\"')) return 0;
2054 if (!PUT(emitter, '\\')) return 0;
2058 if (!PUT(emitter, 'N')) return 0;
2062 if (!PUT(emitter, '_')) return 0;
2066 if (!PUT(emitter, 'L')) return 0;
2070 if (!PUT(emitter, 'P')) return 0;
2074 if (value <= 0xFF) {
2075 if (!PUT(emitter, 'x')) return 0;
2078 else if (value <= 0xFFFF) {
2079 if (!PUT(emitter, 'u')) return 0;
2083 if (!PUT(emitter, 'U')) return 0;
2086 for (k = width*4; k >= 0; k -= 4) {
2087 if (!PUT(emitter, (value >> k) & 0x0F)) return 0;
2092 else if (IS_SPACE(string))
2094 if (allow_breaks && !spaces
2095 && emitter->column > emitter->best_width
2096 && string.pointer != string.start
2097 && string.pointer != string.end - 1) {
2098 if (!yaml_emitter_write_indent(emitter)) return 0;
2099 if (IS_SPACE_AT(string, 1)) {
2100 if (!PUT(emitter, '\\')) return 0;
2105 if (!WRITE(emitter, string)) return 0;
2111 if (!WRITE(emitter, string)) return 0;
2116 if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
2119 emitter->whitespace = 0;
2120 emitter->indention = 0;
2126 yaml_emitter_determine_chomping(yaml_emitter_t *emitter,
2127 yaml_string_t string)
2129 string.pointer = string.end;
2130 if (string.start == string.pointer)
2132 while ((string.pointer[-1] & 0xC0) == 0x80) {
2135 if (!IS_BREAK(string))
2137 if (string.start == string.pointer)
2139 while ((string.pointer[-1] & 0xC0) == 0x80) {
2142 if (!IS_BREAK(string))
2149 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
2150 yaml_char_t *value, size_t length)
2152 yaml_string_t string = STRING(value, length);
2153 int chomp = yaml_emitter_determine_chomping(emitter, string);
2156 if (!yaml_emitter_write_indicator(emitter,
2157 chomp == -1 ? "|-" : chomp == +1 ? "|+" : "|", 1, 0, 0))
2159 if (!yaml_emitter_write_indent(emitter))
2162 while (string.pointer != string.end)
2164 if (IS_BREAK(string))
2166 if (!WRITE_BREAK(emitter, string)) return 0;
2167 emitter->indention = 1;
2173 if (!yaml_emitter_write_indent(emitter)) return 0;
2175 if (!WRITE(emitter, string)) return 0;
2176 emitter->indention = 0;
2181 if (!yaml_emitter_write_indent(emitter)) return 0;
2187 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
2188 yaml_char_t *value, size_t length)
2190 yaml_string_t string = STRING(value, length);
2191 int chomp = yaml_emitter_determine_chomping(emitter, string);
2193 int leading_spaces = 1;
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 (!breaks && !leading_spaces && CHECK(string, '\n')) {
2207 while (IS_BREAK_AT(string, k)) {
2208 k += WIDTH_AT(string, k);
2210 if (!IS_BLANK_AT(string, k)) {
2211 if (!PUT_BREAK(emitter)) return 0;
2214 if (!WRITE_BREAK(emitter, string)) return 0;
2215 emitter->indention = 1;
2221 if (!yaml_emitter_write_indent(emitter)) return 0;
2222 leading_spaces = IS_BLANK(string);
2224 if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
2225 && emitter->column > emitter->best_width) {
2226 if (!yaml_emitter_write_indent(emitter)) return 0;
2230 if (!WRITE(emitter, string)) return 0;
2232 emitter->indention = 0;
2237 if (!yaml_emitter_write_indent(emitter)) return 0;