2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2009-02-06 00:31:37 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2009-02-06 00:31:37 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2009-02-06 00:31:37 +03:00
|
|
|
#include "cmXMLSafe.h"
|
|
|
|
|
2009-12-08 23:43:55 +03:00
|
|
|
#include "cm_utf8.h"
|
|
|
|
|
2015-08-20 23:02:20 +03:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2009-02-06 00:31:37 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <string.h>
|
2009-02-06 00:31:37 +03:00
|
|
|
|
|
|
|
cmXMLSafe::cmXMLSafe(const char* s):
|
|
|
|
Data(s),
|
|
|
|
Size(static_cast<unsigned long>(strlen(s))),
|
|
|
|
DoQuotes(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:19:40 +03:00
|
|
|
cmXMLSafe::cmXMLSafe(std::string const& s):
|
2009-02-06 17:08:02 +03:00
|
|
|
Data(s.c_str()),
|
|
|
|
Size(static_cast<unsigned long>(s.length())),
|
2009-02-06 00:31:37 +03:00
|
|
|
DoQuotes(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cmXMLSafe& cmXMLSafe::Quotes(bool b)
|
|
|
|
{
|
|
|
|
this->DoQuotes = b;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:19:40 +03:00
|
|
|
std::string cmXMLSafe::str()
|
2009-02-06 00:31:37 +03:00
|
|
|
{
|
2015-08-20 23:02:20 +03:00
|
|
|
std::ostringstream ss;
|
2009-02-06 00:31:37 +03:00
|
|
|
ss << *this;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:02:20 +03:00
|
|
|
std::ostream& operator<<(std::ostream& os, cmXMLSafe const& self)
|
2009-02-06 00:31:37 +03:00
|
|
|
{
|
|
|
|
char const* first = self.Data;
|
|
|
|
char const* last = self.Data + self.Size;
|
2009-12-08 23:43:55 +03:00
|
|
|
while(first != last)
|
2009-02-06 00:31:37 +03:00
|
|
|
{
|
2009-12-08 23:43:55 +03:00
|
|
|
unsigned int ch;
|
|
|
|
if(const char* next = cm_utf8_decode_character(first, last, &ch))
|
2009-02-06 00:31:37 +03:00
|
|
|
{
|
2009-12-08 23:43:55 +03:00
|
|
|
// http://www.w3.org/TR/REC-xml/#NT-Char
|
|
|
|
if((ch >= 0x20 && ch <= 0xD7FF) ||
|
|
|
|
(ch >= 0xE000 && ch <= 0xFFFD) ||
|
|
|
|
(ch >= 0x10000 && ch <= 0x10FFFF) ||
|
|
|
|
ch == 0x9 || ch == 0xA || ch == 0xD)
|
|
|
|
{
|
|
|
|
switch(ch)
|
2009-02-06 00:31:37 +03:00
|
|
|
{
|
2009-12-08 23:43:55 +03:00
|
|
|
// Escape XML control characters.
|
|
|
|
case '&': os << "&"; break;
|
|
|
|
case '<': os << "<"; break;
|
|
|
|
case '>': os << ">"; break;
|
|
|
|
case '"': os << (self.DoQuotes? """ : "\""); break;
|
|
|
|
case '\'': os << (self.DoQuotes? "'" : "'"); break;
|
|
|
|
case '\r': break; // Ignore CR
|
|
|
|
// Print the UTF-8 character.
|
|
|
|
default: os.write(first, next-first); break;
|
2009-02-06 00:31:37 +03:00
|
|
|
}
|
2009-12-08 23:43:55 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Use a human-readable hex value for this invalid character.
|
|
|
|
char buf[16];
|
|
|
|
sprintf(buf, "%X", ch);
|
|
|
|
os << "[NON-XML-CHAR-0x" << buf << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
first = next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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-06 00:31:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|