xml_woarchive_impl.ipp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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> // std::copy
  12. #include <locale>
  13. #include <cstring> // strlen
  14. #include <cstdlib> // mbtowc
  15. #ifndef BOOST_NO_CWCHAR
  16. #include <cwchar> // wcslen
  17. #endif
  18. #include <boost/config.hpp>
  19. #if defined(BOOST_NO_STDC_NAMESPACE)
  20. namespace std{
  21. using ::strlen;
  22. #if ! defined(BOOST_NO_INTRINSIC_WCHAR_T)
  23. using ::mbtowc;
  24. using ::wcslen;
  25. #endif
  26. } // namespace std
  27. #endif
  28. #include <boost/core/uncaught_exceptions.hpp>
  29. #include <boost/archive/xml_woarchive.hpp>
  30. #include <boost/archive/detail/utf8_codecvt_facet.hpp>
  31. #include <boost/serialization/throw_exception.hpp>
  32. #include <boost/archive/iterators/xml_escape.hpp>
  33. #include <boost/archive/iterators/wchar_from_mb.hpp>
  34. #include <boost/archive/iterators/ostream_iterator.hpp>
  35. #include <boost/archive/iterators/dataflow_exception.hpp>
  36. namespace boost {
  37. namespace archive {
  38. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  39. // implemenations of functions specific to wide char archives
  40. // copy chars to output escaping to xml and widening characters as we go
  41. template<class InputIterator>
  42. void save_iterator(std::wostream &os, InputIterator begin, InputIterator end){
  43. typedef iterators::wchar_from_mb<
  44. iterators::xml_escape<InputIterator>
  45. > xmbtows;
  46. std::copy(
  47. xmbtows(begin),
  48. xmbtows(end),
  49. boost::archive::iterators::ostream_iterator<wchar_t>(os)
  50. );
  51. }
  52. template<class Archive>
  53. BOOST_WARCHIVE_DECL void
  54. xml_woarchive_impl<Archive>::save(const std::string & s){
  55. // note: we don't use s.begin() and s.end() because dinkumware
  56. // doesn't have string::value_type defined. So use a wrapper
  57. // around these values to implement the definitions.
  58. const char * begin = s.data();
  59. const char * end = begin + s.size();
  60. save_iterator(os, begin, end);
  61. }
  62. #ifndef BOOST_NO_STD_WSTRING
  63. template<class Archive>
  64. BOOST_WARCHIVE_DECL void
  65. xml_woarchive_impl<Archive>::save(const std::wstring & ws){
  66. #if 0
  67. typedef iterators::xml_escape<std::wstring::const_iterator> xmbtows;
  68. std::copy(
  69. xmbtows(ws.begin()),
  70. xmbtows(ws.end()),
  71. boost::archive::iterators::ostream_iterator<wchar_t>(os)
  72. );
  73. #endif
  74. typedef iterators::xml_escape<const wchar_t *> xmbtows;
  75. std::copy(
  76. xmbtows(ws.data()),
  77. xmbtows(ws.data() + ws.size()),
  78. boost::archive::iterators::ostream_iterator<wchar_t>(os)
  79. );
  80. }
  81. #endif //BOOST_NO_STD_WSTRING
  82. template<class Archive>
  83. BOOST_WARCHIVE_DECL void
  84. xml_woarchive_impl<Archive>::save(const char * s){
  85. save_iterator(os, s, s + std::strlen(s));
  86. }
  87. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  88. template<class Archive>
  89. BOOST_WARCHIVE_DECL void
  90. xml_woarchive_impl<Archive>::save(const wchar_t * ws){
  91. typedef iterators::xml_escape<const wchar_t *> xmbtows;
  92. std::copy(
  93. xmbtows(ws),
  94. xmbtows(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
  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. if(0 == (flags & no_codecvt)){
  112. archive_locale = std::locale(
  113. os_.getloc(),
  114. new boost::archive::detail::utf8_codecvt_facet
  115. );
  116. os_.flush();
  117. os_.imbue(archive_locale);
  118. }
  119. if(0 == (flags & no_header))
  120. this->init();
  121. }
  122. template<class Archive>
  123. BOOST_WARCHIVE_DECL
  124. xml_woarchive_impl<Archive>::~xml_woarchive_impl(){
  125. if(boost::core::uncaught_exceptions() > 0)
  126. return;
  127. if(0 == (this->get_flags() & no_header)){
  128. os << L"</boost_serialization>";
  129. }
  130. }
  131. template<class Archive>
  132. BOOST_WARCHIVE_DECL void
  133. xml_woarchive_impl<Archive>::save_binary(
  134. const void *address,
  135. std::size_t count
  136. ){
  137. this->end_preamble();
  138. #if ! defined(__MWERKS__)
  139. this->basic_text_oprimitive<std::wostream>::save_binary(
  140. #else
  141. this->basic_text_oprimitive::save_binary(
  142. #endif
  143. address,
  144. count
  145. );
  146. this->indent_next = true;
  147. }
  148. } // namespace archive
  149. } // namespace boost
  150. #endif //BOOST_NO_STD_WSTREAMBUF