X-Git-Url: http://andersk.mit.edu/gitweb/libyaml.git/blobdiff_plain/e27a3c886ebe97dda5b6f961b04949bb3003d4a6..e4fd0393e6e97439666527db71c1fa18482778a7:/src/emitter.c diff --git a/src/emitter.c b/src/emitter.c index 0affaab..901f07f 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -249,7 +249,7 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, yaml_char_t *value, size_t length, int allow_breaks); static int -yaml_emitter_determine_chomping(yaml_emitter_t *emitter, +yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, yaml_string_t string); static int @@ -517,7 +517,7 @@ yaml_emitter_emit_stream_start(yaml_emitter_t *emitter, if (emitter->best_width < 0) { emitter->best_width = INT_MAX; } - + if (!emitter->line_break) { emitter->line_break = YAML_LN_BREAK; } @@ -587,6 +587,17 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter, implicit = 0; } + if ((event->data.document_start.version_directive || + (event->data.document_start.tag_directives.start + != event->data.document_start.tag_directives.end)) && + emitter->open_ended) + { + if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) + return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; + } + if (event->data.document_start.version_directive) { implicit = 0; if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0)) @@ -596,7 +607,7 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter, if (!yaml_emitter_write_indent(emitter)) return 0; } - + if (event->data.document_start.tag_directives.start != event->data.document_start.tag_directives.end) { implicit = 0; @@ -638,6 +649,14 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter, else if (event->type == YAML_STREAM_END_EVENT) { + if (emitter->open_ended) + { + if (!yaml_emitter_write_indicator(emitter, "...", 1, 0, 0)) + return 0; + if (!yaml_emitter_write_indent(emitter)) + return 0; + } + if (!yaml_emitter_flush(emitter)) return 0; @@ -702,7 +721,7 @@ yaml_emitter_emit_document_end(yaml_emitter_t *emitter, } /* - * + * * Expect a flow item node. */ @@ -1135,7 +1154,7 @@ yaml_emitter_check_simple_key(yaml_emitter_t *emitter) break; case YAML_MAPPING_START_EVENT: - if (!yaml_emitter_check_empty_sequence(emitter)) + if (!yaml_emitter_check_empty_mapping(emitter)) return 0; length += emitter->anchor_data.anchor_length + emitter->tag_data.handle_length @@ -1330,10 +1349,15 @@ static int yaml_emitter_analyze_tag_directive(yaml_emitter_t *emitter, yaml_tag_directive_t tag_directive) { - yaml_string_t handle = STRING(tag_directive.handle, - strlen((char *)tag_directive.handle)); - yaml_string_t prefix = STRING(tag_directive.prefix, - strlen((char *)tag_directive.prefix)); + yaml_string_t handle; + yaml_string_t prefix; + size_t handle_length; + size_t prefix_length; + + handle_length = strlen((char *)tag_directive.handle); + prefix_length = strlen((char *)tag_directive.prefix); + STRING_ASSIGN(handle, tag_directive.handle, handle_length); + STRING_ASSIGN(prefix, tag_directive.prefix, prefix_length); if (handle.start == handle.end) { return yaml_emitter_set_emitter_error(emitter, @@ -1376,7 +1400,11 @@ static int yaml_emitter_analyze_anchor(yaml_emitter_t *emitter, yaml_char_t *anchor, int alias) { - yaml_string_t string = STRING(anchor, strlen((char *)anchor)); + size_t anchor_length; + yaml_string_t string; + + anchor_length = strlen((char *)anchor); + STRING_ASSIGN(string, anchor, anchor_length); if (string.start == string.end) { return yaml_emitter_set_emitter_error(emitter, alias ? @@ -1408,9 +1436,13 @@ static int yaml_emitter_analyze_tag(yaml_emitter_t *emitter, yaml_char_t *tag) { - yaml_string_t string = STRING(tag, strlen((char *)tag)); + size_t tag_length; + yaml_string_t string; yaml_tag_directive_t *tag_directive; + tag_length = strlen((char *)tag); + STRING_ASSIGN(string, tag, tag_length); + if (string.start == string.end) { return yaml_emitter_set_emitter_error(emitter, "tag value must not be empty"); @@ -1419,7 +1451,7 @@ yaml_emitter_analyze_tag(yaml_emitter_t *emitter, for (tag_directive = emitter->tag_directives.start; tag_directive != emitter->tag_directives.top; tag_directive ++) { size_t prefix_length = strlen((char *)tag_directive->prefix); - if (prefix_length < (string.end - string.start) + if (prefix_length < (size_t)(string.end - string.start) && strncmp((char *)tag_directive->prefix, (char *)string.start, prefix_length) == 0) { @@ -1447,28 +1479,26 @@ static int yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, yaml_char_t *value, size_t length) { - yaml_string_t string = STRING(value, length); + yaml_string_t string; int block_indicators = 0; int flow_indicators = 0; int line_breaks = 0; int special_characters = 0; - int inline_spaces = 0; - int inline_breaks = 0; - int leading_spaces = 0; - int leading_breaks = 0; - int trailing_spaces = 0; - int trailing_breaks = 0; - int inline_breaks_spaces = 0; - int mixed_breaks_spaces = 0; - - int preceeded_by_space = 0; - int followed_by_space = 0; - int spaces = 0; - int breaks = 0; - int mixed = 0; - int leading = 0; + int leading_space = 0; + int leading_break = 0; + int trailing_space = 0; + int trailing_break = 0; + int break_space = 0; + int space_break = 0; + + int preceeded_by_whitespace = 0; + int followed_by_whitespace = 0; + int previous_space = 0; + int previous_break = 0; + + STRING_ASSIGN(string, value, length); emitter->scalar_data.value = value; emitter->scalar_data.length = length; @@ -1494,8 +1524,8 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, flow_indicators = 1; } - preceeded_by_space = 1; - followed_by_space = IS_BLANKZ_AT(string, WIDTH(string)); + preceeded_by_whitespace = 1; + followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); while (string.pointer != string.end) { @@ -1515,12 +1545,12 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, if (CHECK(string, '?') || CHECK(string, ':')) { flow_indicators = 1; - if (followed_by_space) { + if (followed_by_whitespace) { block_indicators = 1; } } - if (CHECK(string, '-') && followed_by_space) { + if (CHECK(string, '-') && followed_by_whitespace) { flow_indicators = 1; block_indicators = 1; } @@ -1535,12 +1565,12 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, if (CHECK(string, ':')) { flow_indicators = 1; - if (followed_by_space) { + if (followed_by_whitespace) { block_indicators = 1; } } - if (CHECK(string, '#') && preceeded_by_space) { + if (CHECK(string, '#') && preceeded_by_whitespace) { flow_indicators = 1; block_indicators = 1; } @@ -1557,76 +1587,42 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, if (IS_SPACE(string)) { - spaces = 1; if (string.start == string.pointer) { - leading = 1; + leading_space = 1; + } + if (string.pointer+WIDTH(string) == string.end) { + trailing_space = 1; } + if (previous_break) { + break_space = 1; + } + previous_space = 1; + previous_break = 0; } - else if (IS_BREAK(string)) { - if (spaces) { - mixed = 1; - } - breaks = 1; if (string.start == string.pointer) { - leading = 1; + leading_break = 1; } - } - - else if (spaces || breaks) - { - if (leading) { - if (spaces && breaks) { - mixed_breaks_spaces = 1; - } - else if (spaces) { - leading_spaces = 1; - } - else if (breaks) { - leading_breaks = 1; - } + if (string.pointer+WIDTH(string) == string.end) { + trailing_break = 1; } - else { - if (mixed) { - mixed_breaks_spaces = 1; - } - else if (spaces && breaks) { - inline_breaks_spaces = 1; - } - else if (spaces) { - inline_spaces = 1; - } - else if (breaks) { - inline_breaks = 1; - } + if (previous_space) { + space_break = 1; } - spaces = breaks = mixed = leading = 0; + previous_space = 0; + previous_break = 1; } - - if ((spaces || breaks) && string.pointer == string.end-1) + else { - if (spaces && breaks) { - mixed_breaks_spaces = 1; - } - else if (spaces) { - if (leading) { - leading_spaces = 1; - } - trailing_spaces = 1; - } - else if (breaks) { - if (leading) { - leading_breaks = 1; - } - trailing_breaks = 1; - } + previous_space = 0; + previous_break = 0; } - preceeded_by_space = IS_BLANKZ(string); + preceeded_by_whitespace = IS_BLANKZ(string); MOVE(string); if (string.pointer != string.end) { - followed_by_space = IS_BLANKZ_AT(string, WIDTH(string)); + followed_by_whitespace = IS_BLANKZ_AT(string, WIDTH(string)); } } @@ -1637,24 +1633,22 @@ yaml_emitter_analyze_scalar(yaml_emitter_t *emitter, emitter->scalar_data.single_quoted_allowed = 1; emitter->scalar_data.block_allowed = 1; - if (leading_spaces || leading_breaks || trailing_spaces) { + if (leading_space || leading_break || trailing_space || trailing_break) { emitter->scalar_data.flow_plain_allowed = 0; emitter->scalar_data.block_plain_allowed = 0; - emitter->scalar_data.block_allowed = 0; } - if (trailing_breaks) { - emitter->scalar_data.flow_plain_allowed = 0; - emitter->scalar_data.block_plain_allowed = 0; + if (trailing_space) { + emitter->scalar_data.block_allowed = 0; } - if (inline_breaks_spaces) { + if (break_space) { emitter->scalar_data.flow_plain_allowed = 0; emitter->scalar_data.block_plain_allowed = 0; emitter->scalar_data.single_quoted_allowed = 0; } - if (mixed_breaks_spaces || special_characters) { + if (space_break || special_characters) { emitter->scalar_data.flow_plain_allowed = 0; emitter->scalar_data.block_plain_allowed = 0; emitter->scalar_data.single_quoted_allowed = 0; @@ -1793,7 +1787,11 @@ yaml_emitter_write_indicator(yaml_emitter_t *emitter, char *indicator, int need_whitespace, int is_whitespace, int is_indention) { - yaml_string_t string = STRING((yaml_char_t *)indicator, strlen(indicator)); + size_t indicator_length; + yaml_string_t string; + + indicator_length = strlen(indicator); + STRING_ASSIGN(string, (yaml_char_t *)indicator, indicator_length); if (need_whitespace && !emitter->whitespace) { if (!PUT(emitter, ' ')) return 0; @@ -1805,6 +1803,7 @@ yaml_emitter_write_indicator(yaml_emitter_t *emitter, emitter->whitespace = is_whitespace; emitter->indention = (emitter->indention && is_indention); + emitter->open_ended = 0; return 1; } @@ -1813,7 +1812,8 @@ static int yaml_emitter_write_anchor(yaml_emitter_t *emitter, yaml_char_t *value, size_t length) { - yaml_string_t string = STRING(value, length); + yaml_string_t string; + STRING_ASSIGN(string, value, length); while (string.pointer != string.end) { if (!WRITE(emitter, string)) return 0; @@ -1829,7 +1829,8 @@ static int yaml_emitter_write_tag_handle(yaml_emitter_t *emitter, yaml_char_t *value, size_t length) { - yaml_string_t string = STRING(value, length); + yaml_string_t string; + STRING_ASSIGN(string, value, length); if (!emitter->whitespace) { if (!PUT(emitter, ' ')) return 0; @@ -1850,7 +1851,8 @@ yaml_emitter_write_tag_content(yaml_emitter_t *emitter, yaml_char_t *value, size_t length, int need_whitespace) { - yaml_string_t string = STRING(value, length); + yaml_string_t string; + STRING_ASSIGN(string, value, length); if (need_whitespace && !emitter->whitespace) { if (!PUT(emitter, ' ')) return 0; @@ -1896,10 +1898,12 @@ static int yaml_emitter_write_plain_scalar(yaml_emitter_t *emitter, yaml_char_t *value, size_t length, int allow_breaks) { - yaml_string_t string = STRING(value, length); + yaml_string_t string; int spaces = 0; int breaks = 0; + STRING_ASSIGN(string, value, length); + if (!emitter->whitespace) { if (!PUT(emitter, ' ')) return 0; } @@ -1950,10 +1954,12 @@ static int yaml_emitter_write_single_quoted_scalar(yaml_emitter_t *emitter, yaml_char_t *value, size_t length, int allow_breaks) { - yaml_string_t string = STRING(value, length); + yaml_string_t string; int spaces = 0; int breaks = 0; + STRING_ASSIGN(string, value, length); + if (!yaml_emitter_write_indicator(emitter, "'", 1, 0, 0)) return 0; @@ -2011,9 +2017,11 @@ static int yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, yaml_char_t *value, size_t length, int allow_breaks) { - yaml_string_t string = STRING(value, length); + yaml_string_t string; int spaces = 0; + STRING_ASSIGN(string, value, length); + if (!yaml_emitter_write_indicator(emitter, "\"", 1, 0, 0)) return 0; @@ -2037,7 +2045,7 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, (octet & 0xE0) == 0xC0 ? octet & 0x1F : (octet & 0xF0) == 0xE0 ? octet & 0x0F : (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; - for (k = 1; k < width; k ++) { + for (k = 1; k < (int)width; k ++) { octet = string.pointer[k]; value = (value << 6) + (octet & 0x3F); } @@ -2162,41 +2170,79 @@ yaml_emitter_write_double_quoted_scalar(yaml_emitter_t *emitter, } static int -yaml_emitter_determine_chomping(yaml_emitter_t *emitter, +yaml_emitter_write_block_scalar_hints(yaml_emitter_t *emitter, yaml_string_t string) { + char indent_hint[2]; + char *chomp_hint = NULL; + + if (IS_SPACE(string) || IS_BREAK(string)) + { + indent_hint[0] = '0' + (char)emitter->best_indent; + indent_hint[1] = '\0'; + if (!yaml_emitter_write_indicator(emitter, indent_hint, 0, 0, 0)) + return 0; + } + + emitter->open_ended = 0; + string.pointer = string.end; if (string.start == string.pointer) - return -1; - do { - string.pointer --; - } while ((*string.pointer & 0xC0) == 0x80); - if (!IS_BREAK(string)) - return -1; - if (string.start == string.pointer) - return 0; - do { - string.pointer --; - } while ((*string.pointer & 0xC0) == 0x80); - if (!IS_BREAK(string)) - return 0; - return +1; - + { + chomp_hint = "-"; + } + else + { + do { + string.pointer --; + } while ((*string.pointer & 0xC0) == 0x80); + if (!IS_BREAK(string)) + { + chomp_hint = "-"; + } + else if (string.start == string.pointer) + { + chomp_hint = "+"; + emitter->open_ended = 1; + } + else + { + do { + string.pointer --; + } while ((*string.pointer & 0xC0) == 0x80); + if (IS_BREAK(string)) + { + chomp_hint = "+"; + emitter->open_ended = 1; + } + } + } + + if (chomp_hint) + { + if (!yaml_emitter_write_indicator(emitter, chomp_hint, 0, 0, 0)) + return 0; + } + + return 1; } static int yaml_emitter_write_literal_scalar(yaml_emitter_t *emitter, yaml_char_t *value, size_t length) { - yaml_string_t string = STRING(value, length); - int chomp = yaml_emitter_determine_chomping(emitter, string); - int breaks = 0; + yaml_string_t string; + int breaks = 1; - if (!yaml_emitter_write_indicator(emitter, - chomp == -1 ? "|-" : chomp == +1 ? "|+" : "|", 1, 0, 0)) + STRING_ASSIGN(string, value, length); + + if (!yaml_emitter_write_indicator(emitter, "|", 1, 0, 0)) return 0; - if (!yaml_emitter_write_indent(emitter)) + if (!yaml_emitter_write_block_scalar_hints(emitter, string)) return 0; + if (!PUT_BREAK(emitter)) return 0; + emitter->indention = 1; + emitter->whitespace = 1; while (string.pointer != string.end) { @@ -2224,16 +2270,19 @@ static int yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, yaml_char_t *value, size_t length) { - yaml_string_t string = STRING(value, length); - int chomp = yaml_emitter_determine_chomping(emitter, string); + yaml_string_t string; int breaks = 1; - int leading_spaces = 0; + int leading_spaces = 1; - if (!yaml_emitter_write_indicator(emitter, - chomp == -1 ? ">-" : chomp == +1 ? ">+" : ">", 1, 0, 0)) + STRING_ASSIGN(string, value, length); + + if (!yaml_emitter_write_indicator(emitter, ">", 1, 0, 0)) return 0; - if (!yaml_emitter_write_indent(emitter)) + if (!yaml_emitter_write_block_scalar_hints(emitter, string)) return 0; + if (!PUT_BREAK(emitter)) return 0; + emitter->indention = 1; + emitter->whitespace = 1; while (string.pointer != string.end) { @@ -2244,7 +2293,7 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, while (IS_BREAK_AT(string, k)) { k += WIDTH_AT(string, k); } - if (!IS_BLANK_AT(string, k)) { + if (!IS_BLANKZ_AT(string, k)) { if (!PUT_BREAK(emitter)) return 0; } } @@ -2273,4 +2322,3 @@ yaml_emitter_write_folded_scalar(yaml_emitter_t *emitter, return 1; } -