xml_escape.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef BOOST_ARCHIVE_ITERATORS_XML_ESCAPE_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_XML_ESCAPE_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // xml_escape.hpp
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. #include <boost/assert.hpp>
  15. #include <boost/archive/iterators/escape.hpp>
  16. namespace boost {
  17. namespace archive {
  18. namespace iterators {
  19. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  20. // insert escapes into xml text
  21. template<class Base>
  22. class xml_escape
  23. : public escape<xml_escape<Base>, Base>
  24. {
  25. friend class boost::iterator_core_access;
  26. typedef escape<xml_escape<Base>, Base> super_t;
  27. public:
  28. char fill(const char * & bstart, const char * & bend);
  29. wchar_t fill(const wchar_t * & bstart, const wchar_t * & bend);
  30. template<class T>
  31. xml_escape(T start) :
  32. super_t(Base(static_cast< T >(start)))
  33. {}
  34. // intel 7.1 doesn't like default copy constructor
  35. xml_escape(const xml_escape & rhs) :
  36. super_t(rhs.base_reference())
  37. {}
  38. };
  39. template<class Base>
  40. char xml_escape<Base>::fill(
  41. const char * & bstart,
  42. const char * & bend
  43. ){
  44. char current_value = * this->base_reference();
  45. switch(current_value){
  46. case '<':
  47. bstart = "&lt;";
  48. bend = bstart + 4;
  49. break;
  50. case '>':
  51. bstart = "&gt;";
  52. bend = bstart + 4;
  53. break;
  54. case '&':
  55. bstart = "&amp;";
  56. bend = bstart + 5;
  57. break;
  58. case '"':
  59. bstart = "&quot;";
  60. bend = bstart + 6;
  61. break;
  62. case '\'':
  63. bstart = "&apos;";
  64. bend = bstart + 6;
  65. break;
  66. default:
  67. bstart="";
  68. bend=bstart;
  69. return current_value;
  70. }
  71. return *bstart;
  72. }
  73. template<class Base>
  74. wchar_t xml_escape<Base>::fill(
  75. const wchar_t * & bstart,
  76. const wchar_t * & bend
  77. ){
  78. wchar_t current_value = * this->base_reference();
  79. switch(current_value){
  80. case '<':
  81. bstart = L"&lt;";
  82. bend = bstart + 4;
  83. break;
  84. case '>':
  85. bstart = L"&gt;";
  86. bend = bstart + 4;
  87. break;
  88. case '&':
  89. bstart = L"&amp;";
  90. bend = bstart + 5;
  91. break;
  92. case '"':
  93. bstart = L"&quot;";
  94. bend = bstart + 6;
  95. break;
  96. case '\'':
  97. bstart = L"&apos;";
  98. bend = bstart + 6;
  99. break;
  100. default:
  101. return current_value;
  102. }
  103. return *bstart;
  104. }
  105. } // namespace iterators
  106. } // namespace archive
  107. } // namespace boost
  108. #endif // BOOST_ARCHIVE_ITERATORS_XML_ESCAPE_HPP