xml_unescape.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef BOOST_ARCHIVE_ITERATORS_XML_UNESCAPE_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_XML_UNESCAPE_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_unescape.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/config.hpp>
  15. #include <boost/detail/workaround.hpp>
  16. #include <boost/assert.hpp>
  17. #include <boost/serialization/throw_exception.hpp>
  18. #include <boost/archive/iterators/unescape.hpp>
  19. #include <boost/archive/iterators/dataflow_exception.hpp>
  20. namespace boost {
  21. namespace archive {
  22. namespace iterators {
  23. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  24. // replace &??? xml escape sequences with the corresponding characters
  25. template<class Base>
  26. class xml_unescape
  27. : public unescape<xml_unescape<Base>, Base>
  28. {
  29. friend class boost::iterator_core_access;
  30. typedef xml_unescape<Base> this_t;
  31. typedef unescape<this_t, Base> super_t;
  32. typedef typename boost::iterator_reference<this_t> reference_type;
  33. reference_type dereference() const {
  34. return unescape<xml_unescape<Base>, Base>::dereference();
  35. }
  36. public:
  37. // msvc versions prior to 14.0 crash with and ICE
  38. #if BOOST_WORKAROUND(BOOST_MSVC, < 1900)
  39. typedef int value_type;
  40. #else
  41. typedef typename super_t::value_type value_type;
  42. #endif
  43. void drain_residue(const char *literal);
  44. value_type drain();
  45. template<class T>
  46. xml_unescape(T start) :
  47. super_t(Base(static_cast< T >(start)))
  48. {}
  49. // intel 7.1 doesn't like default copy constructor
  50. xml_unescape(const xml_unescape & rhs) :
  51. super_t(rhs.base_reference())
  52. {}
  53. };
  54. template<class Base>
  55. void xml_unescape<Base>::drain_residue(const char * literal){
  56. do{
  57. if(* literal != * ++(this->base_reference()))
  58. boost::serialization::throw_exception(
  59. dataflow_exception(
  60. dataflow_exception::invalid_xml_escape_sequence
  61. )
  62. );
  63. }
  64. while('\0' != * ++literal);
  65. }
  66. // note key constraint on this function is that can't "look ahead" any
  67. // more than necessary into base iterator. Doing so would alter the base
  68. // iterator refenence which would make subsequent iterator comparisons
  69. // incorrect and thereby break the composiblity of iterators.
  70. template<class Base>
  71. typename xml_unescape<Base>::value_type
  72. //int
  73. xml_unescape<Base>::drain(){
  74. value_type retval = * this->base_reference();
  75. if('&' != retval){
  76. return retval;
  77. }
  78. retval = * ++(this->base_reference());
  79. switch(retval){
  80. case 'l': // &lt;
  81. drain_residue("t;");
  82. retval = '<';
  83. break;
  84. case 'g': // &gt;
  85. drain_residue("t;");
  86. retval = '>';
  87. break;
  88. case 'a':
  89. retval = * ++(this->base_reference());
  90. switch(retval){
  91. case 'p': // &apos;
  92. drain_residue("os;");
  93. retval = '\'';
  94. break;
  95. case 'm': // &amp;
  96. drain_residue("p;");
  97. retval = '&';
  98. break;
  99. }
  100. break;
  101. case 'q':
  102. drain_residue("uot;");
  103. retval = '"';
  104. break;
  105. }
  106. return retval;
  107. }
  108. } // namespace iterators
  109. } // namespace archive
  110. } // namespace boost
  111. #endif // BOOST_ARCHIVE_ITERATORS_XML_UNESCAPE_HPP