2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2009-02-05 21:31:37 +00:00
|
|
|
#include "cmXMLSafe.h"
|
|
|
|
|
2009-12-08 20:43:55 +00:00
|
|
|
#include "cm_utf8.h"
|
|
|
|
|
2015-08-20 20:02:20 +00:00
|
|
|
#include <sstream>
|
2009-02-05 21:31:37 +00:00
|
|
|
#include <stdio.h>
|
2016-04-29 14:53:13 +00:00
|
|
|
#include <string.h>
|
2009-02-05 21:31:37 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmXMLSafe::cmXMLSafe(const char* s)
|
|
|
|
: Data(s)
|
|
|
|
, Size(static_cast<unsigned long>(strlen(s)))
|
|
|
|
, DoQuotes(true)
|
2009-02-05 21:31:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmXMLSafe::cmXMLSafe(std::string const& s)
|
|
|
|
: Data(s.c_str())
|
|
|
|
, Size(static_cast<unsigned long>(s.length()))
|
|
|
|
, DoQuotes(true)
|
2009-02-05 21:31:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cmXMLSafe& cmXMLSafe::Quotes(bool b)
|
|
|
|
{
|
|
|
|
this->DoQuotes = b;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-01-29 22:19:40 +00:00
|
|
|
std::string cmXMLSafe::str()
|
2009-02-05 21:31:37 +00:00
|
|
|
{
|
2015-08-20 20:02:20 +00:00
|
|
|
std::ostringstream ss;
|
2009-02-05 21:31:37 +00:00
|
|
|
ss << *this;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2015-08-20 20:02:20 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, cmXMLSafe const& self)
|
2009-02-05 21:31:37 +00:00
|
|
|
{
|
|
|
|
char const* first = self.Data;
|
|
|
|
char const* last = self.Data + self.Size;
|
2016-05-16 14:34:04 +00:00
|
|
|
while (first != last) {
|
2009-12-08 20:43:55 +00:00
|
|
|
unsigned int ch;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (const char* next = cm_utf8_decode_character(first, last, &ch)) {
|
2009-12-08 20:43:55 +00:00
|
|
|
// http://www.w3.org/TR/REC-xml/#NT-Char
|
2016-05-16 14:34:04 +00:00
|
|
|
if ((ch >= 0x20 && ch <= 0xD7FF) || (ch >= 0xE000 && ch <= 0xFFFD) ||
|
|
|
|
(ch >= 0x10000 && ch <= 0x10FFFF) || ch == 0x9 || ch == 0xA ||
|
|
|
|
ch == 0xD) {
|
|
|
|
switch (ch) {
|
2009-12-08 20:43:55 +00:00
|
|
|
// Escape XML control characters.
|
2016-05-16 14:34:04 +00:00
|
|
|
case '&':
|
|
|
|
os << "&";
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
os << "<";
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
os << ">";
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
os << (self.DoQuotes ? """ : "\"");
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
os << (self.DoQuotes ? "'" : "'");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
break; // Ignore CR
|
2009-12-08 20:43:55 +00:00
|
|
|
// Print the UTF-8 character.
|
2016-05-16 14:34:04 +00:00
|
|
|
default:
|
|
|
|
os.write(first, next - first);
|
|
|
|
break;
|
2009-12-08 20:43:55 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2009-12-08 20:43:55 +00:00
|
|
|
// Use a human-readable hex value for this invalid character.
|
|
|
|
char buf[16];
|
|
|
|
sprintf(buf, "%X", ch);
|
|
|
|
os << "[NON-XML-CHAR-0x" << buf << "]";
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2009-12-08 20:43:55 +00:00
|
|
|
|
|
|
|
first = next;
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2009-12-08 20:43:55 +00:00
|
|
|
ch = static_cast<unsigned char>(*first++);
|
|
|
|
// Use a human-readable hex value for this invalid byte.
|
|
|
|
char buf[16];
|
|
|
|
sprintf(buf, "%X", ch);
|
|
|
|
os << "[NON-UTF-8-BYTE-0x" << buf << "]";
|
2009-02-05 21:31:37 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2009-02-05 21:31:37 +00:00
|
|
|
return os;
|
|
|
|
}
|