Issue #8 invalid

Problem with emitter when emitting 2 subsequent yaml files

Anonymous created an issue

Hi! I'm using libyaml 0.1.5 on windows, building with mingw-builds (gcc 4.8.1). I have extrapolated the following code:

define YAML_DECLARE_STATIC

include <yaml.h>

include <string.h>

include <iostream>

static const char *input2 = "[a,b,c]";

static const char *input = "[1,2,3,4,5,6,7,8,9]";

static int stream_yaml_write_handler(void data, unsigned char buffer, size_t size) { std::ostream out = reinterpret_cast<std::ostream>(data); out << reinterpret_cast<char>(buffer); }

int main(int argc, char *argv) { yaml_parser_t p; yaml_document_t d; yaml_emitter_t e; yaml_parser_t p2; yaml_document_t d2; yaml_emitter_t e2; yaml_parser_initialize(&p); yaml_parser_set_input_string(&p, (const unsigned char)input, strlen(input)); yaml_parser_load(&p,&d);

yaml_parser_initialize(&p2);
yaml_parser_set_input_string(&p2, (const unsigned char*)input2, strlen(input2));
yaml_parser_load(&p2,&d2);

yaml_emitter_initialize(&e);
yaml_emitter_set_output(&e, &stream_yaml_write_handler, &std::cout);
yaml_emitter_open(&e);
yaml_emitter_dump(&e, &d);
yaml_emitter_close(&e);
yaml_emitter_delete(&e);

yaml_emitter_initialize(&e2);
yaml_emitter_set_output(&e2, &stream_yaml_write_handler, &std::cout);
yaml_emitter_open(&e2);
yaml_emitter_dump(&e2, &d2);
yaml_emitter_close(&e2);
yaml_emitter_delete(&e2);

}

This code, on my setup, gives that output:

[1, 2, 3, 4, 5, 6, 7, 8, 9] [a, b, c] 4, 5, 6, 7, 8, 9]

As you can see, the second emitter emits the correct yaml "[a, b, c]" and then emits some more wrong things; it just seems to emit a part of the first stream, but should really not happen as I'm using two different objects for everything (parser, emitter, document). Is there anything wrong in what I'm doing, or is that a libyaml bug? Thanks!

Davide

Comments (3)

  1. Davide Pippa

    Sorry, code has not been formatted very well :) Here it is

    #define YAML_DECLARE_STATIC
    #include <yaml.h>
    #include <string.h>
    #include <iostream>
    
    static const char *input2 = "[a,b,c]";
    
    static const char *input = "[1,2,3,4,5,6,7,8,9]";
    
    static int stream_yaml_write_handler(void *data, unsigned char *buffer, size_t size) {
        std::ostream* out = reinterpret_cast<std::ostream*>(data);
        *out << reinterpret_cast<char*>(buffer);
    }
    
    int main(int argc, char **argv) {
        yaml_parser_t p;
        yaml_document_t d;
        yaml_emitter_t e;
        yaml_parser_t p2;
        yaml_document_t d2;
        yaml_emitter_t e2;
        yaml_parser_initialize(&p);
        yaml_parser_set_input_string(&p, (const unsigned char*)input, strlen(input));
        yaml_parser_load(&p,&d);
    
        yaml_parser_initialize(&p2);
        yaml_parser_set_input_string(&p2, (const unsigned char*)input2, strlen(input2));
        yaml_parser_load(&p2,&d2);
    
        yaml_emitter_initialize(&e);
        yaml_emitter_set_output(&e, &stream_yaml_write_handler, &std::cout);
        yaml_emitter_open(&e);
        yaml_emitter_dump(&e, &d);
        yaml_emitter_close(&e);
        yaml_emitter_delete(&e);
    
        yaml_emitter_initialize(&e2);
        yaml_emitter_set_output(&e2, &stream_yaml_write_handler, &std::cout);
        yaml_emitter_open(&e2);
        yaml_emitter_dump(&e2, &d2);
        yaml_emitter_close(&e2);
        yaml_emitter_delete(&e2);
    }
    
  2. Log in to comment