Chris@16: #ifndef BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP Chris@16: #define BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP Chris@16: Chris@16: // MS compatible compilers support #pragma once Chris@101: #if defined(_MSC_VER) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // wchar_from_mb.hpp Chris@16: Chris@16: // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . Chris@16: // Use, modification and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org for updates, documentation, and revision history. Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include // size_t Chris@16: #include // mblen Chris@16: Chris@101: #include Chris@16: #if defined(BOOST_NO_STDC_NAMESPACE) Chris@16: namespace std{ Chris@16: using ::mblen; Chris@16: using ::mbtowc; Chris@16: } // namespace std Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace archive { Chris@16: namespace iterators { Chris@16: Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // class used by text archives to translate char strings to wchar_t Chris@16: // strings of the currently selected locale Chris@16: template Chris@16: class wchar_from_mb Chris@16: : public boost::iterator_adaptor< Chris@16: wchar_from_mb, Chris@16: Base, Chris@16: wchar_t, Chris@16: single_pass_traversal_tag, Chris@16: wchar_t Chris@16: > Chris@16: { Chris@16: friend class boost::iterator_core_access; Chris@101: typedef typename boost::iterator_adaptor< Chris@16: wchar_from_mb, Chris@16: Base, Chris@16: wchar_t, Chris@16: single_pass_traversal_tag, Chris@16: wchar_t Chris@16: > super_t; Chris@16: Chris@16: typedef wchar_from_mb this_t; Chris@16: Chris@16: wchar_t drain(); Chris@16: Chris@16: wchar_t dereference_impl() { Chris@16: if(! m_full){ Chris@16: m_current_value = drain(); Chris@16: m_full = true; Chris@16: } Chris@16: return m_current_value; Chris@16: } Chris@16: Chris@16: wchar_t dereference() const { Chris@16: return const_cast(this)->dereference_impl(); Chris@16: } Chris@16: Chris@16: void increment(){ Chris@16: dereference_impl(); Chris@16: m_full = false; Chris@16: ++(this->base_reference()); Chris@16: }; Chris@16: Chris@16: wchar_t m_current_value; Chris@16: bool m_full; Chris@16: Chris@16: public: Chris@16: // make composible buy using templated constructor Chris@16: template Chris@16: wchar_from_mb(BOOST_PFTO_WRAPPER(T) start) : Chris@16: super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast< T >(start)))), Chris@16: m_full(false) Chris@16: {} Chris@16: // intel 7.1 doesn't like default copy constructor Chris@16: wchar_from_mb(const wchar_from_mb & rhs) : Chris@16: super_t(rhs.base_reference()), Chris@16: m_full(rhs.m_full) Chris@16: {} Chris@16: }; Chris@16: Chris@16: template Chris@16: wchar_t wchar_from_mb::drain(){ Chris@16: char buffer[9]; Chris@16: char * bptr = buffer; Chris@16: char val; Chris@16: for(std::size_t i = 0; i++ < (unsigned)MB_CUR_MAX;){ Chris@16: val = * this->base_reference(); Chris@16: *bptr++ = val; Chris@16: int result = std::mblen(buffer, i); Chris@16: if(-1 != result) Chris@16: break; Chris@16: ++(this->base_reference()); Chris@16: } Chris@16: wchar_t retval; Chris@16: int result = std::mbtowc(& retval, buffer, MB_CUR_MAX); Chris@16: if(0 >= result) Chris@16: boost::serialization::throw_exception(iterators::dataflow_exception( Chris@16: iterators::dataflow_exception::invalid_conversion Chris@16: )); Chris@16: return retval; Chris@16: } Chris@16: Chris@16: } // namespace iterators Chris@16: } // namespace archive Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP