summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorxi <xi@18f92427-320e-0410-9341-c67f048884a3>2007-01-07 20:11:16 +0000
committerxi <xi@18f92427-320e-0410-9341-c67f048884a3>2007-01-07 20:11:16 +0000
commitdd329a85cdb65dde7ac4a5bc3d47ca25cbc2036a (patch)
treea99269e56362f968985551445cab0267c16d2aa9 /tests
parent07ebb0347fc9ff410b0b93c92f6ece52568d6991 (diff)
downloadlibyaml-dd329a85cdb65dde7ac4a5bc3d47ca25cbc2036a.tar.gz
Add functions for constructing, parsing and emitting YAML documents.
git-svn-id: http://svn.pyyaml.org/libyaml/trunk@238 18f92427-320e-0410-9341-c67f048884a3
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am4
-rw-r--r--tests/example-deconstructor-alt.c800
-rw-r--r--tests/example-deconstructor.c116
-rw-r--r--tests/example-reformatter-alt.c217
-rw-r--r--tests/run-dumper.c305
-rw-r--r--tests/run-emitter.c2
-rw-r--r--tests/run-loader.c59
7 files changed, 1443 insertions, 60 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bfc01d7..72e84d2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -3,4 +3,6 @@ AM_CPPFLAGS = -I$(top_srcdir)/include
LDADD = $(top_builddir)/src/libyaml.la
TESTS = test-version test-reader
check_PROGRAMS = test-version test-reader
-noinst_PROGRAMS = run-scanner run-parser run-emitter example-reformatter example-deconstructor
+noinst_PROGRAMS = run-scanner run-parser run-loader run-emitter run-dumper \
+ example-reformatter example-reformatter-alt \
+ example-deconstructor example-deconstructor-alt
diff --git a/tests/example-deconstructor-alt.c b/tests/example-deconstructor-alt.c
new file mode 100644
index 0000000..7da194a
--- /dev/null
+++ b/tests/example-deconstructor-alt.c
@@ -0,0 +1,800 @@
+
+#include <yaml.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+
+int
+main(int argc, char *argv[])
+{
+ int help = 0;
+ int canonical = 0;
+ int unicode = 0;
+ int k;
+ int done = 0;
+
+ yaml_parser_t parser;
+ yaml_emitter_t emitter;
+ yaml_event_t input_event;
+ yaml_document_t output_document;
+
+ int root;
+
+ /* Clear the objects. */
+
+ memset(&parser, 0, sizeof(parser));
+ memset(&emitter, 0, sizeof(emitter));
+ memset(&input_event, 0, sizeof(input_event));
+ memset(&output_document, 0, sizeof(output_document));
+
+ /* Analyze command line options. */
+
+ for (k = 1; k < argc; k ++)
+ {
+ if (strcmp(argv[k], "-h") == 0
+ || strcmp(argv[k], "--help") == 0) {
+ help = 1;
+ }
+
+ else if (strcmp(argv[k], "-c") == 0
+ || strcmp(argv[k], "--canonical") == 0) {
+ canonical = 1;
+ }
+
+ else if (strcmp(argv[k], "-u") == 0
+ || strcmp(argv[k], "--unicode") == 0) {
+ unicode = 1;
+ }
+
+ else {
+ fprintf(stderr, "Unrecognized option: %s\n"
+ "Try `%s --help` for more information.\n",
+ argv[k], argv[0]);
+ return 1;
+ }
+ }
+
+ /* Display the help string. */
+
+ if (help)
+ {
+ printf("%s <input\n"
+ "or\n%s -h | --help\nDeconstruct a YAML stream\n\nOptions:\n"
+ "-h, --help\t\tdisplay this help and exit\n"
+ "-c, --canonical\t\toutput in the canonical YAML format\n"
+ "-u, --unicode\t\toutput unescaped non-ASCII characters\n",
+ argv[0], argv[0]);
+ return 0;
+ }
+
+ /* Initialize the parser and emitter objects. */
+
+ if (!yaml_parser_initialize(&parser)) {
+ fprintf(stderr, "Could not initialize the parser object\n");
+ return 1;
+ }
+
+ if (!yaml_emitter_initialize(&emitter)) {
+ yaml_parser_delete(&parser);
+ fprintf(stderr, "Could not inialize the emitter object\n");
+ return 1;
+ }
+
+ /* Set the parser parameters. */
+
+ yaml_parser_set_input_file(&parser, stdin);
+
+ /* Set the emitter parameters. */
+
+ yaml_emitter_set_output_file(&emitter, stdout);
+
+ yaml_emitter_set_canonical(&emitter, canonical);
+ yaml_emitter_set_unicode(&emitter, unicode);
+
+ /* Create and emit the STREAM-START event. */
+
+ if (!yaml_emitter_open(&emitter))
+ goto emitter_error;
+
+ /* Create a output_document object. */
+
+ if (!yaml_document_initialize(&output_document, NULL, NULL, NULL, 0, 0))
+ goto document_error;
+
+ /* Create the root sequence. */
+
+ root = yaml_document_add_sequence(&output_document, NULL,
+ YAML_BLOCK_SEQUENCE_STYLE);
+ if (!root) goto document_error;
+
+ /* Loop through the input events. */
+
+ while (!done)
+ {
+ int properties, key, value, map, seq;
+
+ /* Get the next event. */
+
+ if (!yaml_parser_parse(&parser, &input_event))
+ goto parser_error;
+
+ /* Check if this is the stream end. */
+
+ if (input_event.type == YAML_STREAM_END_EVENT) {
+ done = 1;
+ }
+
+ /* Create a mapping node and attach it to the root sequence. */
+
+ properties = yaml_document_add_mapping(&output_document, NULL,
+ YAML_BLOCK_MAPPING_STYLE);
+ if (!properties) goto document_error;
+ if (!yaml_document_append_sequence_item(&output_document,
+ root, properties)) goto document_error;
+
+ /* Analyze the event. */
+
+ switch (input_event.type)
+ {
+ case YAML_STREAM_START_EVENT:
+
+ /* Add 'type': 'STREAM-START'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "STREAM-START", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Add 'encoding': <encoding>. */
+
+ if (input_event.data.stream_start.encoding)
+ {
+ yaml_encoding_t encoding
+ = input_event.data.stream_start.encoding;
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "encoding", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ (encoding == YAML_UTF8_ENCODING ? "utf-8" :
+ encoding == YAML_UTF16LE_ENCODING ? "utf-16-le" :
+ encoding == YAML_UTF16BE_ENCODING ? "utf-16-be" :
+ "unknown"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ break;
+
+ case YAML_STREAM_END_EVENT:
+
+ /* Add 'type': 'STREAM-END'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "STREAM-END", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ break;
+
+ case YAML_DOCUMENT_START_EVENT:
+
+ /* Add 'type': 'DOCUMENT-START'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "DOCUMENT-START", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Display the output_document version numbers. */
+
+ if (input_event.data.document_start.version_directive)
+ {
+ yaml_version_directive_t *version
+ = input_event.data.document_start.version_directive;
+ char number[64];
+
+ /* Add 'version': {}. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "version", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ map = yaml_document_add_mapping(&output_document, NULL,
+ YAML_FLOW_MAPPING_STYLE);
+ if (!map) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, map)) goto document_error;
+
+ /* Add 'major': <number>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "major", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ sprintf(number, "%d", version->major);
+ value = yaml_document_add_scalar(&output_document, YAML_INT_TAG,
+ number, -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ map, key, value)) goto document_error;
+
+ /* Add 'minor': <number>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "minor", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ sprintf(number, "%d", version->minor);
+ value = yaml_document_add_scalar(&output_document, YAML_INT_TAG,
+ number, -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ map, key, value)) goto document_error;
+ }
+
+ /* Display the output_document tag directives. */
+
+ if (input_event.data.document_start.tag_directives.start
+ != input_event.data.document_start.tag_directives.end)
+ {
+ yaml_tag_directive_t *tag;
+
+ /* Add 'tags': []. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "tags", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ seq = yaml_document_add_sequence(&output_document, NULL,
+ YAML_BLOCK_SEQUENCE_STYLE);
+ if (!seq) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, seq)) goto document_error;
+
+ for (tag = input_event.data.document_start.tag_directives.start;
+ tag != input_event.data.document_start.tag_directives.end;
+ tag ++)
+ {
+ /* Add {}. */
+
+ map = yaml_document_add_mapping(&output_document, NULL,
+ YAML_FLOW_MAPPING_STYLE);
+ if (!map) goto document_error;
+ if (!yaml_document_append_sequence_item(&output_document,
+ seq, map)) goto document_error;
+
+ /* Add 'handle': <handle>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "handle", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ tag->handle, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ map, key, value)) goto document_error;
+
+ /* Add 'prefix': <prefix>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "prefix", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ tag->prefix, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ map, key, value)) goto document_error;
+ }
+ }
+
+ /* Add 'implicit': <flag>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "implicit", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG,
+ (input_event.data.document_start.implicit ?
+ "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ break;
+
+ case YAML_DOCUMENT_END_EVENT:
+
+ /* Add 'type': 'DOCUMENT-END'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "DOCUMENT-END", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Add 'implicit': <flag>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "implicit", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG,
+ (input_event.data.document_end.implicit ?
+ "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ break;
+
+ case YAML_ALIAS_EVENT:
+
+ /* Add 'type': 'ALIAS'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "ALIAS", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Add 'anchor': <anchor>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "anchor", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ input_event.data.alias.anchor, -1,
+ YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ break;
+
+ case YAML_SCALAR_EVENT:
+
+ /* Add 'type': 'SCALAR'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "SCALAR", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Add 'anchor': <anchor>. */
+
+ if (input_event.data.scalar.anchor)
+ {
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "anchor", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ input_event.data.scalar.anchor, -1,
+ YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ /* Add 'tag': <tag>. */
+
+ if (input_event.data.scalar.tag)
+ {
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "tag", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ input_event.data.scalar.tag, -1,
+ YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ /* Add 'value': <value>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "value", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ input_event.data.scalar.value,
+ input_event.data.scalar.length,
+ YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Display if the scalar tag is implicit. */
+
+ /* Add 'implicit': {} */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "version", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ map = yaml_document_add_mapping(&output_document, NULL,
+ YAML_FLOW_MAPPING_STYLE);
+ if (!map) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, map)) goto document_error;
+
+ /* Add 'plain': <flag>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "plain", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG,
+ (input_event.data.scalar.plain_implicit ?
+ "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ map, key, value)) goto document_error;
+
+ /* Add 'quoted': <flag>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "quoted", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG,
+ (input_event.data.scalar.quoted_implicit ?
+ "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ map, key, value)) goto document_error;
+
+ /* Display the style information. */
+
+ if (input_event.data.scalar.style)
+ {
+ yaml_scalar_style_t style = input_event.data.scalar.style;
+
+ /* Add 'style': <style>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "style", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ (style == YAML_PLAIN_SCALAR_STYLE ? "plain" :
+ style == YAML_SINGLE_QUOTED_SCALAR_STYLE ?
+ "single-quoted" :
+ style == YAML_DOUBLE_QUOTED_SCALAR_STYLE ?
+ "double-quoted" :
+ style == YAML_LITERAL_SCALAR_STYLE ? "literal" :
+ style == YAML_FOLDED_SCALAR_STYLE ? "folded" :
+ "unknown"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ break;
+
+ case YAML_SEQUENCE_START_EVENT:
+
+ /* Add 'type': 'SEQUENCE-START'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "SEQUENCE-START", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Add 'anchor': <anchor>. */
+
+ if (input_event.data.sequence_start.anchor)
+ {
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "anchor", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ input_event.data.sequence_start.anchor, -1,
+ YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ /* Add 'tag': <tag>. */
+
+ if (input_event.data.sequence_start.tag)
+ {
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "tag", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ input_event.data.sequence_start.tag, -1,
+ YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ /* Add 'implicit': <flag>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "implicit", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG,
+ (input_event.data.sequence_start.implicit ?
+ "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Display the style information. */
+
+ if (input_event.data.sequence_start.style)
+ {
+ yaml_sequence_style_t style
+ = input_event.data.sequence_start.style;
+
+ /* Add 'style': <style>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "style", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ (style == YAML_BLOCK_SEQUENCE_STYLE ? "block" :
+ style == YAML_FLOW_SEQUENCE_STYLE ? "flow" :
+ "unknown"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ break;
+
+ case YAML_SEQUENCE_END_EVENT:
+
+ /* Add 'type': 'SEQUENCE-END'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "SEQUENCE-END", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ break;
+
+ case YAML_MAPPING_START_EVENT:
+
+ /* Add 'type': 'MAPPING-START'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "MAPPING-START", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Add 'anchor': <anchor>. */
+
+ if (input_event.data.mapping_start.anchor)
+ {
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "anchor", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ input_event.data.mapping_start.anchor, -1,
+ YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ /* Add 'tag': <tag>. */
+
+ if (input_event.data.mapping_start.tag)
+ {
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "tag", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ input_event.data.mapping_start.tag, -1,
+ YAML_DOUBLE_QUOTED_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ /* Add 'implicit': <flag>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "implicit", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG,
+ (input_event.data.mapping_start.implicit ?
+ "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ /* Display the style information. */
+
+ if (input_event.data.sequence_start.style)
+ {
+ yaml_sequence_style_t style
+ = input_event.data.mapping_start.style;
+
+ /* Add 'style': <style>. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "style", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ (style == YAML_BLOCK_MAPPING_STYLE ? "block" :
+ style == YAML_FLOW_MAPPING_STYLE ? "flow" :
+ "unknown"), -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+ }
+
+ break;
+
+ case YAML_MAPPING_END_EVENT:
+
+ /* Add 'type': 'MAPPING-END'. */
+
+ key = yaml_document_add_scalar(&output_document, NULL,
+ "type", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!key) goto document_error;
+ value = yaml_document_add_scalar(&output_document, NULL,
+ "MAPPING-END", -1, YAML_PLAIN_SCALAR_STYLE);
+ if (!value) goto document_error;
+ if (!yaml_document_append_mapping_pair(&output_document,
+ properties, key, value)) goto document_error;
+
+ break;
+
+ default:
+ /* It couldn't really happen. */
+ break;
+ }
+
+ /* Delete the event object. */
+
+ yaml_event_delete(&input_event);
+ }
+
+ if (!yaml_emitter_dump(&emitter, &output_document))
+ goto emitter_error;
+ if (!yaml_emitter_close(&emitter))
+ goto emitter_error;
+
+ yaml_parser_delete(&parser);
+ yaml_emitter_delete(&emitter);
+
+ return 0;
+
+parser_error:
+
+ /* Display a parser error message. */
+
+ switch (parser.error)
+ {
+ case YAML_MEMORY_ERROR:
+ fprintf(stderr, "Memory error: Not enough memory for parsing\n");
+ break;
+
+ case YAML_READER_ERROR:
+ if (parser.problem_value != -1) {
+ fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem,
+ parser.problem_value, parser.problem_offset);
+ }
+ else {
+ fprintf(stderr, "Reader error: %s at %d\n", parser.problem,
+ parser.problem_offset);
+ }
+ break;
+
+ case YAML_SCANNER_ERROR:
+ if (parser.context) {
+ fprintf(stderr, "Scanner error: %s at line %d, column %d\n"
+ "%s at line %d, column %d\n", parser.context,
+ parser.context_mark.line+1, parser.context_mark.column+1,
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ else {
+ fprintf(stderr, "Scanner error: %s at line %d, column %d\n",
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ break;
+
+ case YAML_PARSER_ERROR:
+ if (parser.context) {
+ fprintf(stderr, "Parser error: %s at line %d, column %d\n"
+ "%s at line %d, column %d\n", parser.context,
+ parser.context_mark.line+1, parser.context_mark.column+1,
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ else {
+ fprintf(stderr, "Parser error: %s at line %d, column %d\n",
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ break;
+
+ default:
+ /* Couldn't happen. */
+ fprintf(stderr, "Internal error\n");
+ break;
+ }
+
+ yaml_event_delete(&input_event);
+ yaml_document_delete(&output_document);
+ yaml_parser_delete(&parser);
+ yaml_emitter_delete(&emitter);
+
+ return 1;
+
+emitter_error:
+
+ /* Display an emitter error message. */
+
+ switch (emitter.error)
+ {
+ case YAML_MEMORY_ERROR:
+ fprintf(stderr, "Memory error: Not enough memory for emitting\n");
+ break;
+
+ case YAML_WRITER_ERROR:
+ fprintf(stderr, "Writer error: %s\n", emitter.problem);
+ break;
+
+ case YAML_EMITTER_ERROR:
+ fprintf(stderr, "Emitter error: %s\n", emitter.problem);
+ break;
+
+ default:
+ /* Couldn't happen. */
+ fprintf(stderr, "Internal error\n");
+ break;
+ }
+
+ yaml_event_delete(&input_event);
+ yaml_document_delete(&output_document);
+ yaml_parser_delete(&parser);
+ yaml_emitter_delete(&emitter);
+
+ return 1;
+
+document_error:
+
+ fprintf(stderr, "Memory error: Not enough memory for creating a document\n");
+
+ yaml_event_delete(&input_event);
+ yaml_document_delete(&output_document);
+ yaml_parser_delete(&parser);
+ yaml_emitter_delete(&emitter);
+
+ return 1;
+}
+
diff --git a/tests/example-deconstructor.c b/tests/example-deconstructor.c
index 57e6693..fec7d59 100644
--- a/tests/example-deconstructor.c
+++ b/tests/example-deconstructor.c
@@ -336,67 +336,67 @@ main(int argc, char *argv[])
tag != input_event.data.document_start.tag_directives.end;
tag ++)
{
- /* Write '{'. */
-
- if (!yaml_mapping_start_event_initialize(&output_event,
- NULL, "tag:yaml.org,2002:map", 1,
- YAML_FLOW_MAPPING_STYLE))
- goto event_error;
- if (!yaml_emitter_emit(&emitter, &output_event))
- goto emitter_error;
-
- /* Write 'handle'. */
-
- if (!yaml_scalar_event_initialize(&output_event,
- NULL, "tag:yaml.org,2002:str", "handle", -1,
- 1, 1, YAML_PLAIN_SCALAR_STYLE))
- goto event_error;
- if (!yaml_emitter_emit(&emitter, &output_event))
- goto emitter_error;
-
- /* Write the tag directive handle. */
-
- if (!yaml_scalar_event_initialize(&output_event,
- NULL, "tag:yaml.org,2002:str",
- tag->handle, -1,
- 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
- goto event_error;
- if (!yaml_emitter_emit(&emitter, &output_event))
- goto emitter_error;
-
- /* Write 'prefix'. */
-
- if (!yaml_scalar_event_initialize(&output_event,
- NULL, "tag:yaml.org,2002:str", "prefix", -1,
- 1, 1, YAML_PLAIN_SCALAR_STYLE))
- goto event_error;
- if (!yaml_emitter_emit(&emitter, &output_event))
- goto emitter_error;
-
- /* Write the tag directive prefix. */
-
- if (!yaml_scalar_event_initialize(&output_event,
- NULL, "tag:yaml.org,2002:str",
- tag->prefix, -1,
- 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
- goto event_error;
- if (!yaml_emitter_emit(&emitter, &output_event))
- goto emitter_error;
-
- /* Write '}'. */
-
- if (!yaml_mapping_end_event_initialize(&output_event))
- goto event_error;
- if (!yaml_emitter_emit(&emitter, &output_event))
- goto emitter_error;
- }
-
- /* End a block sequence. */
-
- if (!yaml_sequence_end_event_initialize(&output_event))
+ /* Write '{'. */
+
+ if (!yaml_mapping_start_event_initialize(&output_event,
+ NULL, "tag:yaml.org,2002:map", 1,
+ YAML_FLOW_MAPPING_STYLE))
+ goto event_error;
+ if (!yaml_emitter_emit(&emitter, &output_event))
+ goto emitter_error;
+
+ /* Write 'handle'. */
+
+ if (!yaml_scalar_event_initialize(&output_event,
+ NULL, "tag:yaml.org,2002:str", "handle", -1,
+ 1, 1, YAML_PLAIN_SCALAR_STYLE))
+ goto event_error;
+ if (!yaml_emitter_emit(&emitter, &output_event))
+ goto emitter_error;
+
+ /* Write the tag directive handle. */
+
+ if (!yaml_scalar_event_initialize(&output_event,
+ NULL, "tag:yaml.org,2002:str",
+ tag->handle, -1,
+ 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
+ goto event_error;
+ if (!yaml_emitter_emit(&emitter, &output_event))
+ goto emitter_error;
+
+ /* Write 'prefix'. */
+
+ if (!yaml_scalar_event_initialize(&output_event,
+ NULL, "tag:yaml.org,2002:str", "prefix", -1,
+ 1, 1, YAML_PLAIN_SCALAR_STYLE))
goto event_error;
if (!yaml_emitter_emit(&emitter, &output_event))
goto emitter_error;
+
+ /* Write the tag directive prefix. */
+
+ if (!yaml_scalar_event_initialize(&output_event,
+ NULL, "tag:yaml.org,2002:str",
+ tag->prefix, -1,
+ 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
+ goto event_error;
+ if (!yaml_emitter_emit(&emitter, &output_event))
+ goto emitter_error;
+
+ /* Write '}'. */
+
+ if (!yaml_mapping_end_event_initialize(&output_event))
+ goto event_error;
+ if (!yaml_emitter_emit(&emitter, &output_event))
+ goto emitter_error;
+ }
+
+ /* End a block sequence. */
+
+ if (!yaml_sequence_end_event_initialize(&output_event))
+ goto event_error;
+ if (!yaml_emitter_emit(&emitter, &output_event))
+ goto emitter_error;
}
/* Write 'implicit'. */
diff --git a/tests/example-reformatter-alt.c b/tests/example-reformatter-alt.c
new file mode 100644
index 0000000..550e06c
--- /dev/null
+++ b/tests/example-reformatter-alt.c
@@ -0,0 +1,217 @@
+
+#include <yaml.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+
+int
+main(int argc, char *argv[])
+{
+ int help = 0;
+ int canonical = 0;
+ int unicode = 0;
+ int k;
+ int done = 0;
+
+ yaml_parser_t parser;
+ yaml_emitter_t emitter;
+ yaml_document_t document;
+
+ /* Clear the objects. */
+
+ memset(&parser, 0, sizeof(parser));
+ memset(&emitter, 0, sizeof(emitter));
+ memset(&document, 0, sizeof(document));
+
+ /* Analyze command line options. */
+
+ for (k = 1; k < argc; k ++)
+ {
+ if (strcmp(argv[k], "-h") == 0
+ || strcmp(argv[k], "--help") == 0) {
+ help = 1;
+ }
+
+ else if (strcmp(argv[k], "-c") == 0
+ || strcmp(argv[k], "--canonical") == 0) {
+ canonical = 1;
+ }
+
+ else if (strcmp(argv[k], "-u") == 0
+ || strcmp(argv[k], "--unicode") == 0) {
+ unicode = 1;
+ }
+
+ else {
+ fprintf(stderr, "Unrecognized option: %s\n"
+ "Try `%s --help` for more information.\n",
+ argv[k], argv[0]);
+ return 1;
+ }
+ }
+
+ /* Display the help string. */
+
+ if (help)
+ {
+ printf("%s [--canonical] [--unicode] <input >output\n"
+ "or\n%s -h | --help\nReformat a YAML stream\n\nOptions:\n"
+ "-h, --help\t\tdisplay this help and exit\n"
+ "-c, --canonical\t\toutput in the canonical YAML format\n"
+ "-u, --unicode\t\toutput unescaped non-ASCII characters\n",
+ argv[0], argv[0]);
+ return 0;
+ }
+
+ /* Initialize the parser and emitter objects. */
+
+ if (!yaml_parser_initialize(&parser))
+ goto parser_error;
+
+ if (!yaml_emitter_initialize(&emitter))
+ goto emitter_error;
+
+ /* Set the parser parameters. */
+
+ yaml_parser_set_input_file(&parser, stdin);
+
+ /* Set the emitter parameters. */
+
+ yaml_emitter_set_output_file(&emitter, stdout);
+
+ yaml_emitter_set_canonical(&emitter, canonical);
+ yaml_emitter_set_unicode(&emitter, unicode);
+
+ /* The main loop. */
+
+ while (!done)
+ {
+ /* Get the next event. */
+
+ if (!yaml_parser_load(&parser, &document))
+ goto parser_error;
+
+ /* Check if this is the stream end. */
+
+ if (!yaml_document_get_root_node(&document)) {
+ done = 1;
+ }
+
+ /* Emit the event. */
+
+ if (!yaml_emitter_dump(&emitter, &document))
+ goto emitter_error;
+ }
+
+ yaml_parser_delete(&parser);
+ yaml_emitter_delete(&emitter);
+
+ return 0;
+
+parser_error:
+
+ /* Display a parser error message. */
+
+ switch (parser.error)
+ {
+ case YAML_MEMORY_ERROR:
+ fprintf(stderr, "Memory error: Not enough memory for parsing\n");
+ break;
+
+ case YAML_READER_ERROR:
+ if (parser.problem_value != -1) {
+ fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem,
+ parser.problem_value, parser.problem_offset);
+ }
+ else {
+ fprintf(stderr, "Reader error: %s at %d\n", parser.problem,
+ parser.problem_offset);
+ }
+ break;
+
+ case YAML_SCANNER_ERROR:
+ if (parser.context) {
+ fprintf(stderr, "Scanner error: %s at line %d, column %d\n"
+ "%s at line %d, column %d\n", parser.context,
+ parser.context_mark.line+1, parser.context_mark.column+1,
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ else {
+ fprintf(stderr, "Scanner error: %s at line %d, column %d\n",
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ break;
+
+ case YAML_PARSER_ERROR:
+ if (parser.context) {
+ fprintf(stderr, "Parser error: %s at line %d, column %d\n"
+ "%s at line %d, column %d\n", parser.context,
+ parser.context_mark.line+1, parser.context_mark.column+1,
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ else {
+ fprintf(stderr, "Parser error: %s at line %d, column %d\n",
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ break;
+
+ case YAML_COMPOSER_ERROR:
+ if (parser.context) {
+ fprintf(stderr, "Composer error: %s at line %d, column %d\n"
+ "%s at line %d, column %d\n", parser.context,
+ parser.context_mark.line+1, parser.context_mark.column+1,
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ else {
+ fprintf(stderr, "Composer error: %s at line %d, column %d\n",
+ parser.problem, parser.problem_mark.line+1,
+ parser.problem_mark.column+1);
+ }
+ break;
+
+ default:
+ /* Couldn't happen. */
+ fprintf(stderr, "Internal error\n");
+ break;
+ }
+
+ yaml_parser_delete(&parser);
+ yaml_emitter_delete(&emitter);
+
+ return 1;
+
+emitter_error:
+
+ /* Display an emitter error message. */
+
+ switch (emitter.error)
+ {
+ case YAML_MEMORY_ERROR:
+ fprintf(stderr, "Memory error: Not enough memory for emitting\n");
+ break;
+
+ case YAML_WRITER_ERROR:
+ fprintf(stderr, "Writer error: %s\n", emitter.problem);
+ break;
+
+ case YAML_EMITTER_ERROR:
+ fprintf(stderr, "Emitter error: %s\n", emitter.problem);
+ break;
+
+ default:
+ /* Couldn't happen. */
+ fprintf(stderr, "Internal error\n");
+ break;
+ }
+
+ yaml_parser_delete(&parser);
+ yaml_emitter_delete(&emitter);
+
+ return 1;
+}
+
diff --git a/tests/run-dumper.c b/tests/run-dumper.c
new file mode 100644
index 0000000..149dde0
--- /dev/null
+++ b/tests/run-dumper.c
@@ -0,0 +1,305 @@
+#include <yaml.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+
+#define BUFFER_SIZE 65536
+#define MAX_DOCUMENTS 16
+
+int copy_document(yaml_document_t *document_to, yaml_document_t *document_from)
+{
+ yaml_node_t *node;
+ yaml_node_item_t *item;
+ yaml_node_pair_t *pair;
+
+ if (!yaml_document_initialize(document_to, document_from->version_directive,
+ document_from->tag_directives.start,
+ document_from->tag_directives.end,
+ document_from->start_implicit, document_from->end_implicit))
+ return 0;
+
+ for (node = document_from->nodes.start;
+ node < document_from->nodes.top; node ++) {
+ switch (node->type) {
+ case YAML_SCALAR_NODE:
+ if (!yaml_document_add_scalar(document_to, node->tag,
+ node->data.scalar.value, node->data.scalar.length,
+ node->data.scalar.style)) goto error;
+ break;
+ case YAML_SEQUENCE_NODE:
+ if (!yaml_document_add_sequence(document_to, node->tag,
+ node->data.sequence.style)) goto error;
+ break;
+ case YAML_MAPPING_NODE:
+ if (!yaml_document_add_mapping(document_to, node->tag,
+ node->data.mapping.style)) goto error;
+ break;
+ default:
+ assert(0);
+ break;
+ }
+ }
+
+ for (node = document_from->nodes.start;
+ node < document_from->nodes.top; node ++) {
+ switch (node->type) {
+ case YAML_SEQUENCE_NODE:
+ for (item = node->data.sequence.items.start;
+ item < node->data.sequence.items.top; item ++) {
+ if (!yaml_document_append_sequence_item(document_to,
+ node - document_from->nodes.start + 1,
+ *item)) goto error;
+ }
+ break;
+ case YAML_MAPPING_NODE:
+ for (pair = node->data.mapping.pairs.start;
+ pair < node->data.mapping.pairs.top; pair ++) {
+ if (!yaml_document_append_mapping_pair(document_to,
+ node - document_from->nodes.start + 1,
+ pair->key, pair->value)) goto error;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ return 1;
+
+error:
+ yaml_document_delete(document_to);
+ return 0;
+}
+
+int compare_nodes(yaml_document_t *document1, int index1,
+ yaml_document_t *document2, int index2)
+{
+ yaml_node_t *node1 = yaml_document_get_node(document1, index1);
+ yaml_node_t *node2 = yaml_document_get_node(document2, index2);
+ int k;
+
+ assert(node1);
+ assert(node2);
+
+ if (node1->type != node2->type)
+ return 0;
+
+ if (strcmp((char *)node1->tag, (char *)node2->tag) != 0) return 0;
+
+ switch (node1->type) {
+ case YAML_SCALAR_NODE:
+ if (node1->data.scalar.length != node2->data.scalar.length)
+ return 0;
+ if (strncmp((char *)node1->data.scalar.value, (char *)node2->data.scalar.value,
+ node1->data.scalar.length) != 0) return 0;
+ break;
+ case YAML_SEQUENCE_NODE:
+ if ((node1->data.sequence.items.top - node1->data.sequence.items.start) !=
+ (node2->data.sequence.items.top - node2->data.sequence.items.start))
+ return 0;
+ for (k = 0; k < (node1->data.sequence.items.top - node1->data.sequence.items.start); k ++) {
+ if (!compare_nodes(document1, node1->data.sequence.items.start[k],
+ document2, node2->data.sequence.items.start[k])) return 0;
+ }
+ break;
+ case YAML_MAPPING_NODE:
+ if ((node1->data.mapping.pairs.top - node1->data.mapping.pairs.start) !=
+ (node2->data.mapping.pairs.top - node2->data.mapping.pairs.start))
+ return 0;
+ for (k = 0; k < (node1->data.mapping.pairs.top - node1->data.mapping.pairs.start); k ++) {
+ if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].key,
+ document2, node2->data.mapping.pairs.start[k].key)) return 0;
+ if (!compare_nodes(document1, node1->data.mapping.pairs.start[k].value,
+ document2, node2->data.mapping.pairs.start[k].value)) return 0;
+ }
+ break;
+
+ }
+ return 1;
+}
+
+int compare_documents(yaml_document_t *document1, yaml_document_t *document2)
+{
+ int k;
+
+ if ((document1->version_directive && !document2->version_directive)
+ || (!document1->version_directive && document2->version_directive)
+ || (document1->version_directive && document2->version_directive
+ && (document1->version_directive->major != document2->version_directive->major
+ || document1->version_directive->minor != document2->version_directive->minor)))
+ return 0;
+
+ if ((document1->tag_directives.end - document1->tag_directives.start) !=
+ (document2->tag_directives.end - document2->tag_directives.start))
+ return 0;
+ for (k = 0; k < (document1->tag_directives.end - document1->tag_directives.start); k ++) {
+ if ((strcmp((char *)document1->tag_directives.start[k].handle,
+ (char *)document2->tag_directives.start[k].handle) != 0)
+ || (strcmp((char *)document1->tag_directives.start[k].prefix,
+ (char *)document2->tag_directives.start[k].prefix) != 0))
+ return 0;
+ }
+
+ if ((document1->nodes.top - document1->nodes.start) !=
+ (document2->nodes.top - document2->nodes.start))
+ return 0;
+
+ if (document1->nodes.top != document1->nodes.start) {
+ if (!compare_nodes(document1, 1, document2, 1))
+ return 0;
+ }
+
+ return 1;
+}
+
+int print_output(char *name, unsigned char *buffer, size_t size, int count)
+{
+ FILE *file;
+ char data[BUFFER_SIZE];
+ size_t data_size = 1;
+ size_t total_size = 0;
+ if (count >= 0) {
+ printf("FAILED (at the document #%d)\nSOURCE:\n", count+1);
+ }
+ file = fopen(name, "rb");
+ assert(file);
+ while (data_size > 0) {
+ data_size = fread(data, 1, BUFFER_SIZE, file);
+ assert(!ferror(file));
+ if (!data_size) break;
+ assert(fwrite(data, 1, data_size, stdout) == data_size);
+ total_size += data_size;
+ if (feof(file)) break;
+ }
+ fclose(file);
+ printf("#### (length: %d)\n", total_size);
+ printf("OUTPUT:\n%s#### (length: %d)\n", buffer, size);
+ return 0;
+}
+
+int
+main(int argc, char *argv[])
+{
+ int number;
+ int canonical = 0;
+ int unicode = 0;
+
+ number = 1;
+ while (number < argc) {
+ if (strcmp(argv[number], "-c") == 0) {
+ canonical = 1;
+ }
+ else if (strcmp(argv[number], "-u") == 0) {
+ unicode = 1;
+ }
+ else if (argv[number][0] == '-') {
+ printf("Unknown option: '%s'\n", argv[number]);
+ return 0;
+ }
+ if (argv[number][0] == '-') {
+ if (number < argc-1) {
+ memmove(argv+number, argv+number+1, (argc-number-1)*sizeof(char *));
+ }
+ argc --;
+ }
+ else {
+ number ++;
+ }
+ }
+
+ if (argc < 2) {
+ printf("Usage: %s [-c] [-u] file1.yaml ...\n", argv[0]);
+ return 0;
+ }
+
+ for (number = 1; number < argc; number ++)
+ {
+ FILE *file;
+ yaml_parser_t parser;
+ yaml_emitter_t emitter;
+
+ yaml_document_t document;
+ unsigned char buffer[BUFFER_SIZE];
+ size_t written = 0;
+ yaml_document_t documents[MAX_DOCUMENTS];
+ size_t document_number = 0;
+ int done = 0;
+ int count = 0;
+ int error = 0;
+ int k;
+ memset(buffer, 0, BUFFER_SIZE);
+ memset(documents, 0, MAX_DOCUMENTS*sizeof(yaml_document_t));
+
+ printf("[%d] Loading, dumping, and loading again '%s': ", number, argv[number]);
+ fflush(stdout);
+
+ file = fopen(argv[number], "rb");
+ assert(file);
+
+ assert(yaml_parser_initialize(&parser));
+ yaml_parser_set_input_file(&parser, file);
+ assert(yaml_emitter_initialize(&emitter));
+ if (canonical) {
+ yaml_emitter_set_canonical(&emitter, 1);
+ }
+ if (unicode) {
+ yaml_emitter_set_unicode(&emitter, 1);
+ }
+ yaml_emitter_set_output_string(&emitter, buffer, BUFFER_SIZE, &written);
+ yaml_emitter_open(&emitter);
+
+ while (!done)
+ {
+ if (!yaml_parser_load(&parser, &document)) {
+ error = 1;
+ break;
+ }
+
+ done = (!yaml_document_get_root_node(&document));
+ if (!done) {
+ assert(document_number < MAX_DOCUMENTS);
+ assert(copy_document(&(documents[document_number++]), &document));
+ assert(yaml_emitter_dump(&emitter, &document) ||
+ (yaml_emitter_flush(&emitter) && print_output(argv[number], buffer, written, count)));
+ count ++;
+ }
+ else {
+ yaml_document_delete(&document);
+ }
+ }
+
+ yaml_parser_delete(&parser);
+ assert(!fclose(file));
+ yaml_emitter_close(&emitter);
+ yaml_emitter_delete(&emitter);
+
+ if (!error)
+ {
+ count = done = 0;
+ assert(yaml_parser_initialize(&parser));
+ yaml_parser_set_input_string(&parser, buffer, written);
+
+ while (!done)
+ {
+ assert(yaml_parser_load(&parser, &document) || print_output(argv[number], buffer, written, count));
+ done = (!yaml_document_get_root_node(&document));
+ if (!done) {
+ assert(compare_documents(documents+count, &document) || print_output(argv[number], buffer, written, count));
+ count ++;
+ }
+ yaml_document_delete(&document);
+ }
+ yaml_parser_delete(&parser);
+ }
+
+ for (k = 0; k < document_number; k ++) {
+ yaml_document_delete(documents+k);
+ }
+
+ printf("PASSED (length: %d)\n", written);
+ print_output(argv[number], buffer, written, -1);
+ }
+
+ return 0;
+}
diff --git a/tests/run-emitter.c b/tests/run-emitter.c
index 3008ab5..0787895 100644
--- a/tests/run-emitter.c
+++ b/tests/run-emitter.c
@@ -256,7 +256,7 @@ main(int argc, char *argv[])
int error = 0;
int k;
memset(buffer, 0, BUFFER_SIZE);
- memset(events, 0, MAX_EVENTS);
+ memset(events, 0, MAX_EVENTS*sizeof(yaml_event_t));
printf("[%d] Parsing, emitting, and parsing again '%s': ", number, argv[number]);
fflush(stdout);
diff --git a/tests/run-loader.c b/tests/run-loader.c
new file mode 100644
index 0000000..a34ad07
--- /dev/null
+++ b/tests/run-loader.c
@@ -0,0 +1,59 @@
+#include <yaml.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+
+int
+main(int argc, char *argv[])
+{
+ int number;
+
+ if (argc < 2) {
+ printf("Usage: %s file1.yaml ...\n", argv[0]);
+ return 0;
+ }
+
+ for (number = 1; number < argc; number ++)
+ {
+ FILE *file;
+ yaml_parser_t parser;
+ yaml_document_t document;
+ int done = 0;
+ int count = 0;
+ int error = 0;
+
+ printf("[%d] Loading '%s': ", number, argv[number]);
+ fflush(stdout);
+
+ file = fopen(argv[number], "rb");
+ assert(file);
+
+ assert(yaml_parser_initialize(&parser));
+
+ yaml_parser_set_input_file(&parser, file);
+
+ while (!done)
+ {
+ if (!yaml_parser_load(&parser, &document)) {
+ error = 1;
+ break;
+ }
+
+ done = (!yaml_document_get_root_node(&document));
+
+ yaml_document_delete(&document);
+
+ if (!done) count ++;
+ }
+
+ yaml_parser_delete(&parser);
+
+ assert(!fclose(file));
+
+ printf("%s (%d documents)\n", (error ? "FAILURE" : "SUCCESS"), count);
+ }
+
+ return 0;
+}
+