basic_text_oprimitive.ipp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // basic_text_oprimitive.ipp:
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org for updates, documentation, and revision history.
  8. #include <cstddef> // NULL
  9. #include <algorithm> // std::copy
  10. #include <boost/config.hpp>
  11. #if defined(BOOST_NO_STDC_NAMESPACE)
  12. namespace std{
  13. using ::size_t;
  14. } // namespace std
  15. #endif
  16. #include <boost/core/uncaught_exceptions.hpp>
  17. #include <boost/archive/basic_text_oprimitive.hpp>
  18. #include <boost/archive/iterators/base64_from_binary.hpp>
  19. #include <boost/archive/iterators/insert_linebreaks.hpp>
  20. #include <boost/archive/iterators/transform_width.hpp>
  21. #include <boost/archive/iterators/ostream_iterator.hpp>
  22. namespace boost {
  23. namespace archive {
  24. // translate to base64 and copy in to buffer.
  25. template<class OStream>
  26. BOOST_ARCHIVE_OR_WARCHIVE_DECL void
  27. basic_text_oprimitive<OStream>::save_binary(
  28. const void *address,
  29. std::size_t count
  30. ){
  31. typedef typename OStream::char_type CharType;
  32. if(0 == count)
  33. return;
  34. if(os.fail())
  35. boost::serialization::throw_exception(
  36. archive_exception(archive_exception::output_stream_error)
  37. );
  38. os.put('\n');
  39. typedef
  40. boost::archive::iterators::insert_linebreaks<
  41. boost::archive::iterators::base64_from_binary<
  42. boost::archive::iterators::transform_width<
  43. const char *,
  44. 6,
  45. 8
  46. >
  47. >
  48. ,76
  49. ,const char // cwpro8 needs this
  50. >
  51. base64_text;
  52. boost::archive::iterators::ostream_iterator<CharType> oi(os);
  53. std::copy(
  54. base64_text(static_cast<const char *>(address)),
  55. base64_text(
  56. static_cast<const char *>(address) + count
  57. ),
  58. oi
  59. );
  60. std::size_t tail = count % 3;
  61. if(tail > 0){
  62. *oi++ = '=';
  63. if(tail < 2)
  64. *oi = '=';
  65. }
  66. }
  67. template<class OStream>
  68. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  69. basic_text_oprimitive<OStream>::basic_text_oprimitive(
  70. OStream & os_,
  71. bool no_codecvt
  72. ) :
  73. os(os_),
  74. flags_saver(os_),
  75. #ifndef BOOST_NO_STD_LOCALE
  76. precision_saver(os_),
  77. codecvt_null_facet(1),
  78. archive_locale(os.getloc(), & codecvt_null_facet),
  79. locale_saver(os)
  80. {
  81. if(! no_codecvt){
  82. os_.flush();
  83. os_.imbue(archive_locale);
  84. }
  85. os_ << std::noboolalpha;
  86. }
  87. #else
  88. precision_saver(os_)
  89. {}
  90. #endif
  91. template<class OStream>
  92. BOOST_ARCHIVE_OR_WARCHIVE_DECL
  93. basic_text_oprimitive<OStream>::~basic_text_oprimitive(){
  94. if(boost::core::uncaught_exceptions() > 0)
  95. return;
  96. os << std::endl;
  97. }
  98. } //namespace boost
  99. } //namespace archive