BOSS 7.0.5
BESIII Offline Software System
Loading...
Searching...
No Matches
XmlRpcUtil.cpp
Go to the documentation of this file.
1
2#include "XmlRpcUtil.h"
3
4#ifndef MAKEDEPEND
5# include <ctype.h>
6# include <iostream>
7# include <stdarg.h>
8# include <stdio.h>
9# include <string.h>
10#endif
11
12#include "XmlRpc.h"
13
14using namespace XmlRpc;
15
16
17//#define USE_WINDOWS_DEBUG // To make the error and log messages go to VC++ debug output
18#ifdef USE_WINDOWS_DEBUG
19#define WIN32_LEAN_AND_MEAN
20#include <windows.h>
21#endif
22
23// Version id
24const char XmlRpc::XMLRPC_VERSION[] = "XMLRPC++ 0.7";
25
26// Default log verbosity: 0 for no messages through 5 (writes everything)
28
29// Default log handler
30static class DefaultLogHandler : public XmlRpcLogHandler {
31public:
32
33 void log(int level, const char* msg) {
34#ifdef USE_WINDOWS_DEBUG
35 if (level <= _verbosity) { OutputDebugString(msg); OutputDebugString("\n"); }
36#else
37 if (level <= _verbosity) std::cout << msg << std::endl;
38#endif
39 }
40
41} defaultLogHandler;
42
43// Message log singleton
45
46
47// Default error handler
48static class DefaultErrorHandler : public XmlRpcErrorHandler {
49public:
50
51 void error(const char* msg) {
52#ifdef USE_WINDOWS_DEBUG
53 OutputDebugString(msg); OutputDebugString("\n");
54#else
55 std::cerr << msg << std::endl;
56#endif
57 }
58} defaultErrorHandler;
59
60
61// Error handler singleton
63
64
65// Easy API for log verbosity
68
69
70
71void XmlRpcUtil::log(int level, const char* fmt, ...)
72{
73 if (level <= XmlRpcLogHandler::getVerbosity())
74 {
75 va_list va;
76 char buf[1024];
77 va_start( va, fmt);
78 vsnprintf(buf,sizeof(buf)-1,fmt,va);
79 buf[sizeof(buf)-1] = 0;
81 }
82}
83
84
85void XmlRpcUtil::error(const char* fmt, ...)
86{
87 va_list va;
88 va_start(va, fmt);
89 char buf[1024];
90 vsnprintf(buf,sizeof(buf)-1,fmt,va);
91 buf[sizeof(buf)-1] = 0;
93}
94
95
96// Returns contents between <tag> and </tag>, updates offset to char after </tag>
97std::string
98XmlRpcUtil::parseTag(const char* tag, std::string const& xml, int* offset)
99{
100 if (*offset >= int(xml.length())) return std::string();
101 size_t istart = xml.find(tag, *offset);
102 if (istart == std::string::npos) return std::string();
103 istart += strlen(tag);
104 std::string etag = "</";
105 etag += tag + 1;
106 size_t iend = xml.find(etag, istart);
107 if (iend == std::string::npos) return std::string();
108
109 *offset = int(iend + etag.length());
110 return xml.substr(istart, iend-istart);
111}
112
113
114// Returns true if the tag is found and updates offset to the char after the tag
115bool
116XmlRpcUtil::findTag(const char* tag, std::string const& xml, int* offset)
117{
118 if (*offset >= int(xml.length())) return false;
119 size_t istart = xml.find(tag, *offset);
120 if (istart == std::string::npos)
121 return false;
122
123 *offset = int(istart + strlen(tag));
124 return true;
125}
126
127
128// Returns true if the tag is found at the specified offset (modulo any whitespace)
129// and updates offset to the char after the tag
130bool
131XmlRpcUtil::nextTagIs(const char* tag, std::string const& xml, int* offset)
132{
133 if (*offset >= int(xml.length())) return false;
134 const char* cp = xml.c_str() + *offset;
135 int nc = 0;
136 while (*cp && isspace(*cp)) {
137 ++cp;
138 ++nc;
139 }
140
141 int len = int(strlen(tag));
142 if (*cp && (strncmp(cp, tag, len) == 0)) {
143 *offset += nc + len;
144 return true;
145 }
146 return false;
147}
148
149// Returns the next tag and updates offset to the char after the tag, or empty string
150// if the next non-whitespace character is not '<'
151std::string
152XmlRpcUtil::getNextTag(std::string const& xml, int* offset)
153{
154 if (*offset >= int(xml.length())) return std::string();
155
156 size_t pos = *offset;
157 const char* cp = xml.c_str() + pos;
158 while (*cp && isspace(*cp)) {
159 ++cp;
160 ++pos;
161 }
162
163 if (*cp != '<') return std::string();
164
165 std::string s;
166 do {
167 s += *cp;
168 ++pos;
169 } while (*cp++ != '>' && *cp != 0);
170
171 *offset = int(pos);
172 return s;
173}
174
175
176
177// xml encodings (xml-encoded entities are preceded with '&')
178static const char AMP = '&';
179static const char rawEntity[] = { '<', '>', '&', '\'', '\"', 0 };
180static const char* xmlEntity[] = { "lt;", "gt;", "amp;", "apos;", "quot;", 0 };
181static const int xmlEntLen[] = { 3, 3, 4, 5, 5 };
182
183
184// Replace xml-encoded entities with the raw text equivalents.
185
186std::string
187XmlRpcUtil::xmlDecode(const std::string& encoded)
188{
189 std::string::size_type iAmp = encoded.find(AMP);
190 if (iAmp == std::string::npos)
191 return encoded;
192
193 std::string decoded(encoded, 0, iAmp);
194 std::string::size_type iSize = encoded.size();
195 decoded.reserve(iSize);
196
197 const char* ens = encoded.c_str();
198 while (iAmp != iSize) {
199 if (encoded[iAmp] == AMP && iAmp+1 < iSize) {
200 int iEntity;
201 for (iEntity=0; xmlEntity[iEntity] != 0; ++iEntity)
202 //if (encoded.compare(iAmp+1, xmlEntLen[iEntity], xmlEntity[iEntity]) == 0)
203 if (strncmp(ens+iAmp+1, xmlEntity[iEntity], xmlEntLen[iEntity]) == 0)
204 {
205 decoded += rawEntity[iEntity];
206 iAmp += xmlEntLen[iEntity]+1;
207 break;
208 }
209 if (xmlEntity[iEntity] == 0) // unrecognized sequence
210 decoded += encoded[iAmp++];
211
212 } else {
213 decoded += encoded[iAmp++];
214 }
215 }
216
217 return decoded;
218}
219
220
221// Replace raw text with xml-encoded entities.
222
223std::string
224XmlRpcUtil::xmlEncode(const std::string& raw)
225{
226 std::string::size_type iRep = raw.find_first_of(rawEntity);
227 if (iRep == std::string::npos)
228 return raw;
229
230 std::string encoded(raw, 0, iRep);
231 std::string::size_type iSize = raw.size();
232
233 while (iRep != iSize) {
234 int iEntity;
235 for (iEntity=0; rawEntity[iEntity] != 0; ++iEntity)
236 if (raw[iRep] == rawEntity[iEntity])
237 {
238 encoded += AMP;
239 encoded += xmlEntity[iEntity];
240 break;
241 }
242 if (rawEntity[iEntity] == 0)
243 encoded += raw[iRep];
244 ++iRep;
245 }
246 return encoded;
247}
248
249
250
XmlRpcServer s
Definition: HelloServer.cpp:11
An interface allowing custom handling of error message reporting.
Definition: XmlRpc.h:39
static XmlRpcErrorHandler * getErrorHandler()
Returns a pointer to the currently installed error handling object.
Definition: XmlRpc.h:42
virtual void error(const char *msg)=0
Report an error. Custom error handlers should define this method.
static XmlRpcErrorHandler * _errorHandler
Definition: XmlRpc.h:53
An interface allowing custom handling of informational message reporting.
Definition: XmlRpc.h:57
static int _verbosity
Definition: XmlRpc.h:80
static XmlRpcLogHandler * getLogHandler()
Returns a pointer to the currently installed message reporting object.
Definition: XmlRpc.h:60
virtual void log(int level, const char *msg)=0
Output a message. Custom error handlers should define this method.
static int getVerbosity()
Returns the level of verbosity of informational messages. 0 is no output, 5 is very verbose.
Definition: XmlRpc.h:68
static void setVerbosity(int v)
Specify the level of verbosity of informational messages. 0 is no output, 5 is very verbose.
Definition: XmlRpc.h:72
static XmlRpcLogHandler * _logHandler
Definition: XmlRpc.h:79
static bool nextTagIs(const char *tag, std::string const &xml, int *offset)
Definition: XmlRpcUtil.cpp:131
static std::string parseTag(const char *tag, std::string const &xml, int *offset)
Returns contents between <tag> and </tag>, updates offset to char after </tag>
Definition: XmlRpcUtil.cpp:98
static void error(const char *fmt,...)
Dump error messages somewhere.
Definition: XmlRpcUtil.cpp:85
static std::string xmlEncode(const std::string &raw)
Convert raw text to encoded xml.
Definition: XmlRpcUtil.cpp:224
static bool findTag(const char *tag, std::string const &xml, int *offset)
Returns true if the tag is found and updates offset to the char after the tag.
Definition: XmlRpcUtil.cpp:116
static void log(int level, const char *fmt,...)
Dump messages somewhere.
Definition: XmlRpcUtil.cpp:71
static std::string getNextTag(std::string const &xml, int *offset)
Definition: XmlRpcUtil.cpp:152
static std::string xmlDecode(const std::string &encoded)
Convert encoded xml to raw text.
Definition: XmlRpcUtil.cpp:187
const int nc
Definition: histgen.cxx:26
Definition: XmlRpc.h:35
int getVerbosity()
Returns log message verbosity. This is short for XmlRpcLogHandler::getVerbosity()
Definition: XmlRpcUtil.cpp:66
const char XMLRPC_VERSION[]
Version identifier.
Definition: XmlRpcUtil.cpp:24
void setVerbosity(int level)
Sets log message verbosity. This is short for XmlRpcLogHandler::setVerbosity(level)
Definition: XmlRpcUtil.cpp:67