1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmXMLSafe.cxx,v $
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>
25 //----------------------------------------------------------------------------
26 cmXMLSafe::cmXMLSafe(const char* s
):
28 Size(static_cast<unsigned long>(strlen(s
))),
33 //----------------------------------------------------------------------------
34 cmXMLSafe::cmXMLSafe(cmsys_stl::string
const& s
):
36 Size(static_cast<unsigned long>(s
.length())),
41 //----------------------------------------------------------------------------
42 cmXMLSafe
& cmXMLSafe::Quotes(bool b
)
48 //----------------------------------------------------------------------------
49 cmsys_stl::string
cmXMLSafe::str()
51 cmsys_ios::ostringstream ss
;
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
);
66 case '&': os
<< "&"; break;
67 case '<': os
<< "<"; break;
68 case '>': os
<< ">"; break;
69 case '"': os
<< (self
.DoQuotes
? """ : "\""); break;
70 case '\'': os
<< (self
.DoQuotes
? "'" : "'"); break;
71 case '\t': os
<< "\t"; break;
72 case '\n': os
<< "\n"; break;
73 case '\r': break; // Ignore CR
75 if(c
>= 0x20 && c
<= 0x7f)
77 os
.put(static_cast<char>(c
));
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.
85 // http://www.w3.org/TR/REC-xml/#NT-Char
88 sprintf(buf
, "&#x%hx;", static_cast<unsigned short>(c
));
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
, "<0x%hx>", static_cast<unsigned short>(c
));