Stef1611
Stef1611

Reputation: 2387

Libyaml : How to directly remove the three dashes at the start and the three periods at the end?

I am trying to use the libyaml library to generate a simple yaml file.
I wrote this code :

#include <yaml.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void add_comment(yaml_emitter_t * emitter, char * comment) {
    yaml_emitter_flush(emitter);
    printf("\n%s",comment);

}

int yaml_list_emit(yaml_emitter_t *emitter, yaml_event_t *event, char* list_name, char ** list, int list_length) {

    yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
        (yaml_char_t *)list_name, strlen(list_name), 1, 0, YAML_PLAIN_SCALAR_STYLE);
    if (!yaml_emitter_emit(emitter, event)) goto error;
    
    yaml_sequence_start_event_initialize(event, NULL, (yaml_char_t *)YAML_SEQ_TAG,
       1,  YAML_FLOW_SEQUENCE_STYLE);
    if (!yaml_emitter_emit(emitter, event)) goto error;
    
    for (int i=0; i<list_length; i++) {
        yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
            (yaml_char_t *)list[i], strlen(list[i]), 1, 0, YAML_PLAIN_SCALAR_STYLE);
        if (!yaml_emitter_emit(emitter, event)) goto error;
    }

    yaml_sequence_end_event_initialize(event);
    if (!yaml_emitter_emit(emitter, event)) goto error;
    return EXIT_SUCCESS;
    
error:
    fprintf(stderr, "Failed to emit event %d: %s\n", event->type, emitter->problem);
    yaml_emitter_delete(emitter);
    return EXIT_FAILURE;
}


int main(int argc, char *argv[]) {
    char* my_list1[]={"one","two"};
    int my_list1_length=2;
    char* my_list2[]={"aaa","bbb","ccc"};
    int my_list2_length=3;
    
    yaml_emitter_t emitter;
    yaml_event_t event;
    
    yaml_emitter_initialize(&emitter);
    yaml_emitter_set_output_file(&emitter, stdout);

    yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
    if (!yaml_emitter_emit(&emitter, &event)) goto error;

    yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
    if (!yaml_emitter_emit(&emitter, &event)) goto error;
           
    yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
        1, YAML_ANY_MAPPING_STYLE);
    if (!yaml_emitter_emit(&emitter, &event)) goto error;

   
    add_comment(&emitter, "#my first list");
    if (yaml_list_emit(&emitter, &event, "list1", my_list1, my_list1_length)) return EXIT_FAILURE;

    add_comment(&emitter, "#my second list");
    if (yaml_list_emit(&emitter, &event, "list2", my_list2, my_list2_length)) return EXIT_FAILURE;

        
    yaml_mapping_end_event_initialize(&event);
    if (!yaml_emitter_emit(&emitter, &event)) goto error; 
    
    yaml_document_end_event_initialize(&event, 0);
    if (!yaml_emitter_emit(&emitter, &event)) goto error;

    yaml_stream_end_event_initialize(&event);
    if (!yaml_emitter_emit(&emitter, &event)) goto error;

    yaml_emitter_delete(&emitter);
    return EXIT_SUCCESS;

error:
    fprintf(stderr, "Failed to emit event %d: %s\n", event.type, emitter.problem);
    yaml_emitter_delete(&emitter);
    return EXIT_FAILURE;
}

And the output is :

---
#my first list
list1: [one, two]
#my second list
list2: [aaa, bbb, ccc]
...

I wondered if an option exists to remove the three dashes at the start and the three periods at the end of the document ?
(n.b. : I know how to process the file after it has been generated but I would like to do it directly)
I think I do not use the good library to generate such simple yaml files. Indeed, for example, I wrote functions to add comments, to emit a list, ... Perhaps, I am reinventing the wheel but I was not able to find a higher level library.

Upvotes: 0

Views: 99

Answers (1)

flyx
flyx

Reputation: 39738

These lines are the problem:

yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
/* snip */
yaml_document_end_event_initialize(&event, 0);

The last parameter, implicit, defines whether to emit explicit markers or not. You set it to 0 which means emit explicit markers. Set it to 1 to avoid the markers.

If you're looking for a higher-level API in C, you can check out libyaml_constructor (my work) which reads a C header file and autogenerates code that uses libyaml to serialize/deserialize the structures in that file. Beware that libyaml_constructor has been developed until it was able to do what it needed to, and never saw any maintenance beyond that.

Upvotes: 1

Related Questions