8 #ifndef BOOST_LOCALE_GENERIC_CODECVT_HPP 
    9 #define BOOST_LOCALE_GENERIC_CODECVT_HPP 
   11 #include <boost/locale/utf.hpp> 
   12 #include <boost/cstdint.hpp> 
   13 #include <boost/static_assert.hpp> 
   19 #ifndef BOOST_LOCALE_DOXYGEN 
   23 BOOST_STATIC_ASSERT(
sizeof(std::mbstate_t)>=2);
 
   26 #if defined(_MSC_VER) && _MSC_VER < 1700 
   28 #define BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST 
  139 template<
typename CharType,
typename CodecvtImpl,
int CharSize=sizeof(CharType)>
 
  150 template<
typename CharType,
typename CodecvtImpl>
 
  155     typedef CharType uchar;
 
  158         std::codecvt<CharType,char,std::mbstate_t>(refs)
 
  161     CodecvtImpl 
const &implementation()
 const 
  163         return *
static_cast<CodecvtImpl 
const *
>(
this);
 
  169     virtual std::codecvt_base::result do_unshift(std::mbstate_t &s,
char *from,
char * ,
char *&next)
 const 
  171         boost::uint16_t &state = *
reinterpret_cast<boost::uint16_t *
>(&s);
 
  173         std::cout << 
"Entering unshift " << std::hex << state << std::dec << std::endl;
 
  176             return std::codecvt_base::error;
 
  178         return std::codecvt_base::ok;
 
  180     virtual int do_encoding() 
const throw()
 
  184     virtual int do_max_length() 
const throw()
 
  186         return implementation().max_encoding_length();
 
  188     virtual bool do_always_noconv() 
const throw()
 
  194     do_length(  std::mbstate_t 
 
  195     #ifdef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
 
  200             char const *from_end,
 
  203         #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST 
  204         char const *save_from = from;
 
  205         boost::uint16_t &state = *
reinterpret_cast<boost::uint16_t *
>(&std_state);
 
  207         size_t save_max = max;
 
  208         boost::uint16_t state = *
reinterpret_cast<boost::uint16_t 
const *
>(&std_state);
 
  211         typedef typename CodecvtImpl::state_type state_type;
 
  213         while(max > 0 && from < from_end){
 
  214             char const *prev_from = from;
 
  215             boost::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
 
  231         #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST 
  232         return from - save_from;
 
  234         return save_max - max;
 
  239     virtual std::codecvt_base::result 
 
  240     do_in(  std::mbstate_t &std_state,
 
  242             char const *from_end,
 
  243             char const *&from_next,
 
  246             uchar *&to_next)
 const 
  248         std::codecvt_base::result r=std::codecvt_base::ok;
 
  255         boost::uint16_t &state = *
reinterpret_cast<boost::uint16_t *
>(&std_state);
 
  256         typedef typename CodecvtImpl::state_type state_type;
 
  258         while(to < to_end && from < from_end)
 
  261             std::cout << 
"Entering IN--------------" << std::endl;
 
  262             std::cout << 
"State " << std::hex << state <<std::endl;
 
  263             std::cout << 
"Left in " << std::dec << from_end - from << 
" out " << to_end -to << std::endl;
 
  265             char const *from_saved = from;
 
  267             uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
 
  271                 r=std::codecvt_base::error;
 
  276                 r=std::codecvt_base::partial;
 
  294                 boost::uint16_t vh = ch >> 10;
 
  295                 boost::uint16_t vl = ch & 0x3FF;
 
  296                 boost::uint16_t w1 = vh + 0xD800;
 
  297                 boost::uint16_t w2 = vl + 0xDC00;
 
  311         if(r == std::codecvt_base::ok && (from!=from_end || state!=0))
 
  312             r = std::codecvt_base::partial;
 
  314         std::cout << 
"Returning ";
 
  316         case std::codecvt_base::ok:
 
  317             std::cout << 
"ok" << std::endl;
 
  319         case std::codecvt_base::partial:
 
  320             std::cout << 
"partial" << std::endl;
 
  322         case std::codecvt_base::error:
 
  323             std::cout << 
"error" << std::endl;
 
  326             std::cout << 
"other" << std::endl;
 
  329         std::cout << 
"State " << std::hex << state <<std::endl;
 
  330         std::cout << 
"Left in " << std::dec << from_end - from << 
" out " << to_end -to << std::endl;
 
  335     virtual std::codecvt_base::result 
 
  336     do_out( std::mbstate_t &std_state,
 
  338             uchar 
const *from_end,
 
  339             uchar 
const *&from_next,
 
  342             char *&to_next)
 const 
  344         std::codecvt_base::result r=std::codecvt_base::ok;
 
  352         boost::uint16_t &state = *
reinterpret_cast<boost::uint16_t *
>(&std_state);
 
  353         typedef typename CodecvtImpl::state_type state_type;
 
  355         while(to < to_end && from < from_end)
 
  358         std::cout << 
"Entering OUT --------------" << std::endl;
 
  359         std::cout << 
"State " << std::hex << state <<std::endl;
 
  360         std::cout << 
"Left in " << std::dec << from_end - from << 
" out " << to_end -to << std::endl;
 
  362             boost::uint32_t ch=0;
 
  367                 boost::uint16_t w1 = state;
 
  368                 boost::uint16_t w2 = *from; 
 
  371                 if(0xDC00 <= w2 && w2<=0xDFFF) {
 
  372                     boost::uint16_t vh = w1 - 0xD800;
 
  373                     boost::uint16_t vl = w2 - 0xDC00;
 
  374                     ch=((uint32_t(vh) << 10)  | vl) + 0x10000;
 
  378                     r=std::codecvt_base::error;
 
  384                 if(0xD800 <= ch && ch<=0xDBFF) {
 
  393                 else if(0xDC00 <= ch && ch<=0xDFFF) {
 
  397                     r=std::codecvt_base::error;
 
  402                 r=std::codecvt_base::error;
 
  405             boost::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
 
  407                 r=std::codecvt_base::partial;
 
  411                 r=std::codecvt_base::error;
 
  421         if(r==std::codecvt_base::ok && from!=from_end)
 
  422             r = std::codecvt_base::partial;
 
  424         std::cout << 
"Returning ";
 
  426         case std::codecvt_base::ok:
 
  427             std::cout << 
"ok" << std::endl;
 
  429         case std::codecvt_base::partial:
 
  430             std::cout << 
"partial" << std::endl;
 
  432         case std::codecvt_base::error:
 
  433             std::cout << 
"error" << std::endl;
 
  436             std::cout << 
"other" << std::endl;
 
  439         std::cout << 
"State " << std::hex << state <<std::endl;
 
  440         std::cout << 
"Left in " << std::dec << from_end - from << 
" out " << to_end -to << std::endl;
 
  453 template<
typename CharType,
typename CodecvtImpl>
 
  457     typedef CharType uchar;
 
  460         std::codecvt<CharType,char,std::mbstate_t>(refs)
 
  464     CodecvtImpl 
const &implementation()
 const 
  466         return *
static_cast<CodecvtImpl 
const *
>(
this);
 
  471     virtual std::codecvt_base::result do_unshift(std::mbstate_t &,
char *from,
char * ,
char *&next)
 const 
  474         return std::codecvt_base::ok;
 
  476     virtual int do_encoding() 
const throw()
 
  480     virtual int do_max_length() 
const throw()
 
  482         return implementation().max_encoding_length();
 
  484     virtual bool do_always_noconv() 
const throw()
 
  490     do_length(  std::mbstate_t 
 
  491     #ifdef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST
 
  496             char const *from_end,
 
  499         #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST  
  500         char const *start_from = from;
 
  502         size_t save_max = max;
 
  504         typedef typename CodecvtImpl::state_type state_type;
 
  506         while(max > 0 && from < from_end){
 
  507             char const *save_from = from;
 
  508             boost::uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
 
  515         #ifndef BOOST_LOCALE_DO_LENGTH_MBSTATE_CONST  
  516         return from - start_from;
 
  518         return save_max - max;
 
  523     virtual std::codecvt_base::result 
 
  524     do_in(  std::mbstate_t &,
 
  526             char const *from_end,
 
  527             char const *&from_next,
 
  530             uchar *&to_next)
 const 
  532         std::codecvt_base::result r=std::codecvt_base::ok;
 
  539         typedef typename CodecvtImpl::state_type state_type;
 
  541         while(to < to_end && from < from_end)
 
  544             std::cout << 
"Entering IN--------------" << std::endl;
 
  545             std::cout << 
"State " << std::hex << state <<std::endl;
 
  546             std::cout << 
"Left in " << std::dec << from_end - from << 
" out " << to_end -to << std::endl;
 
  548             char const *from_saved = from;
 
  550             uint32_t ch=implementation().to_unicode(cvt_state,from,from_end);
 
  553                 r=std::codecvt_base::error;
 
  558                 r=std::codecvt_base::partial;
 
  566         if(r == std::codecvt_base::ok && from!=from_end)
 
  567             r = std::codecvt_base::partial;
 
  569         std::cout << 
"Returning ";
 
  571         case std::codecvt_base::ok:
 
  572             std::cout << 
"ok" << std::endl;
 
  574         case std::codecvt_base::partial:
 
  575             std::cout << 
"partial" << std::endl;
 
  577         case std::codecvt_base::error:
 
  578             std::cout << 
"error" << std::endl;
 
  581             std::cout << 
"other" << std::endl;
 
  584         std::cout << 
"State " << std::hex << state <<std::endl;
 
  585         std::cout << 
"Left in " << std::dec << from_end - from << 
" out " << to_end -to << std::endl;
 
  590     virtual std::codecvt_base::result 
 
  591     do_out( std::mbstate_t &,
 
  593             uchar 
const *from_end,
 
  594             uchar 
const *&from_next,
 
  597             char *&to_next)
 const 
  599         std::codecvt_base::result r=std::codecvt_base::ok;
 
  600         typedef typename CodecvtImpl::state_type state_type;
 
  602         while(to < to_end && from < from_end)
 
  605         std::cout << 
"Entering OUT --------------" << std::endl;
 
  606         std::cout << 
"State " << std::hex << state <<std::endl;
 
  607         std::cout << 
"Left in " << std::dec << from_end - from << 
" out " << to_end -to << std::endl;
 
  609             boost::uint32_t ch=0;
 
  612                 r=std::codecvt_base::error;
 
  615             boost::uint32_t len = implementation().from_unicode(cvt_state,ch,to,to_end);
 
  617                 r=std::codecvt_base::partial;
 
  621                 r=std::codecvt_base::error;
 
  629         if(r==std::codecvt_base::ok && from!=from_end)
 
  630             r = std::codecvt_base::partial;
 
  632         std::cout << 
"Returning ";
 
  634         case std::codecvt_base::ok:
 
  635             std::cout << 
"ok" << std::endl;
 
  637         case std::codecvt_base::partial:
 
  638             std::cout << 
"partial" << std::endl;
 
  640         case std::codecvt_base::error:
 
  641             std::cout << 
"error" << std::endl;
 
  644             std::cout << 
"other" << std::endl;
 
  647         std::cout << 
"State " << std::hex << state <<std::endl;
 
  648         std::cout << 
"Left in " << std::dec << from_end - from << 
" out " << to_end -to << std::endl;
 
  655 template<
typename CharType,
typename CodecvtImpl>
 
  659     typedef CharType uchar;
 
  661     CodecvtImpl 
const &implementation()
 const 
  663         return *
static_cast<CodecvtImpl 
const *
>(
this);
 
  666     generic_codecvt(
size_t refs = 0) :  std::codecvt<char,char,std::mbstate_t>(refs)
 
bool is_valid_codepoint(code_point v)
the function checks if v is a valid code point 
Definition: utf.hpp:49
The state would be used by to_unicode functions. 
Definition: generic_codecvt.hpp:40
static const code_point incomplete
Special constant that defines incomplete code point. 
Definition: utf.hpp:44
initial_convertion_state
Definition: generic_codecvt.hpp:39
static const code_point illegal
Special constant that defines illegal code point. 
Definition: utf.hpp:39
A base class that used to define constants for generic_codecvt. 
Definition: generic_codecvt.hpp:34
Geneneric generic codecvt facet, various stateless encodings to UTF-16 and UTF-32 using wchar_t...
Definition: generic_codecvt.hpp:140
The state would be used by from_unicode functions. 
Definition: generic_codecvt.hpp:41