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)
653 if (!yaml_emitter_flush(emitter))
656 emitter->state = YAML_EMIT_END_STATE;
661 return yaml_emitter_set_emitter_error(emitter,
662 "expected DOCUMENT-START or STREAM-END");
666 * Expect the root node.
670 yaml_emitter_emit_document_content(yaml_emitter_t *emitter,
673 if (!PUSH(emitter, emitter->states, YAML_EMIT_DOCUMENT_END_STATE))
676 return yaml_emitter_emit_node(emitter, event, 1, 0, 0, 0);
680 * Expect DOCUMENT-END.
684 yaml_emitter_emit_document_end(yaml_emitter_t *emitter,
687 if (event->type == YAML_DOCUMENT_END_EVENT)
689 if (!yaml_emitter_write_indent(emitter))
691 if (!event->data.document_end.implicit) {
692 if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0))
694 if (!yaml_emitter_write_indent(emitter))
697 if (!yaml_emitter_flush(emitter))
700 emitter->state = YAML_EMIT_DOCUMENT_START_STATE;
702 while (!STACK_EMPTY(emitter, emitter->tag_directives)) {
703 yaml_tag_directive_t tag_directive = POP(emitter,
704 emitter->tag_directives);
705 yaml_free(tag_directive.handle);
706 yaml_free(tag_directive.prefix);
712 return yaml_emitter_set_emitter_error(emitter,
713 "expected DOCUMENT-END");
718 * Expect a flow item node.
722 yaml_emitter_emit_flow_sequence_item(yaml_emitter_t *emitter,
723 yaml_event_t *event, int first)
727 if (!yaml_emitter_write_indicator(emitter, "[", 1, 1, 0))
729 if (!yaml_emitter_increase_indent(emitter, 1, 0))
731 emitter->flow_level ++;
734 if (event->type == YAML_SEQUENCE_END_EVENT)
736 emitter->flow_level --;
737 emitter->indent = POP(emitter, emitter->indents);
738 if (emitter->canonical && !first) {
739 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
741 if (!yaml_emitter_write_indent(emitter))
744 if (!yaml_emitter_write_indicator(emitter, "]", 0, 0, 0))
746 emitter->state = POP(emitter, emitter->states);
752 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
756 if (emitter->canonical || emitter->column > emitter->best_width) {
757 if (!yaml_emitter_write_indent(emitter))
760 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_SEQUENCE_ITEM_STATE))
763 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
767 * Expect a flow key node.
771 yaml_emitter_emit_flow_mapping_key(yaml_emitter_t *emitter,
772 yaml_event_t *event, int first)
776 if (!yaml_emitter_write_indicator(emitter, "{", 1, 1, 0))
778 if (!yaml_emitter_increase_indent(emitter, 1, 0))
780 emitter->flow_level ++;
783 if (event->type == YAML_MAPPING_END_EVENT)
785 emitter->flow_level --;
786 emitter->indent = POP(emitter, emitter->indents);
787 if (emitter->canonical && !first) {
788 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
790 if (!yaml_emitter_write_indent(emitter))
793 if (!yaml_emitter_write_indicator(emitter, "}", 0, 0, 0))
795 emitter->state = POP(emitter, emitter->states);
801 if (!yaml_emitter_write_indicator(emitter, ",", 0, 0, 0))
804 if (emitter->canonical || emitter->column > emitter->best_width) {
805 if (!yaml_emitter_write_indent(emitter))
809 if (!emitter->canonical && yaml_emitter_check_simple_key(emitter))
811 if (!PUSH(emitter, emitter->states,
812 YAML_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE))
815 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
819 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 0))
821 if (!PUSH(emitter, emitter->states,
822 YAML_EMIT_FLOW_MAPPING_VALUE_STATE))
825 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
830 * Expect a flow value node.
834 yaml_emitter_emit_flow_mapping_value(yaml_emitter_t *emitter,
835 yaml_event_t *event, int simple)
838 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
842 if (emitter->canonical || emitter->column > emitter->best_width) {
843 if (!yaml_emitter_write_indent(emitter))
846 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 0))
849 if (!PUSH(emitter, emitter->states, YAML_EMIT_FLOW_MAPPING_KEY_STATE))
851 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
855 * Expect a block item node.
859 yaml_emitter_emit_block_sequence_item(yaml_emitter_t *emitter,
860 yaml_event_t *event, int first)
864 if (!yaml_emitter_increase_indent(emitter, 0,
865 (emitter->mapping_context && !emitter->indention)))
869 if (event->type == YAML_SEQUENCE_END_EVENT)
871 emitter->indent = POP(emitter, emitter->indents);
872 emitter->state = POP(emitter, emitter->states);
877 if (!yaml_emitter_write_indent(emitter))
879 if (!yaml_emitter_write_indicator(emitter, "-", 1, 0, 1))
881 if (!PUSH(emitter, emitter->states,
882 YAML_EMIT_BLOCK_SEQUENCE_ITEM_STATE))
885 return yaml_emitter_emit_node(emitter, event, 0, 1, 0, 0);
889 * Expect a block key node.
893 yaml_emitter_emit_block_mapping_key(yaml_emitter_t *emitter,
894 yaml_event_t *event, int first)
898 if (!yaml_emitter_increase_indent(emitter, 0, 0))
902 if (event->type == YAML_MAPPING_END_EVENT)
904 emitter->indent = POP(emitter, emitter->indents);
905 emitter->state = POP(emitter, emitter->states);
910 if (!yaml_emitter_write_indent(emitter))
913 if (yaml_emitter_check_simple_key(emitter))
915 if (!PUSH(emitter, emitter->states,
916 YAML_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE))
919 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 1);
923 if (!yaml_emitter_write_indicator(emitter, "?", 1, 0, 1))
925 if (!PUSH(emitter, emitter->states,
926 YAML_EMIT_BLOCK_MAPPING_VALUE_STATE))
929 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
934 * Expect a block value node.
938 yaml_emitter_emit_block_mapping_value(yaml_emitter_t *emitter,
939 yaml_event_t *event, int simple)
942 if (!yaml_emitter_write_indicator(emitter, ":", 0, 0, 0))
946 if (!yaml_emitter_write_indent(emitter))
948 if (!yaml_emitter_write_indicator(emitter, ":", 1, 0, 1))
951 if (!PUSH(emitter, emitter->states,
952 YAML_EMIT_BLOCK_MAPPING_KEY_STATE))
955 return yaml_emitter_emit_node(emitter, event, 0, 0, 1, 0);
963 yaml_emitter_emit_node(yaml_emitter_t *emitter, yaml_event_t *event,
964 int root, int sequence, int mapping, int simple_key)
966 emitter->root_context = root;
967 emitter->sequence_context = sequence;
968 emitter->mapping_context = mapping;
969 emitter->simple_key_context = simple_key;
973 case YAML_ALIAS_EVENT:
974 return yaml_emitter_emit_alias(emitter, event);
976 case YAML_SCALAR_EVENT:
977 return yaml_emitter_emit_scalar(emitter, event);
979 case YAML_SEQUENCE_START_EVENT:
980 return yaml_emitter_emit_sequence_start(emitter, event);
982 case YAML_MAPPING_START_EVENT:
983 return yaml_emitter_emit_mapping_start(emitter, event);
986 return yaml_emitter_set_emitter_error(emitter,
987 "expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS");
998 yaml_emitter_emit_alias(yaml_emitter_t *emitter, SHIM(yaml_event_t *event))
1000 if (!yaml_emitter_process_anchor(emitter))
1002 emitter->state = POP(emitter, emitter->states);
1012 yaml_emitter_emit_scalar(yaml_emitter_t *emitter, yaml_event_t *event)
1014 if (!yaml_emitter_select_scalar_style(emitter, event))
1016 if (!yaml_emitter_process_anchor(emitter))
1018 if (!yaml_emitter_process_tag(emitter))
1020 if (!yaml_emitter_increase_indent(emitter, 1, 0))
1022 if (!yaml_emitter_process_scalar(emitter))
1024 emitter->indent = POP(emitter, emitter->indents);
1025 emitter->state = POP(emitter, emitter->states);
1031 * Expect SEQUENCE-START.
1035 yaml_emitter_emit_sequence_start(yaml_emitter_t *emitter, yaml_event_t *event)
1037 if (!yaml_emitter_process_anchor(emitter))
1039 if (!yaml_emitter_process_tag(emitter))
1042 if (emitter->flow_level || emitter->canonical
1043 || event->data.sequence_start.style == YAML_FLOW_SEQUENCE_STYLE
1044 || yaml_emitter_check_empty_sequence(emitter)) {
1045 emitter->state = YAML_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE;
1048 emitter->state = YAML_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE;
1055 * Expect MAPPING-START.
1059 yaml_emitter_emit_mapping_start(yaml_emitter_t *emitter, yaml_event_t *event)
1061 if (!yaml_emitter_process_anchor(emitter))
1063 if (!yaml_emitter_process_tag(emitter))
1066 if (emitter->flow_level || emitter->canonical
1067 || event->data.mapping_start.style == YAML_FLOW_MAPPING_STYLE
1068 || yaml_emitter_check_empty_mapping(emitter)) {
1069 emitter->state = YAML_EMIT_FLOW_MAPPING_FIRST_KEY_STATE;
1072 emitter->state = YAML_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE;
1079 * Check if the document content is an empty scalar.
1083 yaml_emitter_check_empty_document(SHIM(yaml_emitter_t *emitter))
1089 * Check if the next events represent an empty sequence.
1093 yaml_emitter_check_empty_sequence(yaml_emitter_t *emitter)
1095 if (emitter->events.tail - emitter->events.head < 2)
1098 return (emitter->events.head[0].type == YAML_SEQUENCE_START_EVENT
1099 && emitter->events.head[1].type == YAML_SEQUENCE_END_EVENT);
1103 * Check if the next events represent an empty mapping.
1107 yaml_emitter_check_empty_mapping(yaml_emitter_t *emitter)
1109 if (emitter->events.tail - emitter->events.head < 2)
1112 return (emitter->events.head[0].type == YAML_MAPPING_START_EVENT
1113 && emitter->events.head[1].type == YAML_MAPPING_END_EVENT);
1117 * Check if the next node can be expressed as a simple key.
1121 yaml_emitter_check_simple_key(yaml_emitter_t *emitter)
1123 yaml_event_t *event = emitter->events.head;
1126 switch (event->type)
1128 case YAML_ALIAS_EVENT:
1129 length += emitter->anchor_data.anchor_length;
1132 case YAML_SCALAR_EVENT:
1133 if (emitter->scalar_data.multiline)
1135 length += emitter->anchor_data.anchor_length
1136 + emitter->tag_data.handle_length
1137 + emitter->tag_data.suffix_length
1138 + emitter->scalar_data.length;
1141 case YAML_SEQUENCE_START_EVENT:
1142 if (!yaml_emitter_check_empty_sequence(emitter))
1144 length += emitter->anchor_data.anchor_length
1145 + emitter->tag_data.handle_length
1146 + emitter->tag_data.suffix_length;
1149 case YAML_MAPPING_START_EVENT:
1150 if (!yaml_emitter_check_empty_mapping(emitter))
1152 length += emitter->anchor_data.anchor_length
1153 + emitter->tag_data.handle_length
1154 + emitter->tag_data.suffix_length;
1168 * Determine an acceptable scalar style.
1172 yaml_emitter_select_scalar_style(yaml_emitter_t *emitter, yaml_event_t *event)
1174 yaml_scalar_style_t style = event->data.scalar.style;
1175 int no_tag = (!emitter->tag_data.handle && !emitter->tag_data.suffix);
1177 if (no_tag && !event->data.scalar.plain_implicit
1178 && !event->data.scalar.quoted_implicit) {
1179 return yaml_emitter_set_emitter_error(emitter,
1180 "neither tag nor implicit flags are specified");
1183 if (style == YAML_ANY_SCALAR_STYLE)
1184 style = YAML_PLAIN_SCALAR_STYLE;
1186 if (emitter->canonical)
1187 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1189 if (emitter->simple_key_context && emitter->scalar_data.multiline)
1190 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1192 if (style == YAML_PLAIN_SCALAR_STYLE)
1194 if ((emitter->flow_level && !emitter->scalar_data.flow_plain_allowed)
1195 || (!emitter->flow_level && !emitter->scalar_data.block_plain_allowed))
1196 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1197 if (!emitter->scalar_data.length
1198 && (emitter->flow_level || emitter->simple_key_context))
1199 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1200 if (no_tag && !event->data.scalar.plain_implicit)
1201 style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
1204 if (style == YAML_SINGLE_QUOTED_SCALAR_STYLE)
1206 if (!emitter->scalar_data.single_quoted_allowed)
1207 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1210 if (style == YAML_LITERAL_SCALAR_STYLE || style == YAML_FOLDED_SCALAR_STYLE)
1212 if (!emitter->scalar_data.block_allowed
1213 || emitter->flow_level || emitter->simple_key_context)
1214 style = YAML_DOUBLE_QUOTED_SCALAR_STYLE;
1217 if (no_tag && !event->data.scalar.quoted_implicit
1218 && style != YAML_PLAIN_SCALAR_STYLE)
1220 emitter->tag_data.handle = (yaml_char_t *)"!";
1221 emitter->tag_data.handle_length = 1;
1224 emitter->scalar_data.style = style;
1234 yaml_emitter_process_anchor(yaml_emitter_t *emitter)
1236 if (!emitter->anchor_data.anchor)
1239 if (!yaml_emitter_write_indicator(emitter,
1240 (emitter->anchor_data.alias ? "*" : "&"), 1, 0, 0))
1243 return yaml_emitter_write_anchor(emitter,
1244 emitter->anchor_data.anchor, emitter->anchor_data.anchor_length);
1252 yaml_emitter_process_tag(yaml_emitter_t *emitter)
1254 if (!emitter->tag_data.handle && !emitter->tag_data.suffix)
1257 if (emitter->tag_data.handle)
1259 if (!yaml_emitter_write_tag_handle(emitter, emitter->tag_data.handle,
1260 emitter->tag_data.handle_length))
1262 if (emitter->tag_data.suffix) {
1263 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1264 emitter->tag_data.suffix_length, 0))
1270 if (!yaml_emitter_write_indicator(emitter, "!<", 1, 0, 0))
1272 if (!yaml_emitter_write_tag_content(emitter, emitter->tag_data.suffix,
1273 emitter->tag_data.suffix_length, 0))
1275 if (!yaml_emitter_write_indicator(emitter, ">", 0, 0, 0))
1287 yaml_emitter_process_scalar(yaml_emitter_t *emitter)
1289 switch (emitter->scalar_data.style)
1291 case YAML_PLAIN_SCALAR_STYLE:
1292 return yaml_emitter_write_plain_scalar(emitter,
1293 emitter->scalar_data.value, emitter->scalar_data.length,
1294 !emitter->simple_key_context);
1296 case YAML_SINGLE_QUOTED_SCALAR_STYLE:
1297 return yaml_emitter_write_single_quoted_scalar(emitter,
1298 emitter->scalar_data.value, emitter->scalar_data.length,
1299 !emitter->simple_key_context);
1301 case YAML_DOUBLE_QUOTED_SCALAR_STYLE:
1302 return yaml_emitter_write_double_quoted_scalar(emitter,
1303 emitter->scalar_data.value, emitter->scalar_data.length,
1304 !emitter->simple_key_context);
1306 case YAML_LITERAL_SCALAR_STYLE:
1307 return yaml_emitter_write_literal_scalar(emitter,
1308 emitter->scalar_data.value, emitter->scalar_data.length);
1310 case YAML_FOLDED_SCALAR_STYLE:
1311 return yaml_emitter_write_folded_scalar(emitter,
1312 emitter->scalar_data.value, emitter->scalar_data.length);
1315 assert(1); /* Impossible. */
1322 * Check if a %YAML directive is valid.
1326 yaml_emitter_analyze_version_directive(yaml_emitter_t *emitter,
1327 yaml_version_directive_t version_directive)
1329 if (version_directive.major != 1 || version_directive.minor != 1) {
1330 return yaml_emitter_set_emitter_error(emitter,
1331 "incompatible %YAML directive");
1338 * Check if a %TAG directive is valid.
1342 yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter,
1343 yaml_tag_directive_t tag_directive)
1345 yaml_string_t handle;
1346 yaml_string_t prefix;
1347 size_t handle_length;
1348 size_t prefix_length;
1350 handle_length = strlen((char *)tag_directive.handle);
1351 prefix_length = strlen((char *)tag_directive.prefix);
1352 STRING_ASSIGN(handle, tag_directive.handle, handle_length);
1353 STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length);
1355 if (handle.start == handle.end) {
1356 return yaml_emitter_set_emitter_error(emitter,
1357 "tag handle must not be empty");
1360 if (handle.start[0] != '!') {
1361 return yaml_emitter_set_emitter_error(emitter,
1362 "tag handle must start with '!'");
1365 if (handle.end[-1] != '!') {
1366 return yaml_emitter_set_emitter_error(emitter,
1367 "tag handle must end with '!'");
1372 while (handle.pointer < handle.end-1) {
1373 if (!IS_ALPHA(handle)) {
1374 return yaml_emitter_set_emitter_error(emitter,
1375 "tag handle must contain alphanumerical characters only");
1380 if (prefix.start == prefix.end) {
1381 return yaml_emitter_set_emitter_error(emitter,
1382 "tag prefix must not be empty");
1389 * Check if an anchor is valid.
1393 yaml_emitter_analyze_anchor(yaml_emitter_t *emitter,
1394 yaml_char_t *anchor, int alias)
1396 size_t anchor_length;
1397 yaml_string_t string;
1399 anchor_length = strlen((char *)anchor);
1400 STRING_ASSIGN(string, anchor, anchor_length);
1402 if (string.start == string.end) {
1403 return yaml_emitter_set_emitter_error(emitter, alias ?
1404 "alias value must not be empty" :
1405 "anchor value must not be empty");
1408 while (string.pointer != string.end) {
1409 if (!IS_ALPHA(string)) {
1410 return yaml_emitter_set_emitter_error(emitter, alias ?
1411 "alias value must contain alphanumerical characters only" :
1412 "anchor value must contain alphanumerical characters only");
1417 emitter->anchor_data.anchor = string.start;
1418 emitter->anchor_data.anchor_length = string.end - string.start;
1419 emitter->anchor_data.alias = alias;
1425 * Check if a tag is valid.
1429 yaml_emitter_analyze_tag(yaml_emitter_t *emitter,
1433 yaml_string_t string;
1434 yaml_tag_directive_t *tag_directive;
1436 tag_length = strlen((char *)tag);
1437 STRING_ASSIGN(string, tag, tag_length);
1439 if (string.start == string.end) {
1440 return yaml_emitter_set_emitter_error(emitter,
1441 "tag value must not be empty");
1444 for (tag_directive = emitter->tag_directives.start;
1445 tag_directive != emitter->tag_directives.top; tag_directive ++) {
1446 size_t prefix_length = strlen((char *)tag_directive->prefix);
1447 if (prefix_length < (size_t)(string.end - string.start)
1448 && strncmp((char *)tag_directive->prefix, (char *)string.start,
1449 prefix_length) == 0)
1451 emitter->tag_data.handle = tag_directive->handle;
1452 emitter->tag_data.handle_length =
1453 strlen((char *)tag_directive->handle);
1454 emitter->tag_data.suffix = string.start + prefix_length;
1455 emitter->tag_data.suffix_length =
1456 (string.end - string.start) - prefix_length;
1461 emitter->tag_data.suffix = string.start;
1462 emitter->tag_data.suffix_length = string.end - string.start;
1468 * Check if a scalar is valid.
1472 yaml_emitter_analyze_scalar(yaml_emitter_t *emitter,
1473 yaml_char_t *value, size_t length)
1475 yaml_string_t string;
1477 int block_indicators = 0;
1478 int flow_indicators = 0;
1479 int line_breaks = 0;
1480 int special_characters = 0;
1482 int leading_space = 0;
1483 int leading_break = 0;
1484 int trailing_space = 0;
1485 int trailing_break = 0;
1486 int break_space = 0;
1487 int space_break = 0;
1489 int preceded_by_whitespace = 0;
1490 int followed_by_whitespace = 0;
1491 int previous_space = 0;
1492 int previous_break = 0;
1494 STRING_ASSIGN(string, value, length);
1496 emitter->scalar_data.value = value;
1497 emitter->scalar_data.length = length;
1499 if (string.start == string.end)
1501 emitter->scalar_data.multiline = 0;
1502 emitter->scalar_data.flow_plain_allowed = 0;
1503 emitter->scalar_data.block_plain_allowed = 1;
1504 emitter->scalar_data.single_quoted_allowed = 1;
1505 emitter->scalar_data.block_allowed = 0;
1510 if ((CHECK_AT(string, '-', 0)
1511 && CHECK_AT(string, '-', 1)
1512 && CHECK_AT(string, '-', 2))
1513 || (CHECK_AT(string, '.', 0)
1514 && CHECK_AT(string, '.', 1)
1515 && CHECK_AT(string, '.', 2))) {
1516 block_indicators = 1;
1517 flow_indicators = 1;
1520 preceded_by_whitespace = 1;
1521 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
1523 while (string.pointer != string.end)
1525 if (string.start == string.pointer)
1527 if (CHECK(string, '#') || CHECK(string, ',')
1528 || CHECK(string, '[') || CHECK(string, ']')
1529 || CHECK(string, '{') || CHECK(string, '}')
1530 || CHECK(string, '&') || CHECK(string, '*')
1531 || CHECK(string, '!') || CHECK(string, '|')
1532 || CHECK(string, '>') || CHECK(string, '\'')
1533 || CHECK(string, '"') || CHECK(string, '%')
1534 || CHECK(string, '@') || CHECK(string, '`')) {
1535 flow_indicators = 1;
1536 block_indicators = 1;
1539 if (CHECK(string, '?') || CHECK(string, ':')) {
1540 flow_indicators = 1;
1541 if (followed_by_whitespace) {
1542 block_indicators = 1;
1546 if (CHECK(string, '-') && followed_by_whitespace) {
1547 flow_indicators = 1;
1548 block_indicators = 1;
1553 if (CHECK(string, ',') || CHECK(string, '?')
1554 || CHECK(string, '[') || CHECK(string, ']')
1555 || CHECK(string, '{') || CHECK(string, '}')) {
1556 flow_indicators = 1;
1559 if (CHECK(string, ':')) {
1560 flow_indicators = 1;
1561 if (followed_by_whitespace) {
1562 block_indicators = 1;
1566 if (CHECK(string, '#') && preceded_by_whitespace) {
1567 flow_indicators = 1;
1568 block_indicators = 1;
1572 if (!IS_PRINTABLE(string)
1573 || (!IS_ASCII(string) && !emitter->unicode)) {
1574 special_characters = 1;
1577 if (IS_BREAK(string)) {
1581 if (IS_SPACE(string))
1583 if (string.start == string.pointer) {
1586 if (string.pointer+WIDTH(string) == string.end) {
1589 if (previous_break) {
1595 else if (IS_BREAK(string))
1597 if (string.start == string.pointer) {
1600 if (string.pointer+WIDTH(string) == string.end) {
1603 if (previous_space) {
1615 preceded_by_whitespace = IS_BLANKZ(string);
1617 if (string.pointer != string.end) {
1618 followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string));
1622 emitter->scalar_data.multiline = line_breaks;
1624 emitter->scalar_data.flow_plain_allowed = 1;
1625 emitter->scalar_data.block_plain_allowed = 1;
1626 emitter->scalar_data.single_quoted_allowed = 1;
1627 emitter->scalar_data.block_allowed = 1;
1629 if (leading_space || leading_break || trailing_space || trailing_break) {
1630 emitter->scalar_data.flow_plain_allowed = 0;
1631 emitter->scalar_data.block_plain_allowed = 0;
1634 if (trailing_space) {
1635 emitter->scalar_data.block_allowed = 0;
1639 emitter->scalar_data.flow_plain_allowed = 0;
1640 emitter->scalar_data.block_plain_allowed = 0;
1641 emitter->scalar_data.single_quoted_allowed = 0;
1644 if (space_break || special_characters) {
1645 emitter->scalar_data.flow_plain_allowed = 0;
1646 emitter->scalar_data.block_plain_allowed = 0;
1647 emitter->scalar_data.single_quoted_allowed = 0;
1648 emitter->scalar_data.block_allowed = 0;
1652 emitter->scalar_data.flow_plain_allowed = 0;
1653 emitter->scalar_data.block_plain_allowed = 0;
1656 if (flow_indicators) {
1657 emitter->scalar_data.flow_plain_allowed = 0;
1660 if (block_indicators) {
1661 emitter->scalar_data.block_plain_allowed = 0;
1668 * Check if the event data is valid.
1672 yaml_emitter_analyze_event(yaml_emitter_t *emitter,
1673 yaml_event_t *event)
1675 emitter->anchor_data.anchor = NULL;
1676 emitter->anchor_data.anchor_length = 0;
1677 emitter->tag_data.handle = NULL;
1678 emitter->tag_data.handle_length = 0;
1679 emitter->tag_data.suffix = NULL;
1680 emitter->tag_data.suffix_length = 0;
1681 emitter->scalar_data.value = NULL;
1682 emitter->scalar_data.length = 0;
1684 switch (event->type)
1686 case YAML_ALIAS_EVENT:
1687 if (!yaml_emitter_analyze_anchor(emitter,
1688 event->data.alias.anchor, 1))
1692 case YAML_SCALAR_EVENT:
1693 if (event->data.scalar.anchor) {
1694 if (!yaml_emitter_analyze_anchor(emitter,
1695 event->data.scalar.anchor, 0))
1698 if (event->data.scalar.tag && (emitter->canonical ||
1699 (!event->data.scalar.plain_implicit
1700 && !event->data.scalar.quoted_implicit))) {
1701 if (!yaml_emitter_analyze_tag(emitter, event->data.scalar.tag))
1704 if (!yaml_emitter_analyze_scalar(emitter,
1705 event->data.scalar.value, event->data.scalar.length))
1709 case YAML_SEQUENCE_START_EVENT:
1710 if (event->data.sequence_start.anchor) {
1711 if (!yaml_emitter_analyze_anchor(emitter,
1712 event->data.sequence_start.anchor, 0))
1715 if (event->data.sequence_start.tag && (emitter->canonical ||
1716 !event->data.sequence_start.implicit)) {
1717 if (!yaml_emitter_analyze_tag(emitter,
1718 event->data.sequence_start.tag))
1723 case YAML_MAPPING_START_EVENT:
1724 if (event->data.mapping_start.anchor) {
1725 if (!yaml_emitter_analyze_anchor(emitter,
1726 event->data.mapping_start.anchor, 0))
1729 if (event->data.mapping_start.tag && (emitter->canonical ||
1730 !event->data.mapping_start.implicit)) {
1731 if (!yaml_emitter_analyze_tag(emitter,
1732 event->data.mapping_start.tag))
1743 * Write the BOM character.
1747 yaml_emitter_write_bom(yaml_emitter_t *emitter)
1749 if (!FLUSH(emitter)) return 0;
1751 *(emitter->buffer.pointer++) = (yaml_char_t) '\xEF';
1752 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBB';
1753 *(emitter->buffer.pointer++) = (yaml_char_t) '\xBF';
1759 yaml_emitter_write_indent(yaml_emitter_t *emitter)
1761 int indent = (emitter->indent >= 0) ? emitter->indent : 0;
1763 if (!emitter->indention || emitter->column > indent
1764 || (emitter->column == indent && !emitter->whitespace)) {
1765 if (!PUT_BREAK(emitter)) return 0;
1768 while (emitter->column < indent) {
1769 if (!PUT(emitter, ' ')) return 0;
1772 emitter->whitespace = 1;
1773 emitter->indention = 1;
1779 yaml_emitter_write_indicator(yaml_emitter_t *emitter,
1780 char *indicator, int need_whitespace,
1781 int is_whitespace, int is_indention)
1783 size_t indicator_length;
1784 yaml_string_t string;
1786 indicator_length = strlen(indicator);
1787 STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length);
1789 if (need_whitespace && !emitter->whitespace) {
1790 if (!PUT(emitter, ' ')) return 0;
1793 while (string.pointer != string.end) {
1794 if (!WRITE(emitter, string)) return 0;
1797 emitter->whitespace = is_whitespace;
1798 emitter->indention = (emitter->indention && is_indention);
1799 emitter->open_ended = 0;
1805 yaml_emitter_write_anchor(yaml_emitter_t *emitter,
1806 yaml_char_t *value, size_t length)
1808 yaml_string_t string;
1809 STRING_ASSIGN(string, value, length);
1811 while (string.pointer != string.end) {
1812 if (!WRITE(emitter, string)) return 0;
1815 emitter->whitespace = 0;
1816 emitter->indention = 0;
1822 yaml_emitter_write_tag_handle(yaml_emitter_t *emitter,
1823 yaml_char_t *value, size_t length)
1825 yaml_string_t string;
1826 STRING_ASSIGN(string, value, length);
1828 if (!emitter->whitespace) {
1829 if (!PUT(emitter, ' ')) return 0;
1832 while (string.pointer != string.end) {
1833 if (!WRITE(emitter, string)) return 0;
1836 emitter->whitespace = 0;
1837 emitter->indention = 0;
1843 yaml_emitter_write_tag_content(yaml_emitter_t *emitter,
1844 yaml_char_t *value, size_t length,
1845 int need_whitespace)
1847 yaml_string_t string;
1848 STRING_ASSIGN(string, value, length);
1850 if (need_whitespace && !emitter->whitespace) {
1851 if (!PUT(emitter, ' ')) return 0;
1854 while (string.pointer != string.end) {
1855 if (IS_ALPHA(string)
1856 || CHECK(string, ';') || CHECK(string, '/')
1857 || CHECK(string, '?') || CHECK(string, ':')
1858 || CHECK(string, '@') || CHECK(string, '&')
1859 || CHECK(string, '=') || CHECK(string, '+')
1860 || CHECK(string, '$') || CHECK(string, ',')
1861 || CHECK(string, '_') || CHECK(string, '.')
1862 || CHECK(string, '~') || CHECK(string, '*')
1863 || CHECK(string, '\'') || CHECK(string, '(')
1864 || CHECK(string, ')') || CHECK(string, '[')
1865 || CHECK(string, ']')) {
1866 if (!WRITE(emitter, string)) return 0;
1869 int width = WIDTH(string);
1872 value = *(string.pointer++);
1873 if (!PUT(emitter, '%')) return 0;
1874 if (!PUT(emitter, (value >> 4)
1875 + ((value >> 4) < 10 ? '0' : 'A' - 10)))
1877 if (!PUT(emitter, (value & 0x0F)
1878 + ((value & 0x0F) < 10 ? '0' : 'A' - 10)))
1884 emitter->whitespace = 0;
1885 emitter->indention = 0;
1891 yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter,
1892 yaml_char_t *value, size_t length, int allow_breaks)
1894 yaml_string_t string;
1898 STRING_ASSIGN(string, value, length);
1900 if (!emitter->whitespace) {
1901 if (!PUT(emitter, ' ')) return 0;
1904 while (string.pointer != string.end)
1906 if (IS_SPACE(string))
1908 if (allow_breaks && !spaces
1909 && emitter->column > emitter->best_width
1910 && !IS_SPACE_AT(string, 1)) {
1911 if (!yaml_emitter_write_indent(emitter)) return 0;
1915 if (!WRITE(emitter, string)) return 0;
1919 else if (IS_BREAK(string))
1921 if (!breaks && CHECK(string, '\n')) {
1922 if (!PUT_BREAK(emitter)) return 0;
1924 if (!WRITE_BREAK(emitter, string)) return 0;
1925 emitter->indention = 1;
1931 if (!yaml_emitter_write_indent(emitter)) return 0;
1933 if (!WRITE(emitter, string)) return 0;
1934 emitter->indention = 0;
1940 emitter->whitespace = 0;
1941 emitter->indention = 0;
1942 if (emitter->root_context)
1944 emitter->open_ended = 1;
1951 yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter,
1952 yaml_char_t *value, size_t length, int allow_breaks)
1954 yaml_string_t string;
1958 STRING_ASSIGN(string, value, length);
1960 if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0))
1963 while (string.pointer != string.end)
1965 if (IS_SPACE(string))
1967 if (allow_breaks && !spaces
1968 && emitter->column > emitter->best_width
1969 && string.pointer != string.start
1970 && string.pointer != string.end - 1
1971 && !IS_SPACE_AT(string, 1)) {
1972 if (!yaml_emitter_write_indent(emitter)) return 0;
1976 if (!WRITE(emitter, string)) return 0;
1980 else if (IS_BREAK(string))
1982 if (!breaks && CHECK(string, '\n')) {
1983 if (!PUT_BREAK(emitter)) return 0;
1985 if (!WRITE_BREAK(emitter, string)) return 0;
1986 emitter->indention = 1;
1992 if (!yaml_emitter_write_indent(emitter)) return 0;
1994 if (CHECK(string, '\'')) {
1995 if (!PUT(emitter, '\'')) return 0;
1997 if (!WRITE(emitter, string)) return 0;
1998 emitter->indention = 0;
2004 if (!yaml_emitter_write_indicator(emitter, "'", 0, 0, 0))
2007 emitter->whitespace = 0;
2008 emitter->indention = 0;
2014 yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter,
2015 yaml_char_t *value, size_t length, int allow_breaks)
2017 yaml_string_t string;
2020 STRING_ASSIGN(string, value, length);
2022 if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0))
2025 while (string.pointer != string.end)
2027 if (!IS_PRINTABLE(string) || (!emitter->unicode && !IS_ASCII(string))
2028 || IS_BOM(string) || IS_BREAK(string)
2029 || CHECK(string, '"') || CHECK(string, '\\'))
2031 unsigned char octet;
2036 octet = string.pointer[0];
2037 width = (octet & 0x80) == 0x00 ? 1 :
2038 (octet & 0xE0) == 0xC0 ? 2 :
2039 (octet & 0xF0) == 0xE0 ? 3 :
2040 (octet & 0xF8) == 0xF0 ? 4 : 0;
2041 value = (octet & 0x80) == 0x00 ? octet & 0x7F :
2042 (octet & 0xE0) == 0xC0 ? octet & 0x1F :
2043 (octet & 0xF0) == 0xE0 ? octet & 0x0F :
2044 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
2045 for (k = 1; k < (int)width; k ++) {
2046 octet = string.pointer[k];
2047 value = (value << 6) + (octet & 0x3F);
2049 string.pointer += width;
2051 if (!PUT(emitter, '\\')) return 0;
2056 if (!PUT(emitter, '0')) return 0;
2060 if (!PUT(emitter, 'a')) return 0;
2064 if (!PUT(emitter, 'b')) return 0;
2068 if (!PUT(emitter, 't')) return 0;
2072 if (!PUT(emitter, 'n')) return 0;
2076 if (!PUT(emitter, 'v')) return 0;
2080 if (!PUT(emitter, 'f')) return 0;
2084 if (!PUT(emitter, 'r')) return 0;
2088 if (!PUT(emitter, 'e')) return 0;
2092 if (!PUT(emitter, '\"')) return 0;
2096 if (!PUT(emitter, '\\')) return 0;
2100 if (!PUT(emitter, 'N')) return 0;
2104 if (!PUT(emitter, '_')) return 0;
2108 if (!PUT(emitter, 'L')) return 0;
2112 if (!PUT(emitter, 'P')) return 0;
2116 if (value <= 0xFF) {
2117 if (!PUT(emitter, 'x')) return 0;
2120 else if (value <= 0xFFFF) {
2121 if (!PUT(emitter, 'u')) return 0;
2125 if (!PUT(emitter, 'U')) return 0;
2128 for (k = (width-1)*4; k >= 0; k -= 4) {
2129 int digit = (value >> k) & 0x0F;
2130 if (!PUT(emitter, digit + (digit < 10 ? '0' : 'A'-10)))
2136 else if (IS_SPACE(string))
2138 if (allow_breaks && !spaces
2139 && emitter->column > emitter->best_width
2140 && string.pointer != string.start
2141 && string.pointer != string.end - 1) {
2142 if (!yaml_emitter_write_indent(emitter)) return 0;
2143 if (IS_SPACE_AT(string, 1)) {
2144 if (!PUT(emitter, '\\')) return 0;
2149 if (!WRITE(emitter, string)) return 0;
2155 if (!WRITE(emitter, string)) return 0;
2160 if (!yaml_emitter_write_indicator(emitter, "\"", 0, 0, 0))
2163 emitter->whitespace = 0;
2164 emitter->indention = 0;
2170 yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter,
2171 yaml_string_t string)
2173 char indent_hint[2];
2174 char *chomp_hint = NULL;
2176 if (IS_SPACE(string) || IS_BREAK(string))
2178 indent_hint[0] = '0' + (char)emitter->best_indent;
2179 indent_hint[1] = '\0';
2180 if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0))
2184 emitter->open_ended = 0;
2186 string.pointer = string.end;
2187 if (string.start == string.pointer)
2195 } while ((*string.pointer & 0xC0) == 0x80);
2196 if (!IS_BREAK(string))
2200 else if (string.start == string.pointer)
2203 emitter->open_ended = 1;
2209 } while ((*string.pointer & 0xC0) == 0x80);
2210 if (IS_BREAK(string))
2213 emitter->open_ended = 1;
2220 if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0))
2228 yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter,
2229 yaml_char_t *value, size_t length)
2231 yaml_string_t string;
2234 STRING_ASSIGN(string, value, length);
2236 if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0))
2238 if (!yaml_emitter_write_block_scalar_hints(emitter, string))
2240 if (!PUT_BREAK(emitter)) return 0;
2241 emitter->indention = 1;
2242 emitter->whitespace = 1;
2244 while (string.pointer != string.end)
2246 if (IS_BREAK(string))
2248 if (!WRITE_BREAK(emitter, string)) return 0;
2249 emitter->indention = 1;
2255 if (!yaml_emitter_write_indent(emitter)) return 0;
2257 if (!WRITE(emitter, string)) return 0;
2258 emitter->indention = 0;
2267 yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter,
2268 yaml_char_t *value, size_t length)
2270 yaml_string_t string;
2272 int leading_spaces = 1;
2274 STRING_ASSIGN(string, value, length);
2276 if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0))
2278 if (!yaml_emitter_write_block_scalar_hints(emitter, string))
2280 if (!PUT_BREAK(emitter)) return 0;
2281 emitter->indention = 1;
2282 emitter->whitespace = 1;
2284 while (string.pointer != string.end)
2286 if (IS_BREAK(string))
2288 if (!breaks && !leading_spaces && CHECK(string, '\n')) {
2290 while (IS_BREAK_AT(string, k)) {
2291 k += WIDTH_AT(string, k);
2293 if (!IS_BLANKZ_AT(string, k)) {
2294 if (!PUT_BREAK(emitter)) return 0;
2297 if (!WRITE_BREAK(emitter, string)) return 0;
2298 emitter->indention = 1;
2304 if (!yaml_emitter_write_indent(emitter)) return 0;
2305 leading_spaces = IS_BLANK(string);
2307 if (!breaks && IS_SPACE(string) && !IS_SPACE_AT(string, 1)
2308 && emitter->column > emitter->best_width) {
2309 if (!yaml_emitter_write_indent(emitter)) return 0;
2313 if (!WRITE(emitter, string)) return 0;
2315 emitter->indention = 0;