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