cmXMLSafe.cxx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmXMLSafe.h"
  14. #include <cmsys/ios/iostream>
  15. #include <cmsys/ios/sstream>
  16. #include <string.h>
  17. #include <stdio.h>
  18. //----------------------------------------------------------------------------
  19. cmXMLSafe::cmXMLSafe(const char* s):
  20. Data(s),
  21. Size(static_cast<unsigned long>(strlen(s))),
  22. DoQuotes(true)
  23. {
  24. }
  25. //----------------------------------------------------------------------------
  26. cmXMLSafe::cmXMLSafe(cmsys_stl::string const& s):
  27. Data(s.c_str()),
  28. Size(static_cast<unsigned long>(s.length())),
  29. DoQuotes(true)
  30. {
  31. }
  32. //----------------------------------------------------------------------------
  33. cmXMLSafe& cmXMLSafe::Quotes(bool b)
  34. {
  35. this->DoQuotes = b;
  36. return *this;
  37. }
  38. //----------------------------------------------------------------------------
  39. cmsys_stl::string cmXMLSafe::str()
  40. {
  41. cmsys_ios::ostringstream ss;
  42. ss << *this;
  43. return ss.str();
  44. }
  45. //----------------------------------------------------------------------------
  46. cmsys_ios::ostream& operator<<(cmsys_ios::ostream& os, cmXMLSafe const& self)
  47. {
  48. char const* first = self.Data;
  49. char const* last = self.Data + self.Size;
  50. for(char const* ci = first; ci != last; ++ci)
  51. {
  52. unsigned char c = static_cast<unsigned char>(*ci);
  53. switch(c)
  54. {
  55. case '&': os << "&amp;"; break;
  56. case '<': os << "&lt;"; break;
  57. case '>': os << "&gt;"; break;
  58. case '"': os << (self.DoQuotes? "&quot;" : "\""); break;
  59. case '\'': os << (self.DoQuotes? "&apos;" : "'"); break;
  60. case '\t': os << "\t"; break;
  61. case '\n': os << "\n"; break;
  62. case '\r': break; // Ignore CR
  63. default:
  64. if(c >= 0x20 && c <= 0x7f)
  65. {
  66. os.put(static_cast<char>(c));
  67. }
  68. else
  69. {
  70. // TODO: More complete treatment of program output character
  71. // encoding. Instead of escaping these bytes, we should
  72. // handle the current locale and its encoding.
  73. char buf[16];
  74. sprintf(buf, "[bad-char-%hx]", static_cast<unsigned short>(c));
  75. os << buf;
  76. }
  77. break;
  78. }
  79. }
  80. return os;
  81. }