Path: blob/main/contrib/libyaml/tests/example-deconstructor-alt.c
39534 views
1#include <yaml.h>23#include <stdlib.h>4#include <stdio.h>56int7main(int argc, char *argv[])8{9int help = 0;10int canonical = 0;11int unicode = 0;12int k;13int done = 0;1415yaml_parser_t parser;16yaml_emitter_t emitter;17yaml_event_t input_event;18yaml_document_t output_document;1920int root;2122/* Clear the objects. */2324memset(&parser, 0, sizeof(parser));25memset(&emitter, 0, sizeof(emitter));26memset(&input_event, 0, sizeof(input_event));27memset(&output_document, 0, sizeof(output_document));2829/* Analyze command line options. */3031for (k = 1; k < argc; k ++)32{33if (strcmp(argv[k], "-h") == 034|| strcmp(argv[k], "--help") == 0) {35help = 1;36}3738else if (strcmp(argv[k], "-c") == 039|| strcmp(argv[k], "--canonical") == 0) {40canonical = 1;41}4243else if (strcmp(argv[k], "-u") == 044|| strcmp(argv[k], "--unicode") == 0) {45unicode = 1;46}4748else {49fprintf(stderr, "Unrecognized option: %s\n"50"Try `%s --help` for more information.\n",51argv[k], argv[0]);52return 1;53}54}5556/* Display the help string. */5758if (help)59{60printf("%s <input\n"61"or\n%s -h | --help\nDeconstruct a YAML stream\n\nOptions:\n"62"-h, --help\t\tdisplay this help and exit\n"63"-c, --canonical\t\toutput in the canonical YAML format\n"64"-u, --unicode\t\toutput unescaped non-ASCII characters\n",65argv[0], argv[0]);66return 0;67}6869/* Initialize the parser and emitter objects. */7071if (!yaml_parser_initialize(&parser)) {72fprintf(stderr, "Could not initialize the parser object\n");73return 1;74}7576if (!yaml_emitter_initialize(&emitter)) {77yaml_parser_delete(&parser);78fprintf(stderr, "Could not inialize the emitter object\n");79return 1;80}8182/* Set the parser parameters. */8384yaml_parser_set_input_file(&parser, stdin);8586/* Set the emitter parameters. */8788yaml_emitter_set_output_file(&emitter, stdout);8990yaml_emitter_set_canonical(&emitter, canonical);91yaml_emitter_set_unicode(&emitter, unicode);9293/* Create and emit the STREAM-START event. */9495if (!yaml_emitter_open(&emitter))96goto emitter_error;9798/* Create a output_document object. */99100if (!yaml_document_initialize(&output_document, NULL, NULL, NULL, 0, 0))101goto document_error;102103/* Create the root sequence. */104105root = yaml_document_add_sequence(&output_document, NULL,106YAML_BLOCK_SEQUENCE_STYLE);107if (!root) goto document_error;108109/* Loop through the input events. */110111while (!done)112{113int properties, key, value, map, seq;114115/* Get the next event. */116117if (!yaml_parser_parse(&parser, &input_event))118goto parser_error;119120/* Check if this is the stream end. */121122if (input_event.type == YAML_STREAM_END_EVENT) {123done = 1;124}125126/* Create a mapping node and attach it to the root sequence. */127128properties = yaml_document_add_mapping(&output_document, NULL,129YAML_BLOCK_MAPPING_STYLE);130if (!properties) goto document_error;131if (!yaml_document_append_sequence_item(&output_document,132root, properties)) goto document_error;133134/* Analyze the event. */135136switch (input_event.type)137{138case YAML_STREAM_START_EVENT:139140/* Add 'type': 'STREAM-START'. */141142key = yaml_document_add_scalar(&output_document, NULL,143(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);144if (!key) goto document_error;145value = yaml_document_add_scalar(&output_document, NULL,146(yaml_char_t *)"STREAM-START", -1, YAML_PLAIN_SCALAR_STYLE);147if (!value) goto document_error;148if (!yaml_document_append_mapping_pair(&output_document,149properties, key, value)) goto document_error;150151/* Add 'encoding': <encoding>. */152153if (input_event.data.stream_start.encoding)154{155yaml_encoding_t encoding156= input_event.data.stream_start.encoding;157158key = yaml_document_add_scalar(&output_document, NULL,159(yaml_char_t *)"encoding", -1, YAML_PLAIN_SCALAR_STYLE);160if (!key) goto document_error;161value = yaml_document_add_scalar(&output_document, NULL,162(yaml_char_t *)(encoding == YAML_UTF8_ENCODING ? "utf-8" :163encoding == YAML_UTF16LE_ENCODING ? "utf-16-le" :164encoding == YAML_UTF16BE_ENCODING ? "utf-16-be" :165"unknown"), -1, YAML_PLAIN_SCALAR_STYLE);166if (!value) goto document_error;167if (!yaml_document_append_mapping_pair(&output_document,168properties, key, value)) goto document_error;169}170171break;172173case YAML_STREAM_END_EVENT:174175/* Add 'type': 'STREAM-END'. */176177key = yaml_document_add_scalar(&output_document, NULL,178(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);179if (!key) goto document_error;180value = yaml_document_add_scalar(&output_document, NULL,181(yaml_char_t *)"STREAM-END", -1, YAML_PLAIN_SCALAR_STYLE);182if (!value) goto document_error;183if (!yaml_document_append_mapping_pair(&output_document,184properties, key, value)) goto document_error;185186break;187188case YAML_DOCUMENT_START_EVENT:189190/* Add 'type': 'DOCUMENT-START'. */191192key = yaml_document_add_scalar(&output_document, NULL,193(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);194if (!key) goto document_error;195value = yaml_document_add_scalar(&output_document, NULL,196(yaml_char_t *)"DOCUMENT-START", -1, YAML_PLAIN_SCALAR_STYLE);197if (!value) goto document_error;198if (!yaml_document_append_mapping_pair(&output_document,199properties, key, value)) goto document_error;200201/* Display the output_document version numbers. */202203if (input_event.data.document_start.version_directive)204{205yaml_version_directive_t *version206= input_event.data.document_start.version_directive;207char number[64];208209/* Add 'version': {}. */210211key = yaml_document_add_scalar(&output_document, NULL,212(yaml_char_t *)"version", -1, YAML_PLAIN_SCALAR_STYLE);213if (!key) goto document_error;214map = yaml_document_add_mapping(&output_document, NULL,215YAML_FLOW_MAPPING_STYLE);216if (!map) goto document_error;217if (!yaml_document_append_mapping_pair(&output_document,218properties, key, map)) goto document_error;219220/* Add 'major': <number>. */221222key = yaml_document_add_scalar(&output_document, NULL,223(yaml_char_t *)"major", -1, YAML_PLAIN_SCALAR_STYLE);224if (!key) goto document_error;225sprintf(number, "%d", version->major);226value = yaml_document_add_scalar(&output_document, (yaml_char_t *)YAML_INT_TAG,227(yaml_char_t *)number, -1, YAML_PLAIN_SCALAR_STYLE);228if (!value) goto document_error;229if (!yaml_document_append_mapping_pair(&output_document,230map, key, value)) goto document_error;231232/* Add 'minor': <number>. */233234key = yaml_document_add_scalar(&output_document, NULL,235(yaml_char_t *)"minor", -1, YAML_PLAIN_SCALAR_STYLE);236if (!key) goto document_error;237sprintf(number, "%d", version->minor);238value = yaml_document_add_scalar(&output_document, (yaml_char_t *)YAML_INT_TAG,239(yaml_char_t *)number, -1, YAML_PLAIN_SCALAR_STYLE);240if (!value) goto document_error;241if (!yaml_document_append_mapping_pair(&output_document,242map, key, value)) goto document_error;243}244245/* Display the output_document tag directives. */246247if (input_event.data.document_start.tag_directives.start248!= input_event.data.document_start.tag_directives.end)249{250yaml_tag_directive_t *tag;251252/* Add 'tags': []. */253254key = yaml_document_add_scalar(&output_document, NULL,255(yaml_char_t *)"tags", -1, YAML_PLAIN_SCALAR_STYLE);256if (!key) goto document_error;257seq = yaml_document_add_sequence(&output_document, NULL,258YAML_BLOCK_SEQUENCE_STYLE);259if (!seq) goto document_error;260if (!yaml_document_append_mapping_pair(&output_document,261properties, key, seq)) goto document_error;262263for (tag = input_event.data.document_start.tag_directives.start;264tag != input_event.data.document_start.tag_directives.end;265tag ++)266{267/* Add {}. */268269map = yaml_document_add_mapping(&output_document, NULL,270YAML_FLOW_MAPPING_STYLE);271if (!map) goto document_error;272if (!yaml_document_append_sequence_item(&output_document,273seq, map)) goto document_error;274275/* Add 'handle': <handle>. */276277key = yaml_document_add_scalar(&output_document, NULL,278(yaml_char_t *)"handle", -1, YAML_PLAIN_SCALAR_STYLE);279if (!key) goto document_error;280value = yaml_document_add_scalar(&output_document, NULL,281tag->handle, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE);282if (!value) goto document_error;283if (!yaml_document_append_mapping_pair(&output_document,284map, key, value)) goto document_error;285286/* Add 'prefix': <prefix>. */287288key = yaml_document_add_scalar(&output_document, NULL,289(yaml_char_t *)"prefix", -1, YAML_PLAIN_SCALAR_STYLE);290if (!key) goto document_error;291value = yaml_document_add_scalar(&output_document, NULL,292tag->prefix, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE);293if (!value) goto document_error;294if (!yaml_document_append_mapping_pair(&output_document,295map, key, value)) goto document_error;296}297}298299/* Add 'implicit': <flag>. */300301key = yaml_document_add_scalar(&output_document, NULL,302(yaml_char_t *)"implicit", -1, YAML_PLAIN_SCALAR_STYLE);303if (!key) goto document_error;304value = yaml_document_add_scalar(&output_document, (yaml_char_t *)YAML_BOOL_TAG,305(yaml_char_t *)(input_event.data.document_start.implicit ?306"true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);307if (!value) goto document_error;308if (!yaml_document_append_mapping_pair(&output_document,309properties, key, value)) goto document_error;310311break;312313case YAML_DOCUMENT_END_EVENT:314315/* Add 'type': 'DOCUMENT-END'. */316317key = yaml_document_add_scalar(&output_document, NULL,318(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);319if (!key) goto document_error;320value = yaml_document_add_scalar(&output_document, NULL,321(yaml_char_t *)"DOCUMENT-END", -1, YAML_PLAIN_SCALAR_STYLE);322if (!value) goto document_error;323if (!yaml_document_append_mapping_pair(&output_document,324properties, key, value)) goto document_error;325326/* Add 'implicit': <flag>. */327328key = yaml_document_add_scalar(&output_document, NULL,329(yaml_char_t *)"implicit", -1, YAML_PLAIN_SCALAR_STYLE);330if (!key) goto document_error;331value = yaml_document_add_scalar(&output_document, (yaml_char_t *)YAML_BOOL_TAG,332(yaml_char_t *)(input_event.data.document_end.implicit ?333"true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);334if (!value) goto document_error;335if (!yaml_document_append_mapping_pair(&output_document,336properties, key, value)) goto document_error;337338break;339340case YAML_ALIAS_EVENT:341342/* Add 'type': 'ALIAS'. */343344key = yaml_document_add_scalar(&output_document, NULL,345(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);346if (!key) goto document_error;347value = yaml_document_add_scalar(&output_document, NULL,348(yaml_char_t *)"ALIAS", -1, YAML_PLAIN_SCALAR_STYLE);349if (!value) goto document_error;350if (!yaml_document_append_mapping_pair(&output_document,351properties, key, value)) goto document_error;352353/* Add 'anchor': <anchor>. */354355key = yaml_document_add_scalar(&output_document, NULL,356(yaml_char_t *)"anchor", -1, YAML_PLAIN_SCALAR_STYLE);357if (!key) goto document_error;358value = yaml_document_add_scalar(&output_document, NULL,359input_event.data.alias.anchor, -1,360YAML_DOUBLE_QUOTED_SCALAR_STYLE);361if (!value) goto document_error;362if (!yaml_document_append_mapping_pair(&output_document,363properties, key, value)) goto document_error;364365break;366367case YAML_SCALAR_EVENT:368369/* Add 'type': 'SCALAR'. */370371key = yaml_document_add_scalar(&output_document, NULL,372(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);373if (!key) goto document_error;374value = yaml_document_add_scalar(&output_document, NULL,375(yaml_char_t *)"SCALAR", -1, YAML_PLAIN_SCALAR_STYLE);376if (!value) goto document_error;377if (!yaml_document_append_mapping_pair(&output_document,378properties, key, value)) goto document_error;379380/* Add 'anchor': <anchor>. */381382if (input_event.data.scalar.anchor)383{384key = yaml_document_add_scalar(&output_document, NULL,385(yaml_char_t *)"anchor", -1, YAML_PLAIN_SCALAR_STYLE);386if (!key) goto document_error;387value = yaml_document_add_scalar(&output_document, NULL,388input_event.data.scalar.anchor, -1,389YAML_DOUBLE_QUOTED_SCALAR_STYLE);390if (!value) goto document_error;391if (!yaml_document_append_mapping_pair(&output_document,392properties, key, value)) goto document_error;393}394395/* Add 'tag': <tag>. */396397if (input_event.data.scalar.tag)398{399key = yaml_document_add_scalar(&output_document, NULL,400(yaml_char_t *)"tag", -1, YAML_PLAIN_SCALAR_STYLE);401if (!key) goto document_error;402value = yaml_document_add_scalar(&output_document, NULL,403input_event.data.scalar.tag, -1,404YAML_DOUBLE_QUOTED_SCALAR_STYLE);405if (!value) goto document_error;406if (!yaml_document_append_mapping_pair(&output_document,407properties, key, value)) goto document_error;408}409410/* Add 'value': <value>. */411412key = yaml_document_add_scalar(&output_document, NULL,413(yaml_char_t *)"value", -1, YAML_PLAIN_SCALAR_STYLE);414if (!key) goto document_error;415value = yaml_document_add_scalar(&output_document, NULL,416input_event.data.scalar.value,417input_event.data.scalar.length,418YAML_DOUBLE_QUOTED_SCALAR_STYLE);419if (!value) goto document_error;420if (!yaml_document_append_mapping_pair(&output_document,421properties, key, value)) goto document_error;422423/* Display if the scalar tag is implicit. */424425/* Add 'implicit': {} */426427key = yaml_document_add_scalar(&output_document, NULL,428(yaml_char_t *)"version", -1, YAML_PLAIN_SCALAR_STYLE);429if (!key) goto document_error;430map = yaml_document_add_mapping(&output_document, NULL,431YAML_FLOW_MAPPING_STYLE);432if (!map) goto document_error;433if (!yaml_document_append_mapping_pair(&output_document,434properties, key, map)) goto document_error;435436/* Add 'plain': <flag>. */437438key = yaml_document_add_scalar(&output_document, NULL,439(yaml_char_t *)"plain", -1, YAML_PLAIN_SCALAR_STYLE);440if (!key) goto document_error;441value = yaml_document_add_scalar(&output_document, (yaml_char_t *)YAML_BOOL_TAG,442(yaml_char_t *)(input_event.data.scalar.plain_implicit ?443"true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);444if (!value) goto document_error;445if (!yaml_document_append_mapping_pair(&output_document,446map, key, value)) goto document_error;447448/* Add 'quoted': <flag>. */449450key = yaml_document_add_scalar(&output_document, NULL,451(yaml_char_t *)"quoted", -1, YAML_PLAIN_SCALAR_STYLE);452if (!key) goto document_error;453value = yaml_document_add_scalar(&output_document, (yaml_char_t *)YAML_BOOL_TAG,454(yaml_char_t *)(input_event.data.scalar.quoted_implicit ?455"true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);456if (!value) goto document_error;457if (!yaml_document_append_mapping_pair(&output_document,458map, key, value)) goto document_error;459460/* Display the style information. */461462if (input_event.data.scalar.style)463{464yaml_scalar_style_t style = input_event.data.scalar.style;465466/* Add 'style': <style>. */467468key = yaml_document_add_scalar(&output_document, NULL,469(yaml_char_t *)"style", -1, YAML_PLAIN_SCALAR_STYLE);470if (!key) goto document_error;471value = yaml_document_add_scalar(&output_document, NULL,472(yaml_char_t *)(style == YAML_PLAIN_SCALAR_STYLE ? "plain" :473style == YAML_SINGLE_QUOTED_SCALAR_STYLE ?474"single-quoted" :475style == YAML_DOUBLE_QUOTED_SCALAR_STYLE ?476"double-quoted" :477style == YAML_LITERAL_SCALAR_STYLE ? "literal" :478style == YAML_FOLDED_SCALAR_STYLE ? "folded" :479"unknown"), -1, YAML_PLAIN_SCALAR_STYLE);480if (!value) goto document_error;481if (!yaml_document_append_mapping_pair(&output_document,482properties, key, value)) goto document_error;483}484485break;486487case YAML_SEQUENCE_START_EVENT:488489/* Add 'type': 'SEQUENCE-START'. */490491key = yaml_document_add_scalar(&output_document, NULL,492(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);493if (!key) goto document_error;494value = yaml_document_add_scalar(&output_document, NULL,495(yaml_char_t *)"SEQUENCE-START", -1, YAML_PLAIN_SCALAR_STYLE);496if (!value) goto document_error;497if (!yaml_document_append_mapping_pair(&output_document,498properties, key, value)) goto document_error;499500/* Add 'anchor': <anchor>. */501502if (input_event.data.sequence_start.anchor)503{504key = yaml_document_add_scalar(&output_document, NULL,505(yaml_char_t *)"anchor", -1, YAML_PLAIN_SCALAR_STYLE);506if (!key) goto document_error;507value = yaml_document_add_scalar(&output_document, NULL,508input_event.data.sequence_start.anchor, -1,509YAML_DOUBLE_QUOTED_SCALAR_STYLE);510if (!value) goto document_error;511if (!yaml_document_append_mapping_pair(&output_document,512properties, key, value)) goto document_error;513}514515/* Add 'tag': <tag>. */516517if (input_event.data.sequence_start.tag)518{519key = yaml_document_add_scalar(&output_document, NULL,520(yaml_char_t *)"tag", -1, YAML_PLAIN_SCALAR_STYLE);521if (!key) goto document_error;522value = yaml_document_add_scalar(&output_document, NULL,523input_event.data.sequence_start.tag, -1,524YAML_DOUBLE_QUOTED_SCALAR_STYLE);525if (!value) goto document_error;526if (!yaml_document_append_mapping_pair(&output_document,527properties, key, value)) goto document_error;528}529530/* Add 'implicit': <flag>. */531532key = yaml_document_add_scalar(&output_document, NULL,533(yaml_char_t *)"implicit", -1, YAML_PLAIN_SCALAR_STYLE);534if (!key) goto document_error;535value = yaml_document_add_scalar(&output_document, (yaml_char_t *)YAML_BOOL_TAG,536(yaml_char_t *)(input_event.data.sequence_start.implicit ?537"true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);538if (!value) goto document_error;539if (!yaml_document_append_mapping_pair(&output_document,540properties, key, value)) goto document_error;541542/* Display the style information. */543544if (input_event.data.sequence_start.style)545{546yaml_sequence_style_t style547= input_event.data.sequence_start.style;548549/* Add 'style': <style>. */550551key = yaml_document_add_scalar(&output_document, NULL,552(yaml_char_t *)"style", -1, YAML_PLAIN_SCALAR_STYLE);553if (!key) goto document_error;554value = yaml_document_add_scalar(&output_document, NULL,555(yaml_char_t *)(style == YAML_BLOCK_SEQUENCE_STYLE ? "block" :556style == YAML_FLOW_SEQUENCE_STYLE ? "flow" :557"unknown"), -1, YAML_PLAIN_SCALAR_STYLE);558if (!value) goto document_error;559if (!yaml_document_append_mapping_pair(&output_document,560properties, key, value)) goto document_error;561}562563break;564565case YAML_SEQUENCE_END_EVENT:566567/* Add 'type': 'SEQUENCE-END'. */568569key = yaml_document_add_scalar(&output_document, NULL,570(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);571if (!key) goto document_error;572value = yaml_document_add_scalar(&output_document, NULL,573(yaml_char_t *)"SEQUENCE-END", -1, YAML_PLAIN_SCALAR_STYLE);574if (!value) goto document_error;575if (!yaml_document_append_mapping_pair(&output_document,576properties, key, value)) goto document_error;577578break;579580case YAML_MAPPING_START_EVENT:581582/* Add 'type': 'MAPPING-START'. */583584key = yaml_document_add_scalar(&output_document, NULL,585(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);586if (!key) goto document_error;587value = yaml_document_add_scalar(&output_document, NULL,588(yaml_char_t *)"MAPPING-START", -1, YAML_PLAIN_SCALAR_STYLE);589if (!value) goto document_error;590if (!yaml_document_append_mapping_pair(&output_document,591properties, key, value)) goto document_error;592593/* Add 'anchor': <anchor>. */594595if (input_event.data.mapping_start.anchor)596{597key = yaml_document_add_scalar(&output_document, NULL,598(yaml_char_t *)"anchor", -1, YAML_PLAIN_SCALAR_STYLE);599if (!key) goto document_error;600value = yaml_document_add_scalar(&output_document, NULL,601input_event.data.mapping_start.anchor, -1,602YAML_DOUBLE_QUOTED_SCALAR_STYLE);603if (!value) goto document_error;604if (!yaml_document_append_mapping_pair(&output_document,605properties, key, value)) goto document_error;606}607608/* Add 'tag': <tag>. */609610if (input_event.data.mapping_start.tag)611{612key = yaml_document_add_scalar(&output_document, NULL,613(yaml_char_t *)"tag", -1, YAML_PLAIN_SCALAR_STYLE);614if (!key) goto document_error;615value = yaml_document_add_scalar(&output_document, NULL,616input_event.data.mapping_start.tag, -1,617YAML_DOUBLE_QUOTED_SCALAR_STYLE);618if (!value) goto document_error;619if (!yaml_document_append_mapping_pair(&output_document,620properties, key, value)) goto document_error;621}622623/* Add 'implicit': <flag>. */624625key = yaml_document_add_scalar(&output_document, NULL,626(yaml_char_t *)"implicit", -1, YAML_PLAIN_SCALAR_STYLE);627if (!key) goto document_error;628value = yaml_document_add_scalar(&output_document, (yaml_char_t *)YAML_BOOL_TAG,629(yaml_char_t *)(input_event.data.mapping_start.implicit ?630"true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE);631if (!value) goto document_error;632if (!yaml_document_append_mapping_pair(&output_document,633properties, key, value)) goto document_error;634635/* Display the style information. */636637if (input_event.data.mapping_start.style)638{639yaml_mapping_style_t style640= input_event.data.mapping_start.style;641642/* Add 'style': <style>. */643644key = yaml_document_add_scalar(&output_document, NULL,645(yaml_char_t *)"style", -1, YAML_PLAIN_SCALAR_STYLE);646if (!key) goto document_error;647value = yaml_document_add_scalar(&output_document, NULL,648(yaml_char_t *)(style == YAML_BLOCK_MAPPING_STYLE ? "block" :649style == YAML_FLOW_MAPPING_STYLE ? "flow" :650"unknown"), -1, YAML_PLAIN_SCALAR_STYLE);651if (!value) goto document_error;652if (!yaml_document_append_mapping_pair(&output_document,653properties, key, value)) goto document_error;654}655656break;657658case YAML_MAPPING_END_EVENT:659660/* Add 'type': 'MAPPING-END'. */661662key = yaml_document_add_scalar(&output_document, NULL,663(yaml_char_t *)"type", -1, YAML_PLAIN_SCALAR_STYLE);664if (!key) goto document_error;665value = yaml_document_add_scalar(&output_document, NULL,666(yaml_char_t *)"MAPPING-END", -1, YAML_PLAIN_SCALAR_STYLE);667if (!value) goto document_error;668if (!yaml_document_append_mapping_pair(&output_document,669properties, key, value)) goto document_error;670671break;672673default:674/* It couldn't really happen. */675break;676}677678/* Delete the event object. */679680yaml_event_delete(&input_event);681}682683if (!yaml_emitter_dump(&emitter, &output_document))684goto emitter_error;685if (!yaml_emitter_close(&emitter))686goto emitter_error;687688yaml_parser_delete(&parser);689yaml_emitter_delete(&emitter);690691return 0;692693parser_error:694695/* Display a parser error message. */696697switch (parser.error)698{699case YAML_MEMORY_ERROR:700fprintf(stderr, "Memory error: Not enough memory for parsing\n");701break;702703case YAML_READER_ERROR:704if (parser.problem_value != -1) {705fprintf(stderr, "Reader error: %s: #%X at %zd\n", parser.problem,706parser.problem_value, parser.problem_offset);707}708else {709fprintf(stderr, "Reader error: %s at %zd\n", parser.problem,710parser.problem_offset);711}712break;713714case YAML_SCANNER_ERROR:715if (parser.context) {716fprintf(stderr, "Scanner error: %s at line %lu, column %lu\n"717"%s at line %lu, column %lu\n", parser.context,718parser.context_mark.line+1, parser.context_mark.column+1,719parser.problem, parser.problem_mark.line+1,720parser.problem_mark.column+1);721}722else {723fprintf(stderr, "Scanner error: %s at line %lu, column %lu\n",724parser.problem, parser.problem_mark.line+1,725parser.problem_mark.column+1);726}727break;728729case YAML_PARSER_ERROR:730if (parser.context) {731fprintf(stderr, "Parser error: %s at line %lu, column %lu\n"732"%s at line %lu, column %lu\n", parser.context,733parser.context_mark.line+1, parser.context_mark.column+1,734parser.problem, parser.problem_mark.line+1,735parser.problem_mark.column+1);736}737else {738fprintf(stderr, "Parser error: %s at line %lu, column %lu\n",739parser.problem, parser.problem_mark.line+1,740parser.problem_mark.column+1);741}742break;743744default:745/* Couldn't happen. */746fprintf(stderr, "Internal error\n");747break;748}749750yaml_event_delete(&input_event);751yaml_document_delete(&output_document);752yaml_parser_delete(&parser);753yaml_emitter_delete(&emitter);754755return 1;756757emitter_error:758759/* Display an emitter error message. */760761switch (emitter.error)762{763case YAML_MEMORY_ERROR:764fprintf(stderr, "Memory error: Not enough memory for emitting\n");765break;766767case YAML_WRITER_ERROR:768fprintf(stderr, "Writer error: %s\n", emitter.problem);769break;770771case YAML_EMITTER_ERROR:772fprintf(stderr, "Emitter error: %s\n", emitter.problem);773break;774775default:776/* Couldn't happen. */777fprintf(stderr, "Internal error\n");778break;779}780781yaml_event_delete(&input_event);782yaml_document_delete(&output_document);783yaml_parser_delete(&parser);784yaml_emitter_delete(&emitter);785786return 1;787788document_error:789790fprintf(stderr, "Memory error: Not enough memory for creating a document\n");791792yaml_event_delete(&input_event);793yaml_document_delete(&output_document);794yaml_parser_delete(&parser);795yaml_emitter_delete(&emitter);796797return 1;798}799800801802