basic_binary_iarchive.ipp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // basic_binary_iarchive.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 <string>
  9. #include <boost/assert.hpp>
  10. #include <algorithm>
  11. #include <cstring>
  12. #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
  13. #if defined(BOOST_NO_STDC_NAMESPACE)
  14. namespace std{
  15. using ::memcpy;
  16. using ::strlen;
  17. using ::size_t;
  18. }
  19. #endif
  20. #include <boost/detail/workaround.hpp>
  21. #include <boost/detail/endian.hpp>
  22. #include <boost/archive/basic_binary_iarchive.hpp>
  23. namespace boost {
  24. namespace archive {
  25. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  26. // implementation of binary_binary_archive
  27. template<class Archive>
  28. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  29. basic_binary_iarchive<Archive>::load_override(class_name_type & t, int){
  30. std::string cn;
  31. cn.reserve(BOOST_SERIALIZATION_MAX_KEY_SIZE);
  32. load_override(cn, 0);
  33. if(cn.size() > (BOOST_SERIALIZATION_MAX_KEY_SIZE - 1))
  34. boost::serialization::throw_exception(
  35. archive_exception(archive_exception::invalid_class_name)
  36. );
  37. std::memcpy(t, cn.data(), cn.size());
  38. // borland tweak
  39. t.t[cn.size()] = '\0';
  40. }
  41. template<class Archive>
  42. BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
  43. basic_binary_iarchive<Archive>::init(){
  44. // read signature in an archive version independent manner
  45. std::string file_signature;
  46. try {
  47. std::size_t l;
  48. this->This()->load(l);
  49. if(l == std::strlen(BOOST_ARCHIVE_SIGNATURE())) {
  50. // borland de-allocator fixup
  51. #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
  52. if(NULL != file_signature.data())
  53. #endif
  54. file_signature.resize(l);
  55. // note breaking a rule here - could be a problem on some platform
  56. if(0 < l)
  57. this->This()->load_binary(&(*file_signature.begin()), l);
  58. }
  59. }
  60. catch(archive_exception const &) { // catch stream_error archive exceptions
  61. // will cause invalid_signature archive exception to be thrown below
  62. file_signature = "";
  63. }
  64. if(file_signature != BOOST_ARCHIVE_SIGNATURE())
  65. boost::serialization::throw_exception(
  66. archive_exception(archive_exception::invalid_signature)
  67. );
  68. // make sure the version of the reading archive library can
  69. // support the format of the archive being read
  70. library_version_type input_library_version;
  71. //* this->This() >> input_library_version;
  72. {
  73. int v = 0;
  74. v = this->This()->m_sb.sbumpc();
  75. #if defined(BOOST_LITTLE_ENDIAN)
  76. if(v < 6){
  77. ;
  78. }
  79. else
  80. if(v < 7){
  81. // version 6 - next byte should be zero
  82. this->This()->m_sb.sbumpc();
  83. }
  84. else
  85. if(v < 8){
  86. int x1;
  87. // version 7 = might be followed by zero or some other byte
  88. x1 = this->This()->m_sb.sgetc();
  89. // it's =a zero, push it back
  90. if(0 == x1)
  91. this->This()->m_sb.sbumpc();
  92. }
  93. else{
  94. // version 8+ followed by a zero
  95. this->This()->m_sb.sbumpc();
  96. }
  97. #elif defined(BOOST_BIG_ENDIAN)
  98. if(v == 0)
  99. v = this->This()->m_sb.sbumpc();
  100. #endif
  101. input_library_version = static_cast<library_version_type>(v);
  102. }
  103. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
  104. this->set_library_version(input_library_version);
  105. #else
  106. #if ! BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
  107. detail::
  108. #endif
  109. basic_iarchive::set_library_version(input_library_version);
  110. #endif
  111. if(BOOST_ARCHIVE_VERSION() < input_library_version)
  112. boost::serialization::throw_exception(
  113. archive_exception(archive_exception::unsupported_version)
  114. );
  115. }
  116. } // namespace archive
  117. } // namespace boost