KWSys Nightly Date Stamp
[cmake.git] / Source / cmXMLSafe.cxx
blob1ced6b626061bb6619ebd86dd62d3e3f73a3ba67
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmXMLSafe.cxx,v $
5 Language: C++
6 Date: $Date: 2009-03-02 21:27:50 $
7 Version: $Revision: 1.5 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmXMLSafe.h"
19 #include <cmsys/ios/iostream>
20 #include <cmsys/ios/sstream>
22 #include <string.h>
23 #include <stdio.h>
25 //----------------------------------------------------------------------------
26 cmXMLSafe::cmXMLSafe(const char* s):
27 Data(s),
28 Size(static_cast<unsigned long>(strlen(s))),
29 DoQuotes(true)
33 //----------------------------------------------------------------------------
34 cmXMLSafe::cmXMLSafe(cmsys_stl::string const& s):
35 Data(s.c_str()),
36 Size(static_cast<unsigned long>(s.length())),
37 DoQuotes(true)
41 //----------------------------------------------------------------------------
42 cmXMLSafe& cmXMLSafe::Quotes(bool b)
44 this->DoQuotes = b;
45 return *this;
48 //----------------------------------------------------------------------------
49 cmsys_stl::string cmXMLSafe::str()
51 cmsys_ios::ostringstream ss;
52 ss << *this;
53 return ss.str();
56 //----------------------------------------------------------------------------
57 cmsys_ios::ostream& operator<<(cmsys_ios::ostream& os, cmXMLSafe const& self)
59 char const* first = self.Data;
60 char const* last = self.Data + self.Size;
61 for(char const* ci = first; ci != last; ++ci)
63 unsigned char c = static_cast<unsigned char>(*ci);
64 switch(c)
66 case '&': os << "&amp;"; break;
67 case '<': os << "&lt;"; break;
68 case '>': os << "&gt;"; break;
69 case '"': os << (self.DoQuotes? "&quot;" : "\""); break;
70 case '\'': os << (self.DoQuotes? "&apos;" : "'"); break;
71 case '\t': os << "\t"; break;
72 case '\n': os << "\n"; break;
73 case '\r': break; // Ignore CR
74 default:
75 if(c >= 0x20 && c <= 0x7f)
77 os.put(static_cast<char>(c));
79 else
81 // TODO: More complete treatment of program output character
82 // encoding. Instead of escaping these bytes, we should
83 // handle the current locale and its encoding.
84 char buf[16];
85 // http://www.w3.org/TR/REC-xml/#NT-Char
86 if(c >= 0x80)
88 sprintf(buf, "&#x%hx;", static_cast<unsigned short>(c));
90 else
92 // We cannot use "&#x%hx;" here because this value is not
93 // valid in XML. Instead use a human-readable hex value.
94 sprintf(buf, "&lt;0x%hx&gt;", static_cast<unsigned short>(c));
96 os << buf;
98 break;
101 return os;