summaryrefslogtreecommitdiff
path: root/Source/cmXMLSafe.cxx
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2009-02-05 16:31:37 -0500
committerBrad King <brad.king@kitware.com>2009-02-05 16:31:37 -0500
commitdc13914cd698b49cbae491c1e1aebbcd44fbe932 (patch)
tree4eaa716c3e2160a7e81125360d83dc9f87241ede /Source/cmXMLSafe.cxx
parentf16a471576199ec275463e35ff17849350c0da79 (diff)
downloadcmake-dc13914cd698b49cbae491c1e1aebbcd44fbe932.tar.gz
ENH: Create cmXMLSafe to help escapes in XML
This class provides easy syntax to efficiently insert blocks of data into XML documents with proper escapes. It replaces the old cmCTest::MakeXMLSafe and cmSystemTools::MakeXMLSafe methods which allocated extra memory instead of directly streaming the data.
Diffstat (limited to 'Source/cmXMLSafe.cxx')
-rw-r--r--Source/cmXMLSafe.cxx93
1 files changed, 93 insertions, 0 deletions
diff --git a/Source/cmXMLSafe.cxx b/Source/cmXMLSafe.cxx
new file mode 100644
index 0000000000..861d75c0b9
--- /dev/null
+++ b/Source/cmXMLSafe.cxx
@@ -0,0 +1,93 @@
+/*=========================================================================
+
+ Program: CMake - Cross-Platform Makefile Generator
+ Module: $RCSfile$
+ Language: C++
+ Date: $Date$
+ Version: $Revision$
+
+ Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
+ See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
+
+ This software is distributed WITHOUT ANY WARRANTY; without even
+ the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ PURPOSE. See the above copyright notices for more information.
+
+=========================================================================*/
+#include "cmXMLSafe.h"
+
+#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)
+{
+}
+
+//----------------------------------------------------------------------------
+cmXMLSafe::cmXMLSafe(cmsys_stl::string const& str):
+ Data(str.c_str()),
+ Size(static_cast<unsigned long>(str.length())),
+ 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;
+ for(char const* ci = first; ci != last; ++ci)
+ {
+ char c = *ci;
+ switch(c)
+ {
+ case '&': os << "&amp;"; break;
+ case '<': os << "&lt;"; break;
+ case '>': os << "&gt;"; break;
+ case '"': os << (self.DoQuotes? "&quot;" : "\""); break;
+ case '\'': os << (self.DoQuotes? "&apos;" : "'"); break;
+ case '\t': os << "\t"; break;
+ case '\n': os << "\n"; break;
+ case '\r': break; // Ignore CR
+ default:
+ if(c >= 0x20 && c <= 0x7f)
+ {
+ os.put(c);
+ }
+ else
+ {
+ // TODO: More complete treatment of program output character
+ // encoding. Instead of escaping these bytes, we should
+ // handle the current locale and its encoding.
+ unsigned char uc = static_cast<unsigned char>(c);
+ char buf[16];
+ sprintf(buf, "&#x%hx;", static_cast<unsigned short>(uc));
+ os << buf;
+ }
+ break;
+ }
+ }
+ return os;
+}