YamlCpp

A C++ wrapper for YAML based on libsyck. Allows users to parse and access YAML data in C++. Eventually it will allow users to create YAML data and write it to a file as well.

Currently this code hasn't been tested much, there is an example of how to use it in the tarball but, more testing needs to be done. Please provide feedback if you're able to.

Requirements:

Use this software at your own risk, it is still in it's early stages of development. If you have an suggestions for improvements or you need help in some way you can email me at: alex at this domain name

Documentation:

View the doxygen generated documentation here: doc.

Download:

Get the most recent tar.gz file here: the file directory

Donate:

If you find that this project is useful I would appreciate your support. Please go to my donations page for more information.

Example:

Below is an example C++ program that uses this api. A more in depth example is included in the download.


#include "parser.hpp"
#include <iostream>

class printit : public boost::static_visitor<> {
  public:
    //print each sequence elem
    void operator()(yaml::seq_ptr v) const {
      std::cout << "[" << std::endl;
      for(std::vector<yaml::node>::iterator it = v->begin(); it != v->end(); it++){
        std::cout << "\t";
        boost::apply_visitor(printit(), *it);
      }
      std::cout << "\t]" << std::endl;;
    }
    //print each map elem key => value
    void operator()(yaml::map_ptr m) const {
      for(yaml::map::iterator it = m->begin(); it != m->end(); it++){
        boost::apply_visitor(printit(), it->first);
        std::cout << " => ";
        boost::apply_visitor(printit(), it->second);
      }
    }
    void operator()(DateTime t) const {
      std::cout << t.tostr("%F %T %z") << std::endl;
    }
    //bool, int, double, std::string all have << defined so we don't
    //need special functions for them
    template <typename T>
    void operator()(T t) const {
      std::cout << t << std::endl;
    }
};

//this example prints parsed yaml data
//the yaml data comes from the file indicated by argv[1]
int main(int argc, char * argv[]){
  yaml::Parser y;
  yaml::node top;
  if(argc < 2)
    exit(-1);
  top = y.parseFile(argv[1]);
  boost::apply_visitor(printit(), top);
  return 0;
}

		

Links:

  • YAML (YAML Ain't Markup Language)
  • boost free peer-reviewed portable C++ source libraries.
  • my webpage