Parse YAML Files in C++

后端 未结 4 1343
醉话见心
醉话见心 2020-12-07 18:28

I want a simple tutorial to show me to load a yaml file and parse the data. Expat style would be great but any solution that actually shows me the data in some form would be

4条回答
  •  眼角桃花
    2020-12-07 18:54

    A Google Code Search (now defunct) for "yaml load lang:c++" gave this as the first link: demo.cc:

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    // What should libyaml++ do when a YAML entity is parsed?
    // NOTE:  if any of the event handlers is not defined, a respective default
    // no-op handler will be used.  For example, not defining on_eos() is
    // equivalent to defining void on_eos() { }.
    class my_handler : public yaml::event_handler {
        void on_string(const std::string& s) { cout << "parsed string:  " << s << endl; }
        void on_integer(const std::string& s) { cout << "parsed integer:  " << s << endl; }
        void on_sequence_begin() { cout << "parsed sequence-begin." << endl; }
        void on_mapping_begin() { cout << "parsed mapping-begin." << endl; }
        void on_sequence_end() { cout << "parsed sequence-end." << endl; }
        void on_mapping_end() { cout << "parsed mapping-end." << endl; }
        void on_document() { cout << "parsed document." << endl; }
        void on_pair() { cout << "parsed pair." << endl; }
        void on_eos() { cout << "parsed eos." << endl; }
    };
    
    // ok then, now that i know how to behave on each YAML entity encountered, just
    // give me a stream to parse!
    int main(int argc, char* argv[])
    {
        tr1::shared_ptr handler(new my_handler());
        while( cin ) {
            try { yaml::load(cin, handler); } // throws on syntax error
    
            catch( const runtime_error& e ) {
                cerr << e.what() << endl;
            }
        }
        return 0;
    }
    

提交回复
热议问题