c++ 解析yaml文件的步骤

 更新时间:2020年12月15日 11:33:14   作者:li-peng  
这篇文章主要介绍了c++ 解析yaml文件的步骤,帮助大家更好的理解和使用c++,感兴趣的朋友可以了解下

作者:李鹏
出处:http://www.cnblogs.com/li-peng/

一直用c++操作ini做配置文件,想换成yaml,在全球最大的同性交友网站github上搜索,看有没有开源的库,功夫不负有心人,找到了yaml-cpp,用他解析了一个yaml的例子非常好使,分享一下如何使用他。
git clone git@github.com:jbeder/yaml-cpp.git下来编译成静态库

mkdir build
cd build
cmake ..
make

运行完后,会得到libyaml-cpp.a。
新建一个项目,结构大致如下

yaml_demo
 |__ include
     |__yaml-cpp 头文件夹
 |__ lib 
     |__yaml-cpp 库文件夹
 |__ main.cpp

配置CMakeLists.txt把头文件和静态库加到项目里,这样在编译和链接时才能通过

project(yaml_demo)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) # 二进制文件的输出目录
link_directories(${PROJECT_SOURCE_DIR}/lib/yaml-cpp)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(${PROJECT_NAME} yaml-cpp.a) 

对yaml-cpp的配置就完成了。看一下我的config文件

api: aaaaa
v: 1
label:
 app: hello
 image: abc
containers:
 - name: abc
  age: 18
 - name: 222
  age: 12

其中api和v是比较简单的键值,我们可以直接读取他们的值

  std::cout << "api: " << config["api"].as<std::string>() << std::endl;
  std::cout << "v: " << config["v"].as<int>() << std::endl;

label是一个map,containers是一个列表,这就要特殊处理一下,yaml-cpp有自己的转换模板

template <typename T>
struct convert;

在进行转换的时候他会判断有没有实现 decode方法

struct as_if<T, void> {
 explicit as_if(const Node& node_) : node(node_) {}
 const Node& node;

 T operator()() const {
  if (!node.m_pNode)
   throw TypedBadConversion<T>(node.Mark());

  T t;
  if (convert<T>::decode(node, t))
   return t;
  throw TypedBadConversion<T>(node.Mark());
 }
};

Node是yaml-cpp的核心,我们的配置的所有操作都从这个类中进行。
我们只要具体化自定义的struct就可以使用了

struct label {
  std::string app;
  std::string image;
};

namespace YAML {
  template<>
  struct convert<label> {
    static Node encode(const label &rhs) {
      Node node;
      node.push_back(rhs.app);
      node.push_back(rhs.image);
      return node;
    }

    static bool decode(const Node &node, label &rhs) {
      std::cout << node.Type() << std::endl;
      rhs.app = node["app"].as<std::string>();
      rhs.image = node["image"].as<std::string>();
      return true;
    }
  };
}

encode方法是把我们自定义的struct转换成yaml-cpp的Node,
转换时可以这样

if (config["label"]) {
  label l = config["label"].as<label>();
  std::cout << "app: " << l.app << " image: " << l.image << std::endl;
}

container也是一样的具体化

struct container {
  std::string name;
  int age;
};

namespace YAML {
  template<>
  struct convert<container> {
    static Node encode(const container &rhs) {
      Node node;
      node.push_back(rhs.name);
      node.push_back(rhs.age);
      return node;
    }

    static bool decode(const Node &node, container &rhs) {
      rhs.name = node["name"].as<std::string>();
      rhs.age = node["age"].as<int>();
      return true;
    }
  };
}

完整代码如下:

#include <string>
#include <iostream>
#include <yaml-cpp/yaml.h>
#include <yaml-cpp/node/parse.h>

struct container {
  std::string name;
  int age;
};

namespace YAML {
  template<>
  struct convert<container> {
    static Node encode(const container &rhs) {
      Node node;
      node.push_back(rhs.name);
      node.push_back(rhs.age);
      return node;
    }

