xml_unescape.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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) && (_MSC_VER >= 1020)
  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 <cassert>
  15. #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
  16. #include <boost/serialization/throw_exception.hpp>
  17. #include <boost/serialization/pfto.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 BOOST_DEDUCED_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. void drain_residue(const char *literal);
  38. int drain();
  39. template<class T>
  40. xml_unescape(BOOST_PFTO_WRAPPER(T) start) :
  41. super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast<T>(start))))
  42. {}
  43. // intel 7.1 doesn't like default copy constructor
  44. xml_unescape(const xml_unescape & rhs) :
  45. super_t(rhs.base_reference())
  46. {}
  47. };
  48. template<class Base>
  49. void xml_unescape<Base>::drain_residue(const char * literal){
  50. do{
  51. if(* literal != * ++(this->base_reference()))
  52. boost::serialization::throw_exception(
  53. dataflow_exception(
  54. dataflow_exception::invalid_xml_escape_sequence
  55. )
  56. );
  57. }
  58. while('\0' != * ++literal);
  59. }
  60. // note key constraint on this function is that can't "look ahead" any
  61. // more than necessary into base iterator. Doing so would alter the base
  62. // iterator refenence which would make subsequent iterator comparisons
  63. // incorrect and thereby break the composiblity of iterators.
  64. template<class Base>
  65. int xml_unescape<Base>::drain(){
  66. int retval = * this->base_reference();
  67. if('&' != retval){
  68. return retval;
  69. }
  70. retval = * ++(this->base_reference());
  71. switch(retval){
  72. case 'l': // &lt;
  73. drain_residue("t;");
  74. retval = '<';
  75. break;
  76. case 'g': // &gt;
  77. drain_residue("t;");
  78. retval = '>';
  79. break;
  80. case 'a':
  81. retval = * ++(this->base_reference());
  82. switch(retval){
  83. case 'p': // &apos;
  84. drain_residue("os;");
  85. retval = '\'';
  86. break;
  87. case 'm': // &amp;
  88. drain_residue("p;");
  89. retval = '&';
  90. break;
  91. }
  92. break;
  93. case 'q':
  94. drain_residue("uot;");
  95. retval = '"';
  96. break;
  97. }
  98. return retval;
  99. }
  100. } // namespace iterators
  101. } // namespace archive
  102. } // namespace boost
  103. #endif // BOOST_ARCHIVE_ITERATORS_XML_UNESCAPE_HPP