xml_oarchive_impl.ipp 4.0 KB

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