Add yamldumper utility
Just dumps YAML parser events to the console. Handy for a few things I'm pondering...
This commit is contained in:
		
							parent
							
								
									1e73d839f3
								
							
						
					
					
						commit
						36a7954df4
					
				
					 3 changed files with 120 additions and 1 deletions
				
			
		
							
								
								
									
										8
									
								
								util/yamldumper/SConscript
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								util/yamldumper/SConscript
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
# SConscript
 | 
			
		||||
# vim: set ft=python:
 | 
			
		||||
# Eryn Wells <eryn@erynwells.me>
 | 
			
		||||
 | 
			
		||||
Import('env')
 | 
			
		||||
 | 
			
		||||
prog = env.Program('yamldumper', ['yamldumper.cc'], LIBS=['yaml'])
 | 
			
		||||
env.Alias('yamldumper', prog)
 | 
			
		||||
							
								
								
									
										101
									
								
								util/yamldumper/yamldumper.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								util/yamldumper/yamldumper.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,101 @@
 | 
			
		|||
/* yamldumper.cc
 | 
			
		||||
 * vim: set tw=80:
 | 
			
		||||
 * Eryn Wells <eryn@erynwells.me>
 | 
			
		||||
 */
 | 
			
		||||
/**
 | 
			
		||||
 * A small utility that reads a YAML file and dumps the parser events.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
#include "yaml.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
main(int argc,
 | 
			
		||||
     char* argv[])
 | 
			
		||||
{
 | 
			
		||||
    if (argc < 2) {
 | 
			
		||||
        fprintf(stderr, "Usage: %s [yaml_file ...]\n", argv[0]);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bool writeNewline = false;
 | 
			
		||||
    for (int i = 1; i < argc; i++) {
 | 
			
		||||
        if (writeNewline) {
 | 
			
		||||
            printf("\n");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        printf("--- %s\n", argv[i]);
 | 
			
		||||
 | 
			
		||||
        yaml_parser_t parser;
 | 
			
		||||
        yaml_parser_initialize(&parser);
 | 
			
		||||
 | 
			
		||||
        FILE *f = fopen(argv[i], "rb");
 | 
			
		||||
        if (!f) {
 | 
			
		||||
            fprintf(stderr, "Couldn't open file.\n");
 | 
			
		||||
        }
 | 
			
		||||
        yaml_parser_set_input_file(&parser, f);
 | 
			
		||||
 | 
			
		||||
        bool done = false;
 | 
			
		||||
        yaml_event_t event;
 | 
			
		||||
        while (!done) {
 | 
			
		||||
            if (!yaml_parser_parse(&parser, &event)) {
 | 
			
		||||
                fprintf(stderr, "Error parsing file.\n");
 | 
			
		||||
                goto error;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            switch (event.type) {
 | 
			
		||||
                case YAML_NO_EVENT:
 | 
			
		||||
                    std::cout << "YAML_NO_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case YAML_STREAM_START_EVENT:
 | 
			
		||||
                    std::cout << "YAML_STREAM_START_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
                case YAML_STREAM_END_EVENT:
 | 
			
		||||
                    std::cout << "YAML_STREAM_END_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case YAML_DOCUMENT_START_EVENT:
 | 
			
		||||
                    std::cout << "YAML_DOCUMENT_START_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
                case YAML_DOCUMENT_END_EVENT:
 | 
			
		||||
                    std::cout << "YAML_DOCUMENT_END_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case YAML_ALIAS_EVENT:
 | 
			
		||||
                    std::cout << "YAML_ALIAS_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
                case YAML_SCALAR_EVENT:
 | 
			
		||||
                    std::cout << "YAML_SCALAR_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case YAML_SEQUENCE_START_EVENT:
 | 
			
		||||
                    std::cout << "YAML_SEQUENCE_START_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
                case YAML_SEQUENCE_END_EVENT:
 | 
			
		||||
                    std::cout << "YAML_SEQUENCE_END_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case YAML_MAPPING_START_EVENT:
 | 
			
		||||
                    std::cout << "YAML_MAPPING_START_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
                case YAML_MAPPING_END_EVENT:
 | 
			
		||||
                    std::cout << "YAML_MAPPING_END_EVENT\n";
 | 
			
		||||
                    break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            done = (event.type == YAML_STREAM_END_EVENT);
 | 
			
		||||
            yaml_event_delete(&event);
 | 
			
		||||
        } /* while (!done) */
 | 
			
		||||
 | 
			
		||||
error:
 | 
			
		||||
        yaml_parser_delete(&parser);
 | 
			
		||||
        fclose(f);
 | 
			
		||||
        writeNewline = true;
 | 
			
		||||
    } /* for file : argv */
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue