From 444bf61c73b4f3b3a0fceaf1b799ed46b534cda1 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Sat, 12 May 2012 12:27:35 +0800 Subject: [PATCH] added copy ctor & op= to NodeSet --- libgrive/src/drive/Entry.cc | 28 ++++++++++++++++++++-------- libgrive/src/drive/Entry.hh | 2 ++ libgrive/src/xml/Node.cc | 7 ++++++- libgrive/src/xml/Node.hh | 1 + libgrive/src/xml/NodeSet.cc | 25 +++++++++++++++++++++++-- libgrive/src/xml/NodeSet.hh | 4 ++++ libgrive/test/Assert.hh | 2 +- libgrive/test/drive/EntryTest.cc | 6 ++++++ 8 files changed, 63 insertions(+), 12 deletions(-) diff --git a/libgrive/src/drive/Entry.cc b/libgrive/src/drive/Entry.cc index 37ac4c2..518b81e 100644 --- a/libgrive/src/drive/Entry.cc +++ b/libgrive/src/drive/Entry.cc @@ -95,15 +95,22 @@ void Entry::Update( const Json& entry ) void Entry::Update( const xml::Node& n ) { - m_title = n["title"] ; - m_etag = n["@gd:etag"] ; - m_filename = n["docs:suggestedFilename"] ; - m_content_src = n["content"]["@src"] ; - m_self_href = n["link"].Find( "@rel", "self" )["@href"] ; - + m_title = n["title"] ; + m_etag = n["@gd:etag"] ; + m_filename = n["docs:suggestedFilename"] ; + m_content_src = n["content"]["@src"] ; + m_self_href = n["link"].Find( "@rel", "self" )["@href"] ; + m_server_modified = DateTime( n["updated"] ) ; + + m_resource_id = n["gd:resourceId"] ; + m_server_md5 = n["docs:md5Checksum"] ; + m_kind = n["category"].Find( "@scheme", "http://schemas.google.com/g/2005#kind" )["@label"] ; + m_upload_link = n["link"].Find( "!rel", "http://schemas.google.com/g/2005#resumable-edit-media")["href"] ; + + m_parent_hrefs.clear( ) ; xml::NodeSet parents = n["link"].Find( "@rel", "http://schemas.google.com/docs/2007#parent" ) ; - m_parent_hrefs.resize( parents.size() ) ; - std::copy( parents.begin(), parents.end(), m_parent_hrefs.begin() ) ; + for ( xml::NodeSet::iterator i = parents.begin() ; i != parents.end() ; ++i ) + m_parent_hrefs.push_back( (*i)["@href"] ) ; } std::string Entry::Parent( const Json& entry ) @@ -113,6 +120,11 @@ std::string Entry::Parent( const Json& entry ) node["href"].Str() : std::string() ; } +const std::vector& Entry::ParentHrefs() const +{ + return m_parent_hrefs ; +} + std::string Entry::Title() const { return m_title ; diff --git a/libgrive/src/drive/Entry.hh b/libgrive/src/drive/Entry.hh index 81f0466..9d69702 100644 --- a/libgrive/src/drive/Entry.hh +++ b/libgrive/src/drive/Entry.hh @@ -62,6 +62,8 @@ public : std::string SelfHref() const ; std::string ParentHref() const ; + const std::vector& ParentHrefs() const ; + void Download( gr::http::Agent* http, const Path& file, const http::Headers& auth ) const ; bool Upload( gr::http::Agent* http, std::streambuf *file, const http::Headers& auth ) ; void Delete( gr::http::Agent* http, const gr::http::Headers& auth ) ; diff --git a/libgrive/src/xml/Node.cc b/libgrive/src/xml/Node.cc index f3f2336..1fe071e 100644 --- a/libgrive/src/xml/Node.cc +++ b/libgrive/src/xml/Node.cc @@ -238,10 +238,15 @@ Node::~Node() Node& Node::operator=( const Node& node ) { Node tmp( node ) ; - std::swap( tmp.m_ptr, m_ptr ) ; + Swap( tmp ) ; return *this ; } +void Node::Swap( Node& node ) +{ + std::swap( node.m_ptr, m_ptr ) ; +} + bool Node::IsCompatible( Type parent, Type child ) { static const bool map[][3] = diff --git a/libgrive/src/xml/Node.hh b/libgrive/src/xml/Node.hh index 3c972e2..d8c6055 100644 --- a/libgrive/src/xml/Node.hh +++ b/libgrive/src/xml/Node.hh @@ -48,6 +48,7 @@ public : static Node Text( const std::string& name ) ; Node& operator=( const Node& node ) ; + void Swap( Node& node ) ; Node AddElement( const std::string& name ) ; Node AddText( const std::string& text ) ; diff --git a/libgrive/src/xml/NodeSet.cc b/libgrive/src/xml/NodeSet.cc index f66dfa1..85086aa 100644 --- a/libgrive/src/xml/NodeSet.cc +++ b/libgrive/src/xml/NodeSet.cc @@ -37,7 +37,28 @@ NodeSet::NodeSet( iterator first, iterator last ) : m_last( last ) { } - + +NodeSet::NodeSet( const NodeSet& n ) : + m_tmp( n.m_tmp ), + m_first( m_tmp.begin() + (n.m_first - n.m_tmp.begin()) ), + m_last( m_tmp.begin() + (n.m_last - n.m_tmp.begin()) ) +{ +} + +NodeSet& NodeSet::operator=( const NodeSet& ns ) +{ + NodeSet tmp( ns ) ; + Swap( tmp ) ; + return *this ; +} + +void NodeSet::Swap( NodeSet& ns ) +{ + m_tmp.Swap( ns.m_tmp ) ; + std::swap( m_first, ns.m_first ) ; + std::swap( m_last, ns.m_last ) ; +} + NodeSet::iterator NodeSet::begin() const { return m_first ; @@ -96,7 +117,7 @@ NodeSet NodeSet::operator[]( const std::string& name ) const if ( !r.empty() ) return r ; } - throw std::runtime_error( "can't find node " + name ) ; + return NodeSet() ; } Node NodeSet::front() const diff --git a/libgrive/src/xml/NodeSet.hh b/libgrive/src/xml/NodeSet.hh index 5e77285..8f41498 100644 --- a/libgrive/src/xml/NodeSet.hh +++ b/libgrive/src/xml/NodeSet.hh @@ -34,8 +34,12 @@ public : public : NodeSet() ; + NodeSet( const NodeSet& n ) ; NodeSet( iterator first, iterator last ) ; + NodeSet& operator=( const NodeSet& ns ) ; + void Swap( NodeSet& ns ) ; + void Add( const Node& n ) ; iterator begin() const ; diff --git a/libgrive/test/Assert.hh b/libgrive/test/Assert.hh index 01fa1eb..6863ce1 100644 --- a/libgrive/test/Assert.hh +++ b/libgrive/test/Assert.hh @@ -87,7 +87,7 @@ namespace grut { CPPUNIT_SOURCELINE(), \ "["#actualFirst","#actualLast") == "#expectFirst) ) -#define GRUT_ASSERT_EQUAL(actual, expected) \ +#define GRUT_ASSERT_EQUAL(expected, actual) \ ( grut::AssertEquals( (expected), \ (actual), \ CPPUNIT_SOURCELINE(), \ diff --git a/libgrive/test/drive/EntryTest.cc b/libgrive/test/drive/EntryTest.cc index 4023b6b..66d052c 100644 --- a/libgrive/test/drive/EntryTest.cc +++ b/libgrive/test/drive/EntryTest.cc @@ -47,6 +47,12 @@ void EntryTest::TestXml( ) GRUT_ASSERT_EQUAL( "\"WxYPGE8CDyt7ImBk\"", subject.ETag() ) ; GRUT_ASSERT_EQUAL( "https://docs.google.com/feeds/default/private/full/folder%3A0B5KhdsbryVeGMl83OEV1ZVc3cUE", subject.SelfHref() ) ; + + GRUT_ASSERT_EQUAL( 1, subject.ParentHrefs().size() ) ; + GRUT_ASSERT_EQUAL( "https://docs.google.com/feeds/default/private/full/folder%3A0B5KhdsbryVeGNEZjdUxzZHl3Sjg", + subject.ParentHrefs().front() ) ; + + GRUT_ASSERT_EQUAL( "folder", subject.Kind() ) ; } } // end of namespace grut