    static bool decode(const Node &node, container &rhs) {
      rhs.name = node["name"].as<std::string>();
      rhs.age = node["age"].as<int>();
      return true;
    }
  };
}

struct label {
  std::string app;
  std::string image;
};

namespace YAML {
  template<>
  struct convert<label> {
    static Node encode(const label &rhs) {
      Node node;
      node.push_back(rhs.app);
      node.push_back(rhs.image);
      return node;
    }

    static bool decode(const Node &node, label &rhs) {
      std::cout << node.Type() << std::endl;
      rhs.app = node["app"].as<std::string>();
      rhs.image = node["image"].as<std::string>();
      return true;
    }
  };
}

int main(int argc, char **argv) {
  std::string config_path = "./config.yaml";
  std::cout << config_path << std::endl;
  YAML::Node config = YAML::LoadFile(config_path);

  std::cout << "api: " << config["api"].as<std::string>() << std::endl;
  std::cout << "v: " << config["v"].as<int>() << std::endl;

  if (config["label"]) {
    label l = config["label"].as<label>();
    std::cout << "app: " << l.app << " image: " << l.image << std::endl;
  }

  if (config["containers"]) {
    std::vector<container> vi = config["containers"].as<std::vector<container>>();

    for (std::vector<container>::iterator it = vi.begin(); it != vi.end(); ++it) {
      std::cout << "vector: name: " << it->name << " age: " << it->age << std::endl;
    }
  }

  return 0;
}

以上就是c++ 解析yaml文件的步骤的详细内容,更多关于c++ 解析yaml文件的资料请关注脚本之家其它相关文章!

相关文章

  • 深入理解C++函数栈帧

    深入理解C++函数栈帧

    本文主要介绍了C++函数栈帧,详细的介绍了C++函数栈帧的概念以及使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • 一文搞懂C++多态的用法

    一文搞懂C++多态的用法

    C++多态是在继承的基础上实现的,了解多态之前我们需要掌握一定的C++继承的知识,本文将介绍C++中多态的概念,构成条件以及用法,感兴趣的可以学习一下
    2022-04-04
  • C++回溯算法广度优先搜索举例分析

    C++回溯算法广度优先搜索举例分析

    回溯在迷宫搜索中使用很常见,就是这条路走不通,然后返回前一个路口,继续下一条路。回溯算法说白了就是穷举法,下面让我们一起来看看吧
    2022-03-03
  • 深入分析为Visual Assist设置快捷键的方法

    深入分析为Visual Assist设置快捷键的方法

    本篇文章是对为Visual Assist设置快捷键的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言实现车辆出租管理系统

    C语言实现车辆出租管理系统

    这篇文章主要为大家详细介绍了C语言实现车辆出租管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • C语言实现会员计费系统

    C语言实现会员计费系统

    这篇文章主要为大家详细介绍了C语言实现会员计费系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • C语言实现扫雷游戏(可以自动展开)

    C语言实现扫雷游戏(可以自动展开)

    这篇文章主要为大家详细介绍了C语言实现扫雷游戏,可以自动展开,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • C++使用easyX库实现三星环绕效果流程详解

    C++使用easyX库实现三星环绕效果流程详解

    EasyX是针对C/C++的图形库,可以帮助使用C/C++语言的程序员快速上手图形和游戏编程。这篇文章主要介绍了C++使用easyX库实现三星环绕效果,需要的可以参考一下
    2022-10-10
  • C++入门之vector使用详解

    C++入门之vector使用详解

    这篇文章主要为大家介绍了C++入门之vector使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-11-11
  • C++ STL容器与函数谓词示例分析讲解

    C++ STL容器与函数谓词示例分析讲解

    这篇文章主要介绍了C++ STL容器与函数谓词示例,STL是“Standard Template Library”的缩写,中文译为“标准模板库”。STL是C++标准库的一部分,不用单独安装
    2022-11-11

最新评论