Yaml-CPP -- getting started with C ++ project configuration tool

Source: Internet
Author: User

Reference

Http://code.google.com/p/yaml-cpp/

What is yaml?

Yaml ain't markup language, yaml is not a markup language. Yaml naming is recursive, similar to GNU's not UNIX.

YAML is a human friendly data serialization  standard for all programming languages.
Yaml is used to compile the project configuration file. Its advantages are as follows:
http://zh-cn.w3support.net/index.php?db=so&id=1308536
http://www.ibm.com/developerworks/cn/xml/x-matters/part23/
 

A complete example

Understand this complete demo, yaml is getting started...

Here's a complete example of how to parse a complex yaml file:

Monsters. yaml // This is the yaml file, and the suffix must be 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

About the configuration file
1. Similar to Python, blank indentation indicates a structure. Note that it is not tab indent.
2. Start with-. Each row in the next row is a member of this structure. You can also write multiple members in the same row and mark them.
Main. cpp

# Include "yaml-CPP/yaml. H "// install yaml-CPP refer to the Google Code homepage # include <iostream> # include <fstream> # include <string> # include <vector> // Our data types // the example is taken from the open-source game engine ogre, struct vec3 {// float x, y, z ;}; struct power {// trick, magic STD: string name; // trick name, for example, sunflower collection int damage; // Damage Value}; struct monster {// monster STD: string name; vec3 position; STD: vector <power> powers ;}; // now the extraction operators These types // overload> budget operator .... 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 (unsig Ned I = 0; I <powers. size (); I ++) {power; powers [I]> power; monster. powers. push_back (Power) ;}} int main () // test program {STD: ifstream fin ("monsters. yaml "); // read the yaml configuration file. Yaml: parser Parser (FIN); // configuration file of the yaml analysis input. Error throw yaml: parserexception yaml: node Doc; parser. getnextdocument (DOC); // Doc is our yaml configuration file for (unsigned I = 0; I <Doc. size (); I ++) {// the actual value of I is 0, 1, 2; three big struct values associated with yaml: ogre, dragon, and Wizard monster; doc [I]> monster; STD: cout <monster. name <"\ n";} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.