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

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

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 :

  1. #include <yaml.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. void add_comment(yaml_emitter_t * emitter, char * comment) {
  6. yaml_emitter_flush(emitter);
  7. printf("\n%s", comment);
  8. }
  9. int yaml_list_emit(yaml_emitter_t *emitter, yaml_event_t *event, char* list_name, char ** list, int list_length) {
  10. yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
  11. (yaml_char_t *)list_name, strlen(list_name), 1, 0, YAML_PLAIN_SCALAR_STYLE);
  12. if (!yaml_emitter_emit(emitter, event)) goto error;
  13. yaml_sequence_start_event_initialize(event, NULL, (yaml_char_t *)YAML_SEQ_TAG,
  14. 1, YAML_FLOW_SEQUENCE_STYLE);
  15. if (!yaml_emitter_emit(emitter, event)) goto error;
  16. for (int i=0; i<list_length; i++) {
  17. yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
  18. (yaml_char_t *)list[i], strlen(list[i]), 1, 0, YAML_PLAIN_SCALAR_STYLE);
  19. if (!yaml_emitter_emit(emitter, event)) goto error;
  20. }
  21. yaml_sequence_end_event_initialize(event);
  22. if (!yaml_emitter_emit(emitter, event)) goto error;
  23. return EXIT_SUCCESS;
  24. error:
  25. fprintf(stderr, "Failed to emit event %d: %s\n", event->type, emitter->problem);
  26. yaml_emitter_delete(emitter);
  27. return EXIT_FAILURE;
  28. }
  29. int main(int argc, char *argv[]) {
  30. char* my_list1[]={"one","two"};
  31. int my_list1_length=2;
  32. char* my_list2[]={"aaa","bbb","ccc"};
  33. int my_list2_length=3;
  34. yaml_emitter_t emitter;
  35. yaml_event_t event;
  36. yaml_emitter_initialize(&emitter);
  37. yaml_emitter_set_output_file(&emitter, stdout);
  38. yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
  39. if (!yaml_emitter_emit(&emitter, &event)) goto error;
  40. yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
  41. if (!yaml_emitter_emit(&emitter, &event)) goto error;
  42. yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG,
  43. 1, YAML_ANY_MAPPING_STYLE);
  44. if (!yaml_emitter_emit(&emitter, &event)) goto error;
  45. add_comment(&emitter, "#my first list");
  46. if (yaml_list_emit(&emitter, &event, "list1", my_list1, my_list1_length)) return EXIT_FAILURE;
  47. add_comment(&emitter, "#my second list");
  48. if (yaml_list_emit(&emitter, &event, "list2", my_list2, my_list2_length)) return EXIT_FAILURE;
  49. yaml_mapping_end_event_initialize(&event);
  50. if (!yaml_emitter_emit(&emitter, &event)) goto error;
  51. yaml_document_end_event_initialize(&event, 0);
  52. if (!yaml_emitter_emit(&emitter, &event)) goto error;
  53. yaml_stream_end_event_initialize(&event);
  54. if (!yaml_emitter_emit(&emitter, &event)) goto error;
  55. yaml_emitter_delete(&emitter);
  56. return EXIT_SUCCESS;
  57. error:
  58. fprintf(stderr, "Failed to emit event %d: %s\n", event.type, emitter.problem);
  59. yaml_emitter_delete(&emitter);
  60. return EXIT_FAILURE;
  61. }

