Unescape XML before sending messages over IRC
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
std::string xml_escape(const std::string& data)
|
std::string xml_escape(const std::string& data)
|
||||||
{
|
{
|
||||||
std::string res;
|
std::string res;
|
||||||
@@ -35,6 +37,49 @@ std::string xml_escape(const std::string& data)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string xml_unescape(const std::string& data)
|
||||||
|
{
|
||||||
|
std::string res;
|
||||||
|
res.reserve(data.size());
|
||||||
|
const char* str = data.c_str();
|
||||||
|
while (str && *str && (str - data.c_str()) < data.size())
|
||||||
|
{
|
||||||
|
if (*str == '&')
|
||||||
|
{
|
||||||
|
if (strncmp(str+1, "amp;", 4) == 0)
|
||||||
|
{
|
||||||
|
res += "&";
|
||||||
|
str += 4;
|
||||||
|
}
|
||||||
|
else if (strncmp(str+1, "lt;", 3) == 0)
|
||||||
|
{
|
||||||
|
res += "<";
|
||||||
|
str += 3;
|
||||||
|
}
|
||||||
|
else if (strncmp(str+1, "gt;", 3) == 0)
|
||||||
|
{
|
||||||
|
res += ">";
|
||||||
|
str += 3;
|
||||||
|
}
|
||||||
|
else if (strncmp(str+1, "quot;", 5) == 0)
|
||||||
|
{
|
||||||
|
res += "\"";
|
||||||
|
str += 5;
|
||||||
|
}
|
||||||
|
else if (strncmp(str+1, "apos;", 5) == 0)
|
||||||
|
{
|
||||||
|
res += "'";
|
||||||
|
str += 5;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
res += "&";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
res += *str;
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
XmlNode::XmlNode(const std::string& name, XmlNode* parent):
|
XmlNode::XmlNode(const std::string& name, XmlNode* parent):
|
||||||
name(name),
|
name(name),
|
||||||
@@ -89,12 +134,12 @@ void XmlNode::add_to_inner(const std::string& data)
|
|||||||
|
|
||||||
std::string XmlNode::get_inner() const
|
std::string XmlNode::get_inner() const
|
||||||
{
|
{
|
||||||
return this->inner;
|
return xml_unescape(this->inner);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string XmlNode::get_tail() const
|
std::string XmlNode::get_tail() const
|
||||||
{
|
{
|
||||||
return this->tail;
|
return xml_unescape(this->tail);
|
||||||
}
|
}
|
||||||
|
|
||||||
XmlNode* XmlNode::get_child(const std::string& name) const
|
XmlNode* XmlNode::get_child(const std::string& name) const
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
std::string xml_escape(const std::string& data);
|
std::string xml_escape(const std::string& data);
|
||||||
|
std::string xml_unescape(const std::string& data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Raised on operator[] when the attribute does not exist
|
* Raised on operator[] when the attribute does not exist
|
||||||
@@ -66,7 +67,6 @@ public:
|
|||||||
void add_to_inner(const std::string& data);
|
void add_to_inner(const std::string& data);
|
||||||
/**
|
/**
|
||||||
* Get the content of inner
|
* Get the content of inner
|
||||||
* TODO: unescape it here.
|
|
||||||
*/
|
*/
|
||||||
std::string get_inner() const;
|
std::string get_inner() const;
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user