• yaml-cpp开源库使用


    源码下载:https://github.com/jbeder/yaml-cpp

    1.yaml-cpp编译

    步骤主要如下:进入源码目录后

    1. mkdir build
    2. cd build
    3. cmake …
    4. make
    5. make install
    2.代码示例
    1. #include "funset.hpp"
    2. #include
    3. #include
    4. #include
    5. int test_parse_yaml_file()
    6. {
    7. #ifdef _MSC_VER
    8. YAML::Node config = YAML::LoadFile("E:/GitCode/Messy_Test/testdata/test_yaml-cpp.yml");
    9. #else
    10. YAML::Node config = YAML::LoadFile("testdata/test_yaml-cpp.yml");
    11. #endif
    12. std::string blog_name{"xxxxx"}, blog_id{"xxxxx"}, blog_url{"xxxxx"};
    13. if (config["blog"])
    14. {
    15. if (config["blog"]["name"])
    16. blog_name = config["blog"]["name"].as();
    17. if (config["blog"]["id"])
    18. blog_id = config["blog"]["id"].as();
    19. if (config["blog"]["url"])
    20. blog_url = config["blog"]["url"].as();
    21. }
    22. else
    23. {
    24. fprintf(stderr, "the node blog doesn't exist\n");
    25. }
    26. fprintf(stdout, "blog name: %s, id: %s, url: %s\n",
    27. blog_name.c_str(), blog_id.c_str(), blog_url.c_str());
    28. bool value1, value2;
    29. if (config["value1"])
    30. value1 = config["value1"].as<bool>();
    31. if (config["value2"])
    32. value2 = config["value2"].as<bool>();
    33. fprintf(stdout, "value1: %d, value2: %d\n", value1, value2);
    34. int number1;
    35. std::string number2, number3;
    36. float number4;
    37. if (config["number1"])
    38. number1 = config["number1"].as<int>();
    39. if (config["number2"])
    40. number2 = config["number2"].as();
    41. if (config["number3"])
    42. number3 = config["number3"].as();
    43. if (config["number4"])
    44. number4 = config["number4"].as<float>();
    45. fprintf(stdout, "number1: %d, number2: %s, number3: %s, number4: %f\n",
    46. number1, number2.c_str(), number3.c_str(), number4);
    47. std::string github_url, github_repos;
    48. if (config["github"])
    49. github_url = config["github"][0].as();
    50. github_repos = config["github"][1].as();
    51. fprintf(stdout, "github url: %s, repos: %s\n", github_url.c_str(), github_repos.c_str());
    52. return 0;
    53. }
    54. int test_generate_yaml_file()
    55. {
    56. YAML::Node node;
    57. node["language"] = "cpp";
    58. node["version"] = 2;
    59. node["url"].push_back("https://blog.csdn.net/fengbingchun");
    60. node["url"].push_back("https://github.com/fengbingchun");
    61. YAML::Node primes = YAML::Load("[2, 3, 5, 7, 11]");
    62. primes.push_back(13);
    63. fprintf(stdout, "primes size: %d\n", primes.size());
    64. node["primes"] = primes;
    65. YAML::Node lineup = YAML::Load("{1B: Prince Fielder, 2B: Rickie Weeks, LF: Ryan Braun}");
    66. lineup["RF"] = "Corey Hart";
    67. lineup["C"] = "Jonathan Lucroy";
    68. node["name"] = lineup;
    69. node["platform"]["linux"].push_back("x86");
    70. node["platform"]["linux"].push_back("x86_64");
    71. node["platform"]["linux"].push_back("armv7");
    72. node["platform"]["windows"].push_back("x86");
    73. node["platform"]["windows"].push_back("x86_64");
    74. #ifdef _MSC_VER
    75. std::ofstream fout("E:/GitCode/Messy_Test/testdata/tmp.yml");
    76. #else
    77. std::ofstream fout("testdata/tmp.yaml");
    78. #endif
    79. fout << node;
    80. return 0;
    81. }

    yml文件内容如下所示:

    1. blog:
    2. name: csdn
    3. id: fengbingchun
    4. url: https://blog.csdn.net/fengbingchun
    5. commands:
    6. version:
    7. - g++ --version
    8. - cmake --version
    9. - git --version
    10. value1: true
    11. value2: false
    12. value3: ~
    13. number1: 123
    14. number2: !!str 123
    15. number3: "123"
    16. number4: !!float 123
    17. github:
    18. - https://github.com/fengbingchun
    19. - NN_Test Face_Test OpenCV_Test
    20. Messy_Test CUDA_Test
    21. data1: |
    22. There once
    23. was a
    24. short man
    25. data2: >
    26. There once
    27. was a
    28. short man
    29. date1: 2019-03-03
    30. step: &id001 # defines anchor label &id001
    31. instrument: Lasik 2000
    32. pulseEnergy: 5.4
    33. step2: *id001 # refers to the first step (with anchor &id001)

  • 相关阅读:
    java计算机毕业设计springboot+vue的在线课程教学网站 视频考试等功能 elementUI
    JAVA面向对象三大特征
    技术分享 | 静态扫描体系集成
    【轮式平衡机器人】——TMS320F28069片内外设之ePWM
    [Hackthebox] Dancing (SMB)
    上海再发区块链专项方案 和数集团欲打造新一代Web3.0创新生态
    实训笔记8.31
    [lettcode top 100] 0917 两数之和,有效的括号
    不是吧,还有人连Java最强大的技术之一:反射还没搞懂?赶紧码住
    leetcode-链表类题目
  • 原文地址:https://blog.csdn.net/weixin_55238862/article/details/136285424