basic_text_iprimitive.ipp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // basic_text_iprimitive.ipp:
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org for updates, documentation, and revision history.
  8. #include <cstddef> // size_t, NULL
  9. #include <limits> // NULL
  10. #include <boost/config.hpp>
  11. #if defined(BOOST_NO_STDC_NAMESPACE)
  12. namespace std{
  13. using ::size_t;
  14. } // namespace std
  15. #endif
  16. #include <boost/serialization/throw_exception.hpp>
  17. #include <boost/archive/basic_text_iprimitive.hpp>
  18. #include <boost/archive/iterators/remove_whitespace.hpp>
  19. #include <boost/archive/iterators/istream_iterator.hpp>
  20. #include <boost/archive/iterators/binary_from_base64.hpp>
  21. #include <boost/archive/iterators/transform_width.hpp>
  22. namespace boost {
  23. namespace archive {
  24. namespace detail {
  25. template<class CharType>
  26. static inline bool is_whitespace(CharType c);
  27. template<>
  28. inline bool is_whitespace(char t){
  29. return 0 != std::isspace(t);
  30. }
  31. #ifndef BOOST_NO_CWCHAR
  32. template<>
  33. inline bool is_whitespace(wchar_t t){
  34. return 0 != std::iswspace(t);
  35. }
  36. #endif
  37. } // detail
  38. // translate base64 text into binary and copy into buffer
  39. // until buffer is full.
  40. template<class IStream>
  41. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  42. basic_text_iprimitive<IStream>::load_binary(
  43. void *address,
  44. std::size_t count
  45. ){
  46. typedef typename IStream::char_type CharType;
  47. if(0 == count)
  48. return;
  49. BOOST_ASSERT(
  50. static_cast<std::size_t>((std::numeric_limits<std::streamsize>::max)())
  51. > (count + sizeof(CharType) - 1)/sizeof(CharType)
  52. );
  53. if(is.fail())
  54. boost::serialization::throw_exception(
  55. archive_exception(archive_exception::input_stream_error)
  56. );
  57. // convert from base64 to binary
  58. typedef typename
  59. iterators::transform_width<
  60. iterators::binary_from_base64<
  61. iterators::remove_whitespace<
  62. iterators::istream_iterator<CharType>
  63. >
  64. ,typename IStream::int_type
  65. >
  66. ,8
  67. ,6
  68. ,CharType
  69. >
  70. binary;
  71. binary i = binary(iterators::istream_iterator<CharType>(is));
  72. char * caddr = static_cast<char *>(address);
  73. // take care that we don't increment anymore than necessary
  74. while(count-- > 0){
  75. *caddr++ = static_cast<char>(*i++);
  76. }
  77. // skip over any excess input
  78. for(;;){
  79. typename IStream::int_type r;
  80. r = is.get();
  81. if(is.eof())
  82. break;
  83. if(detail::is_whitespace(static_cast<CharType>(r)))
  84. break;
  85. }
  86. }
  87. template<class IStream>
  88. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  89. basic_text_iprimitive<IStream>::basic_text_iprimitive(
  90. IStream &is_,
  91. bool no_codecvt
  92. ) :
  93. is(is_),
  94. flags_saver(is_),
  95. precision_saver(is_),
  96. #ifndef BOOST_NO_STD_LOCALE
  97. codecvt_null_facet(1),
  98. archive_locale(is.getloc(), & codecvt_null_facet),
  99. locale_saver(is)
  100. {
  101. if(! no_codecvt){
  102. is_.sync();
  103. is_.imbue(archive_locale);
  104. }
  105. is_ >> std::noboolalpha;
  106. }
  107. #else
  108. {}
  109. #endif
  110. template<class IStream>
  111. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  112. basic_text_iprimitive<IStream>::~basic_text_iprimitive(){
  113. }
  114. } // namespace archive
  115. } // namespace boost