source: NonGTP/Boost/boost/archive/impl/xml_wiarchive_impl.ipp @ 857

Revision 857, 5.6 KB checked in by igarcia, 19 years ago (diff)
Line 
1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// xml_wiprimitive.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> // for BOOST_DEDUCED_TYPENAME
12
13#include <cstring>
14#if defined(BOOST_NO_STDC_NAMESPACE)
15namespace std{
16    using ::memcpy;
17} //std
18#endif
19
20#include <boost/config.hpp> // msvc 6.0 needs this to suppress warnings
21#ifndef BOOST_NO_STD_WSTREAMBUF
22
23#include <cassert>
24#include <algorithm>
25
26#include <boost/detail/workaround.hpp> // Dinkumware and RogueWave
27#if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
28#include <boost/archive/dinkumware.hpp>
29#endif
30
31#include <boost/io/ios_state.hpp>
32#include <boost/detail/no_exceptions_support.hpp>
33#include <boost/pfto.hpp>
34
35#include <boost/serialization/string.hpp>
36#include <boost/archive/add_facet.hpp>
37#include <boost/archive/archive_exception.hpp>
38#include <boost/archive/detail/utf8_codecvt_facet.hpp>
39
40#include <boost/archive/iterators/mb_from_wchar.hpp>
41
42#include <boost/archive/basic_xml_archive.hpp>
43#include <boost/archive/xml_wiarchive.hpp>
44
45#include "basic_xml_grammar.hpp"
46
47namespace boost {
48namespace archive {
49
50/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
51// implemenations of functions specific to wide char archives
52
53namespace { // anonymous
54
55void copy_to_ptr(char * s, const std::wstring & ws){
56    std::copy(
57        iterators::mb_from_wchar<std::wstring::const_iterator>(
58            BOOST_MAKE_PFTO_WRAPPER(ws.begin())
59        ),
60        iterators::mb_from_wchar<std::wstring::const_iterator>(
61            BOOST_MAKE_PFTO_WRAPPER(ws.end())
62        ),
63        s
64    );
65    s[ws.size()] = 0;
66}
67
68} // anonymous
69
70template<class Archive>
71BOOST_WARCHIVE_DECL(void)
72xml_wiarchive_impl<Archive>::load(std::string & s){
73    std::wstring ws;
74    bool result = gimpl->parse_string(is, ws);
75    if(! result)
76        boost::throw_exception(
77            xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
78        );
79    #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
80    if(NULL != s.data())
81    #endif
82        s.resize(0);
83    s.reserve(ws.size());
84    std::copy(
85        iterators::mb_from_wchar<std::wstring::iterator>(
86            BOOST_MAKE_PFTO_WRAPPER(ws.begin())
87        ),
88        iterators::mb_from_wchar<std::wstring::iterator>(
89            BOOST_MAKE_PFTO_WRAPPER(ws.end())
90        ),
91        std::back_inserter(s)
92    );
93}
94
95#ifndef BOOST_NO_STD_WSTRING
96template<class Archive>
97BOOST_WARCHIVE_DECL(void)
98xml_wiarchive_impl<Archive>::load(std::wstring & ws){
99    bool result = gimpl->parse_string(is, ws);
100    if(! result)
101        boost::throw_exception(
102            xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
103        );
104}
105#endif
106
107template<class Archive>
108BOOST_WARCHIVE_DECL(void)
109xml_wiarchive_impl<Archive>::load(char * s){
110    std::wstring ws;
111    bool result = gimpl->parse_string(is, ws);
112    if(! result)
113        boost::throw_exception(
114            xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
115        );
116    copy_to_ptr(s, ws);
117}
118
119#ifndef BOOST_NO_INTRINSIC_WCHAR_T
120template<class Archive>
121BOOST_WARCHIVE_DECL(void)
122xml_wiarchive_impl<Archive>::load(wchar_t * ws){
123    std::wstring twstring;
124    bool result = gimpl->parse_string(is, twstring);
125    if(! result)
126        boost::throw_exception(
127            xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
128        );
129    std::memcpy(ws, twstring.c_str(), twstring.size());
130    ws[twstring.size()] = L'\0';
131}
132#endif
133
134template<class Archive>
135BOOST_WARCHIVE_DECL(void)
136xml_wiarchive_impl<Archive>::load_override(class_name_type & t, int){
137    const std::wstring & ws = gimpl->rv.class_name;
138    if(ws.size() > BOOST_SERIALIZATION_MAX_KEY_SIZE - 1)
139        boost::throw_exception(archive_exception::invalid_class_name);
140    copy_to_ptr(t, ws);
141}
142
143template<class Archive>
144BOOST_WARCHIVE_DECL(void)
145xml_wiarchive_impl<Archive>::init(){
146    gimpl->init(is);
147}
148
149template<class Archive>
150BOOST_WARCHIVE_DECL(BOOST_PP_EMPTY())
151xml_wiarchive_impl<Archive>::xml_wiarchive_impl(
152    std::wistream &is_,
153    unsigned int flags
154) :
155    basic_text_iprimitive<std::wistream>(
156        is_,
157        true // don't change the codecvt - use the one below
158    ),
159    basic_xml_iarchive<Archive>(flags),
160    gimpl(new xml_wgrammar())
161{
162    if(0 == (flags & no_codecvt)){
163        archive_locale.reset(
164            add_facet(
165                std::locale::classic(),
166                new detail::utf8_codecvt_facet
167            )
168        );
169        is.imbue(* archive_locale);
170    }
171    if(0 == (flags & no_header)){
172        BOOST_TRY{
173            this->init();
174        }
175        BOOST_CATCH(...){
176            delete gimpl;
177            #ifndef BOOST_NO_EXCEPTIONS
178                throw; // re-throw
179            #endif
180        }
181        BOOST_CATCH_END
182    }
183}
184
185template<class Archive>
186BOOST_WARCHIVE_DECL(BOOST_PP_EMPTY())
187xml_wiarchive_impl<Archive>::~xml_wiarchive_impl(){
188    if(0 == (this->get_flags() & no_header)){
189        BOOST_TRY{
190            gimpl->windup(is);
191        }
192        BOOST_CATCH(...){}
193        BOOST_CATCH_END
194    }
195    delete gimpl;
196}
197
198} // namespace archive
199} // namespace boost
200
201#endif  // BOOST_NO_STD_WSTREAMBUF
Note: See TracBrowser for help on using the repository browser.