Libyaml:如何直接删除开头的三个破折号和结尾的三个句点?

huangapple go评论73阅读模式
英文:

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.

英文:

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

#include &lt;yaml.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
void add_comment(yaml_emitter_t * emitter, char * comment) {
yaml_emitter_flush(emitter);
printf(&quot;\n%s&quot;,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&lt;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, &quot;Failed to emit event %d: %s\n&quot;, event-&gt;type, emitter-&gt;problem);
yaml_emitter_delete(emitter);
return EXIT_FAILURE;
}
int main(int argc, char *argv[]) {
char* my_list1[]={&quot;one&quot;,&quot;two&quot;};
int my_list1_length=2;
char* my_list2[]={&quot;aaa&quot;,&quot;bbb&quot;,&quot;ccc&quot;};
int my_list2_length=3;
yaml_emitter_t emitter;
yaml_event_t event;
yaml_emitter_initialize(&amp;emitter);
yaml_emitter_set_output_file(&amp;emitter, stdout);
yaml_stream_start_event_initialize(&amp;event, YAML_UTF8_ENCODING);
if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
yaml_document_start_event_initialize(&amp;event, NULL, NULL, NULL, 0);
if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
yaml_mapping_start_event_initialize(&amp;event, NULL, (yaml_char_t *)YAML_MAP_TAG,
1, YAML_ANY_MAPPING_STYLE);
if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
add_comment(&amp;emitter, &quot;#my first list&quot;);
if (yaml_list_emit(&amp;emitter, &amp;event, &quot;list1&quot;, my_list1, my_list1_length)) return EXIT_FAILURE;
add_comment(&amp;emitter, &quot;#my second list&quot;);
if (yaml_list_emit(&amp;emitter, &amp;event, &quot;list2&quot;, my_list2, my_list2_length)) return EXIT_FAILURE;
yaml_mapping_end_event_initialize(&amp;event);
if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error; 
yaml_document_end_event_initialize(&amp;event, 0);
if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
yaml_stream_end_event_initialize(&amp;event);
if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
yaml_emitter_delete(&amp;emitter);
return EXIT_SUCCESS;
error:
fprintf(stderr, &quot;Failed to emit event %d: %s\n&quot;, event.type, emitter.problem);
yaml_emitter_delete(&amp;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 ? <br/>
(n.b. : I know how to process the file after it has been generated but I would like to do it directly)<br/>
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.

答案1

得分: 1

这些行是问题所在:

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

最后一个参数`implicit`定义是否发出显式标记。您将其设置为`0`,这意味着*发出显式标记*。将其设置为`1`以避免标记。

如果您正在寻找C中的更高级API,您可以查看[libyaml_constructor](https://github.com/flyx/libyaml_constructor)(我的工作),它读取C头文件并自动生成使用libyaml来序列化/反序列化该文件中的结构的代码。请注意,`libyaml_constructor`已开发到能够完成其所需工作,并且在那之后没有进行维护。
英文:

These lines are the problem:

yaml_document_start_event_initialize(&amp;event, NULL, NULL, NULL, 0);
/* snip */
yaml_document_end_event_initialize(&amp;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.

huangapple
  • 本文由 发表于 2023年4月19日 18:53:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定