Path: blob/main/contrib/libyaml/tests/example-reformatter.c
39535 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 event;1819/* Clear the objects. */2021memset(&parser, 0, sizeof(parser));22memset(&emitter, 0, sizeof(emitter));23memset(&event, 0, sizeof(event));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_parse(&parser, &event))91goto parser_error;9293/* Check if this is the stream end. */9495if (event.type == YAML_STREAM_END_EVENT) {96done = 1;97}9899/* Emit the event. */100101if (!yaml_emitter_emit(&emitter, &event))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 %ld\n", parser.problem,123parser.problem_value, (long)parser.problem_offset);124}125else {126fprintf(stderr, "Reader error: %s at %ld\n", parser.problem,127(long)parser.problem_offset);128}129break;130131case YAML_SCANNER_ERROR:132if (parser.context) {133fprintf(stderr, "Scanner error: %s at line %d, column %d\n"134"%s at line %d, column %d\n", parser.context,135(int)parser.context_mark.line+1, (int)parser.context_mark.column+1,136parser.problem, (int)parser.problem_mark.line+1,137(int)parser.problem_mark.column+1);138}139else {140fprintf(stderr, "Scanner error: %s at line %d, column %d\n",141parser.problem, (int)parser.problem_mark.line+1,142(int)parser.problem_mark.column+1);143}144break;145146case YAML_PARSER_ERROR:147if (parser.context) {148fprintf(stderr, "Parser error: %s at line %d, column %d\n"149"%s at line %d, column %d\n", parser.context,150(int)parser.context_mark.line+1, (int)parser.context_mark.column+1,151parser.problem, (int)parser.problem_mark.line+1,152(int)parser.problem_mark.column+1);153}154else {155fprintf(stderr, "Parser error: %s at line %d, column %d\n",156parser.problem, (int)parser.problem_mark.line+1,157(int)parser.problem_mark.column+1);158}159break;160161default:162/* Couldn't happen. */163fprintf(stderr, "Internal error\n");164break;165}166167yaml_parser_delete(&parser);168yaml_emitter_delete(&emitter);169170return 1;171172emitter_error:173174/* Display an emitter error message. */175176switch (emitter.error)177{178case YAML_MEMORY_ERROR:179fprintf(stderr, "Memory error: Not enough memory for emitting\n");180break;181182case YAML_WRITER_ERROR:183fprintf(stderr, "Writer error: %s\n", emitter.problem);184break;185186case YAML_EMITTER_ERROR:187fprintf(stderr, "Emitter error: %s\n", emitter.problem);188break;189190default:191/* Couldn't happen. */192fprintf(stderr, "Internal error\n");193break;194}195196yaml_parser_delete(&parser);197yaml_emitter_delete(&emitter);198199return 1;200}201202203204