123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #ifndef BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP
- #define BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1020)
- # pragma once
- #endif
- #include <cassert>
- #include <cctype>
- #include <cstddef>
- #include <cstdlib>
- #include <boost/config.hpp>
- #if defined(BOOST_NO_STDC_NAMESPACE)
- namespace std{
- using ::mblen;
- using ::mbtowc;
- }
- #endif
- #include <boost/serialization/throw_exception.hpp>
- #include <boost/serialization/pfto.hpp>
- #include <boost/iterator/iterator_adaptor.hpp>
- #include <boost/archive/iterators/dataflow_exception.hpp>
- namespace boost {
- namespace archive {
- namespace iterators {
- template<class Base>
- class wchar_from_mb
- : public boost::iterator_adaptor<
- wchar_from_mb<Base>,
- Base,
- wchar_t,
- single_pass_traversal_tag,
- wchar_t
- >
- {
- friend class boost::iterator_core_access;
- typedef BOOST_DEDUCED_TYPENAME boost::iterator_adaptor<
- wchar_from_mb<Base>,
- Base,
- wchar_t,
- single_pass_traversal_tag,
- wchar_t
- > super_t;
- typedef wchar_from_mb<Base> this_t;
- wchar_t drain();
- wchar_t dereference_impl() {
- if(! m_full){
- m_current_value = drain();
- m_full = true;
- }
- return m_current_value;
- }
- wchar_t dereference() const {
- return const_cast<this_t *>(this)->dereference_impl();
- }
- void increment(){
- dereference_impl();
- m_full = false;
- ++(this->base_reference());
- };
- wchar_t m_current_value;
- bool m_full;
- public:
-
- template<class T>
- wchar_from_mb(BOOST_PFTO_WRAPPER(T) start) :
- super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast<T>(start)))),
- m_full(false)
- {}
-
- wchar_from_mb(const wchar_from_mb & rhs) :
- super_t(rhs.base_reference()),
- m_full(rhs.m_full)
- {}
- };
- template<class Base>
- wchar_t wchar_from_mb<Base>::drain(){
- char buffer[9];
- char * bptr = buffer;
- char val;
- for(std::size_t i = 0; i++ < (unsigned)MB_CUR_MAX;){
- val = * this->base_reference();
- *bptr++ = val;
- int result = std::mblen(buffer, i);
- if(-1 != result)
- break;
- ++(this->base_reference());
- }
- wchar_t retval;
- int result = std::mbtowc(& retval, buffer, MB_CUR_MAX);
- if(0 >= result)
- boost::serialization::throw_exception(iterators::dataflow_exception(
- iterators::dataflow_exception::invalid_conversion
- ));
- return retval;
- }
- }
- }
- }
- #endif
|