yaml-cpp开源库使用

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

1.yaml-cpp编译

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

复制代码
mkdir build
cd build
cmake ...
make
make install
2.代码示例
cpp 复制代码
#include "funset.hpp"
#include <string>
#include <fstream>
#include <yaml-cpp/yaml.h>

int test_parse_yaml_file()
{
#ifdef _MSC_VER
    YAML::Node config = YAML::LoadFile("E:/GitCode/Messy_Test/testdata/test_yaml-cpp.yml");
#else
    YAML::Node config = YAML::LoadFile("testdata/test_yaml-cpp.yml");
#endif

    std::string blog_name{"xxxxx"}, blog_id{"xxxxx"}, blog_url{"xxxxx"};
    if (config["blog"])
    {
        if (config["blog"]["name"])
            blog_name = config["blog"]["name"].as<std::string>();
        if (config["blog"]["id"])
            blog_id = config["blog"]["id"].as<std::string>();
        if (config["blog"]["url"])
            blog_url = config["blog"]["url"].as<std::string>();
    }
    else
    {
        fprintf(stderr, "the node blog doesn't exist\n");
    }

    fprintf(stdout, "blog name: %s, id: %s, url: %s\n",
            blog_name.c_str(), blog_id.c_str(), blog_url.c_str());

    bool value1, value2;
    if (config["value1"])
        value1 = config["value1"].as<bool>();
    if (config["value2"])
        value2 = config["value2"].as<bool>();

    fprintf(stdout, "value1: %d, value2: %d\n", value1, value2);

    int number1;
    std::string number2, number3;
    float number4;
    if (config["number1"])
        number1 = config["number1"].as<int>();
    if (config["number2"])
        number2 = config["number2"].as<std::string>();
    if (config["number3"])
        number3 = config["number3"].as<std::string>();
    if (config["number4"])
        number4 = config["number4"].as<float>();
    fprintf(stdout, "number1: %d, number2: %s, number3: %s, number4: %f\n",
            number1, number2.c_str(), number3.c_str(), number4);

    std::string github_url, github_repos;
    if (config["github"])
        github_url = config["github"][0].as<std::string>();
    github_repos = config["github"][1].as<std::string>();
    fprintf(stdout, "github url: %s, repos: %s\n", github_url.c_str(), github_repos.c_str());

    return 0;
}

int test_generate_yaml_file()
{
    YAML::Node node;

    node["language"] = "cpp";
    node["version"] = 2;

    node["url"].push_back("https://blog.csdn.net/fengbingchun");
    node["url"].push_back("https://github.com/fengbingchun");

    YAML::Node primes = YAML::Load("[2, 3, 5, 7, 11]");
    primes.push_back(13);
    fprintf(stdout, "primes size: %d\n", primes.size());
    node["primes"] = primes;

    YAML::Node lineup = YAML::Load("{1B: Prince Fielder, 2B: Rickie Weeks, LF: Ryan Braun}");
    lineup["RF"] = "Corey Hart";
    lineup["C"] = "Jonathan Lucroy";
    node["name"] = lineup;

    node["platform"]["linux"].push_back("x86");
    node["platform"]["linux"].push_back("x86_64");
    node["platform"]["linux"].push_back("armv7");

    node["platform"]["windows"].push_back("x86");
    node["platform"]["windows"].push_back("x86_64");

#ifdef _MSC_VER
    std::ofstream fout("E:/GitCode/Messy_Test/testdata/tmp.yml");
#else
    std::ofstream fout("testdata/tmp.yaml");
#endif
    fout << node;

    return 0;
}

yml文件内容如下所示:

cpp 复制代码
blog:
  name: csdn
  id: fengbingchun
  url: https://blog.csdn.net/fengbingchun
 
commands:
  version:
  - g++ --version
  - cmake --version
  - git --version
 
value1: true
value2: false
value3: ~
 
number1: 123
number2: !!str 123
number3: "123"
number4: !!float 123
 
github: 
  - https://github.com/fengbingchun
  - NN_Test Face_Test OpenCV_Test
    Messy_Test CUDA_Test
 
data1: |
  There once
  was a
     short man
 
data2: >
  There once
  was a
     short man
 
date1: 2019-03-03
 
step: &id001   # defines anchor label &id001
  instrument:  Lasik 2000
  pulseEnergy: 5.4
 
step2: *id001   # refers to the first step (with anchor &id001)
相关推荐
Hommy881 小时前
【开源剪映小助手】项目概述
开源·剪映小助手·视频剪辑自动化
CJH(本人账号)1 小时前
AI Agent 安全危机:当你的“智能助手“变成攻击者的“远程武器“
网络·人工智能·安全·ai·开源·github
henrylin99991 小时前
开源量化软件功能介绍
python·开源
AI_零食2 小时前
鸿蒙原生 ArkTS:margin 溢出、Row 弹性分配与 alignItems 的交互
学习·华为·开源·harmonyos·鸿蒙·鸿蒙系统
爱上纯净的蓝天2 小时前
30 分钟上手 AtomCode:用它写一个 Python 批量整理文件/改名/生成报告小工具(新手教程)
python·开源·自动化脚本·atomcode·ai 编码助手
GitCode官方2 小时前
AtomGit 5月:下载中心上线;AtomCode Air 新品发布会顺利开展;AtomGit AI 荣获「昇腾开源合作杰出团队奖」
人工智能·开源·atomgit
m0_737302583 小时前
OpenClaw:让AI从对话走向落地的开源智能体新范式
人工智能·开源
xixixi777773 小时前
英伟达 Cosmos3 开源物理世界模型、国内具身智能评测标准落地、宇树冲刺人形机器人第一股|具身智能进入技术、标准、商业化三重爆发期
大数据·人工智能·ai·机器人·开源·英伟达·人形机器人
该昵称用户已存在3 小时前
技术栈无关化设计:MyEMS 能源中台的兼容层架构与开源
架构·开源·能源
咖啡星人k4 小时前
MonkeyCode 开源安全审计:第三方依赖风险管理与供应链安全
安全·开源·monkeycode