remove_whitespace.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef BOOST_ARCHIVE_ITERATORS_REMOVE_WHITESPACE_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_REMOVE_WHITESPACE_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. // remove_whitespace.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/iterator/iterator_adaptor.hpp>
  18. #include <boost/iterator/filter_iterator.hpp>
  19. //#include <boost/detail/workaround.hpp>
  20. //#if ! BOOST_WORKAROUND(BOOST_MSVC, <=1300)
  21. // here is the default standard implementation of the functor used
  22. // by the filter iterator to remove spaces. Unfortunately usage
  23. // of this implementation in combination with spirit trips a bug
  24. // VC 6.5. The only way I can find to work around it is to
  25. // implement a special non-standard version for this platform
  26. #ifndef BOOST_NO_CWCTYPE
  27. #include <cwctype> // iswspace
  28. #if defined(BOOST_NO_STDC_NAMESPACE)
  29. namespace std{ using ::iswspace; }
  30. #endif
  31. #endif
  32. #include <cctype> // isspace
  33. #if defined(BOOST_NO_STDC_NAMESPACE)
  34. namespace std{ using ::isspace; }
  35. #endif
  36. #if defined(__STD_RWCOMPILER_H__) || defined(_RWSTD_VER)
  37. // this is required for the RW STL on Linux and Tru64.
  38. #undef isspace
  39. #undef iswspace
  40. #endif
  41. //#endif // BOOST_WORKAROUND
  42. namespace { // anonymous
  43. template<class CharType>
  44. struct remove_whitespace_predicate;
  45. template<>
  46. struct remove_whitespace_predicate<char>
  47. {
  48. bool operator()(unsigned char t){
  49. return ! std::isspace(t);
  50. }
  51. };
  52. #ifndef BOOST_NO_CWCHAR
  53. template<>
  54. struct remove_whitespace_predicate<wchar_t>
  55. {
  56. bool operator()(wchar_t t){
  57. return ! std::iswspace(t);
  58. }
  59. };
  60. #endif
  61. } // namespace anonymous
  62. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  63. // convert base64 file data (including whitespace and padding) to binary
  64. namespace boost {
  65. namespace archive {
  66. namespace iterators {
  67. // custom version of filter iterator which doesn't look ahead further than
  68. // necessary
  69. template<class Predicate, class Base>
  70. class filter_iterator
  71. : public boost::iterator_adaptor<
  72. filter_iterator<Predicate, Base>,
  73. Base,
  74. use_default,
  75. single_pass_traversal_tag
  76. >
  77. {
  78. friend class boost::iterator_core_access;
  79. typedef BOOST_DEDUCED_TYPENAME boost::iterator_adaptor<
  80. filter_iterator<Predicate, Base>,
  81. Base,
  82. use_default,
  83. single_pass_traversal_tag
  84. > super_t;
  85. typedef filter_iterator<Predicate, Base> this_t;
  86. typedef BOOST_DEDUCED_TYPENAME super_t::reference reference_type;
  87. reference_type dereference_impl(){
  88. if(! m_full){
  89. while(! m_predicate(* this->base_reference()))
  90. ++(this->base_reference());
  91. m_full = true;
  92. }
  93. return * this->base_reference();
  94. }
  95. reference_type dereference() const {
  96. return const_cast<this_t *>(this)->dereference_impl();
  97. }
  98. Predicate m_predicate;
  99. bool m_full;
  100. public:
  101. // note: this function is public only because comeau compiler complained
  102. // I don't know if this is because the compiler is wrong or what
  103. void increment(){
  104. m_full = false;
  105. ++(this->base_reference());
  106. }
  107. filter_iterator(Base start) :
  108. super_t(start),
  109. m_full(false)
  110. {}
  111. filter_iterator(){}
  112. };
  113. template<class Base>
  114. class remove_whitespace :
  115. public filter_iterator<
  116. remove_whitespace_predicate<BOOST_DEDUCED_TYPENAME Base::value_type>,
  117. Base
  118. >
  119. {
  120. friend class boost::iterator_core_access;
  121. typedef filter_iterator<
  122. remove_whitespace_predicate<BOOST_DEDUCED_TYPENAME Base::value_type>,
  123. Base
  124. > super_t;
  125. public:
  126. // remove_whitespace(){} // why is this needed?
  127. // make composible buy using templated constructor
  128. template<class T>
  129. remove_whitespace(BOOST_PFTO_WRAPPER(T) start) :
  130. super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast<T>(start))))
  131. {}
  132. // intel 7.1 doesn't like default copy constructor
  133. remove_whitespace(const remove_whitespace & rhs) :
  134. super_t(rhs.base_reference())
  135. {}
  136. };
  137. } // namespace iterators
  138. } // namespace archive
  139. } // namespace boost
  140. #endif // BOOST_ARCHIVE_ITERATORS_REMOVE_WHITESPACE_HPP