/Src/Dependencies/Boost/boost/archive/impl/xml_iarchive_impl.ipp

http://hadesmem.googlecode.com/ · C++ Header · 204 lines · 172 code · 22 blank · 10 comment · 17 complexity · 8d61babd2c459275382e22fe854b7a6f MD5 · raw file

  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // xml_iarchive_impl.cpp:
  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. // See http://www.boost.org for updates, documentation, and revision history.
  8. #include <boost/config.hpp>
  9. #include <cstring> // memcpy
  10. #include <cstddef> // NULL
  11. #if defined(BOOST_NO_STDC_NAMESPACE)
  12. namespace std{
  13. using ::memcpy;
  14. } // namespace std
  15. #endif
  16. #ifndef BOOST_NO_CWCHAR
  17. #include <cstdlib> // mbtowc
  18. #if defined(BOOST_NO_STDC_NAMESPACE)
  19. namespace std{
  20. using ::mbtowc;
  21. } // namespace std
  22. #endif
  23. #endif // BOOST_NO_CWCHAR
  24. #include <boost/detail/workaround.hpp> // RogueWave and Dinkumware
  25. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
  26. #include <boost/archive/dinkumware.hpp>
  27. #endif
  28. #include <boost/detail/no_exceptions_support.hpp>
  29. #include <boost/archive/xml_archive_exception.hpp>
  30. #include <boost/archive/iterators/dataflow_exception.hpp>
  31. #include <boost/archive/basic_xml_archive.hpp>
  32. #include <boost/archive/xml_iarchive.hpp>
  33. #include "basic_xml_grammar.hpp"
  34. namespace boost {
  35. namespace archive {
  36. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  37. // implemenations of functions specific to char archives
  38. // wide char stuff used by char archives
  39. #ifndef BOOST_NO_CWCHAR
  40. #ifndef BOOST_NO_STD_WSTRING
  41. template<class Archive>
  42. BOOST_ARCHIVE_DECL(void)
  43. xml_iarchive_impl<Archive>::load(std::wstring &ws){
  44. std::string s;
  45. bool result = gimpl->parse_string(is, s);
  46. if(! result)
  47. boost::serialization::throw_exception(
  48. xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
  49. );
  50. #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
  51. if(NULL != ws.data())
  52. #endif
  53. ws.resize(0);
  54. const char * start = s.data();
  55. const char * end = start + s.size();
  56. while(start < end){
  57. wchar_t wc;
  58. int resultx = std::mbtowc(&wc, start, end - start);
  59. if(0 < resultx){
  60. start += resultx;
  61. ws += wc;
  62. continue;
  63. }
  64. boost::serialization::throw_exception(
  65. iterators::dataflow_exception(
  66. iterators::dataflow_exception::invalid_conversion
  67. )
  68. );
  69. }
  70. }
  71. #endif // BOOST_NO_STD_WSTRING
  72. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  73. template<class Archive>
  74. BOOST_ARCHIVE_DECL(void)
  75. xml_iarchive_impl<Archive>::load(wchar_t * ws){
  76. std::string s;
  77. bool result = gimpl->parse_string(is, s);
  78. if(! result)
  79. boost::serialization::throw_exception(
  80. xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
  81. );
  82. const char * start = s.data();
  83. const char * end = start + s.size();
  84. while(start < end){
  85. wchar_t wc;
  86. int result = std::mbtowc(&wc, start, end - start);
  87. if(0 < result){
  88. start += result;
  89. *ws++ = wc;
  90. continue;
  91. }
  92. boost::serialization::throw_exception(
  93. iterators::dataflow_exception(
  94. iterators::dataflow_exception::invalid_conversion
  95. )
  96. );
  97. }
  98. *ws = L'\0';
  99. }
  100. #endif // BOOST_NO_INTRINSIC_WCHAR_T
  101. #endif // BOOST_NO_CWCHAR
  102. template<class Archive>
  103. BOOST_ARCHIVE_DECL(void)
  104. xml_iarchive_impl<Archive>::load(std::string &s){
  105. bool result = gimpl->parse_string(is, s);
  106. if(! result)
  107. boost::serialization::throw_exception(
  108. xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
  109. );
  110. }
  111. template<class Archive>
  112. BOOST_ARCHIVE_DECL(void)
  113. xml_iarchive_impl<Archive>::load(char * s){
  114. std::string tstring;
  115. bool result = gimpl->parse_string(is, tstring);
  116. if(! result)
  117. boost::serialization::throw_exception(
  118. xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
  119. );
  120. std::memcpy(s, tstring.data(), tstring.size());
  121. s[tstring.size()] = 0;
  122. }
  123. template<class Archive>
  124. BOOST_ARCHIVE_DECL(void)
  125. xml_iarchive_impl<Archive>::load_override(class_name_type & t, int){
  126. const std::string & s = gimpl->rv.class_name;
  127. if(s.size() > BOOST_SERIALIZATION_MAX_KEY_SIZE - 1)
  128. boost::serialization::throw_exception(
  129. archive_exception(archive_exception::invalid_class_name)
  130. );
  131. char * tptr = t;
  132. std::memcpy(tptr, s.data(), s.size());
  133. tptr[s.size()] = '\0';
  134. }
  135. template<class Archive>
  136. BOOST_ARCHIVE_DECL(void)
  137. xml_iarchive_impl<Archive>::init(){
  138. gimpl->init(is);
  139. this->set_library_version(
  140. library_version_type(gimpl->rv.version)
  141. );
  142. }
  143. template<class Archive>
  144. BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY())
  145. xml_iarchive_impl<Archive>::xml_iarchive_impl(
  146. std::istream &is_,
  147. unsigned int flags
  148. ) :
  149. basic_text_iprimitive<std::istream>(
  150. is_,
  151. 0 != (flags & no_codecvt)
  152. ),
  153. basic_xml_iarchive<Archive>(flags),
  154. gimpl(new xml_grammar())
  155. {
  156. if(0 == (flags & no_header)){
  157. BOOST_TRY{
  158. init();
  159. }
  160. BOOST_CATCH(...){
  161. delete gimpl;
  162. #ifndef BOOST_NO_EXCEPTIONS
  163. throw; // re-throw
  164. #endif
  165. }
  166. BOOST_CATCH_END
  167. }
  168. }
  169. template<class Archive>
  170. BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY())
  171. xml_iarchive_impl<Archive>::~xml_iarchive_impl(){
  172. if(0 == (this->get_flags() & no_header)){
  173. BOOST_TRY{
  174. gimpl->windup(is);
  175. }
  176. BOOST_CATCH(...){}
  177. BOOST_CATCH_END
  178. }
  179. delete gimpl;
  180. }
  181. } // namespace archive
  182. } // namespace boost