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