Path: blob/main/contrib/libyaml/tests/example-reformatter-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_document_t document;1819/* Clear the objects. */2021memset(&parser, 0, sizeof(parser));22memset(&emitter, 0, sizeof(emitter));23memset(&document, 0, sizeof(document));2425/* Analyze command line options. */2627for (k = 1; k < argc; k ++)28{29if (strcmp(argv[k], "-h") == 030|| strcmp(argv[k], "--help") == 0) {31help = 1;32}3334else if (strcmp(argv[k], "-c") == 035|| strcmp(argv[k], "--canonical") == 0) {36canonical = 1;37}3839else if (strcmp(argv[k], "-u") == 040|| strcmp(argv[k], "--unicode") == 0) {41unicode = 1;42}4344else {45fprintf(stderr, "Unrecognized option: %s\n"46"Try `%s --help` for more information.\n",47argv[k], argv[0]);48return 1;49}50}5152/* Display the help string. */5354if (help)55{56printf("%s [--canonical] [--unicode] <input >output\n"57"or\n%s -h | --help\nReformat a YAML stream\n\nOptions:\n"58"-h, --help\t\tdisplay this help and exit\n"59"-c, --canonical\t\toutput in the canonical YAML format\n"60"-u, --unicode\t\toutput unescaped non-ASCII characters\n",61argv[0], argv[0]);62return 0;63}6465/* Initialize the parser and emitter objects. */6667if (!yaml_parser_initialize(&parser))68goto parser_error;6970if (!yaml_emitter_initialize(&emitter))71goto emitter_error;7273/* Set the parser parameters. */7475yaml_parser_set_input_file(&parser, stdin);7677/* Set the emitter parameters. */7879yaml_emitter_set_output_file(&emitter, stdout);8081yaml_emitter_set_canonical(&emitter, canonical);82yaml_emitter_set_unicode(&emitter, unicode);8384/* The main loop. */8586while (!done)87{88/* Get the next event. */8990if (!yaml_parser_load(&parser, &document))91goto parser_error;9293/* Check if this is the stream end. */9495if (!yaml_document_get_root_node(&document)) {96done = 1;97}9899/* Emit the event. */100101if (!yaml_emitter_dump(&emitter, &document))102goto emitter_error;103}104105yaml_parser_delete(&parser);106yaml_emitter_delete(&emitter);107108return 0;109110parser_error:111112/* Display a parser error message. */113114switch (parser.error)115{116case YAML_MEMORY_ERROR:117fprintf(stderr, "Memory error: Not enough memory for parsing\n");118break;119120case YAML_READER_ERROR:121if (parser.problem_value != -1) {122fprintf(stderr, "Reader error: %s: #%X at %zd\n", parser.problem,123parser.problem_value, parser.problem_offset);124}125else {126fprintf(stderr, "Reader error: %s at %lu\n", parser.problem,127parser.problem_offset);128}129break;130131case YAML_SCANNER_ERROR:132if (parser.context) {133fprintf(stderr, "Scanner error: %s at line %lu, column %lu\n"134"%s at line %lu, column %lu\n", parser.context,135parser.context_mark.line+1, parser.context_mark.column+1,136parser.problem, parser.problem_mark.line+1,137parser.problem_mark.column+1);138}139else {140fprintf(stderr, "Scanner error: %s at line %lu, column %lu\n",141parser.problem, parser.problem_mark.line+1,142parser.problem_mark.column+1);143}144break;145146case YAML_PARSER_ERROR:147if (parser.context) {148fprintf(stderr, "Parser error: %s at line %lu, column %lu\n"149"%s at line %lu, column %lu\n", parser.context,150parser.context_mark.line+1, parser.context_mark.column+1,151parser.problem, parser.problem_mark.line+1,152parser.problem_mark.column+1);153}154else {155fprintf(stderr, "Parser error: %s at line %lu, column %lu\n",156parser.problem, parser.problem_mark.line+1,157parser.problem_mark.column+1);158}159break;160161case YAML_COMPOSER_ERROR:162if (parser.context) {163fprintf(stderr, "Composer error: %s at line %lu, column %lu\n"164"%s at line %lu, column %lu\n", parser.context,165parser.context_mark.line+1, parser.context_mark.column+1,166parser.problem, parser.problem_mark.line+1,167parser.problem_mark.column+1);168}169else {170fprintf(stderr, "Composer error: %s at line %lu, column %lu\n",171parser.problem, parser.problem_mark.line+1,172parser.problem_mark.column+1);173}174break;175176default:177/* Couldn't happen. */178fprintf(stderr, "Internal error\n");179break;180}181182yaml_parser_delete(&parser);183yaml_emitter_delete(&emitter);184185return 1;186187emitter_error:188189/* Display an emitter error message. */190191switch (emitter.error)192{193case YAML_MEMORY_ERROR:194fprintf(stderr, "Memory error: Not enough memory for emitting\n");195break;196197case YAML_WRITER_ERROR:198fprintf(stderr, "Writer error: %s\n", emitter.problem);199break;200201case YAML_EMITTER_ERROR:202fprintf(stderr, "Emitter error: %s\n", emitter.problem);203break;204205default:206/* Couldn't happen. */207fprintf(stderr, "Internal error\n");208break;209}210211yaml_parser_delete(&parser);212yaml_emitter_delete(&emitter);213214return 1;215}216217218219