cmXMLSafe.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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& str):
  27. Data(str.c_str()),
  28. Size(static_cast<unsigned long>(str.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. char c = *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(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. unsigned char uc = static_cast<unsigned char>(c);
  74. char buf[16];
  75. sprintf(buf, "&#x%hx;", static_cast<unsigned short>(uc));
  76. os << buf;
  77. }
  78. break;
  79. }
  80. }
  81. return os;
  82. }