/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 // xml_woarchive_impl.ipp: // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include #ifndef BOOST_NO_STD_WSTREAMBUF #include #include #include #include #include // strlen #include // msvc 6.0 needs this to suppress warnings #if defined(BOOST_NO_STDC_NAMESPACE) namespace std{ using ::strlen; } // namespace std #endif #include #include #include #include // mbtowc #include // for BOOST_DEDUCED_TYPENAME #if defined(BOOST_NO_STDC_NAMESPACE) && ! defined(__LIBCOMO__) namespace std{ using ::strlen; using ::mbtowc; } //std #endif #include #include #include #include #include #include namespace boost { namespace archive { /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 // implemenations of functions specific to wide char archives std::wostream & operator<<(std::wostream &os, const char *t){ for(;;){ wchar_t wc; int result = std::mbtowc(&wc, t, 10 /* max number */); if(0 < result) os.put(wc); else if(0 == result) break; else boost::throw_exception( iterators::dataflow_exception( iterators::dataflow_exception::invalid_conversion ) ); } return os; } std::wostream & operator<<(std::wostream &os, const char t){ wchar_t wc; std::mbtowc(&wc, &t, 1); os.put(wc); return os; } // copy chars to output escaping to xml and widening characters as we go template void save_iterator(std::wostream &os, InputIterator begin, InputIterator end){ typedef iterators::wchar_from_mb< iterators::xml_escape > xmbtows; std::copy( xmbtows(BOOST_MAKE_PFTO_WRAPPER(begin)), xmbtows(BOOST_MAKE_PFTO_WRAPPER(end)), boost::archive::iterators::ostream_iterator(os) ); } template void xml_woarchive_impl::save(const std::string & s){ // note: we don't use s.begin() and s.end() because dinkumware // doesn't have string::value_type defined. So use a wrapper // around these values to implement the definitions. const char * begin = s.data(); const char * end = begin + s.size(); save_iterator(os, begin, end); } #ifndef BOOST_NO_STD_WSTRING template void xml_woarchive_impl::save(const std::wstring & ws){ typedef iterators::xml_escape xmbtows; std::copy( xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws.data())), xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws.data() + ws.size())), boost::archive::iterators::ostream_iterator(os) ); } #endif //BOOST_NO_STD_WSTRING template void xml_woarchive_impl::save(const char * s){ save_iterator(os, s, s + std::strlen(s)); } #ifndef BOOST_NO_INTRINSIC_WCHAR_T template void xml_woarchive_impl::save(const wchar_t * ws){ os << ws; typedef iterators::xml_escape xmbtows; std::copy( xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws)), xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws + std::wcslen(ws))), boost::archive::iterators::ostream_iterator(os) ); } #endif template xml_woarchive_impl::xml_woarchive_impl( std::wostream & os_, unsigned int flags ) : basic_text_oprimitive( os_, true // don't change the codecvt - use the one below ), basic_xml_oarchive(flags) { // Standard behavior is that imbue can be called // a) before output is invoked or // b) after flush has been called. This prevents one-to-many // transforms (such as one to many transforms from getting // mixed up. Unfortunately, STLPort doesn't respect b) above // so the restoration of the original archive locale done by // the locale_saver doesn't get processed, // before the current one is destroyed. // so the codecvt doesn't get replaced with the orginal // so closing the stream invokes codecvt::do_unshift // so it crashes because the corresponding locale that contained // the codecvt isn't around any more. // we can hack around this by using a static codecvt that never // gets destroyed. if(0 == (flags & no_codecvt)){ utf8_codecvt_facet_wchar_t *pfacet; #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION) static utf8_codecvt_facet_wchar_t facet(static_cast(1)); pfacet = & facet; #else pfacet = new utf8_codecvt_facet_wchar_t; #endif archive_locale.reset(add_facet(std::locale::classic(), pfacet)); os.imbue(* archive_locale); } if(0 == (flags & no_header)) this->init(); } } // namespace archive } // namespace boost #endif //BOOST_NO_STD_WSTREAMBUF