~alza/grive/grive

« back to all changes in this revision

Viewing changes to libgrive/src/xml/NodeSet.cc

  • Committer: Raviyanto Ahmad
  • Date: 2013-04-20 14:46:52 UTC
  • Revision ID: git-v1:02a7d102129842c50a9af0e4991b997f2de2a044
unggah semua berkas grive

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        grive: an GPL program to sync a local directory with Google Drive
 
3
        Copyright (C) 2012  Wan Wai Ho
 
4
 
 
5
        This program is free software; you can redistribute it and/or
 
6
        modify it under the terms of the GNU General Public License
 
7
        as published by the Free Software Foundation version 2
 
8
        of the License.
 
9
 
 
10
        This program is distributed in the hope that it will be useful,
 
11
        but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
        GNU General Public License for more details.
 
14
 
 
15
        You should have received a copy of the GNU General Public License
 
16
        along with this program; if not, write to the Free Software
 
17
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "NodeSet.hh"
 
21
 
 
22
#include "Error.hh"
 
23
 
 
24
#include <algorithm>
 
25
 
 
26
#include <iostream>
 
27
 
 
28
namespace gr { namespace xml {
 
29
 
 
30
NodeSet::NodeSet() :
 
31
        m_first( m_tmp.begin() ),
 
32
        m_last( m_tmp.end() )
 
33
{
 
34
}
 
35
 
 
36
NodeSet::NodeSet( iterator first, iterator last ) :
 
37
        m_first( first ),
 
38
        m_last( last )
 
39
{
 
40
}
 
41
 
 
42
NodeSet::NodeSet( const NodeSet& n ) :
 
43
        m_tmp( n.m_tmp ),
 
44
        m_first( m_tmp.begin() + (n.m_first - n.m_tmp.begin()) ),
 
45
        m_last( m_tmp.begin() + (n.m_last - n.m_tmp.begin()) )
 
46
{
 
47
}
 
48
 
 
49
NodeSet& NodeSet::operator=( const NodeSet& ns )
 
50
{
 
51
        NodeSet tmp( ns ) ;
 
52
        Swap( tmp ) ;
 
53
        return *this ;
 
54
}
 
55
 
 
56
void NodeSet::Swap( NodeSet& ns )
 
57
{
 
58
        m_tmp.Swap( ns.m_tmp ) ;
 
59
        std::swap( m_first, ns.m_first ) ;
 
60
        std::swap( m_last, ns.m_last ) ;
 
61
}
 
62
 
 
63
NodeSet::iterator NodeSet::begin() const
 
64
{
 
65
        return m_first ;
 
66
}
 
67
 
 
68
NodeSet::iterator NodeSet::end() const
 
69
{
 
70
        return m_last ;
 
71
}
 
72
 
 
73
/*!     This function search the members in the node set. If any members in the node
 
74
        set has a children named \a name , with value equal to \a value , it will
 
75
        be returned.
 
76
        \param  name    name to be found. prefix with '@' for attributes
 
77
        \param  value   value to be matched.
 
78
        \return the node set contained all children nodes that matches \a name and \a value
 
79
*/
 
80
NodeSet NodeSet::Find( const std::string& name, const std::string& value ) const
 
81
{
 
82
        NodeSet result ;
 
83
        for ( iterator i = m_first ; i != m_last ; ++i )
 
84
        {
 
85
                NodeSet cand = (*i)[name] ;
 
86
                for ( iterator j = cand.m_first ; j != cand.m_last ; ++j )
 
87
                {
 
88
                        if ( j->Value() == value )
 
89
                        {
 
90
                                result.Add( *i ) ;
 
91
                                break ;
 
92
                        }
 
93
                }
 
94
        }
 
95
        return result ;
 
96
}
 
97
 
 
98
void NodeSet::Add( const Node& n )
 
99
{
 
100
        // the tmp node is not used, that means the first,last iterators points to elsewhere
 
101
        if ( m_tmp.size() == 0 )
 
102
        {
 
103
                m_tmp.AddNode( m_first, m_last ) ;
 
104
        }
 
105
 
 
106
        m_tmp.AddNode( n ) ;
 
107
        
 
108
        // the iterators may be invalidated after adding the node
 
109
        m_first = m_tmp.begin() ;
 
110
        m_last  = m_tmp.end() ;
 
111
}
 
112
 
 
113
NodeSet NodeSet::operator[]( const std::string& name ) const
 
114
{
 
115
        for ( iterator i = m_first ; i != m_last ; ++i )
 
116
        {
 
117
                NodeSet r = (*i)[name] ;
 
118
                if ( !r.empty() )
 
119
                        return r ;
 
120
        }
 
121
        return NodeSet() ;
 
122
}
 
123
 
 
124
Node NodeSet::front() const
 
125
{
 
126
        if ( empty() )
 
127
                throw Error() << expt::ErrMsg( "empty node set" ) ;
 
128
                
 
129
        return *m_first ;
 
130
}
 
131
 
 
132
NodeSet::operator std::string() const
 
133
{
 
134
        return empty() ? "" : front().Value() ;
 
135
}
 
136
 
 
137
bool NodeSet::operator==( const std::string& value ) const
 
138
{
 
139
        return operator std::string() == value ;
 
140
}
 
141
 
 
142
bool NodeSet::empty() const
 
143
{
 
144
        return m_first == m_last ;
 
145
}
 
146
 
 
147
std::size_t NodeSet::size() const
 
148
{
 
149
        return m_last - m_first ;
 
150
}
 
151
 
 
152
std::ostream& operator<<( std::ostream& os, const NodeSet& node )
 
153
{
 
154
        std::copy( node.begin(), node.end(), std::ostream_iterator<Node>(os, " ") ) ;
 
155
        return os ;
 
156
}
 
157
 
 
158
} } // end of namespace