Reference
http://code.google.com/p/yaml-cpp/
What is yaml?
YAML Ain't Markup Language, yaml不是标记语言。yaml命名是递归的,类似于GNU's Not Unix.
YAML is a human friendly data serialization standard for all programming languages.
yaml 用来编写项目的配置文件,优势如下,友好,简明:
http://zh-cn.w3support.net/index.php?db=so&id=1308536
http://www.ibm.com/developerworks/cn/xml/x-matters/part23/
看懂这个完整的演示程序,yaml 就入门了。。。
Here's a complete example of how to parse a complex YAML file:
monsters.yaml //这是yaml文件,后缀必须是yaml
- name: Ogre
position: [0, 5, 0]
powers:
- name: Club
damage: 10
- name: Fist
damage: 8
- name: Dragon
position: [1, 0, 10]
powers:
- name: Fire Breath
damage: 25
- name: Claws
damage: 15
- name: Wizard
position: [5, -3, 0]
powers:
- name: Acid Rain
damage: 50
- name: Staff
damage: 3
关于配置文件
#include "yaml-cpp/yaml.h" //安装yaml-cpp参考google code 主页
#include
#include
#include
#include
// our data types //这个例子好像是取自开源游戏引擎ogre,随便说的
struct Vec3 { //位置坐标
float x, y, z;
};
struct Power { //招式,魔法
std::string name; //招式名字,如 葵花宝典
int damage; //伤害值
};
struct Monster { //怪兽
std::string name;
Vec3 position;
std::vector powers;
};
// now the extraction operators for these types //重载 >> 预算符。。。。
void operator >> (const YAML::Node& node, Vec3& v) {
node[0] >> v.x;
node[1] >> v.y;
node[2] >> v.z;
}
void operator >> (const YAML::Node& node, Power& power) {
node["name"] >> power.name;
node["damage"] >> power.damage;
}
void operator >> (const YAML::Node& node, Monster& monster) {
node["name"] >> monster.name;
node["position"] >> monster.position;
const YAML::Node& powers = node["powers"];
for(unsigned i=0;i> power;
monster.powers.push_back(power);
}
}
int main() //测试程序
{
std::ifstream fin("monsters.yaml"); // 读入yaml配置文件。
YAML::Parser parser(fin); //yaml 分析输入的配文件。出错抛出YAML::ParserException
YAML::Node doc;
parser.GetNextDocument(doc); //doc 就是我们的yaml配置文件
for(unsigned i=0;i> monster;
std::cout << monster.name << "\n";
}
return 0;
}