basic_text_iprimitive.ipp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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
  9. #include <cstddef> // 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/serialization/pfto.hpp>
  18. #include <boost/archive/basic_text_iprimitive.hpp>
  19. #include <boost/archive/codecvt_null.hpp>
  20. #include <boost/archive/add_facet.hpp>
  21. #include <boost/archive/iterators/remove_whitespace.hpp>
  22. #include <boost/archive/iterators/istream_iterator.hpp>
  23. #include <boost/archive/iterators/binary_from_base64.hpp>
  24. #include <boost/archive/iterators/transform_width.hpp>
  25. namespace boost {
  26. namespace archive {
  27. namespace {
  28. template<class CharType>
  29. bool is_whitespace(CharType c);
  30. template<>
  31. bool is_whitespace(char t){
  32. return 0 != std::isspace(t);
  33. }
  34. #ifndef BOOST_NO_CWCHAR
  35. template<>
  36. bool is_whitespace(wchar_t t){
  37. return 0 != std::iswspace(t);
  38. }
  39. #endif
  40. }
  41. // translate base64 text into binary and copy into buffer
  42. // until buffer is full.
  43. template<class IStream>
  44. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  45. basic_text_iprimitive<IStream>::load_binary(
  46. void *address,
  47. std::size_t count
  48. ){
  49. typedef BOOST_DEDUCED_TYPENAME IStream::char_type CharType;
  50. if(0 == count)
  51. return;
  52. BOOST_ASSERT(
  53. static_cast<std::size_t>((std::numeric_limits<std::streamsize>::max)())
  54. > (count + sizeof(CharType) - 1)/sizeof(CharType)
  55. );
  56. if(is.fail())
  57. boost::serialization::throw_exception(
  58. archive_exception(archive_exception::input_stream_error)
  59. );
  60. // convert from base64 to binary
  61. typedef BOOST_DEDUCED_TYPENAME
  62. iterators::transform_width<
  63. iterators::binary_from_base64<
  64. iterators::remove_whitespace<
  65. iterators::istream_iterator<CharType>
  66. >
  67. ,CharType
  68. >
  69. ,8
  70. ,6
  71. ,CharType
  72. >
  73. binary;
  74. binary i = binary(
  75. BOOST_MAKE_PFTO_WRAPPER(
  76. iterators::istream_iterator<CharType>(is)
  77. )
  78. );
  79. char * caddr = static_cast<char *>(address);
  80. // take care that we don't increment anymore than necessary
  81. while(count-- > 0){
  82. *caddr++ = static_cast<char>(*i++);
  83. }
  84. // skip over any excess input
  85. for(;;){
  86. BOOST_DEDUCED_TYPENAME IStream::int_type r;
  87. r = is.get();
  88. if(is.eof())
  89. break;
  90. if(is_whitespace(static_cast<CharType>(r)))
  91. break;
  92. }
  93. }
  94. template<class IStream>
  95. BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
  96. basic_text_iprimitive<IStream>::basic_text_iprimitive(
  97. IStream &is_,
  98. bool no_codecvt
  99. ) :
  100. #ifndef BOOST_NO_STD_LOCALE
  101. is(is_),
  102. flags_saver(is_),
  103. precision_saver(is_),
  104. archive_locale(NULL),
  105. locale_saver(* is_.rdbuf())
  106. {
  107. if(! no_codecvt){
  108. archive_locale.reset(
  109. add_facet(
  110. std::locale::classic(),
  111. new codecvt_null<BOOST_DEDUCED_TYPENAME IStream::char_type>
  112. )
  113. );
  114. is.imbue(* archive_locale);
  115. }
  116. is >> std::noboolalpha;
  117. }
  118. #else
  119. is(is_),
  120. flags_saver(is_),
  121. precision_saver(is_)
  122. {}
  123. #endif
  124. template<class IStream>
  125. BOOST_ARCHIVE_OR_WARCHIVE_DECL(BOOST_PP_EMPTY())
  126. basic_text_iprimitive<IStream>::~basic_text_iprimitive(){
  127. is.sync();
  128. }
  129. } // namespace archive
  130. } // namespace boost