/* Copyright (C) 2014 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /** @file src/xml.h * @brief Some useful XML helper functions. */ #ifndef LIBSUB_XML_H #define LIBSUB_XML_H #include "exceptions.h" #include namespace sub { template boost::shared_ptr optional_type_child (cxml::Node const & node, std::string name) { std::list > n = node.node_children (name); if (n.size() > 1) { throw XMLError ("duplicate XML tag"); } else if (n.empty ()) { return boost::shared_ptr (); } return boost::shared_ptr (new T (n.front ())); } template boost::shared_ptr type_child (boost::shared_ptr node, std::string name) { return boost::shared_ptr (new T (node->node_child (name))); } template boost::shared_ptr optional_type_child (boost::shared_ptr node, std::string name) { return optional_type_child (*node.get(), name); } template std::list > type_children (cxml::Node const & node, std::string name) { std::list > n = node.node_children (name); std::list > r; for (typename std::list >::iterator i = n.begin(); i != n.end(); ++i) { r.push_back (boost::shared_ptr (new T (*i))); } return r; } template std::list > type_children (boost::shared_ptr node, std::string name) { return type_children (*node.get(), name); } template std::list > type_grand_children (cxml::Node const & node, std::string name, std::string sub) { boost::shared_ptr p = node.node_child (name); return type_children (p, sub); } template std::list > type_grand_children (boost::shared_ptr node, std::string name, std::string sub) { return type_grand_children (*node.get(), name, sub); } } #endif