forked from parsley72/tinyxpath2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_set.h
More file actions
164 lines (136 loc) · 5.46 KB
/
node_set.h
File metadata and controls
164 lines (136 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
www.sourceforge.net/projects/tinyxpath
Copyright (c) 2002-2004 Yves Berquin (yvesb@users.sourceforge.net)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef __NODE_SET_H
#define __NODE_SET_H
#include <cassert>
#include <string>
#include "tinyxml2.h"
#include "tinyxpath_conf.h"
namespace TinyXPath {
/// Node set class. A node set is an unordered collection of node
class node_set {
public:
/// constructor : creates an empty set
node_set() {
_u_nb_node = 0;
_vpp_node_set = nullptr;
_op_attrib = nullptr;
}
/// copy constructor
node_set(const node_set& ns2);
/// destructor
~node_set() {
if (_u_nb_node && _vpp_node_set)
delete[] _vpp_node_set;
if (_u_nb_node && _op_attrib)
delete[] _op_attrib;
_u_nb_node = 0;
_vpp_node_set = nullptr;
_op_attrib = nullptr;
}
node_set& operator=(const node_set& ns2);
void v_add_base_in_set(const void* XBp_member, bool o_attrib);
/// Adds an attribute in the node set
void v_add_attrib_in_set(const tinyxml2::XMLAttribute* XAp_attrib) {
v_add_base_in_set(XAp_attrib, true);
}
/// Adds a node in the node set
void v_add_node_in_set(const tinyxml2::XMLNode* XNp_node) {
v_add_base_in_set(XNp_node, false);
}
bool o_exist_in_set(const void* XBp_member);
void v_add_all_foll_node(const tinyxml2::XMLNode* XNp_node, const std::string& S_name);
void v_add_all_prec_node(const tinyxml2::XMLNode* XNp_node, const std::string& S_name);
/// Add a new node, if the name is "*" or if the name is the same as the node
void v_add_node_in_set_if_name_or_star(const tinyxml2::XMLNode* XNp_node, const std::string& S_name) {
bool o_keep;
if (S_name == "*")
o_keep = true;
else
o_keep = !strcmp(XNp_node->Value(), S_name.c_str());
if (o_keep)
v_add_node_in_set(XNp_node);
}
/// Add a new attrib, if the name is "*" or if the name is the same as the node
void v_add_attrib_in_set_if_name_or_star(const tinyxml2::XMLAttribute* XAp_attrib, const std::string& S_name) {
bool o_keep;
if (S_name == "*")
o_keep = true;
else
o_keep = !strcmp(XAp_attrib->Name(), S_name.c_str());
if (o_keep)
v_add_attrib_in_set(XAp_attrib);
}
/// Get nb of nodes in the node set
unsigned u_get_nb_node_in_set() const {
return _u_nb_node;
}
/// Get a node
const tinyxml2::XMLNode* XNp_get_node_in_set(unsigned u_which) {
assert(u_which < _u_nb_node);
assert(!o_is_attrib(u_which));
return (const tinyxml2::XMLNode*)_vpp_node_set[u_which];
}
/// Get an attribute
const tinyxml2::XMLAttribute* XAp_get_attribute_in_set(unsigned u_which) {
assert(u_which < _u_nb_node);
assert(o_is_attrib(u_which));
return (const tinyxml2::XMLAttribute*)_vpp_node_set[u_which];
}
/// Check if a node is an attribute or another node. This is needed because TinyXML has a weird exception for
/// attributes not being children of XMLNode
bool o_is_attrib(unsigned u_which) {
assert(u_which < _u_nb_node);
return _op_attrib[u_which];
}
/// Get a node value. The value is the name for an element, and the attribute value for an attribute
std::string S_get_value(unsigned u_which) {
std::string S_res;
if (o_is_attrib(u_which))
S_res = XAp_get_attribute_in_set(u_which)->Value();
else
S_res = XNp_get_node_in_set(u_which)->Value();
return S_res;
}
/// Get the integer value of a node
int i_get_value(unsigned u_which) {
return atoi(S_get_value(u_which).c_str());
}
/// Get the real value of a node
double d_get_value(unsigned u_which) {
return atof(S_get_value(u_which).c_str());
}
void v_copy_node_children(const tinyxml2::XMLNode* XNp_root, const char* cp_lookup = nullptr);
void v_copy_selected_node_recursive(const tinyxml2::XMLNode* XNp_root);
void v_copy_selected_node_recursive(const tinyxml2::XMLNode* XNp_root, const char* cp_lookup);
void v_copy_selected_node_recursive_no_attrib(const tinyxml2::XMLNode* XNp_root, const char* cp_lookup);
void v_copy_selected_node_recursive_root_only(const tinyxml2::XMLNode* XNp_root, const tinyxml2::XMLNode* XNp_base);
std::string S_get_string_value() const;
void v_dump();
void v_document_sort(const tinyxml2::XMLNode* XNp_root);
protected:
/// Nb of nodes in the set
unsigned _u_nb_node;
/// List of node pointers to the
const void** _vpp_node_set;
/// Attributes flag list
bool* _op_attrib;
};
} // namespace TinyXPath
#endif // __NODE_SET_H