xml_escape.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. return current_value;
  68. }
  69. return *bstart;
  70. }
  71. template<class Base>
  72. wchar_t xml_escape<Base>::fill(
  73. const wchar_t * & bstart,
  74. const wchar_t * & bend
  75. ){
  76. wchar_t current_value = * this->base_reference();
  77. switch(current_value){
  78. case '<':
  79. bstart = L"&lt;";
  80. bend = bstart + 4;
  81. break;
  82. case '>':
  83. bstart = L"&gt;";
  84. bend = bstart + 4;
  85. break;
  86. case '&':
  87. bstart = L"&amp;";
  88. bend = bstart + 5;
  89. break;
  90. case '"':
  91. bstart = L"&quot;";
  92. bend = bstart + 6;
  93. break;
  94. case '\'':
  95. bstart = L"&apos;";
  96. bend = bstart + 6;
  97. break;
  98. default:
  99. return current_value;
  100. }
  101. return *bstart;
  102. }
  103. } // namespace iterators
  104. } // namespace archive
  105. } // namespace boost
  106. #endif // BOOST_ARCHIVE_ITERATORS_XML_ESCAPE_HPP