remove_whitespace.hpp 4.4 KB

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