2009-09-28 15:43:28 +00:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2009-02-05 21:31:37 +00:00
|
|
|
|
2009-09-28 15:43:28 +00:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2009-02-05 21:31:37 +00:00
|
|
|
|
2009-09-28 15:43:28 +00: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-05 21:31:37 +00:00
|
|
|
#include "cmXMLSafe.h"
|
|
|
|
|
2009-12-08 20:43:55 +00:00
|
|
|
#include "cm_utf8.h"
|
|
|
|
|
2009-02-05 21:31:37 +00:00
|
|
|
#include <cmsys/ios/iostream>
|
|
|
|
#include <cmsys/ios/sstream>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmXMLSafe::cmXMLSafe(const char* s):
|
|
|
|
Data(s),
|
|
|
|
Size(static_cast<unsigned long>(strlen(s))),
|
|
|
|
DoQuotes(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2009-02-06 14:08:02 +00:00
|
|
|
cmXMLSafe::cmXMLSafe(cmsys_stl::string const& s):
|
|
|
|
Data(s.c_str()),
|
|
|
|
Size(static_cast<unsigned long>(s.length())),
|
2009-02-05 21:31:37 +00:00
|
|
|
DoQuotes(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmXMLSafe& cmXMLSafe::Quotes(bool b)
|
|
|
|
{
|
|
|
|
this->DoQuotes = b;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmsys_stl::string cmXMLSafe::str()
|
|
|
|
{
|
|
|
|
cmsys_ios::ostringstream ss;
|
|
|
|
ss << *this;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmsys_ios::ostream& operator<<(cmsys_ios::ostream& os, cmXMLSafe const& self)
|
|
|
|
{
|
|
|
|
char const* first = self.Data;
|
|
|
|
char const* last = self.Data + self.Size;
|
2009-12-08 20:43:55 +00:00
|
|
|
while(first != last)
|
2009-02-05 21:31:37 +00:00
|
|
|
{
|
2009-12-08 20:43:55 +00:00
|
|
|
unsigned int ch;
|
|
|
|
if(const char* next = cm_utf8_decode_character(first, last, &ch))
|
2009-02-05 21:31:37 +00:00
|
|
|
{
|
2009-12-08 20:43:55 +00: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-05 21:31:37 +00:00
|
|
|
{
|
2009-12-08 20:43:55 +00: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-05 21:31:37 +00:00
|
|
|
}
|
2009-12-08 20:43:55 +00: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-05 21:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|