• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// xml_oarchive_impl.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#include <ostream>
10#include <iomanip>
11#include <algorithm> // std::copy
12#include <string>
13
14#include <cstring> // strlen
15#include <boost/config.hpp> // msvc 6.0 needs this to suppress warnings
16#if defined(BOOST_NO_STDC_NAMESPACE)
17namespace std{
18    using ::strlen;
19} // namespace std
20#endif
21
22#include <boost/core/uncaught_exceptions.hpp>
23#include <boost/archive/iterators/xml_escape.hpp>
24#include <boost/archive/iterators/ostream_iterator.hpp>
25
26#ifndef BOOST_NO_CWCHAR
27#include <boost/archive/wcslen.hpp>
28#include <boost/archive/iterators/mb_from_wchar.hpp>
29#endif
30
31namespace boost {
32namespace archive {
33
34/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
35// implemenations of functions specific to char archives
36
37// wide char stuff used by char archives
38#ifndef BOOST_NO_CWCHAR
39// copy chars to output escaping to xml and translating wide chars to mb chars
40template<class InputIterator>
41void save_iterator(std::ostream &os, InputIterator begin, InputIterator end){
42    typedef boost::archive::iterators::mb_from_wchar<
43        boost::archive::iterators::xml_escape<InputIterator>
44    > translator;
45    std::copy(
46        translator(begin),
47        translator(end),
48        boost::archive::iterators::ostream_iterator<char>(os)
49    );
50}
51
52#ifndef BOOST_NO_STD_WSTRING
53template<class Archive>
54BOOST_ARCHIVE_DECL void
55xml_oarchive_impl<Archive>::save(const std::wstring & ws){
56//  at least one library doesn't typedef value_type for strings
57//  so rather than using string directly make a pointer iterator out of it
58//    save_iterator(os, ws.data(), ws.data() + std::wcslen(ws.data()));
59    save_iterator(os, ws.data(), ws.data() + ws.size());
60}
61#endif
62
63#ifndef BOOST_NO_INTRINSIC_WCHAR_T
64template<class Archive>
65BOOST_ARCHIVE_DECL void
66xml_oarchive_impl<Archive>::save(const wchar_t * ws){
67    save_iterator(os, ws, ws + std::wcslen(ws));
68}
69#endif
70
71#endif // BOOST_NO_CWCHAR
72
73template<class Archive>
74BOOST_ARCHIVE_DECL void
75xml_oarchive_impl<Archive>::save(const std::string & s){
76//  at least one library doesn't typedef value_type for strings
77//  so rather than using string directly make a pointer iterator out of it
78    typedef boost::archive::iterators::xml_escape<
79        const char *
80    > xml_escape_translator;
81    std::copy(
82        xml_escape_translator(s.data()),
83        xml_escape_translator(s.data()+ s.size()),
84        boost::archive::iterators::ostream_iterator<char>(os)
85    );
86}
87
88template<class Archive>
89BOOST_ARCHIVE_DECL void
90xml_oarchive_impl<Archive>::save(const char * s){
91    typedef boost::archive::iterators::xml_escape<
92        const char *
93    > xml_escape_translator;
94    std::copy(
95        xml_escape_translator(s),
96        xml_escape_translator(s + std::strlen(s)),
97        boost::archive::iterators::ostream_iterator<char>(os)
98    );
99}
100
101template<class Archive>
102BOOST_ARCHIVE_DECL
103xml_oarchive_impl<Archive>::xml_oarchive_impl(
104    std::ostream & os_,
105    unsigned int flags
106) :
107    basic_text_oprimitive<std::ostream>(
108        os_,
109        0 != (flags & no_codecvt)
110    ),
111    basic_xml_oarchive<Archive>(flags)
112{}
113
114template<class Archive>
115BOOST_ARCHIVE_DECL void
116xml_oarchive_impl<Archive>::save_binary(const void *address, std::size_t count){
117    this->end_preamble();
118    #if ! defined(__MWERKS__)
119    this->basic_text_oprimitive<std::ostream>::save_binary(
120    #else
121    this->basic_text_oprimitive::save_binary(
122    #endif
123        address,
124        count
125    );
126    this->indent_next = true;
127}
128
129template<class Archive>
130BOOST_ARCHIVE_DECL
131xml_oarchive_impl<Archive>::~xml_oarchive_impl(){
132    if(boost::core::uncaught_exceptions() > 0)
133        return;
134    if(0 == (this->get_flags() & no_header))
135        this->windup();
136}
137
138} // namespace archive
139} // namespace boost
140