portable_binary_iarchive.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // portable_binary_iarchive.cpp
  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 <istream>
  9. #include <string>
  10. #include <boost/predef/other/endian.h>
  11. #include <boost/serialization/throw_exception.hpp>
  12. #include <boost/archive/archive_exception.hpp>
  13. #include "portable_binary_iarchive.hpp"
  14. void
  15. portable_binary_iarchive::load_impl(boost::intmax_t & l, char maxsize){
  16. char size;
  17. l = 0;
  18. this->primitive_base_t::load(size);
  19. if(0 == size){
  20. return;
  21. }
  22. bool negative = (size < 0);
  23. if(negative)
  24. size = -size;
  25. if(size > maxsize)
  26. boost::serialization::throw_exception(
  27. portable_binary_iarchive_exception()
  28. );
  29. char * cptr = reinterpret_cast<char *>(& l);
  30. #if BOOST_ENDIAN_BIG_BYTE
  31. cptr += (sizeof(boost::intmax_t) - size);
  32. #endif
  33. this->primitive_base_t::load_binary(cptr, size);
  34. #if BOOST_ENDIAN_BIG_BYTE
  35. if(m_flags & endian_little)
  36. #else
  37. if(m_flags & endian_big)
  38. #endif
  39. reverse_bytes(size, cptr);
  40. if(negative)
  41. l = -l;
  42. }
  43. void
  44. portable_binary_iarchive::load_override(
  45. boost::archive::class_name_type & t
  46. ){
  47. std::string cn;
  48. cn.reserve(BOOST_SERIALIZATION_MAX_KEY_SIZE);
  49. load_override(cn);
  50. if(cn.size() > (BOOST_SERIALIZATION_MAX_KEY_SIZE - 1))
  51. boost::serialization::throw_exception(
  52. boost::archive::archive_exception(
  53. boost::archive::archive_exception::invalid_class_name)
  54. );
  55. std::memcpy(t, cn.data(), cn.size());
  56. // borland tweak
  57. t.t[cn.size()] = '\0';
  58. }
  59. void
  60. portable_binary_iarchive::init(unsigned int flags){
  61. if(0 == (flags & boost::archive::no_header)){
  62. // read signature in an archive version independent manner
  63. std::string file_signature;
  64. * this >> file_signature;
  65. if(file_signature != boost::archive::BOOST_ARCHIVE_SIGNATURE())
  66. boost::serialization::throw_exception(
  67. boost::archive::archive_exception(
  68. boost::archive::archive_exception::invalid_signature
  69. )
  70. );
  71. // make sure the version of the reading archive library can
  72. // support the format of the archive being read
  73. boost::archive::library_version_type input_library_version;
  74. * this >> input_library_version;
  75. // extra little .t is to get around borland quirk
  76. if(boost::archive::BOOST_ARCHIVE_VERSION() < input_library_version)
  77. boost::serialization::throw_exception(
  78. boost::archive::archive_exception(
  79. boost::archive::archive_exception::unsupported_version
  80. )
  81. );
  82. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
  83. this->set_library_version(input_library_version);
  84. //#else
  85. //#if ! BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
  86. //detail::
  87. //#endif
  88. boost::archive::detail::basic_iarchive::set_library_version(
  89. input_library_version
  90. );
  91. #endif
  92. }
  93. unsigned char x;
  94. load(x);
  95. m_flags = x << CHAR_BIT;
  96. }
  97. #include <boost/archive/impl/archive_serializer_map.ipp>
  98. #include <boost/archive/impl/basic_binary_iprimitive.ipp>
  99. namespace boost {
  100. namespace archive {
  101. namespace detail {
  102. template class archive_serializer_map<portable_binary_iarchive>;
  103. }
  104. template class basic_binary_iprimitive<
  105. portable_binary_iarchive,
  106. std::istream::char_type,
  107. std::istream::traits_type
  108. > ;
  109. } // namespace archive
  110. } // namespace boost