Menu

[r1071]: / advanced / src / cpp / xml.cpp  Maximize  Restore  History

Download this file

184 lines (135 with data), 5.1 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <string>
#include "xmlrpc-c/girerr.hpp"
using girerr::error;
using girerr::throwf;
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/base_int.h"
#include "xmlrpc-c/string_int.h"
#include "xmlrpc-c/base.hpp"
#include "env_wrap.hpp"
#include "xmlrpc-c/xml.hpp"
using namespace std;
using namespace xmlrpc_c;
namespace {
class cValueWrapper {
/*----------------------------------------------------------------------------
Use an object of this class to set up to remove a reference to an
xmlrpc_value object (a C object with manual reference management)
at then end of a scope -- even if the scope ends with a throw.
-----------------------------------------------------------------------------*/
public:
xmlrpc_value * valueP;
cValueWrapper(xmlrpc_value * valueP) : valueP(valueP) {}
~cValueWrapper() { xmlrpc_DECREF(valueP); }
};
xmlrpc_value *
cArrayFromParamList(paramList const& paramList) {
env_wrap env;
xmlrpc_value * paramArrayP;
paramArrayP = xmlrpc_array_new(&env.env_c);
if (!env.env_c.fault_occurred) {
for (unsigned int i = 0;
i < paramList.size() && !env.env_c.fault_occurred;
++i) {
cValueWrapper const param(paramList[i].cValue());
xmlrpc_array_append_item(&env.env_c, paramArrayP, param.valueP);
}
}
if (env.env_c.fault_occurred) {
xmlrpc_DECREF(paramArrayP);
throw(error(env.env_c.fault_string));
}
return paramArrayP;
}
} // namespace
namespace xmlrpc_c {
namespace xml {
void
generateCall(string const& methodName,
paramList const& paramList,
xmlrpc_dialect const dialect,
string * const callXmlP) {
/*----------------------------------------------------------------------------
Generate the XML for an XML-RPC call, given a method name and parameter
list.
Use dialect 'dialect' of XML-RPC.
-----------------------------------------------------------------------------*/
class memblockWrapper {
xmlrpc_mem_block * const memblockP;
public:
memblockWrapper(xmlrpc_mem_block * const memblockP) :
memblockP(memblockP) {}
~memblockWrapper() {
XMLRPC_MEMBLOCK_FREE(char, memblockP);
}
};
xmlrpc_mem_block * callXmlMP;
env_wrap env;
callXmlMP = XMLRPC_MEMBLOCK_NEW(char, &env.env_c, 0);
if (!env.env_c.fault_occurred) {
memblockWrapper callXmlHolder(callXmlMP);
// Makes callXmlMP get freed at end of scope
xmlrpc_value * const paramArrayP(cArrayFromParamList(paramList));
xmlrpc_serialize_call2(&env.env_c, callXmlMP, methodName.c_str(),
paramArrayP, dialect);
*callXmlP = string(XMLRPC_MEMBLOCK_CONTENTS(char, callXmlMP),
XMLRPC_MEMBLOCK_SIZE(char, callXmlMP));
xmlrpc_DECREF(paramArrayP);
}
if (env.env_c.fault_occurred)
throw(error(env.env_c.fault_string));
}
void
generateCall(string const& methodName,
paramList const& paramList,
string * const callXmlP) {
generateCall(methodName, paramList, xmlrpc_dialect_i8, callXmlP);
}
void
parseResponse(string const& responseXml,
rpcOutcome * const outcomeP) {
/*----------------------------------------------------------------------------
Parse the XML for an XML-RPC response into an XML-RPC result value.
-----------------------------------------------------------------------------*/
env_wrap env;
xmlrpc_value * c_resultP;
int faultCode;
const char * faultString;
xmlrpc_parse_response2(&env.env_c, responseXml.c_str(), responseXml.size(),
&c_resultP, &faultCode, &faultString);
if (env.env_c.fault_occurred)
throwf("Unable to find XML-RPC response in what server sent back. %s",
env.env_c.fault_string);
else {
if (faultString) {
*outcomeP =
rpcOutcome(fault(faultString,
static_cast<fault::code_t>(faultCode)));
xmlrpc_strfree(faultString);
} else {
XMLRPC_ASSERT_VALUE_OK(c_resultP);
*outcomeP = rpcOutcome(value(c_resultP));
xmlrpc_DECREF(c_resultP);
}
}
}
void
parseSuccessfulResponse(string const& responseXml,
value * const resultP) {
/*----------------------------------------------------------------------------
Same as parseResponse(), but expects the response to indicate success;
throws an error if it doesn't.
-----------------------------------------------------------------------------*/
rpcOutcome outcome;
parseResponse(responseXml, &outcome);
if (!outcome.succeeded())
throwf("RPC response indicates it failed. %s",
outcome.getFault().getDescription().c_str());
*resultP = outcome.getResult();
}
void
trace(string const& label,
string const& xml) {
xmlrpc_traceXml(label.c_str(), xml.c_str(), xml.size());
}
}} // namespace
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.