xml_woarchive_impl.ipp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // xml_woarchive_impl.ipp:
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <boost/config.hpp>
  8. #ifndef BOOST_NO_STD_WSTREAMBUF
  9. #include <ostream>
  10. #include <string>
  11. #include <algorithm>
  12. #include <locale>
  13. #include <boost/config.hpp> // msvc 6.0 needs this to suppress warnings
  14. // for BOOST_DEDUCED_TYPENAME
  15. #include <cstring> // strlen
  16. #include <cstdlib> // mbtowc
  17. #include <cwchar> // wcslen
  18. #if defined(BOOST_NO_STDC_NAMESPACE)
  19. namespace std{
  20. using ::strlen;
  21. #if ! defined(BOOST_NO_INTRINSIC_WCHAR_T)
  22. using ::mbtowc;
  23. using ::wcslen;
  24. #endif
  25. } // namespace std
  26. #endif
  27. #include <boost/serialization/throw_exception.hpp>
  28. #include <boost/serialization/pfto.hpp>
  29. #include <boost/archive/iterators/xml_escape.hpp>
  30. #include <boost/archive/iterators/wchar_from_mb.hpp>
  31. #include <boost/archive/iterators/ostream_iterator.hpp>
  32. #include <boost/archive/iterators/dataflow_exception.hpp>
  33. #include <boost/archive/add_facet.hpp>
  34. #include <boost/archive/detail/utf8_codecvt_facet.hpp>
  35. namespace boost {
  36. namespace archive {
  37. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  38. // implemenations of functions specific to wide char archives
  39. // copy chars to output escaping to xml and widening characters as we go
  40. template<class InputIterator>
  41. void save_iterator(std::wostream &os, InputIterator begin, InputIterator end){
  42. typedef iterators::wchar_from_mb<
  43. iterators::xml_escape<InputIterator>
  44. > xmbtows;
  45. std::copy(
  46. xmbtows(BOOST_MAKE_PFTO_WRAPPER(begin)),
  47. xmbtows(BOOST_MAKE_PFTO_WRAPPER(end)),
  48. boost::archive::iterators::ostream_iterator<wchar_t>(os)
  49. );
  50. }
  51. template<class Archive>
  52. BOOST_WARCHIVE_DECL(void)
  53. xml_woarchive_impl<Archive>::save(const std::string & s){
  54. // note: we don't use s.begin() and s.end() because dinkumware
  55. // doesn't have string::value_type defined. So use a wrapper
  56. // around these values to implement the definitions.
  57. const char * begin = s.data();
  58. const char * end = begin + s.size();
  59. save_iterator(os, begin, end);
  60. }
  61. #ifndef BOOST_NO_STD_WSTRING
  62. template<class Archive>
  63. BOOST_WARCHIVE_DECL(void)
  64. xml_woarchive_impl<Archive>::save(const std::wstring & ws){
  65. #if 0
  66. typedef iterators::xml_escape<std::wstring::const_iterator> xmbtows;
  67. std::copy(
  68. xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws.begin())),
  69. xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws.end())),
  70. boost::archive::iterators::ostream_iterator<wchar_t>(os)
  71. );
  72. #endif
  73. typedef iterators::xml_escape<const wchar_t *> xmbtows;
  74. std::copy(
  75. xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws.data())),
  76. xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws.data() + ws.size())),
  77. boost::archive::iterators::ostream_iterator<wchar_t>(os)
  78. );
  79. }
  80. #endif //BOOST_NO_STD_WSTRING
  81. template<class Archive>
  82. BOOST_WARCHIVE_DECL(void)
  83. xml_woarchive_impl<Archive>::save(const char * s){
  84. save_iterator(os, s, s + std::strlen(s));
  85. }
  86. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  87. template<class Archive>
  88. BOOST_WARCHIVE_DECL(void)
  89. xml_woarchive_impl<Archive>::save(const wchar_t * ws){
  90. os << ws;
  91. typedef iterators::xml_escape<const wchar_t *> xmbtows;
  92. std::copy(
  93. xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws)),
  94. xmbtows(BOOST_MAKE_PFTO_WRAPPER(ws + std::wcslen(ws))),
  95. boost::archive::iterators::ostream_iterator<wchar_t>(os)
  96. );
  97. }
  98. #endif
  99. template<class Archive>
  100. BOOST_WARCHIVE_DECL(BOOST_PP_EMPTY())
  101. xml_woarchive_impl<Archive>::xml_woarchive_impl(
  102. std::wostream & os_,
  103. unsigned int flags
  104. ) :
  105. basic_text_oprimitive<std::wostream>(
  106. os_,
  107. true // don't change the codecvt - use the one below
  108. ),
  109. basic_xml_oarchive<Archive>(flags)
  110. {
  111. // Standard behavior is that imbue can be called
  112. // a) before output is invoked or
  113. // b) after flush has been called. This prevents one-to-many
  114. // transforms (such as one to many transforms from getting
  115. // mixed up. Unfortunately, STLPort doesn't respect b) above
  116. // so the restoration of the original archive locale done by
  117. // the locale_saver doesn't get processed,
  118. // before the current one is destroyed.
  119. // so the codecvt doesn't get replaced with the orginal
  120. // so closing the stream invokes codecvt::do_unshift
  121. // so it crashes because the corresponding locale that contained
  122. // the codecvt isn't around any more.
  123. // we can hack around this by using a static codecvt that never
  124. // gets destroyed.
  125. if(0 == (flags & no_codecvt)){
  126. boost::archive::detail::utf8_codecvt_facet *pfacet;
  127. #if defined(__SGI_STL_PORT)
  128. static boost::archive::detail::utf8_codecvt_facet
  129. facet(static_cast<size_t>(1));
  130. pfacet = & facet;
  131. #else
  132. pfacet = new boost::archive::detail::utf8_codecvt_facet;
  133. #endif
  134. archive_locale.reset(add_facet(std::locale::classic(), pfacet));
  135. os.imbue(* archive_locale);
  136. }
  137. if(0 == (flags & no_header))
  138. this->init();
  139. }
  140. } // namespace archive
  141. } // namespace boost
  142. #endif //BOOST_NO_STD_WSTREAMBUF