And the output is :

  1. ---
  2. #my first list
  3. list1: [one, two]
  4. #my second list
  5. list2: [aaa, bbb, ccc]
  6. ...

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 :

  1. #include &lt;yaml.h&gt;
  2. #include &lt;stdio.h&gt;
  3. #include &lt;stdlib.h&gt;
  4. #include &lt;string.h&gt;
  5. void add_comment(yaml_emitter_t * emitter, char * comment) {
  6. yaml_emitter_flush(emitter);
  7. printf(&quot;\n%s&quot;,comment);
  8. }
  9. int yaml_list_emit(yaml_emitter_t *emitter, yaml_event_t *event, char* list_name, char ** list, int list_length) {
  10. yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
  11. (yaml_char_t *)list_name, strlen(list_name), 1, 0, YAML_PLAIN_SCALAR_STYLE);
  12. if (!yaml_emitter_emit(emitter, event)) goto error;
  13. yaml_sequence_start_event_initialize(event, NULL, (yaml_char_t *)YAML_SEQ_TAG,
  14. 1, YAML_FLOW_SEQUENCE_STYLE);
  15. if (!yaml_emitter_emit(emitter, event)) goto error;
  16. for (int i=0; i&lt;list_length; i++) {
  17. yaml_scalar_event_initialize(event, NULL, (yaml_char_t *)YAML_STR_TAG,
  18. (yaml_char_t *)list[i], strlen(list[i]), 1, 0, YAML_PLAIN_SCALAR_STYLE);
  19. if (!yaml_emitter_emit(emitter, event)) goto error;
  20. }
  21. yaml_sequence_end_event_initialize(event);
  22. if (!yaml_emitter_emit(emitter, event)) goto error;
  23. return EXIT_SUCCESS;
  24. error:
  25. fprintf(stderr, &quot;Failed to emit event %d: %s\n&quot;, event-&gt;type, emitter-&gt;problem);
  26. yaml_emitter_delete(emitter);
  27. return EXIT_FAILURE;
  28. }
  29. int main(int argc, char *argv[]) {
  30. char* my_list1[]={&quot;one&quot;,&quot;two&quot;};
  31. int my_list1_length=2;
  32. char* my_list2[]={&quot;aaa&quot;,&quot;bbb&quot;,&quot;ccc&quot;};
  33. int my_list2_length=3;
  34. yaml_emitter_t emitter;
  35. yaml_event_t event;
  36. yaml_emitter_initialize(&amp;emitter);
  37. yaml_emitter_set_output_file(&amp;emitter, stdout);
  38. yaml_stream_start_event_initialize(&amp;event, YAML_UTF8_ENCODING);
  39. if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
  40. yaml_document_start_event_initialize(&amp;event, NULL, NULL, NULL, 0);
  41. if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
  42. yaml_mapping_start_event_initialize(&amp;event, NULL, (yaml_char_t *)YAML_MAP_TAG,
  43. 1, YAML_ANY_MAPPING_STYLE);
  44. if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
  45. add_comment(&amp;emitter, &quot;#my first list&quot;);
  46. if (yaml_list_emit(&amp;emitter, &amp;event, &quot;list1&quot;, my_list1, my_list1_length)) return EXIT_FAILURE;
  47. add_comment(&amp;emitter, &quot;#my second list&quot;);
  48. if (yaml_list_emit(&amp;emitter, &amp;event, &quot;list2&quot;, my_list2, my_list2_length)) return EXIT_FAILURE;
  49. yaml_mapping_end_event_initialize(&amp;event);
  50. if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
  51. yaml_document_end_event_initialize(&amp;event, 0);
  52. if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
  53. yaml_stream_end_event_initialize(&amp;event);
  54. if (!yaml_emitter_emit(&amp;emitter, &amp;event)) goto error;
  55. yaml_emitter_delete(&amp;emitter);
  56. return EXIT_SUCCESS;
  57. error:
  58. fprintf(stderr, &quot;Failed to emit event %d: %s\n&quot;, event.type, emitter.problem);
  59. yaml_emitter_delete(&amp;emitter);
  60. return EXIT_FAILURE;
  61. }

And the output is :

  1. ---
  2. #my first list
  3. list1: [one, two]
  4. #my second list
  5. list2: [aaa, bbb, ccc]
  6. ...

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

  1. 这些行是问题所在:
  2. yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
  3. /* 省略 */
  4. yaml_document_end_event_initialize(&event, 0);
  5. 最后一个参数`implicit`定义是否发出显式标记。您将其设置为`0`,这意味着*发出显式标记*。将其设置为`1`以避免标记。
  6. 如果您正在寻找C中的更高级API,您可以查看[libyaml_constructor](https://github.com/flyx/libyaml_constructor)(我的工作),它读取C头文件并自动生成使用libyaml来序列化/反序列化该文件中的结构的代码。请注意,`libyaml_constructor`已开发到能够完成其所需工作,并且在那之后没有进行维护。
英文:

These lines are the problem:

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

确定