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.
This commit is contained in:
Tina Müller 2020-04-17 21:36:49 +02:00 committed by Tina Müller (tinita)
parent 47bf3f086f
commit 4e5cea6419
2 changed files with 50 additions and 10 deletions

View File

@ -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;
}

View File

@ -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;
}