xml_escape.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) && (_MSC_VER >= 1020)
  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 <cassert>
  15. #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
  16. #include <boost/serialization/pfto.hpp>
  17. #include <boost/archive/iterators/escape.hpp>
  18. namespace boost {
  19. namespace archive {
  20. namespace iterators {
  21. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  22. // insert escapes into xml text
  23. template<class Base>
  24. class xml_escape
  25. : public escape<xml_escape<Base>, Base>
  26. {
  27. friend class boost::iterator_core_access;
  28. typedef escape<xml_escape<Base>, Base> super_t;
  29. public:
  30. char fill(const char * & bstart, const char * & bend);
  31. wchar_t fill(const wchar_t * & bstart, const wchar_t * & bend);
  32. template<class T>
  33. xml_escape(BOOST_PFTO_WRAPPER(T) start) :
  34. super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast<T>(start))))
  35. {}
  36. // intel 7.1 doesn't like default copy constructor
  37. xml_escape(const xml_escape & rhs) :
  38. super_t(rhs.base_reference())
  39. {}
  40. };
  41. template<class Base>
  42. char xml_escape<Base>::fill(
  43. const char * & bstart,
  44. const char * & bend
  45. ){
  46. char current_value = * this->base_reference();
  47. switch(current_value){
  48. case '<':
  49. bstart = "&lt;";
  50. bend = bstart + 4;
  51. break;
  52. case '>':
  53. bstart = "&gt;";
  54. bend = bstart + 4;
  55. break;
  56. case '&':
  57. bstart = "&amp;";
  58. bend = bstart + 5;
  59. break;
  60. case '"':
  61. bstart = "&quot;";
  62. bend = bstart + 6;
  63. break;
  64. case '\'':
  65. bstart = "&apos;";
  66. bend = bstart + 6;
  67. break;
  68. default:
  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