example-deconstructor-alt.c revision 247738
1247738Sbapt 2247738Sbapt#include <yaml.h> 3247738Sbapt 4247738Sbapt#include <stdlib.h> 5247738Sbapt#include <stdio.h> 6247738Sbapt 7247738Sbaptint 8247738Sbaptmain(int argc, char *argv[]) 9247738Sbapt{ 10247738Sbapt int help = 0; 11247738Sbapt int canonical = 0; 12247738Sbapt int unicode = 0; 13247738Sbapt int k; 14247738Sbapt int done = 0; 15247738Sbapt 16247738Sbapt yaml_parser_t parser; 17247738Sbapt yaml_emitter_t emitter; 18247738Sbapt yaml_event_t input_event; 19247738Sbapt yaml_document_t output_document; 20247738Sbapt 21247738Sbapt int root; 22247738Sbapt 23247738Sbapt /* Clear the objects. */ 24247738Sbapt 25247738Sbapt memset(&parser, 0, sizeof(parser)); 26247738Sbapt memset(&emitter, 0, sizeof(emitter)); 27247738Sbapt memset(&input_event, 0, sizeof(input_event)); 28247738Sbapt memset(&output_document, 0, sizeof(output_document)); 29247738Sbapt 30247738Sbapt /* Analyze command line options. */ 31247738Sbapt 32247738Sbapt for (k = 1; k < argc; k ++) 33247738Sbapt { 34247738Sbapt if (strcmp(argv[k], "-h") == 0 35247738Sbapt || strcmp(argv[k], "--help") == 0) { 36247738Sbapt help = 1; 37247738Sbapt } 38247738Sbapt 39247738Sbapt else if (strcmp(argv[k], "-c") == 0 40247738Sbapt || strcmp(argv[k], "--canonical") == 0) { 41247738Sbapt canonical = 1; 42247738Sbapt } 43247738Sbapt 44247738Sbapt else if (strcmp(argv[k], "-u") == 0 45247738Sbapt || strcmp(argv[k], "--unicode") == 0) { 46247738Sbapt unicode = 1; 47247738Sbapt } 48247738Sbapt 49247738Sbapt else { 50247738Sbapt fprintf(stderr, "Unrecognized option: %s\n" 51247738Sbapt "Try `%s --help` for more information.\n", 52247738Sbapt argv[k], argv[0]); 53247738Sbapt return 1; 54247738Sbapt } 55247738Sbapt } 56247738Sbapt 57247738Sbapt /* Display the help string. */ 58247738Sbapt 59247738Sbapt if (help) 60247738Sbapt { 61247738Sbapt printf("%s <input\n" 62247738Sbapt "or\n%s -h | --help\nDeconstruct a YAML stream\n\nOptions:\n" 63247738Sbapt "-h, --help\t\tdisplay this help and exit\n" 64247738Sbapt "-c, --canonical\t\toutput in the canonical YAML format\n" 65247738Sbapt "-u, --unicode\t\toutput unescaped non-ASCII characters\n", 66247738Sbapt argv[0], argv[0]); 67247738Sbapt return 0; 68247738Sbapt } 69247738Sbapt 70247738Sbapt /* Initialize the parser and emitter objects. */ 71247738Sbapt 72247738Sbapt if (!yaml_parser_initialize(&parser)) { 73247738Sbapt fprintf(stderr, "Could not initialize the parser object\n"); 74247738Sbapt return 1; 75247738Sbapt } 76247738Sbapt 77247738Sbapt if (!yaml_emitter_initialize(&emitter)) { 78247738Sbapt yaml_parser_delete(&parser); 79247738Sbapt fprintf(stderr, "Could not inialize the emitter object\n"); 80247738Sbapt return 1; 81247738Sbapt } 82247738Sbapt 83247738Sbapt /* Set the parser parameters. */ 84247738Sbapt 85247738Sbapt yaml_parser_set_input_file(&parser, stdin); 86247738Sbapt 87247738Sbapt /* Set the emitter parameters. */ 88247738Sbapt 89247738Sbapt yaml_emitter_set_output_file(&emitter, stdout); 90247738Sbapt 91247738Sbapt yaml_emitter_set_canonical(&emitter, canonical); 92247738Sbapt yaml_emitter_set_unicode(&emitter, unicode); 93247738Sbapt 94247738Sbapt /* Create and emit the STREAM-START event. */ 95247738Sbapt 96247738Sbapt if (!yaml_emitter_open(&emitter)) 97247738Sbapt goto emitter_error; 98247738Sbapt 99247738Sbapt /* Create a output_document object. */ 100247738Sbapt 101247738Sbapt if (!yaml_document_initialize(&output_document, NULL, NULL, NULL, 0, 0)) 102247738Sbapt goto document_error; 103247738Sbapt 104247738Sbapt /* Create the root sequence. */ 105247738Sbapt 106247738Sbapt root = yaml_document_add_sequence(&output_document, NULL, 107247738Sbapt YAML_BLOCK_SEQUENCE_STYLE); 108247738Sbapt if (!root) goto document_error; 109247738Sbapt 110247738Sbapt /* Loop through the input events. */ 111247738Sbapt 112247738Sbapt while (!done) 113247738Sbapt { 114247738Sbapt int properties, key, value, map, seq; 115247738Sbapt 116247738Sbapt /* Get the next event. */ 117247738Sbapt 118247738Sbapt if (!yaml_parser_parse(&parser, &input_event)) 119247738Sbapt goto parser_error; 120247738Sbapt 121247738Sbapt /* Check if this is the stream end. */ 122247738Sbapt 123247738Sbapt if (input_event.type == YAML_STREAM_END_EVENT) { 124247738Sbapt done = 1; 125247738Sbapt } 126247738Sbapt 127247738Sbapt /* Create a mapping node and attach it to the root sequence. */ 128247738Sbapt 129247738Sbapt properties = yaml_document_add_mapping(&output_document, NULL, 130247738Sbapt YAML_BLOCK_MAPPING_STYLE); 131247738Sbapt if (!properties) goto document_error; 132247738Sbapt if (!yaml_document_append_sequence_item(&output_document, 133247738Sbapt root, properties)) goto document_error; 134247738Sbapt 135247738Sbapt /* Analyze the event. */ 136247738Sbapt 137247738Sbapt switch (input_event.type) 138247738Sbapt { 139247738Sbapt case YAML_STREAM_START_EVENT: 140247738Sbapt 141247738Sbapt /* Add 'type': 'STREAM-START'. */ 142247738Sbapt 143247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 144247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 145247738Sbapt if (!key) goto document_error; 146247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 147247738Sbapt "STREAM-START", -1, YAML_PLAIN_SCALAR_STYLE); 148247738Sbapt if (!value) goto document_error; 149247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 150247738Sbapt properties, key, value)) goto document_error; 151247738Sbapt 152247738Sbapt /* Add 'encoding': <encoding>. */ 153247738Sbapt 154247738Sbapt if (input_event.data.stream_start.encoding) 155247738Sbapt { 156247738Sbapt yaml_encoding_t encoding 157247738Sbapt = input_event.data.stream_start.encoding; 158247738Sbapt 159247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 160247738Sbapt "encoding", -1, YAML_PLAIN_SCALAR_STYLE); 161247738Sbapt if (!key) goto document_error; 162247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 163247738Sbapt (encoding == YAML_UTF8_ENCODING ? "utf-8" : 164247738Sbapt encoding == YAML_UTF16LE_ENCODING ? "utf-16-le" : 165247738Sbapt encoding == YAML_UTF16BE_ENCODING ? "utf-16-be" : 166247738Sbapt "unknown"), -1, YAML_PLAIN_SCALAR_STYLE); 167247738Sbapt if (!value) goto document_error; 168247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 169247738Sbapt properties, key, value)) goto document_error; 170247738Sbapt } 171247738Sbapt 172247738Sbapt break; 173247738Sbapt 174247738Sbapt case YAML_STREAM_END_EVENT: 175247738Sbapt 176247738Sbapt /* Add 'type': 'STREAM-END'. */ 177247738Sbapt 178247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 179247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 180247738Sbapt if (!key) goto document_error; 181247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 182247738Sbapt "STREAM-END", -1, YAML_PLAIN_SCALAR_STYLE); 183247738Sbapt if (!value) goto document_error; 184247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 185247738Sbapt properties, key, value)) goto document_error; 186247738Sbapt 187247738Sbapt break; 188247738Sbapt 189247738Sbapt case YAML_DOCUMENT_START_EVENT: 190247738Sbapt 191247738Sbapt /* Add 'type': 'DOCUMENT-START'. */ 192247738Sbapt 193247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 194247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 195247738Sbapt if (!key) goto document_error; 196247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 197247738Sbapt "DOCUMENT-START", -1, YAML_PLAIN_SCALAR_STYLE); 198247738Sbapt if (!value) goto document_error; 199247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 200247738Sbapt properties, key, value)) goto document_error; 201247738Sbapt 202247738Sbapt /* Display the output_document version numbers. */ 203247738Sbapt 204247738Sbapt if (input_event.data.document_start.version_directive) 205247738Sbapt { 206247738Sbapt yaml_version_directive_t *version 207247738Sbapt = input_event.data.document_start.version_directive; 208247738Sbapt char number[64]; 209247738Sbapt 210247738Sbapt /* Add 'version': {}. */ 211247738Sbapt 212247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 213247738Sbapt "version", -1, YAML_PLAIN_SCALAR_STYLE); 214247738Sbapt if (!key) goto document_error; 215247738Sbapt map = yaml_document_add_mapping(&output_document, NULL, 216247738Sbapt YAML_FLOW_MAPPING_STYLE); 217247738Sbapt if (!map) goto document_error; 218247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 219247738Sbapt properties, key, map)) goto document_error; 220247738Sbapt 221247738Sbapt /* Add 'major': <number>. */ 222247738Sbapt 223247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 224247738Sbapt "major", -1, YAML_PLAIN_SCALAR_STYLE); 225247738Sbapt if (!key) goto document_error; 226247738Sbapt sprintf(number, "%d", version->major); 227247738Sbapt value = yaml_document_add_scalar(&output_document, YAML_INT_TAG, 228247738Sbapt number, -1, YAML_PLAIN_SCALAR_STYLE); 229247738Sbapt if (!value) goto document_error; 230247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 231247738Sbapt map, key, value)) goto document_error; 232247738Sbapt 233247738Sbapt /* Add 'minor': <number>. */ 234247738Sbapt 235247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 236247738Sbapt "minor", -1, YAML_PLAIN_SCALAR_STYLE); 237247738Sbapt if (!key) goto document_error; 238247738Sbapt sprintf(number, "%d", version->minor); 239247738Sbapt value = yaml_document_add_scalar(&output_document, YAML_INT_TAG, 240247738Sbapt number, -1, YAML_PLAIN_SCALAR_STYLE); 241247738Sbapt if (!value) goto document_error; 242247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 243247738Sbapt map, key, value)) goto document_error; 244247738Sbapt } 245247738Sbapt 246247738Sbapt /* Display the output_document tag directives. */ 247247738Sbapt 248247738Sbapt if (input_event.data.document_start.tag_directives.start 249247738Sbapt != input_event.data.document_start.tag_directives.end) 250247738Sbapt { 251247738Sbapt yaml_tag_directive_t *tag; 252247738Sbapt 253247738Sbapt /* Add 'tags': []. */ 254247738Sbapt 255247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 256247738Sbapt "tags", -1, YAML_PLAIN_SCALAR_STYLE); 257247738Sbapt if (!key) goto document_error; 258247738Sbapt seq = yaml_document_add_sequence(&output_document, NULL, 259247738Sbapt YAML_BLOCK_SEQUENCE_STYLE); 260247738Sbapt if (!seq) goto document_error; 261247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 262247738Sbapt properties, key, seq)) goto document_error; 263247738Sbapt 264247738Sbapt for (tag = input_event.data.document_start.tag_directives.start; 265247738Sbapt tag != input_event.data.document_start.tag_directives.end; 266247738Sbapt tag ++) 267247738Sbapt { 268247738Sbapt /* Add {}. */ 269247738Sbapt 270247738Sbapt map = yaml_document_add_mapping(&output_document, NULL, 271247738Sbapt YAML_FLOW_MAPPING_STYLE); 272247738Sbapt if (!map) goto document_error; 273247738Sbapt if (!yaml_document_append_sequence_item(&output_document, 274247738Sbapt seq, map)) goto document_error; 275247738Sbapt 276247738Sbapt /* Add 'handle': <handle>. */ 277247738Sbapt 278247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 279247738Sbapt "handle", -1, YAML_PLAIN_SCALAR_STYLE); 280247738Sbapt if (!key) goto document_error; 281247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 282247738Sbapt tag->handle, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); 283247738Sbapt if (!value) goto document_error; 284247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 285247738Sbapt map, key, value)) goto document_error; 286247738Sbapt 287247738Sbapt /* Add 'prefix': <prefix>. */ 288247738Sbapt 289247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 290247738Sbapt "prefix", -1, YAML_PLAIN_SCALAR_STYLE); 291247738Sbapt if (!key) goto document_error; 292247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 293247738Sbapt tag->prefix, -1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); 294247738Sbapt if (!value) goto document_error; 295247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 296247738Sbapt map, key, value)) goto document_error; 297247738Sbapt } 298247738Sbapt } 299247738Sbapt 300247738Sbapt /* Add 'implicit': <flag>. */ 301247738Sbapt 302247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 303247738Sbapt "implicit", -1, YAML_PLAIN_SCALAR_STYLE); 304247738Sbapt if (!key) goto document_error; 305247738Sbapt value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, 306247738Sbapt (input_event.data.document_start.implicit ? 307247738Sbapt "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); 308247738Sbapt if (!value) goto document_error; 309247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 310247738Sbapt properties, key, value)) goto document_error; 311247738Sbapt 312247738Sbapt break; 313247738Sbapt 314247738Sbapt case YAML_DOCUMENT_END_EVENT: 315247738Sbapt 316247738Sbapt /* Add 'type': 'DOCUMENT-END'. */ 317247738Sbapt 318247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 319247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 320247738Sbapt if (!key) goto document_error; 321247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 322247738Sbapt "DOCUMENT-END", -1, YAML_PLAIN_SCALAR_STYLE); 323247738Sbapt if (!value) goto document_error; 324247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 325247738Sbapt properties, key, value)) goto document_error; 326247738Sbapt 327247738Sbapt /* Add 'implicit': <flag>. */ 328247738Sbapt 329247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 330247738Sbapt "implicit", -1, YAML_PLAIN_SCALAR_STYLE); 331247738Sbapt if (!key) goto document_error; 332247738Sbapt value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, 333247738Sbapt (input_event.data.document_end.implicit ? 334247738Sbapt "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); 335247738Sbapt if (!value) goto document_error; 336247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 337247738Sbapt properties, key, value)) goto document_error; 338247738Sbapt 339247738Sbapt break; 340247738Sbapt 341247738Sbapt case YAML_ALIAS_EVENT: 342247738Sbapt 343247738Sbapt /* Add 'type': 'ALIAS'. */ 344247738Sbapt 345247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 346247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 347247738Sbapt if (!key) goto document_error; 348247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 349247738Sbapt "ALIAS", -1, YAML_PLAIN_SCALAR_STYLE); 350247738Sbapt if (!value) goto document_error; 351247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 352247738Sbapt properties, key, value)) goto document_error; 353247738Sbapt 354247738Sbapt /* Add 'anchor': <anchor>. */ 355247738Sbapt 356247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 357247738Sbapt "anchor", -1, YAML_PLAIN_SCALAR_STYLE); 358247738Sbapt if (!key) goto document_error; 359247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 360247738Sbapt input_event.data.alias.anchor, -1, 361247738Sbapt YAML_DOUBLE_QUOTED_SCALAR_STYLE); 362247738Sbapt if (!value) goto document_error; 363247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 364247738Sbapt properties, key, value)) goto document_error; 365247738Sbapt 366247738Sbapt break; 367247738Sbapt 368247738Sbapt case YAML_SCALAR_EVENT: 369247738Sbapt 370247738Sbapt /* Add 'type': 'SCALAR'. */ 371247738Sbapt 372247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 373247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 374247738Sbapt if (!key) goto document_error; 375247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 376247738Sbapt "SCALAR", -1, YAML_PLAIN_SCALAR_STYLE); 377247738Sbapt if (!value) goto document_error; 378247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 379247738Sbapt properties, key, value)) goto document_error; 380247738Sbapt 381247738Sbapt /* Add 'anchor': <anchor>. */ 382247738Sbapt 383247738Sbapt if (input_event.data.scalar.anchor) 384247738Sbapt { 385247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 386247738Sbapt "anchor", -1, YAML_PLAIN_SCALAR_STYLE); 387247738Sbapt if (!key) goto document_error; 388247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 389247738Sbapt input_event.data.scalar.anchor, -1, 390247738Sbapt YAML_DOUBLE_QUOTED_SCALAR_STYLE); 391247738Sbapt if (!value) goto document_error; 392247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 393247738Sbapt properties, key, value)) goto document_error; 394247738Sbapt } 395247738Sbapt 396247738Sbapt /* Add 'tag': <tag>. */ 397247738Sbapt 398247738Sbapt if (input_event.data.scalar.tag) 399247738Sbapt { 400247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 401247738Sbapt "tag", -1, YAML_PLAIN_SCALAR_STYLE); 402247738Sbapt if (!key) goto document_error; 403247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 404247738Sbapt input_event.data.scalar.tag, -1, 405247738Sbapt YAML_DOUBLE_QUOTED_SCALAR_STYLE); 406247738Sbapt if (!value) goto document_error; 407247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 408247738Sbapt properties, key, value)) goto document_error; 409247738Sbapt } 410247738Sbapt 411247738Sbapt /* Add 'value': <value>. */ 412247738Sbapt 413247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 414247738Sbapt "value", -1, YAML_PLAIN_SCALAR_STYLE); 415247738Sbapt if (!key) goto document_error; 416247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 417247738Sbapt input_event.data.scalar.value, 418247738Sbapt input_event.data.scalar.length, 419247738Sbapt YAML_DOUBLE_QUOTED_SCALAR_STYLE); 420247738Sbapt if (!value) goto document_error; 421247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 422247738Sbapt properties, key, value)) goto document_error; 423247738Sbapt 424247738Sbapt /* Display if the scalar tag is implicit. */ 425247738Sbapt 426247738Sbapt /* Add 'implicit': {} */ 427247738Sbapt 428247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 429247738Sbapt "version", -1, YAML_PLAIN_SCALAR_STYLE); 430247738Sbapt if (!key) goto document_error; 431247738Sbapt map = yaml_document_add_mapping(&output_document, NULL, 432247738Sbapt YAML_FLOW_MAPPING_STYLE); 433247738Sbapt if (!map) goto document_error; 434247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 435247738Sbapt properties, key, map)) goto document_error; 436247738Sbapt 437247738Sbapt /* Add 'plain': <flag>. */ 438247738Sbapt 439247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 440247738Sbapt "plain", -1, YAML_PLAIN_SCALAR_STYLE); 441247738Sbapt if (!key) goto document_error; 442247738Sbapt value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, 443247738Sbapt (input_event.data.scalar.plain_implicit ? 444247738Sbapt "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); 445247738Sbapt if (!value) goto document_error; 446247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 447247738Sbapt map, key, value)) goto document_error; 448247738Sbapt 449247738Sbapt /* Add 'quoted': <flag>. */ 450247738Sbapt 451247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 452247738Sbapt "quoted", -1, YAML_PLAIN_SCALAR_STYLE); 453247738Sbapt if (!key) goto document_error; 454247738Sbapt value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, 455247738Sbapt (input_event.data.scalar.quoted_implicit ? 456247738Sbapt "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); 457247738Sbapt if (!value) goto document_error; 458247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 459247738Sbapt map, key, value)) goto document_error; 460247738Sbapt 461247738Sbapt /* Display the style information. */ 462247738Sbapt 463247738Sbapt if (input_event.data.scalar.style) 464247738Sbapt { 465247738Sbapt yaml_scalar_style_t style = input_event.data.scalar.style; 466247738Sbapt 467247738Sbapt /* Add 'style': <style>. */ 468247738Sbapt 469247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 470247738Sbapt "style", -1, YAML_PLAIN_SCALAR_STYLE); 471247738Sbapt if (!key) goto document_error; 472247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 473247738Sbapt (style == YAML_PLAIN_SCALAR_STYLE ? "plain" : 474247738Sbapt style == YAML_SINGLE_QUOTED_SCALAR_STYLE ? 475247738Sbapt "single-quoted" : 476247738Sbapt style == YAML_DOUBLE_QUOTED_SCALAR_STYLE ? 477247738Sbapt "double-quoted" : 478247738Sbapt style == YAML_LITERAL_SCALAR_STYLE ? "literal" : 479247738Sbapt style == YAML_FOLDED_SCALAR_STYLE ? "folded" : 480247738Sbapt "unknown"), -1, YAML_PLAIN_SCALAR_STYLE); 481247738Sbapt if (!value) goto document_error; 482247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 483247738Sbapt properties, key, value)) goto document_error; 484247738Sbapt } 485247738Sbapt 486247738Sbapt break; 487247738Sbapt 488247738Sbapt case YAML_SEQUENCE_START_EVENT: 489247738Sbapt 490247738Sbapt /* Add 'type': 'SEQUENCE-START'. */ 491247738Sbapt 492247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 493247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 494247738Sbapt if (!key) goto document_error; 495247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 496247738Sbapt "SEQUENCE-START", -1, YAML_PLAIN_SCALAR_STYLE); 497247738Sbapt if (!value) goto document_error; 498247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 499247738Sbapt properties, key, value)) goto document_error; 500247738Sbapt 501247738Sbapt /* Add 'anchor': <anchor>. */ 502247738Sbapt 503247738Sbapt if (input_event.data.sequence_start.anchor) 504247738Sbapt { 505247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 506247738Sbapt "anchor", -1, YAML_PLAIN_SCALAR_STYLE); 507247738Sbapt if (!key) goto document_error; 508247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 509247738Sbapt input_event.data.sequence_start.anchor, -1, 510247738Sbapt YAML_DOUBLE_QUOTED_SCALAR_STYLE); 511247738Sbapt if (!value) goto document_error; 512247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 513247738Sbapt properties, key, value)) goto document_error; 514247738Sbapt } 515247738Sbapt 516247738Sbapt /* Add 'tag': <tag>. */ 517247738Sbapt 518247738Sbapt if (input_event.data.sequence_start.tag) 519247738Sbapt { 520247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 521247738Sbapt "tag", -1, YAML_PLAIN_SCALAR_STYLE); 522247738Sbapt if (!key) goto document_error; 523247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 524247738Sbapt input_event.data.sequence_start.tag, -1, 525247738Sbapt YAML_DOUBLE_QUOTED_SCALAR_STYLE); 526247738Sbapt if (!value) goto document_error; 527247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 528247738Sbapt properties, key, value)) goto document_error; 529247738Sbapt } 530247738Sbapt 531247738Sbapt /* Add 'implicit': <flag>. */ 532247738Sbapt 533247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 534247738Sbapt "implicit", -1, YAML_PLAIN_SCALAR_STYLE); 535247738Sbapt if (!key) goto document_error; 536247738Sbapt value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, 537247738Sbapt (input_event.data.sequence_start.implicit ? 538247738Sbapt "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); 539247738Sbapt if (!value) goto document_error; 540247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 541247738Sbapt properties, key, value)) goto document_error; 542247738Sbapt 543247738Sbapt /* Display the style information. */ 544247738Sbapt 545247738Sbapt if (input_event.data.sequence_start.style) 546247738Sbapt { 547247738Sbapt yaml_sequence_style_t style 548247738Sbapt = input_event.data.sequence_start.style; 549247738Sbapt 550247738Sbapt /* Add 'style': <style>. */ 551247738Sbapt 552247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 553247738Sbapt "style", -1, YAML_PLAIN_SCALAR_STYLE); 554247738Sbapt if (!key) goto document_error; 555247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 556247738Sbapt (style == YAML_BLOCK_SEQUENCE_STYLE ? "block" : 557247738Sbapt style == YAML_FLOW_SEQUENCE_STYLE ? "flow" : 558247738Sbapt "unknown"), -1, YAML_PLAIN_SCALAR_STYLE); 559247738Sbapt if (!value) goto document_error; 560247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 561247738Sbapt properties, key, value)) goto document_error; 562247738Sbapt } 563247738Sbapt 564247738Sbapt break; 565247738Sbapt 566247738Sbapt case YAML_SEQUENCE_END_EVENT: 567247738Sbapt 568247738Sbapt /* Add 'type': 'SEQUENCE-END'. */ 569247738Sbapt 570247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 571247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 572247738Sbapt if (!key) goto document_error; 573247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 574247738Sbapt "SEQUENCE-END", -1, YAML_PLAIN_SCALAR_STYLE); 575247738Sbapt if (!value) goto document_error; 576247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 577247738Sbapt properties, key, value)) goto document_error; 578247738Sbapt 579247738Sbapt break; 580247738Sbapt 581247738Sbapt case YAML_MAPPING_START_EVENT: 582247738Sbapt 583247738Sbapt /* Add 'type': 'MAPPING-START'. */ 584247738Sbapt 585247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 586247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 587247738Sbapt if (!key) goto document_error; 588247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 589247738Sbapt "MAPPING-START", -1, YAML_PLAIN_SCALAR_STYLE); 590247738Sbapt if (!value) goto document_error; 591247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 592247738Sbapt properties, key, value)) goto document_error; 593247738Sbapt 594247738Sbapt /* Add 'anchor': <anchor>. */ 595247738Sbapt 596247738Sbapt if (input_event.data.mapping_start.anchor) 597247738Sbapt { 598247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 599247738Sbapt "anchor", -1, YAML_PLAIN_SCALAR_STYLE); 600247738Sbapt if (!key) goto document_error; 601247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 602247738Sbapt input_event.data.mapping_start.anchor, -1, 603247738Sbapt YAML_DOUBLE_QUOTED_SCALAR_STYLE); 604247738Sbapt if (!value) goto document_error; 605247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 606247738Sbapt properties, key, value)) goto document_error; 607247738Sbapt } 608247738Sbapt 609247738Sbapt /* Add 'tag': <tag>. */ 610247738Sbapt 611247738Sbapt if (input_event.data.mapping_start.tag) 612247738Sbapt { 613247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 614247738Sbapt "tag", -1, YAML_PLAIN_SCALAR_STYLE); 615247738Sbapt if (!key) goto document_error; 616247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 617247738Sbapt input_event.data.mapping_start.tag, -1, 618247738Sbapt YAML_DOUBLE_QUOTED_SCALAR_STYLE); 619247738Sbapt if (!value) goto document_error; 620247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 621247738Sbapt properties, key, value)) goto document_error; 622247738Sbapt } 623247738Sbapt 624247738Sbapt /* Add 'implicit': <flag>. */ 625247738Sbapt 626247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 627247738Sbapt "implicit", -1, YAML_PLAIN_SCALAR_STYLE); 628247738Sbapt if (!key) goto document_error; 629247738Sbapt value = yaml_document_add_scalar(&output_document, YAML_BOOL_TAG, 630247738Sbapt (input_event.data.mapping_start.implicit ? 631247738Sbapt "true" : "false"), -1, YAML_PLAIN_SCALAR_STYLE); 632247738Sbapt if (!value) goto document_error; 633247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 634247738Sbapt properties, key, value)) goto document_error; 635247738Sbapt 636247738Sbapt /* Display the style information. */ 637247738Sbapt 638247738Sbapt if (input_event.data.sequence_start.style) 639247738Sbapt { 640247738Sbapt yaml_sequence_style_t style 641247738Sbapt = input_event.data.mapping_start.style; 642247738Sbapt 643247738Sbapt /* Add 'style': <style>. */ 644247738Sbapt 645247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 646247738Sbapt "style", -1, YAML_PLAIN_SCALAR_STYLE); 647247738Sbapt if (!key) goto document_error; 648247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 649247738Sbapt (style == YAML_BLOCK_MAPPING_STYLE ? "block" : 650247738Sbapt style == YAML_FLOW_MAPPING_STYLE ? "flow" : 651247738Sbapt "unknown"), -1, YAML_PLAIN_SCALAR_STYLE); 652247738Sbapt if (!value) goto document_error; 653247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 654247738Sbapt properties, key, value)) goto document_error; 655247738Sbapt } 656247738Sbapt 657247738Sbapt break; 658247738Sbapt 659247738Sbapt case YAML_MAPPING_END_EVENT: 660247738Sbapt 661247738Sbapt /* Add 'type': 'MAPPING-END'. */ 662247738Sbapt 663247738Sbapt key = yaml_document_add_scalar(&output_document, NULL, 664247738Sbapt "type", -1, YAML_PLAIN_SCALAR_STYLE); 665247738Sbapt if (!key) goto document_error; 666247738Sbapt value = yaml_document_add_scalar(&output_document, NULL, 667247738Sbapt "MAPPING-END", -1, YAML_PLAIN_SCALAR_STYLE); 668247738Sbapt if (!value) goto document_error; 669247738Sbapt if (!yaml_document_append_mapping_pair(&output_document, 670247738Sbapt properties, key, value)) goto document_error; 671247738Sbapt 672247738Sbapt break; 673247738Sbapt 674247738Sbapt default: 675247738Sbapt /* It couldn't really happen. */ 676247738Sbapt break; 677247738Sbapt } 678247738Sbapt 679247738Sbapt /* Delete the event object. */ 680247738Sbapt 681247738Sbapt yaml_event_delete(&input_event); 682247738Sbapt } 683247738Sbapt 684247738Sbapt if (!yaml_emitter_dump(&emitter, &output_document)) 685247738Sbapt goto emitter_error; 686247738Sbapt if (!yaml_emitter_close(&emitter)) 687247738Sbapt goto emitter_error; 688247738Sbapt 689247738Sbapt yaml_parser_delete(&parser); 690247738Sbapt yaml_emitter_delete(&emitter); 691247738Sbapt 692247738Sbapt return 0; 693247738Sbapt 694247738Sbaptparser_error: 695247738Sbapt 696247738Sbapt /* Display a parser error message. */ 697247738Sbapt 698247738Sbapt switch (parser.error) 699247738Sbapt { 700247738Sbapt case YAML_MEMORY_ERROR: 701247738Sbapt fprintf(stderr, "Memory error: Not enough memory for parsing\n"); 702247738Sbapt break; 703247738Sbapt 704247738Sbapt case YAML_READER_ERROR: 705247738Sbapt if (parser.problem_value != -1) { 706247738Sbapt fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem, 707247738Sbapt parser.problem_value, parser.problem_offset); 708247738Sbapt } 709247738Sbapt else { 710247738Sbapt fprintf(stderr, "Reader error: %s at %d\n", parser.problem, 711247738Sbapt parser.problem_offset); 712247738Sbapt } 713247738Sbapt break; 714247738Sbapt 715247738Sbapt case YAML_SCANNER_ERROR: 716247738Sbapt if (parser.context) { 717247738Sbapt fprintf(stderr, "Scanner error: %s at line %d, column %d\n" 718247738Sbapt "%s at line %d, column %d\n", parser.context, 719247738Sbapt parser.context_mark.line+1, parser.context_mark.column+1, 720247738Sbapt parser.problem, parser.problem_mark.line+1, 721247738Sbapt parser.problem_mark.column+1); 722247738Sbapt } 723247738Sbapt else { 724247738Sbapt fprintf(stderr, "Scanner error: %s at line %d, column %d\n", 725247738Sbapt parser.problem, parser.problem_mark.line+1, 726247738Sbapt parser.problem_mark.column+1); 727247738Sbapt } 728247738Sbapt break; 729247738Sbapt 730247738Sbapt case YAML_PARSER_ERROR: 731247738Sbapt if (parser.context) { 732247738Sbapt fprintf(stderr, "Parser error: %s at line %d, column %d\n" 733247738Sbapt "%s at line %d, column %d\n", parser.context, 734247738Sbapt parser.context_mark.line+1, parser.context_mark.column+1, 735247738Sbapt parser.problem, parser.problem_mark.line+1, 736247738Sbapt parser.problem_mark.column+1); 737247738Sbapt } 738247738Sbapt else { 739247738Sbapt fprintf(stderr, "Parser error: %s at line %d, column %d\n", 740247738Sbapt parser.problem, parser.problem_mark.line+1, 741247738Sbapt parser.problem_mark.column+1); 742247738Sbapt } 743247738Sbapt break; 744247738Sbapt 745247738Sbapt default: 746247738Sbapt /* Couldn't happen. */ 747247738Sbapt fprintf(stderr, "Internal error\n"); 748247738Sbapt break; 749247738Sbapt } 750247738Sbapt 751247738Sbapt yaml_event_delete(&input_event); 752247738Sbapt yaml_document_delete(&output_document); 753247738Sbapt yaml_parser_delete(&parser); 754247738Sbapt yaml_emitter_delete(&emitter); 755247738Sbapt 756247738Sbapt return 1; 757247738Sbapt 758247738Sbaptemitter_error: 759247738Sbapt 760247738Sbapt /* Display an emitter error message. */ 761247738Sbapt 762247738Sbapt switch (emitter.error) 763247738Sbapt { 764247738Sbapt case YAML_MEMORY_ERROR: 765247738Sbapt fprintf(stderr, "Memory error: Not enough memory for emitting\n"); 766247738Sbapt break; 767247738Sbapt 768247738Sbapt case YAML_WRITER_ERROR: 769247738Sbapt fprintf(stderr, "Writer error: %s\n", emitter.problem); 770247738Sbapt break; 771247738Sbapt 772247738Sbapt case YAML_EMITTER_ERROR: 773247738Sbapt fprintf(stderr, "Emitter error: %s\n", emitter.problem); 774247738Sbapt break; 775247738Sbapt 776247738Sbapt default: 777247738Sbapt /* Couldn't happen. */ 778247738Sbapt fprintf(stderr, "Internal error\n"); 779247738Sbapt break; 780247738Sbapt } 781247738Sbapt 782247738Sbapt yaml_event_delete(&input_event); 783247738Sbapt yaml_document_delete(&output_document); 784247738Sbapt yaml_parser_delete(&parser); 785247738Sbapt yaml_emitter_delete(&emitter); 786247738Sbapt 787247738Sbapt return 1; 788247738Sbapt 789247738Sbaptdocument_error: 790247738Sbapt 791247738Sbapt fprintf(stderr, "Memory error: Not enough memory for creating a document\n"); 792247738Sbapt 793247738Sbapt yaml_event_delete(&input_event); 794247738Sbapt yaml_document_delete(&output_document); 795247738Sbapt yaml_parser_delete(&parser); 796247738Sbapt yaml_emitter_delete(&emitter); 797247738Sbapt 798247738Sbapt return 1; 799247738Sbapt} 800247738Sbapt 801