-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathxml2_utils.h
119 lines (91 loc) · 3.01 KB
/
xml2_utils.h
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
#ifndef __XML2_XML_UTILS__
#define __XML2_XML_UTILS__
#include <Rcpp.h>
#include <libxml/tree.h>
#include <map>
inline xmlChar* asXmlChar(std::string x) {
return (xmlChar*) x.c_str();
}
inline xmlChar* asXmlChar(SEXP x, int n = 0) {
return (xmlChar*) CHAR(STRING_ELT(x, n));
}
inline Rcpp::CharacterVector asCharacterVector(std::string x) {
return Rcpp::CharacterVector(Rf_mkCharCE(x.c_str(), CE_UTF8));
}
// ----------------------------------------------------------------------------
// A wrapper around xmlChar* that frees memory if necessary
class Xml2String {
xmlChar* string_;
bool free_;
public:
Xml2String(): string_(NULL), free_(false) {}
Xml2String(xmlChar* string): string_(string), free_(true) {}
// Pointers into structs are const, so don't need to be freed
Xml2String(const xmlChar* string): string_((xmlChar*) string), free_(false) {}
// Some strings are regular strings
Xml2String(const char* string): string_((xmlChar*) string), free_(false) {}
~Xml2String() {
try {
if (free_ && string_ != NULL)
xmlFree(string_);
} catch (...) {}
}
std::string asStdString(std::string missing = "") {
if (string_ == NULL)
return missing;
return std::string((char*) string_);
}
SEXP asRString(SEXP missing = NA_STRING) {
if (string_ == NULL)
return missing;
return Rf_mkCharCE((char*) string_, CE_UTF8);
};
};
// ----------------------------------------------------------------------------
// A wrapper around a pair of character vectors used to namespaces to prefixes
class NsMap {
// We only store the index to avoid duplicating the data
typedef std::multimap<std::string, std::string> prefix2url_t;
prefix2url_t prefix2url;
public:
NsMap() {
}
// Initialise from an existing character vector
NsMap(Rcpp::CharacterVector x) {
Rcpp::CharacterVector names = Rcpp::as<Rcpp::CharacterVector>(x.attr("names"));
for (R_len_t i = 0; i < x.size(); ++i) {
add(std::string(names[i]), std::string(x[i]));
}
}
bool hasPrefix(const std::string& prefix) {
return prefix2url.find(prefix) != prefix2url.end();
}
std::string findUrl(const std::string& prefix) {
prefix2url_t::const_iterator it = prefix2url.find(prefix);
if (it != prefix2url.end()) {
return it->second;
}
Rcpp::stop("Couldn't find url for prefix %s", prefix);
return std::string();
}
std::string findPrefix(const std::string& url) {
for (prefix2url_t::const_iterator it = prefix2url.begin(); it != prefix2url.end(); ++it) {
if (it->second == url) {
return it->first;
}
}
Rcpp::stop("Couldn't find prefix for url %s", url);
return std::string();
}
bool add(const xmlChar* prefix, const xmlChar* url) {
return add(Xml2String(prefix).asStdString(), Xml2String(url).asStdString());
}
bool add(std::string prefix, std::string url) {
prefix2url.insert(prefix2url_t::value_type(prefix, url));
return true;
}
Rcpp::CharacterVector out() {
return Rcpp::wrap(prefix2url);
}
};
#endif