2 #include "yaml_private.h"
5 * Flush the buffer if needed.
8 #define FLUSH(emitter) \
9 ((emitter->buffer.pointer+5 < emitter->buffer.end) \
10 || yaml_emitter_flush(emitter))
13 * Put a character to the output buffer.
16 #define PUT(emitter,value) \
18 && (*(emitter->buffer.pointer++) = (yaml_char_t)(value), \
23 * Put a line break to the output buffer.
26 #define PUT_BREAK(emitter) \
28 && ((emitter->line_break == YAML_CR_BREAK ? \
29 (*(emitter->buffer.pointer++) = (yaml_char_t) '\r') : \
30 emitter->line_break == YAML_LN_BREAK ? \
31 (*(emitter->buffer.pointer++) = (yaml_char_t) '\n') : \
32 emitter->line_break == YAML_CRLN_BREAK ? \
33 (*(emitter->buffer.pointer++) = (yaml_char_t) '\r', \
34 *(emitter->buffer.pointer++) = (yaml_char_t) '\n') : 0), \
35 emitter->column = 0, \
40 * Copy a character from a string into buffer.
43 #define WRITE(emitter,string) \
45 && (COPY(emitter->buffer,string), \
50 * Copy a line break character from a string into buffer.
53 #define WRITE_BREAK(emitter,string) \
55 && (CHECK(string,'\n') ? \
56 (PUT_BREAK(emitter), \
59 (COPY(emitter->buffer,string), \
60 emitter->column = 0, \
69 yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event);
76 yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem);
79 yaml_emitter_need_more_events(yaml_emitter_t *emitter);
82 yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
83 yaml_tag_directive_t value, int allow_duplicates);
86 yaml_emitter_increase_indent(yaml_emitter_t *emitter,
87 int flow, int indentless);
94 yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event);
97 yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
101 yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
102 yaml_event_t *event, int first);
105 yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
106 yaml_event_t *event);
109 yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
110 yaml_event_t *event);
113 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
114 yaml_event_t *event, int first);
117 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
118 yaml_event_t *event, int first);
121 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
122 yaml_event_t *event, int simple);
125 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
126 yaml_event_t *event, int first);
129 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
130 yaml_event_t *event, int first);
133 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
134 yaml_event_t *event, int simple);
137 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
138 int root, int sequence, int mapping, int simple_key);
141 yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event);
144 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event);
147 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event);
150 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event);
157 yaml_emitter_check_empty_document(yaml_emitter_t *emitter);
160 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter);
163 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter);
166 yaml_emitter_check_simple_key(yaml_emitter_t *emitter);
169 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event);
176 yaml_emitter_process_anchor(yaml_emitter_t *emitter);
179 yaml_emitter_process_tag(yaml_emitter_t *emitter);
182 yaml_emitter_process_scalar(yaml_emitter_t *emitter);
189 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
190 yaml_version_directive_t version_directive);
193 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
194 yaml_tag_directive_t tag_directive);
197 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
198 yaml_char_t *anchor, int alias);
201 yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
205 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
206 yaml_char_t *value, size_t length);
209 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
210 yaml_event_t *event);
217 yaml_emitter_write_bom(yaml_emitter_t *emitter);
220 yaml_emitter_write_indent(yaml_emitter_t *emitter);
223 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
224 char *indicator, int need_whitespace,
225 int is_whitespace, int is_indention);
228 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
229 yaml_char_t *value, size_t length);
232 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
233 yaml_char_t *value, size_t length);
236 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
237 yaml_char_t *value, size_t length, int need_whitespace);
240 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
241 yaml_char_t *value, size_t length, int allow_breaks);
244 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
245 yaml_char_t *value, size_t length, int allow_breaks);
248 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
249 yaml_char_t *value, size_t length, int allow_breaks);
252 yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
253 yaml_string_t string);
256 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
257 yaml_char_t *value, size_t length);
260 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
261 yaml_char_t *value, size_t length);
264 * Set an emitter error and return 0.
268 yaml_emitter_set_emitter_error(yaml_emitter_t *emitter, const char *problem)
270 emitter->error = YAML_EMITTER_ERROR;
271 emitter->problem = problem;
281 yaml_emitter_emit(yaml_emitter_t *emitter, yaml_event_t *event)
283 if (!ENQUEUE(emitter, emitter->events, *event)) {
284 yaml_event_delete(event);
288 while (!yaml_emitter_need_more_events(emitter)) {
289 if (!yaml_emitter_analyze_event(emitter, emitter->events.head))
291 if (!yaml_emitter_state_machine(emitter, emitter->events.head))
293 yaml_event_delete(&DEQUEUE(emitter, emitter->events));
300 * Check if we need to accumulate more events before emitting.
302 * We accumulate extra
303 * - 1 event for DOCUMENT-START
304 * - 2 events for SEQUENCE-START
305 * - 3 events for MAPPING-START
309 yaml_emitter_need_more_events(yaml_emitter_t *emitter)
315 if (QUEUE_EMPTY(emitter, emitter->events))
318 switch (emitter->events.head->type) {
319 case YAML_DOCUMENT_START_EVENT:
322 case YAML_SEQUENCE_START_EVENT:
325 case YAML_MAPPING_START_EVENT:
332 if (emitter->events.tail - emitter->events.head > accumulate)
335 for (event = emitter->events.head; event != emitter->events.tail; event ++) {
336 switch (event->type) {
337 case YAML_STREAM_START_EVENT:
338 case YAML_DOCUMENT_START_EVENT:
339 case YAML_SEQUENCE_START_EVENT:
340 case YAML_MAPPING_START_EVENT:
343 case YAML_STREAM_END_EVENT:
344 case YAML_DOCUMENT_END_EVENT:
345 case YAML_SEQUENCE_END_EVENT:
346 case YAML_MAPPING_END_EVENT:
360 * Append a directive to the directives stack.
364 yaml_emitter_append_tag_directive(yaml_emitter_t *emitter,
365 yaml_tag_directive_t value, int allow_duplicates)
367 yaml_tag_directive_t *tag_directive;
368 yaml_tag_directive_t copy = { NULL, NULL };
370 for (tag_directive = emitter->tag_directives.start;
371 tag_directive != emitter->tag_directives.top; tag_directive ++) {
372 if (strcmp((char *)value.handle, (char *)tag_directive->handle) == 0) {
373 if (allow_duplicates)
375 return yaml_emitter_set_emitter_error(emitter,
376 "duplicate %TAG directive");
380 copy.handle = yaml_strdup(value.handle);
381 copy.prefix = yaml_strdup(value.prefix);
382 if (!copy.handle || !copy.prefix) {
383 emitter->error = YAML_MEMORY_ERROR;
387 if (!PUSH(emitter, emitter->tag_directives, copy))
393 yaml_free(copy.handle);
394 yaml_free(copy.prefix);
399 * Increase the indentation level.
403 yaml_emitter_increase_indent(yaml_emitter_t *emitter,
404 int flow, int indentless)
406 if (!PUSH(emitter, emitter->indents, emitter->indent))
409 if (emitter->indent < 0) {
410 emitter->indent = flow ? emitter->best_indent : 0;
412 else if (!indentless) {
413 emitter->indent += emitter->best_indent;
424 yaml_emitter_state_machine(yaml_emitter_t *emitter, yaml_event_t *event)
426 switch (emitter->state)
428 case YAML_EMIT_STREAM_START_STATE:
429 return yaml_emitter_emit_stream_start(emitter, event);
431 case YAML_EMIT_FIRST_DOCUMENT_START_STATE:
432 return yaml_emitter_emit_document_start(emitter, event, 1);
434 case YAML_EMIT_DOCUMENT_START_STATE:
435 return yaml_emitter_emit_document_start(emitter, event, 0);
437 case YAML_EMIT_DOCUMENT_CONTENT_STATE:
438 return yaml_emitter_emit_document_content(emitter, event);
440 case YAML_EMIT_DOCUMENT_END_STATE:
441 return yaml_emitter_emit_document_end(emitter, event);
443 case YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE:
444 return yaml_emitter_emit_flow_sequence_item(emitter, event, 1);
446 case YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE:
447 return yaml_emitter_emit_flow_sequence_item(emitter, event, 0);
449 case YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE:
450 return yaml_emitter_emit_flow_mapping_key(emitter, event, 1);
452 case YAML_EMIT_FLOW_MAPPING_KEY_STATE:
453 return yaml_emitter_emit_flow_mapping_key(emitter, event, 0);
455 case YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE:
456 return yaml_emitter_emit_flow_mapping_value(emitter, event, 1);
458 case YAML_EMIT_FLOW_MAPPING_VALUE_STATE:
459 return yaml_emitter_emit_flow_mapping_value(emitter, event, 0);
461 case YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE:
462 return yaml_emitter_emit_block_sequence_item(emitter, event, 1);
464 case YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE:
465 return yaml_emitter_emit_block_sequence_item(emitter, event, 0);
467 case YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE:
468 return yaml_emitter_emit_block_mapping_key(emitter, event, 1);
470 case YAML_EMIT_BLOCK_MAPPING_KEY_STATE:
471 return yaml_emitter_emit_block_mapping_key(emitter, event, 0);
473 case YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE:
474 return yaml_emitter_emit_block_mapping_value(emitter, event, 1);
476 case YAML_EMIT_BLOCK_MAPPING_VALUE_STATE:
477 return yaml_emitter_emit_block_mapping_value(emitter, event, 0);
479 case YAML_EMIT_END_STATE:
480 return yaml_emitter_set_emitter_error(emitter,
481 "expected nothing after STREAM-END");
484 assert(1); /* Invalid state. */
491 * Expect STREAM-START.
495 yaml_emitter_emit_stream_start(yaml_emitter_t *emitter,
498 if (event->type == YAML_STREAM_START_EVENT)
500 if (!emitter->encoding) {
501 emitter->encoding = event->data.stream_start.encoding;
504 if (!emitter->encoding) {
505 emitter->encoding = YAML_UTF8_ENCODING;
508 if (emitter->best_indent < 2 || emitter->best_indent > 9) {
509 emitter->best_indent = 2;
512 if (emitter->best_width >= 0
513 && emitter->best_width <= emitter->best_indent*2) {
514 emitter->best_width = 80;
517 if (emitter->best_width < 0) {
518 emitter->best_width = INT_MAX;
521 if (!emitter->line_break) {
522 emitter->line_break = YAML_LN_BREAK;
525 emitter->indent = -1;
529 emitter->whitespace = 1;
530 emitter->indention = 1;
532 if (emitter->encoding != YAML_UTF8_ENCODING) {
533 if (!yaml_emitter_write_bom(emitter))
537 emitter->state = YAML_EMIT_FIRST_DOCUMENT_START_STATE;
542 return yaml_emitter_set_emitter_error(emitter,
543 "expected STREAM-START");
547 * Expect DOCUMENT-START or STREAM-END.
551 yaml_emitter_emit_document_start(yaml_emitter_t *emitter,
552 yaml_event_t *event, int first)
554 if (event->type == YAML_DOCUMENT_START_EVENT)
556 yaml_tag_directive_t default_tag_directives[] = {
557 {(yaml_char_t *)"!", (yaml_char_t *)"!"},
558 {(yaml_char_t *)"!!", (yaml_char_t *)"tag:yaml.org,2002:"},
561 yaml_tag_directive_t *tag_directive;
564 if (event->data.document_start.version_directive) {
565 if (!yaml_emitter_analyze_version_directive(emitter,
566 *event->data.document_start.version_directive))
570 for (tag_directive = event->data.document_start.tag_directives.start;
571 tag_directive != event->data.document_start.tag_directives.end;
573 if (!yaml_emitter_analyze_tag_directive(emitter, *tag_directive))
575 if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 0))
579 for (tag_directive = default_tag_directives;
580 tag_directive->handle; tag_directive ++) {
581 if (!yaml_emitter_append_tag_directive(emitter, *tag_directive, 1))
585 implicit = event->data.document_start.implicit;
586 if (!first || emitter->canonical) {
590 if ((event->data.document_start.version_directive ||
591 (event->data.document_start.tag_directives.start
592 != event->data.document_start.tag_directives.end)) &&
595 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
597 if (!yaml_emitter_write_indent(emitter))
601 if (event->data.document_start.version_directive) {
603 if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0))
605 if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0))
607 if (!yaml_emitter_write_indent(emitter))
611 if (event->data.document_start.tag_directives.start
612 != event->data.document_start.tag_directives.end) {
614 for (tag_directive = event->data.document_start.tag_directives.start;
615 tag_directive != event->data.document_start.tag_directives.end;
617 if (!yaml_emitter_write_indicator(emitter, "%TAG", 1, 0, 0))
619 if (!yaml_emitter_write_tag_handle(emitter, tag_directive->handle,
620 strlen((char *)tag_directive->handle)))
622 if (!yaml_emitter_write_tag_content(emitter, tag_directive->prefix,
623 strlen((char *)tag_directive->prefix), 1))
625 if (!yaml_emitter_write_indent(emitter))
630 if (yaml_emitter_check_empty_document(emitter)) {
635 if (!yaml_emitter_write_indent(emitter))
637 if (!yaml_emitter_write_indicator(emitter, "---", 1, 0, 0))
639 if (emitter->canonical) {
640 if (!yaml_emitter_write_indent(emitter))
645 emitter->state = YAML_EMIT_DOCUMENT_CONTENT_STATE;
650 else if (event->type == YAML_STREAM_END_EVENT)
652 if (emitter->open_ended)
654 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
656 if (!yaml_emitter_write_indent(emitter))
660 if (!yaml_emitter_flush(emitter))
663 emitter->state = YAML_EMIT_END_STATE;
668 return yaml_emitter_set_emitter_error(emitter,
669 "expected DOCUMENT-START or STREAM-END");
673 * Expect the root node.
677 yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
680 if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE))
683 return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0);
687 * Expect DOCUMENT-END.
691 yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
694 if (event->type == YAML_DOCUMENT_END_EVENT)
696 if (!yaml_emitter_write_indent(emitter))
698 if (!event->data.document_end.implicit) {
699 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
701 if (!yaml_emitter_write_indent(emitter))
704 if (!yaml_emitter_flush(emitter))
707 emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
709 while (!STACK_EMPTY(emitter, emitter->tag_directives)) {
710 yaml_tag_directive_t tag_directive = POP(emitter,
711 emitter->tag_directives);
712 yaml_free(tag_directive.handle);
713 yaml_free(tag_directive.prefix);
719 return yaml_emitter_set_emitter_error(emitter,
720 "expected DOCUMENT-END");
725 * Expect a flow item node.
729 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
730 yaml_event_t *event, int first)
734 if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0))
736 if (!yaml_emitter_increase_indent(emitter, 1, 0))
738 emitter->flow_level ++;
741 if (event->type == YAML_SEQUENCE_END_EVENT)
743 emitter->flow_level --;
744 emitter->indent = POP(emitter, emitter->indents);
745 if (emitter->canonical && !first) {
746 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
748 if (!yaml_emitter_write_indent(emitter))
751 if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0))
753 emitter->state = POP(emitter, emitter->states);
759 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
763 if (emitter->canonical || emitter->column > emitter->best_width) {
764 if (!yaml_emitter_write_indent(emitter))
767 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
770 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
774 * Expect a flow key node.
778 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
779 yaml_event_t *event, int first)
783 if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0))
785 if (!yaml_emitter_increase_indent(emitter, 1, 0))
787 emitter->flow_level ++;
790 if (event->type == YAML_MAPPING_END_EVENT)
792 emitter->flow_level --;
793 emitter->indent = POP(emitter, emitter->indents);
794 if (emitter->canonical && !first) {
795 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
797 if (!yaml_emitter_write_indent(emitter))
800 if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0))
802 emitter->state = POP(emitter, emitter->states);
808 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
811 if (emitter->canonical || emitter->column > emitter->best_width) {
812 if (!yaml_emitter_write_indent(emitter))
816 if (!emitter->canonical && yaml_emitter_check_simple_key(emitter))
818 if (!PUSH(emitter, emitter->states,
819 YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE))
822 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
826 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0))
828 if (!PUSH(emitter, emitter->states,
829 YAML_EMIT_FLOW_MAPPING_VALUE_STATE))
832 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
837 * Expect a flow value node.
841 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
842 yaml_event_t *event, int simple)
845 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
849 if (emitter->canonical || emitter->column > emitter->best_width) {
850 if (!yaml_emitter_write_indent(emitter))
853 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0))
856 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE))
858 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
862 * Expect a block item node.
866 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
867 yaml_event_t *event, int first)
871 if (!yaml_emitter_increase_indent(emitter, 0,
872 (emitter->mapping_context && !emitter->indention)))
876 if (event->type == YAML_SEQUENCE_END_EVENT)
878 emitter->indent = POP(emitter, emitter->indents);
879 emitter->state = POP(emitter, emitter->states);
884 if (!yaml_emitter_write_indent(emitter))
886 if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1))
888 if (!PUSH(emitter, emitter->states,
889 YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE))
892 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
896 * Expect a block key node.
900 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
901 yaml_event_t *event, int first)
905 if (!yaml_emitter_increase_indent(emitter, 0, 0))
909 if (event->type == YAML_MAPPING_END_EVENT)
911 emitter->indent = POP(emitter, emitter->indents);
912 emitter->state = POP(emitter, emitter->states);
917 if (!yaml_emitter_write_indent(emitter))
920 if (yaml_emitter_check_simple_key(emitter))
922 if (!PUSH(emitter, emitter->states,
923 YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE))
926 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
930 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1))
932 if (!PUSH(emitter, emitter->states,
933 YAML_EMIT_BLOCK_MAPPING_VALUE_STATE))
936 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
941 * Expect a block value node.
945 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
946 yaml_event_t *event, int simple)
949 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
953 if (!yaml_emitter_write_indent(emitter))
955 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1))
958 if (!PUSH(emitter, emitter->states,
959 YAML_EMIT_BLOCK_MAPPING_KEY_STATE))
962 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
970 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
971 int root, int sequence, int mapping, int simple_key)
973 emitter->root_context = root;
974 emitter->sequence_context = sequence;
975 emitter->mapping_context = mapping;
976 emitter->simple_key_context = simple_key;
980 case YAML_ALIAS_EVENT:
981 return yaml_emitter_emit_alias(emitter, event);
983 case YAML_SCALAR_EVENT:
984 return yaml_emitter_emit_scalar(emitter, event);
986 case YAML_SEQUENCE_START_EVENT:
987 return yaml_emitter_emit_sequence_start(emitter, event);
989 case YAML_MAPPING_START_EVENT:
990 return yaml_emitter_emit_mapping_start(emitter, event);
993 return yaml_emitter_set_emitter_error(emitter,
994 "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS");
1005 yaml_emitter_emit_alias(yaml_emitter_t *emitter, yaml_event_t *event)
1007 if (!yaml_emitter_process_anchor(emitter))
1009 emitter->state = POP(emitter, emitter->states);
1019 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event)
1021 if (!yaml_emitter_select_scalar_style(emitter, event))
1023 if (!yaml_emitter_process_anchor(emitter))
1025 if (!yaml_emitter_process_tag(emitter))
1027 if (!yaml_emitter_increase_indent(emitter, 1, 0))
1029 if (!yaml_emitter_process_scalar(emitter))
1031 emitter->indent = POP(emitter, emitter->indents);
1032 emitter->state = POP(emitter, emitter->states);
1038 * Expect SEQUENCE-START.
1042 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event)
1044 if (!yaml_emitter_process_anchor(emitter))
1046 if (!yaml_emitter_process_tag(emitter))
1049 if (emitter->flow_level || emitter->canonical
1050 || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE
1051 || yaml_emitter_check_empty_sequence(emitter)) {
1052 emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
1055 emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
1062 * Expect MAPPING-START.
1066 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event)
1068 if (!yaml_emitter_process_anchor(emitter))
1070 if (!yaml_emitter_process_tag(emitter))
1073 if (emitter->flow_level || emitter->canonical
1074 || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE
1075 || yaml_emitter_check_empty_mapping(emitter)) {
1076 emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
1079 emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
1086 * Check if the document content is an empty scalar.
1090 yaml_emitter_check_empty_document(yaml_emitter_t *emitter)
1096 * Check if the next events represent an empty sequence.
1100 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter)
1102 if (emitter->events.tail - emitter->events.head < 2)
1105 return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT
1106 && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT);
1110 * Check if the next events represent an empty mapping.
1114 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter)
1116 if (emitter->events.tail - emitter->events.head < 2)
1119 return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT
1120 && emitter->events.head[1].type == YAML_MAPPING_END_EVENT);
1124 * Check if the next node can be expressed as a simple key.
1128 yaml_emitter_check_simple_key(yaml_emitter_t *emitter)
1130 yaml_event_t *event = emitter->events.head;
1133 switch (event->type)
1135 case YAML_ALIAS_EVENT:
1136 length += emitter->anchor_data.anchor_length;
1139 case YAML_SCALAR_EVENT:
1140 if (emitter->scalar_data.multiline)
1142 length += emitter->anchor_data.anchor_length
1143 + emitter->tag_data.handle_length
1144 + emitter->tag_data.suffix_length
1145 + emitter->scalar_data.length;
1148 case YAML_SEQUENCE_START_EVENT:
1149 if (!yaml_emitter_check_empty_sequence(emitter))
1151 length += emitter->anchor_data.anchor_length
1152 + emitter->tag_data.handle_length
1153 + emitter->tag_data.suffix_length;
1156 case YAML_MAPPING_START_EVENT:
1157 if (!yaml_emitter_check_empty_mapping(emitter))
1159 length += emitter->anchor_data.anchor_length
1160 + emitter->tag_data.handle_length
1161 + emitter->tag_data.suffix_length;
1175 * Determine an acceptable scalar style.
1179 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
1181 yaml_scalar_style_t style = event->data.scalar.style;
1182 int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix);
1184 if (no_tag && !event->data.scalar.plain_implicit
1185 && !event->data.scalar.quoted_implicit) {
1186 return yaml_emitter_set_emitter_error(emitter,
1187 "neither tag nor implicit flags are specified");
1190 if (style == YAML_ANY_SCALAR_STYLE)
1191 style = YAML_PLAIN_SCALAR_STYLE;
1193 if (emitter->canonical)
1194 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1196 if (emitter->simple_key_context && emitter->scalar_data.multiline)
1197 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1199 if (style == YAML_PLAIN_SCALAR_STYLE)
1201 if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
1202 || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
1203 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1204 if (!emitter->scalar_data.length
1205 && (emitter->flow_level || emitter->simple_key_context))
1206 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1207 if (no_tag && !event->data.scalar.plain_implicit)
1208 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1211 if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
1213 if (!emitter->scalar_data.single_quoted_allowed)
1214 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1217 if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
1219 if (!emitter->scalar_data.block_allowed
1220 || emitter->flow_level || emitter->simple_key_context)
1221 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1224 if (no_tag && !event->data.scalar.quoted_implicit
1225 && style != YAML_PLAIN_SCALAR_STYLE)
1227 emitter->tag_data.handle = (yaml_char_t *)"!";
1228 emitter->tag_data.handle_length = 1;
1231 emitter->scalar_data.style = style;
1241 yaml_emitter_process_anchor(yaml_emitter_t *emitter)
1243 if (!emitter->anchor_data.anchor)
1246 if (!yaml_emitter_write_indicator(emitter,
1247 (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
1250 return yaml_emitter_write_anchor(emitter,
1251 emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
1259 yaml_emitter_process_tag(yaml_emitter_t *emitter)
1261 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1264 if (emitter->tag_data.handle)
1266 if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
1267 emitter->tag_data.handle_length))
1269 if (emitter->tag_data.suffix) {
1270 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1271 emitter->tag_data.suffix_length, 0))
1277 if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
1279 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1280 emitter->tag_data.suffix_length, 0))
1282 if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
1294 yaml_emitter_process_scalar(yaml_emitter_t *emitter)
1296 switch (emitter->scalar_data.style)
1298 case YAML_PLAIN_SCALAR_STYLE:
1299 return yaml_emitter_write_plain_scalar(emitter,
1300 emitter->scalar_data.value, emitter->scalar_data.length,
1301 !emitter->simple_key_context);
1303 case YAML_SINGLE_QUOTED_SCALAR_STYLE:
1304 return yaml_emitter_write_single_quoted_scalar(emitter,
1305 emitter->scalar_data.value, emitter->scalar_data.length,
1306 !emitter->simple_key_context);
1308 case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
1309 return yaml_emitter_write_double_quoted_scalar(emitter,
1310 emitter->scalar_data.value, emitter->scalar_data.length,
1311 !emitter->simple_key_context);
1313 case YAML_LITERAL_SCALAR_STYLE:
1314 return yaml_emitter_write_literal_scalar(emitter,
1315 emitter->scalar_data.value, emitter->scalar_data.length);
1317 case YAML_FOLDED_SCALAR_STYLE:
1318 return yaml_emitter_write_folded_scalar(emitter,
1319 emitter->scalar_data.value, emitter->scalar_data.length);
1322 assert(1); /* Impossible. */
1329 * Check if a %YAML directive is valid.
1333 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
1334 yaml_version_directive_t version_directive)
1336 if (version_directive.major != 1 || version_directive.minor != 1) {
1337 return yaml_emitter_set_emitter_error(emitter,
1338 "incompatible %YAML directive");
1345 * Check if a %TAG directive is valid.
1349 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
1350 yaml_tag_directive_t tag_directive)
1352 yaml_string_t handle;
1353 yaml_string_t prefix;
1354 size_t handle_length;
1355 size_t prefix_length;
1357 handle_length = strlen((char *)tag_directive.handle);
1358 prefix_length = strlen((char *)tag_directive.prefix);
1359 STRING_ASSIGN(handle, tag_directive.handle, handle_length);
1360 STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length);
1362 if (handle.start == handle.end) {
1363 return yaml_emitter_set_emitter_error(emitter,
1364 "tag handle must not be empty");
1367 if (handle.start[0] != '!') {
1368 return yaml_emitter_set_emitter_error(emitter,
1369 "tag handle must start with '!'");
1372 if (handle.end[-1] != '!') {
1373 return yaml_emitter_set_emitter_error(emitter,
1374 "tag handle must end with '!'");
1379 while (handle.pointer < handle.end-1) {
1380 if (!IS_ALPHA(handle)) {
1381 return yaml_emitter_set_emitter_error(emitter,
1382 "tag handle must contain alphanumerical characters only");
1387 if (prefix.start == prefix.end) {
1388 return yaml_emitter_set_emitter_error(emitter,
1389 "tag prefix must not be empty");
1396 * Check if an anchor is valid.
1400 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
1401 yaml_char_t *anchor, int alias)
1403 size_t anchor_length;
1404 yaml_string_t string;
1406 anchor_length = strlen((char *)anchor);
1407 STRING_ASSIGN(string, anchor, anchor_length);
1409 if (string.start == string.end) {
1410 return yaml_emitter_set_emitter_error(emitter, alias ?
1411 "alias value must not be empty" :
1412 "anchor value must not be empty");
1415 while (string.pointer != string.end) {
1416 if (!IS_ALPHA(string)) {
1417 return yaml_emitter_set_emitter_error(emitter, alias ?
1418 "alias value must contain alphanumerical characters only" :
1419 "anchor value must contain alphanumerical characters only");
1424 emitter->anchor_data.anchor = string.start;
1425 emitter->anchor_data.anchor_length = string.end - string.start;
1426 emitter->anchor_data.alias = alias;
1432 * Check if a tag is valid.
1436 yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
1440 yaml_string_t string;
1441 yaml_tag_directive_t *tag_directive;
1443 tag_length = strlen((char *)tag);
1444 STRING_ASSIGN(string, tag, tag_length);
1446 if (string.start == string.end) {
1447 return yaml_emitter_set_emitter_error(emitter,
1448 "tag value must not be empty");
1451 for (tag_directive = emitter->tag_directives.start;
1452 tag_directive != emitter->tag_directives.top; tag_directive ++) {
1453 size_t prefix_length = strlen((char *)tag_directive->prefix);
1454 if (prefix_length < (size_t)(string.end - string.start)
1455 && strncmp((char *)tag_directive->prefix, (char *)string.start,
1456 prefix_length) == 0)
1458 emitter->tag_data.handle = tag_directive->handle;
1459 emitter->tag_data.handle_length =
1460 strlen((char *)tag_directive->handle);
1461 emitter->tag_data.suffix = string.start + prefix_length;
1462 emitter->tag_data.suffix_length =
1463 (string.end - string.start) - prefix_length;
1468 emitter->tag_data.suffix = string.start;
1469 emitter->tag_data.suffix_length = string.end - string.start;
1475 * Check if a scalar is valid.
1479 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
1480 yaml_char_t *value, size_t length)
1482 yaml_string_t string;
1484 int block_indicators = 0;
1485 int flow_indicators = 0;
1486 int line_breaks = 0;
1487 int special_characters = 0;
1489 int leading_space = 0;
1490 int leading_break = 0;
1491 int trailing_space = 0;
1492 int trailing_break = 0;
1493 int break_space = 0;
1494 int space_break = 0;
1496 int preceded_by_whitespace = 0;
1497 int followed_by_whitespace = 0;
1498 int previous_space = 0;
1499 int previous_break = 0;
1501 STRING_ASSIGN(string, value, length);
1503 emitter->scalar_data.value = value;
1504 emitter->scalar_data.length = length;
1506 if (string.start == string.end)
1508 emitter->scalar_data.multiline = 0;
1509 emitter->scalar_data.flow_plain_allowed = 0;
1510 emitter->scalar_data.block_plain_allowed = 1;
1511 emitter->scalar_data.single_quoted_allowed = 1;
1512 emitter->scalar_data.block_allowed = 0;
1517 if ((CHECK_AT(string, '-', 0)
1518 && CHECK_AT(string, '-', 1)
1519 && CHECK_AT(string, '-', 2))
1520 || (CHECK_AT(string, '.', 0)
1521 && CHECK_AT(string, '.', 1)
1522 && CHECK_AT(string, '.', 2))) {
1523 block_indicators = 1;
1524 flow_indicators = 1;
1527 preceded_by_whitespace = 1;
1528 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
1530 while (string.pointer != string.end)
1532 if (string.start == string.pointer)
1534 if (CHECK(string, '#') || CHECK(string, ',')
1535 || CHECK(string, '[') || CHECK(string, ']')
1536 || CHECK(string, '{') || CHECK(string, '}')
1537 || CHECK(string, '&') || CHECK(string, '*')
1538 || CHECK(string, '!') || CHECK(string, '|')
1539 || CHECK(string, '>') || CHECK(string, '\'')
1540 || CHECK(string, '"') || CHECK(string, '%')
1541 || CHECK(string, '@') || CHECK(string, '`')) {
1542 flow_indicators = 1;
1543 block_indicators = 1;
1546 if (CHECK(string, '?') || CHECK(string, ':')) {
1547 flow_indicators = 1;
1548 if (followed_by_whitespace) {
1549 block_indicators = 1;
1553 if (CHECK(string, '-') && followed_by_whitespace) {
1554 flow_indicators = 1;
1555 block_indicators = 1;
1560 if (CHECK(string, ',') || CHECK(string, '?')
1561 || CHECK(string, '[') || CHECK(string, ']')
1562 || CHECK(string, '{') || CHECK(string, '}')) {
1563 flow_indicators = 1;
1566 if (CHECK(string, ':')) {
1567 flow_indicators = 1;
1568 if (followed_by_whitespace) {
1569 block_indicators = 1;
1573 if (CHECK(string, '#') && preceded_by_whitespace) {
1574 flow_indicators = 1;
1575 block_indicators = 1;
1579 if (!IS_PRINTABLE(string)
1580 || (!IS_ASCII(string) && !emitter->unicode)) {
1581 special_characters = 1;
1584 if (IS_BREAK(string)) {
1588 if (IS_SPACE(string))
1590 if (string.start == string.pointer) {
1593 if (string.pointer+WIDTH(string) == string.end) {
1596 if (previous_break) {
1602 else if (IS_BREAK(string))
1604 if (string.start == string.pointer) {
1607 if (string.pointer+WIDTH(string) == string.end) {
1610 if (previous_space) {
1622 preceded_by_whitespace = IS_BLANKZ(string);
1624 if (string.pointer != string.end) {
1625 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
1629 emitter->scalar_data.multiline = line_breaks;
1631 emitter->scalar_data.flow_plain_allowed = 1;
1632 emitter->scalar_data.block_plain_allowed = 1;
1633 emitter->scalar_data.single_quoted_allowed = 1;
1634 emitter->scalar_data.block_allowed = 1;
1636 if (leading_space || leading_break || trailing_space || trailing_break) {
1637 emitter->scalar_data.flow_plain_allowed = 0;
1638 emitter->scalar_data.block_plain_allowed = 0;
1641 if (trailing_space) {
1642 emitter->scalar_data.block_allowed = 0;
1646 emitter->scalar_data.flow_plain_allowed = 0;
1647 emitter->scalar_data.block_plain_allowed = 0;
1648 emitter->scalar_data.single_quoted_allowed = 0;
1651 if (space_break || special_characters) {
1652 emitter->scalar_data.flow_plain_allowed = 0;
1653 emitter->scalar_data.block_plain_allowed = 0;
1654 emitter->scalar_data.single_quoted_allowed = 0;
1655 emitter->scalar_data.block_allowed = 0;
1659 emitter->scalar_data.flow_plain_allowed = 0;
1660 emitter->scalar_data.block_plain_allowed = 0;
1663 if (flow_indicators) {
1664 emitter->scalar_data.flow_plain_allowed = 0;
1667 if (block_indicators) {
1668 emitter->scalar_data.block_plain_allowed = 0;
1675 * Check if the event data is valid.
1679 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
1680 yaml_event_t *event)
1682 emitter->anchor_data.anchor = NULL;
1683 emitter->anchor_data.anchor_length = 0;
1684 emitter->tag_data.handle = NULL;
1685 emitter->tag_data.handle_length = 0;
1686 emitter->tag_data.suffix = NULL;
1687 emitter->tag_data.suffix_length = 0;
1688 emitter->scalar_data.value = NULL;
1689 emitter->scalar_data.length = 0;
1691 switch (event->type)
1693 case YAML_ALIAS_EVENT:
1694 if (!yaml_emitter_analyze_anchor(emitter,
1695 event->data.alias.anchor, 1))
1699 case YAML_SCALAR_EVENT:
1700 if (event->data.scalar.anchor) {
1701 if (!yaml_emitter_analyze_anchor(emitter,
1702 event->data.scalar.anchor, 0))
1705 if (event->data.scalar.tag && (emitter->canonical ||
1706 (!event->data.scalar.plain_implicit
1707 && !event->data.scalar.quoted_implicit))) {
1708 if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
1711 if (!yaml_emitter_analyze_scalar(emitter,
1712 event->data.scalar.value, event->data.scalar.length))
1716 case YAML_SEQUENCE_START_EVENT:
1717 if (event->data.sequence_start.anchor) {
1718 if (!yaml_emitter_analyze_anchor(emitter,
1719 event->data.sequence_start.anchor, 0))
1722 if (event->data.sequence_start.tag && (emitter->canonical ||
1723 !event->data.sequence_start.implicit)) {
1724 if (!yaml_emitter_analyze_tag(emitter,
1725 event->data.sequence_start.tag))
1730 case YAML_MAPPING_START_EVENT:
1731 if (event->data.mapping_start.anchor) {
1732 if (!yaml_emitter_analyze_anchor(emitter,
1733 event->data.mapping_start.anchor, 0))
1736 if (event->data.mapping_start.tag && (emitter->canonical ||
1737 !event->data.mapping_start.implicit)) {
1738 if (!yaml_emitter_analyze_tag(emitter,
1739 event->data.mapping_start.tag))
1750 * Write the BOM character.
1754 yaml_emitter_write_bom(yaml_emitter_t *emitter)
1756 if (!FLUSH(emitter)) return 0;
1758 *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
1759 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
1760 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
1766 yaml_emitter_write_indent(yaml_emitter_t *emitter)
1768 int indent = (emitter->indent >= 0) ? emitter->indent : 0;
1770 if (!emitter->indention || emitter->column > indent
1771 || (emitter->column == indent && !emitter->whitespace)) {
1772 if (!PUT_BREAK(emitter)) return 0;
1775 while (emitter->column < indent) {
1776 if (!PUT(emitter, ' ')) return 0;
1779 emitter->whitespace = 1;
1780 emitter->indention = 1;
1786 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
1787 char *indicator, int need_whitespace,
1788 int is_whitespace, int is_indention)
1790 size_t indicator_length;
1791 yaml_string_t string;
1793 indicator_length = strlen(indicator);
1794 STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length);
1796 if (need_whitespace && !emitter->whitespace) {
1797 if (!PUT(emitter, ' ')) return 0;
1800 while (string.pointer != string.end) {
1801 if (!WRITE(emitter, string)) return 0;
1804 emitter->whitespace = is_whitespace;
1805 emitter->indention = (emitter->indention && is_indention);
1806 emitter->open_ended = 0;
1812 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
1813 yaml_char_t *value, size_t length)
1815 yaml_string_t string;
1816 STRING_ASSIGN(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;
1833 STRING_ASSIGN(string, value, length);
1835 if (!emitter->whitespace) {
1836 if (!PUT(emitter, ' ')) return 0;
1839 while (string.pointer != string.end) {
1840 if (!WRITE(emitter, string)) return 0;
1843 emitter->whitespace = 0;
1844 emitter->indention = 0;
1850 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
1851 yaml_char_t *value, size_t length,
1852 int need_whitespace)
1854 yaml_string_t string;
1855 STRING_ASSIGN(string, value, length);
1857 if (need_whitespace && !emitter->whitespace) {
1858 if (!PUT(emitter, ' ')) return 0;
1861 while (string.pointer != string.end) {
1862 if (IS_ALPHA(string)
1863 || CHECK(string, ';') || CHECK(string, '/')
1864 || CHECK(string, '?') || CHECK(string, ':')
1865 || CHECK(string, '@') || CHECK(string, '&')
1866 || CHECK(string, '=') || CHECK(string, '+')
1867 || CHECK(string, '$') || CHECK(string, ',')
1868 || CHECK(string, '_') || CHECK(string, '.')
1869 || CHECK(string, '~') || CHECK(string, '*')
1870 || CHECK(string, '\'') || CHECK(string, '(')
1871 || CHECK(string, ')') || CHECK(string, '[')
1872 || CHECK(string, ']')) {
1873 if (!WRITE(emitter, string)) return 0;
1876 int width = WIDTH(string);
1879 value = *(string.pointer++);
1880 if (!PUT(emitter, '%')) return 0;
1881 if (!PUT(emitter, (value >> 4)
1882 + ((value >> 4) < 10 ? '0' : 'A' - 10)))
1884 if (!PUT(emitter, (value & 0x0F)
1885 + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
1891 emitter->whitespace = 0;
1892 emitter->indention = 0;
1898 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
1899 yaml_char_t *value, size_t length, int allow_breaks)
1901 yaml_string_t string;
1905 STRING_ASSIGN(string, value, length);
1907 if (!emitter->whitespace) {
1908 if (!PUT(emitter, ' ')) return 0;
1911 while (string.pointer != string.end)
1913 if (IS_SPACE(string))
1915 if (allow_breaks && !spaces
1916 && emitter->column > emitter->best_width
1917 && !IS_SPACE_AT(string, 1)) {
1918 if (!yaml_emitter_write_indent(emitter)) return 0;
1922 if (!WRITE(emitter, string)) return 0;
1926 else if (IS_BREAK(string))
1928 if (!breaks && CHECK(string, '\n')) {
1929 if (!PUT_BREAK(emitter)) return 0;
1931 if (!WRITE_BREAK(emitter, string)) return 0;
1932 emitter->indention = 1;
1938 if (!yaml_emitter_write_indent(emitter)) return 0;
1940 if (!WRITE(emitter, string)) return 0;
1941 emitter->indention = 0;
1947 emitter->whitespace = 0;
1948 emitter->indention = 0;
1954 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
1955 yaml_char_t *value, size_t length, int allow_breaks)
1957 yaml_string_t string;
1961 STRING_ASSIGN(string, value, length);
1963 if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
1966 while (string.pointer != string.end)
1968 if (IS_SPACE(string))
1970 if (allow_breaks && !spaces
1971 && emitter->column > emitter->best_width
1972 && string.pointer != string.start
1973 && string.pointer != string.end - 1
1974 && !IS_SPACE_AT(string, 1)) {
1975 if (!yaml_emitter_write_indent(emitter)) return 0;
1979 if (!WRITE(emitter, string)) return 0;
1983 else if (IS_BREAK(string))
1985 if (!breaks && CHECK(string, '\n')) {
1986 if (!PUT_BREAK(emitter)) return 0;
1988 if (!WRITE_BREAK(emitter, string)) return 0;
1989 emitter->indention = 1;
1995 if (!yaml_emitter_write_indent(emitter)) return 0;
1997 if (CHECK(string, '\'')) {
1998 if (!PUT(emitter, '\'')) return 0;
2000 if (!WRITE(emitter, string)) return 0;
2001 emitter->indention = 0;
2007 if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
2010 emitter->whitespace = 0;
2011 emitter->indention = 0;
2017 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
2018 yaml_char_t *value, size_t length, int allow_breaks)
2020 yaml_string_t string;
2023 STRING_ASSIGN(string, value, length);
2025 if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
2028 while (string.pointer != string.end)
2030 if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
2031 || IS_BOM(string) || IS_BREAK(string)
2032 || CHECK(string, '"') || CHECK(string, '\\'))
2034 unsigned char octet;
2039 octet = string.pointer[0];
2040 width = (octet & 0x80) == 0x00 ? 1 :
2041 (octet & 0xE0) == 0xC0 ? 2 :
2042 (octet & 0xF0) == 0xE0 ? 3 :
2043 (octet & 0xF8) == 0xF0 ? 4 : 0;
2044 value = (octet & 0x80) == 0x00 ? octet & 0x7F :
2045 (octet & 0xE0) == 0xC0 ? octet & 0x1F :
2046 (octet & 0xF0) == 0xE0 ? octet & 0x0F :
2047 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
2048 for (k = 1; k < (int)width; k ++) {
2049 octet = string.pointer[k];
2050 value = (value << 6) + (octet & 0x3F);
2052 string.pointer += width;
2054 if (!PUT(emitter, '\\')) return 0;
2059 if (!PUT(emitter, '0')) return 0;
2063 if (!PUT(emitter, 'a')) return 0;
2067 if (!PUT(emitter, 'b')) return 0;
2071 if (!PUT(emitter, 't')) return 0;
2075 if (!PUT(emitter, 'n')) return 0;
2079 if (!PUT(emitter, 'v')) return 0;
2083 if (!PUT(emitter, 'f')) return 0;
2087 if (!PUT(emitter, 'r')) return 0;
2091 if (!PUT(emitter, 'e')) return 0;
2095 if (!PUT(emitter, '\"')) return 0;
2099 if (!PUT(emitter, '\\')) return 0;
2103 if (!PUT(emitter, 'N')) return 0;
2107 if (!PUT(emitter, '_')) return 0;
2111 if (!PUT(emitter, 'L')) return 0;
2115 if (!PUT(emitter, 'P')) return 0;
2119 if (value <= 0xFF) {
2120 if (!PUT(emitter, 'x')) return 0;
2123 else if (value <= 0xFFFF) {
2124 if (!PUT(emitter, 'u')) return 0;
2128 if (!PUT(emitter, 'U')) return 0;
2131 for (k = (width-1)*4; k >= 0; k -= 4) {
2132 int digit = (value >> k) & 0x0F;
2133 if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
2139 else if (IS_SPACE(string))
2141 if (allow_breaks && !spaces
2142 && emitter->column > emitter->best_width
2143 && string.pointer != string.start
2144 && string.pointer != string.end - 1) {
2145 if (!yaml_emitter_write_indent(emitter)) return 0;
2146 if (IS_SPACE_AT(string, 1)) {
2147 if (!PUT(emitter, '\\')) return 0;
2152 if (!WRITE(emitter, string)) return 0;
2158 if (!WRITE(emitter, string)) return 0;
2163 if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
2166 emitter->whitespace = 0;
2167 emitter->indention = 0;
2173 yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
2174 yaml_string_t string)
2176 char indent_hint[2];
2177 char *chomp_hint = NULL;
2179 if (IS_SPACE(string) || IS_BREAK(string))
2181 indent_hint[0] = '0' + (char)emitter->best_indent;
2182 indent_hint[1] = '\0';
2183 if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0))
2187 emitter->open_ended = 0;
2189 string.pointer = string.end;
2190 if (string.start == string.pointer)
2198 } while ((*string.pointer & 0xC0) == 0x80);
2199 if (!IS_BREAK(string))
2203 else if (string.start == string.pointer)
2206 emitter->open_ended = 1;
2212 } while ((*string.pointer & 0xC0) == 0x80);
2213 if (IS_BREAK(string))
2216 emitter->open_ended = 1;
2223 if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0))
2231 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
2232 yaml_char_t *value, size_t length)
2234 yaml_string_t string;
2237 STRING_ASSIGN(string, value, length);
2239 if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0))
2241 if (!yaml_emitter_write_block_scalar_hints(emitter, string))
2243 if (!PUT_BREAK(emitter)) return 0;
2244 emitter->indention = 1;
2245 emitter->whitespace = 1;
2247 while (string.pointer != string.end)
2249 if (IS_BREAK(string))
2251 if (!WRITE_BREAK(emitter, string)) return 0;
2252 emitter->indention = 1;
2258 if (!yaml_emitter_write_indent(emitter)) return 0;
2260 if (!WRITE(emitter, string)) return 0;
2261 emitter->indention = 0;
2270 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
2271 yaml_char_t *value, size_t length)
2273 yaml_string_t string;
2275 int leading_spaces = 1;
2277 STRING_ASSIGN(string, value, length);
2279 if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0))
2281 if (!yaml_emitter_write_block_scalar_hints(emitter, string))
2283 if (!PUT_BREAK(emitter)) return 0;
2284 emitter->indention = 1;
2285 emitter->whitespace = 1;
2287 while (string.pointer != string.end)
2289 if (IS_BREAK(string))
2291 if (!breaks && !leading_spaces && CHECK(string, '\n')) {
2293 while (IS_BREAK_AT(string, k)) {
2294 k += WIDTH_AT(string, k);
2296 if (!IS_BLANKZ_AT(string, k)) {
2297 if (!PUT_BREAK(emitter)) return 0;
2300 if (!WRITE_BREAK(emitter, string)) return 0;
2301 emitter->indention = 1;
2307 if (!yaml_emitter_write_indent(emitter)) return 0;
2308 leading_spaces = IS_BLANK(string);
2310 if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
2311 && emitter->column > emitter->best_width) {
2312 if (!yaml_emitter_write_indent(emitter)) return 0;
2316 if (!WRITE(emitter, string)) return 0;
2318 emitter->indention = 0;