diff options
author | Tina Müller <cpan2@tinita.de> | 2020-04-17 21:36:49 +0200 |
---|---|---|
committer | Tina Müller (tinita) <cpan2@tinita.de> | 2020-04-19 12:47:41 +0200 |
commit | 4e5cea6419cc7dc2f7816c239f1e989a9239b4fb (patch) | |
tree | 6329e0daa79befcbc380f96e377ba3c2c2934c9e | |
parent | 47bf3f086fbea5c02b30f99dcf2e56984af1a641 (diff) | |
download | libyaml-git-4e5cea6419cc7dc2f7816c239f1e989a9239b4fb.tar.gz |
Allow emitting 1.2 directive
Before `1.1` was hardcoded in the emitter.
* Also add --directive option to run-emitter-test-suite
Allows to easily test how output looks like if %YAML directives
are output.
-rw-r--r-- | src/emitter.c | 10 | ||||
-rw-r--r-- | tests/run-emitter-test-suite.c | 50 |
2 files changed, 50 insertions, 10 deletions
diff --git a/src/emitter.c b/src/emitter.c index a22c6b1..2c2e42a 100644 --- a/src/emitter.c +++ b/src/emitter.c @@ -603,8 +603,14 @@ yaml_emitter_emit_document_start(yaml_emitter_t *emitter, implicit = 0; if (!yaml_emitter_write_indicator(emitter, "%YAML", 1, 0, 0)) return 0; - if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0)) - return 0; + if (event->data.document_start.version_directive->minor == 1) { + if (!yaml_emitter_write_indicator(emitter, "1.1", 1, 0, 0)) + return 0; + } + else { + if (!yaml_emitter_write_indicator(emitter, "1.2", 1, 0, 0)) + return 0; + } if (!yaml_emitter_write_indent(emitter)) return 0; } diff --git a/tests/run-emitter-test-suite.c b/tests/run-emitter-test-suite.c index f24bb09..9730b2b 100644 --- a/tests/run-emitter-test-suite.c +++ b/tests/run-emitter-test-suite.c @@ -3,30 +3,58 @@ #include <stdlib.h> #include <stdio.h> #include <assert.h> +#include "../src/yaml_private.h" int get_line(FILE * input, char *line); char *get_anchor(char sigil, char *line, char *anchor); char *get_tag(char *line, char *tag); void get_value(char *line, char *value, int *style); +int usage(int ret); int main(int argc, char *argv[]) { FILE *input; yaml_emitter_t emitter; yaml_event_t event; + yaml_version_directive_t *version_directive = NULL; int canonical = 0; int unicode = 0; char line[1024]; + int foundfile = 0; + int i = 0; + int minor = 0; + + for (i = 1; i < argc; i++) { + if (strncmp(argv[i], "--help", 6) == 0) + return usage(0); + if (strncmp(argv[i], "-h", 2) == 0) + return usage(0); + if (strncmp(argv[i], "--directive", 11) == 0) { + if (i+1 == argc) + return usage(1); + i++; + if (strncmp(argv[i], "1.1", 3) == 0) + minor = 1; + else if (strncmp(argv[i], "1.2", 3) == 0) + minor = 2; + else + return usage(1); + } + else if (!foundfile) { + input = fopen(argv[i], "rb"); + foundfile = 1; + } - if (argc == 1) - input = stdin; - else if (argc == 2) - input = fopen(argv[1], "rb"); - else { - fprintf(stderr, "Usage: libyaml-emitter [<input-file>]\n"); - return 1; } + if (minor) { + version_directive = YAML_MALLOC_STATIC(yaml_version_directive_t); + version_directive->major = 1; + version_directive->minor = minor; + } + if (!foundfile) + input = stdin; + assert(input); if (!yaml_emitter_initialize(&emitter)) { @@ -37,6 +65,7 @@ int main(int argc, char *argv[]) yaml_emitter_set_canonical(&emitter, canonical); yaml_emitter_set_unicode(&emitter, unicode); + while (get_line(input, line)) { int ok; char anchor[256]; @@ -51,7 +80,7 @@ int main(int argc, char *argv[]) } else if (strncmp(line, "+DOC", 4) == 0) { implicit = strncmp(line, "+DOC ---", 8) != 0; - ok = yaml_document_start_event_initialize(&event, NULL, NULL, NULL, implicit); + ok = yaml_document_start_event_initialize(&event, version_directive, NULL, NULL, implicit); } else if (strncmp(line, "-DOC", 4) == 0) { implicit = strncmp(line, "-DOC ...", 8) != 0; @@ -229,3 +258,8 @@ void get_value(char *line, char *value, int *style) } value[i] = '\0'; } + +int usage(int ret) { + fprintf(stderr, "Usage: run-emitter-test-suite [--directive (1.1|1.2)] [<input-file>]\n"); + return ret; +} |