コード例 #1
0
ファイル: yaml.cpp プロジェクト: svddries/rsim
bool loadFromYAMLNode(const YAML::Node& node, Writer& config)
{

    for(YAML::const_iterator it = node.begin(); it != node.end(); ++it)
    {
        std::string key = it->first.as<std::string>();
        const YAML::Node& n = it->second;

        switch (n.Type())
        {
        case YAML::NodeType::Scalar:
        {
            std::string error;
            Variant v = yamlScalarToVariant(n, error);
            if (v.valid())
                config.setValue(key, v);
            else
                config.addError("While reading key '" + key +"': " + error);
            break;
        }
        case YAML::NodeType::Sequence:
        {
            config.writeArray(key);

            for(YAML::const_iterator it2 = n.begin(); it2 != n.end(); ++it2)
            {
                const YAML::Node& n2 = *it2;
                if (n2.Type() != YAML::NodeType::Map)
                {
                    config.addError("Sequences must only contains maps");
                    return false;
                }
                else
                {
                    config.addArrayItem();
                    loadFromYAMLNode(n2, config);
                    config.endArrayItem();
                }
            }

            config.endArray();

            break;
        }
        case YAML::NodeType::Map:
            config.writeGroup(key);
            loadFromYAMLNode(n, config);
            config.endGroup();
            break;
        case YAML::NodeType::Null:
            break;
        }
    }

    return true;
}