-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlRpc.cpp
175 lines (147 loc) · 3.47 KB
/
XmlRpc.cpp
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
165
166
167
168
169
170
171
172
173
174
175
#include "stdafx.h"
#include "XmlRpc.h"
XmlRpc::XmlRpc(boost::property_tree::ptree& ptree) :
tree(ptree)
{
node = "";
}
XmlRpc::XmlRpc(boost::property_tree::ptree& ptree, char* methodName) :
tree(ptree)
{
node = "methodCall.";
create(methodName);
}
XmlRpc::XmlRpc(boost::property_tree::ptree& ptree, std::string source) :
tree(ptree)
{
node = "methodResponse.";
load(source);
}
std::string XmlRpc::getNode(char* n)
{
return node + std::string(n);
}
std::string XmlRpc::getNode(std::string n)
{
return node + n;
}
void XmlRpc::create(char* methodName)
{
tree.put(getNode("methodName"), methodName);
tree.put(getNode("params"), "");
}
XmlRpc XmlRpc::operator[](int i)
{
std::string pnode = "params";
std::string cnode = ".param";
bool array = false;
boost::optional< boost::property_tree::ptree& > c = tree.get_child_optional(getNode(pnode));
if (!c) {
array = true;
pnode = "array.data";
cnode = ".value";
if (tree.count(getNode("array")) == 0) tree.put(getNode(pnode), "");
c = tree.get_child_optional(getNode(pnode));
}
boost::property_tree::ptree& child = c.get();
int idx = child.size();
while (idx <= i)
{
boost::property_tree::ptree arg;
if (!array) arg.put("value", "");
tree.add_child(getNode(pnode + cnode), arg);
idx++;
}
idx = -1;
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, child)
{
idx++;
if (idx != i) continue;
if (array) return XmlRpc(v.second);
return XmlRpc(v.second.get_child("value"));
}
return *this;
}
XmlRpc XmlRpc::operator[](char* name)
{
if (tree.count(getNode("struct")) > 0) {
boost::property_tree::ptree& child = tree.get_child(getNode("struct"));
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, child)
{
if (v.second.get<std::string>("name").compare(name)) continue;
return XmlRpc(v.second.get_child("value"));
}
}
// If not found, we create the member
boost::property_tree::ptree member;
member.put("name", name);
member.put("value", "");
return tree.add_child(getNode("struct.member"), member).get_child("value");
}
char* XmlRpc::operator=(char* str)
{
tree.put<char*>("string", str);
return str;
}
int XmlRpc::operator=(int i)
{
tree.put<int>("int", i);
return i;
}
std::string XmlRpc::getString()
{
return tree.get<std::string>("string");
}
boost::property_tree::ptree& XmlRpc::getTree()
{
return tree;
}
bool XmlRpc::load(std::string& str)
{
std::stringstream ss;
ss << str;
try
{
boost::property_tree::read_xml(ss, tree, boost::property_tree::xml_parser::trim_whitespace);
tree = tree.front().second;
}
catch(...)
{
print("Unable to read the XmlRpc received");
return false;
}
return true;
}
void XmlRpc::addValue(char* value, char* type)
{
std::string tmp("value.");
tmp.append(type);
boost::property_tree::ptree arg;
arg.put(tmp, value);
tree.add_child("params.param", arg);
}
boost::property_tree::ptree XmlRpc::getValueInStruct(int structId, char* name)
{
int idx = 0;
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, tree.get_child("methodResponse.params"))
{
if (idx != structId) continue;
boost::property_tree::ptree pt = v.second;
BOOST_FOREACH(boost::property_tree::ptree::value_type &v2, pt.get_child("value.struct"))
{
if (v2.second.get<std::string>("name").compare(name) == 0)
{
return v2.second.get_child("value");
}
}
idx++;
}
return tree;
}
void XmlRpc::toStr(std::string& str)
{
std::stringstream ss;
boost::property_tree::write_xml(ss, tree);
str.append(ss.str());
}
XmlRpc::~XmlRpc(void) {}