From d621aa4296dfab3cb878f694c6d63c73fe9cb763 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Mon, 7 May 2012 00:03:33 +0800 Subject: [PATCH] improved the node printing function --- libgrive/src/xml/Node.cc | 58 ++++++++++++++++++++++++++--------- libgrive/src/xml/Node.hh | 21 +++++++------ libgrive/test/xml/NodeTest.cc | 2 ++ 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/libgrive/src/xml/Node.cc b/libgrive/src/xml/Node.cc index eb1a21a..c2305b4 100644 --- a/libgrive/src/xml/Node.cc +++ b/libgrive/src/xml/Node.cc @@ -135,6 +135,11 @@ public : return m_children.end() ; } + std::pair Attr() + { + return std::make_pair( m_attr.begin(), m_attr.end() ) ; + } + const std::string& Name() const { return m_name ; @@ -177,6 +182,10 @@ private : ImplVec m_children ; } ; +Node::iterator::iterator( ) +{ +} + Node::iterator::iterator( ImplVec::iterator it ) : m_it( it ) { } @@ -332,37 +341,56 @@ std::string Node::Value() const return m_ptr->Value() ; } -std::vector Node::Children() const -{ - std::vector result ; - for ( Impl::iterator i = m_ptr->Begin() ; i != m_ptr->End() ; ++i ) - result.push_back( Node( (*i)->AddRef() ) ) ; - - return result ; -} - std::ostream& operator<<( std::ostream& os, const Node& node ) { if ( node.GetType() == Node::element ) { - os << '<' << node.Name() << ' ' ; + os << '<' << node.Name() ; + + // print attributes + Node::Range attrs = node.Attr() ; + if ( attrs.first != attrs.second ) + os << ' ' ; + + std::copy( attrs.first, attrs.second, std::ostream_iterator(os, " ") ) ; + os << '>' ; + + // recursively print children + for ( Node::iterator i = node.begin() ; i != node.end() ; ++i ) + { + if ( (*i).GetType() != Node::attr ) + os << *i ; + } + os << "' ; + } + else if ( node.GetType() == Node::attr ) + { + os << node.Name() << "=\"" << node.Value() << "\"" ; + } + else + { + os << node.Value() ; } - std::vector c = node.Children() ; - - std::copy( c.begin(), c.end(), std::ostream_iterator(os, "\n") ) ; return os ; } -Node::iterator Node::begin() +Node::iterator Node::begin() const { return iterator( m_ptr->Begin() ) ; } -Node::iterator Node::end() +Node::iterator Node::end() const { return iterator( m_ptr->End() ) ; } +Node::Range Node::Attr() const +{ + std::pair is = m_ptr->Attr() ; + + return std::make_pair( iterator(is.first), iterator(is.second) ) ; +} + } } // end namespace diff --git a/libgrive/src/xml/Node.hh b/libgrive/src/xml/Node.hh index 38f8cfb..a3b6709 100644 --- a/libgrive/src/xml/Node.hh +++ b/libgrive/src/xml/Node.hh @@ -21,6 +21,7 @@ #include #include +#include namespace gr { namespace xml { @@ -28,6 +29,7 @@ class Node { public : class iterator ; + typedef std::pair Range ; public : Node() ; @@ -57,14 +59,10 @@ public : static bool IsCompatible( Type parent, Type child ) ; - // TODO: implement iterator begin/end functions instead - std::vector Children() const ; + iterator begin() const ; + iterator end() const ; - // TODO: implement iterator begin/end functions instead - std::vector Attr() const ; - - iterator begin() ; - iterator end() ; + Range Attr() const ; private : class Impl ; @@ -74,10 +72,15 @@ public : class iterator { public : + iterator() ; explicit iterator( std::vector< gr::xml::Node::Impl* >::iterator it ) ; - - typedef Node value_type ; + typedef Node value_type ; + typedef std::forward_iterator_tag iterator_category ; + typedef std::ptrdiff_t difference_type; + typedef Node* pointer; + typedef Node& reference; + value_type operator*() const ; iterator operator++() ; iterator operator++(int) ; diff --git a/libgrive/test/xml/NodeTest.cc b/libgrive/test/xml/NodeTest.cc index f6f7d1f..fe6ef05 100644 --- a/libgrive/test/xml/NodeTest.cc +++ b/libgrive/test/xml/NodeTest.cc @@ -69,6 +69,8 @@ void NodeTest::TestParseFile( ) CPPUNIT_ASSERT_EQUAL( std::string("href"), (*i).Name() ) ; ++i ; } + + std::cout << n << std::endl ; } } // end of namespace grut