Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / archive / impl / xml_iarchive_impl.ipp
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // xml_iarchive_impl.cpp:
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 <boost/config.hpp>
12 #include <cstring> // memcpy
13 #include <cstddef> // NULL
14 #if defined(BOOST_NO_STDC_NAMESPACE)
15 namespace std{ 
16     using ::memcpy;
17 } // namespace std
18 #endif
19
20 #ifndef BOOST_NO_CWCHAR
21 #include <cstdlib> // mbtowc
22 #if defined(BOOST_NO_STDC_NAMESPACE)
23 namespace std{ 
24     using ::mbtowc;
25  } // namespace std
26 #endif
27 #endif // BOOST_NO_CWCHAR
28
29 #include <boost/detail/workaround.hpp> // RogueWave and Dinkumware
30 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
31 #include <boost/archive/dinkumware.hpp>
32 #endif
33
34 #include <boost/detail/no_exceptions_support.hpp>
35
36 #include <boost/archive/xml_archive_exception.hpp>
37 #include <boost/archive/iterators/dataflow_exception.hpp>
38 #include <boost/archive/basic_xml_archive.hpp>
39 #include <boost/archive/xml_iarchive.hpp>
40
41 #include "basic_xml_grammar.hpp"
42
43 namespace boost {
44 namespace archive {
45
46 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
47 // implemenations of functions specific to char archives
48
49 // wide char stuff used by char archives
50
51 #ifndef BOOST_NO_CWCHAR
52 #ifndef BOOST_NO_STD_WSTRING
53 template<class Archive>
54 BOOST_ARCHIVE_DECL(void)
55 xml_iarchive_impl<Archive>::load(std::wstring &ws){
56     std::string s;
57     bool result = gimpl->parse_string(is, s);
58     if(! result)
59         boost::serialization::throw_exception(
60             xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
61         );
62     
63     #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
64     if(NULL != ws.data())
65     #endif
66         ws.resize(0);
67     const char * start = s.data();
68     const char * end = start + s.size();
69     while(start < end){
70         wchar_t wc;
71         int resultx = std::mbtowc(&wc, start, end - start);
72         if(0 < resultx){
73             start += resultx;
74             ws += wc;
75             continue;
76         }
77         boost::serialization::throw_exception(
78             iterators::dataflow_exception(
79                 iterators::dataflow_exception::invalid_conversion
80             )
81         );
82     }
83 }
84 #endif // BOOST_NO_STD_WSTRING
85
86 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
87 template<class Archive>
88 BOOST_ARCHIVE_DECL(void)
89 xml_iarchive_impl<Archive>::load(wchar_t * ws){
90     std::string s;
91     bool result = gimpl->parse_string(is, s);
92     if(! result)
93         boost::serialization::throw_exception(
94             xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
95         );
96         
97     const char * start = s.data();
98     const char * end = start + s.size();
99     while(start < end){
100         wchar_t wc;
101         int length = std::mbtowc(&wc, start, end - start);
102         if(0 < length){
103             start += length;
104             *ws++ = wc;
105             continue;
106         }
107         boost::serialization::throw_exception(
108             iterators::dataflow_exception(
109                 iterators::dataflow_exception::invalid_conversion
110             )
111         );
112     }
113     *ws = L'\0';
114 }
115 #endif // BOOST_NO_INTRINSIC_WCHAR_T
116
117 #endif // BOOST_NO_CWCHAR
118
119 template<class Archive>
120 BOOST_ARCHIVE_DECL(void)
121 xml_iarchive_impl<Archive>::load(std::string &s){
122     bool result = gimpl->parse_string(is, s);
123     if(! result)
124         boost::serialization::throw_exception(
125             xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
126         );
127 }
128
129 template<class Archive>
130 BOOST_ARCHIVE_DECL(void)
131 xml_iarchive_impl<Archive>::load(char * s){
132     std::string tstring;
133     bool result = gimpl->parse_string(is, tstring);
134     if(! result)
135         boost::serialization::throw_exception(
136             xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
137         );
138     std::memcpy(s, tstring.data(), tstring.size());
139     s[tstring.size()] = 0;
140 }
141
142 template<class Archive>
143 BOOST_ARCHIVE_DECL(void)
144 xml_iarchive_impl<Archive>::load_override(class_name_type & t, int){
145     const std::string & s = gimpl->rv.class_name;
146     if(s.size() > BOOST_SERIALIZATION_MAX_KEY_SIZE - 1)
147         boost::serialization::throw_exception(
148             archive_exception(archive_exception::invalid_class_name)
149        );
150     char * tptr = t;
151     std::memcpy(tptr, s.data(), s.size());
152     tptr[s.size()] = '\0';
153 }
154
155 template<class Archive>
156 BOOST_ARCHIVE_DECL(void)
157 xml_iarchive_impl<Archive>::init(){
158     gimpl->init(is);
159     this->set_library_version(
160         library_version_type(gimpl->rv.version)
161     );
162 }
163
164 template<class Archive>
165 BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY())
166 xml_iarchive_impl<Archive>::xml_iarchive_impl(
167     std::istream &is_,
168     unsigned int flags
169 ) :
170     basic_text_iprimitive<std::istream>(
171         is_, 
172         0 != (flags & no_codecvt)
173     ),
174     basic_xml_iarchive<Archive>(flags),
175     gimpl(new xml_grammar())
176 {
177     if(0 == (flags & no_header)){
178         BOOST_TRY{
179             init();
180         }
181         BOOST_CATCH(...){
182             delete gimpl;
183             #ifndef BOOST_NO_EXCEPTIONS
184                 throw; // re-throw
185             #endif
186         }
187         BOOST_CATCH_END
188     }
189 }
190
191 template<class Archive>
192 BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY())
193 xml_iarchive_impl<Archive>::~xml_iarchive_impl(){
194     if(0 == (this->get_flags() & no_header)){
195         BOOST_TRY{
196             gimpl->windup(is);
197         }
198         BOOST_CATCH(...){}
199         BOOST_CATCH_END
200     }
201     delete gimpl;
202 }
203 } // namespace archive
204 } // namespace boost