# codecvt.cc

    00001 // Copyright (C) 2000, 2002 Free Software Foundation, Inc.
    00002 //
    00003 // This file is part of the GNU ISO C++ Library.  This library is free
    00004 // software; you can redistribute it and/or modify it under the
    00005 // terms of the GNU General Public License as published by the
    00006 // Free Software Foundation; either version 2, or (at your option)
    00007 // any later version.
    00008
    00009 // This library is distributed in the hope that it will be useful,
    00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012 // GNU General Public License for more details.
    00013
    00014 // You should have received a copy of the GNU General Public License along
    00015 // with this library; see the file COPYING.  If not, write to the Free
    00016 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00017 // USA.
    00018
    00019 // As a special exception, you may use this file as part of a free software
    00020 // library without restriction.  Specifically, if other files instantiate
    00021 // templates or use macros or inline functions from this file, or you compile
    00022 // this file and link it with other files to produce an executable, this
    00023 // file does not by itself cause the resulting executable to be covered by
    00024 // the GNU General Public License.  This exception does not however
    00025 // invalidate any other reasons why the executable file might be covered by
    00026 // the GNU General Public License.
    00027
    00028 // Written by Benjamin Kosnik <bkoz@cygnus.com>
    00029
    00030 #include <locale>
    00031
    00032 namespace std
    00033 {
    00034 #ifdef _GLIBCPP_USE___ENC_TRAITS
    00035   // Definitions for static const data members of __enc_traits.
    00036   const int __enc_traits::_S_max_size;
    00037 #endif
    00038
    00039   codecvt<char, char, mbstate_t>::
    00040   codecvt(size_t __refs)
    00041   : __codecvt_abstract_base<char, char, mbstate_t>(__refs)
    00042   { }
    00043
    00044   codecvt<char, char, mbstate_t>::
    00045   ~codecvt() { }
    00046
    00047   codecvt_base::result
    00048   codecvt<char, char, mbstate_t>::
    00049   do_out(state_type&, const intern_type* __from,
    00050      const intern_type* __from_end, const intern_type*& __from_next,
    00051      extern_type* __to, extern_type* __to_end,
    00052      extern_type*& __to_next) const
    00053   {
    00054     size_t __len = min(__from_end - __from, __to_end - __to);
    00055     memcpy(__to, __from, __len);
    00056     __from_next = __from;
    00057     __to_next = __to;
    00058     return noconv;
    00059   }
    00060
    00061   codecvt_base::result
    00062   codecvt<char, char, mbstate_t>::
    00063   do_unshift(state_type&, extern_type* __to,
    00064              extern_type*, extern_type*& __to_next) const
    00065   {
    00066     __to_next = __to;
    00067     return noconv;
    00068   }
    00069
    00070   codecvt_base::result
    00071   codecvt<char, char, mbstate_t>::
    00072   do_in(state_type&, const extern_type* __from,
    00073     const extern_type* __from_end, const extern_type*& __from_next,
    00074     intern_type* __to, intern_type* __to_end,
    00075     intern_type*& __to_next) const
    00076   {
    00077     size_t __len = min(__from_end - __from, __to_end - __to);
    00078     memcpy(__to, __from, __len);
    00079     __from_next = __from;
    00080     __to_next = __to;
    00081     return noconv;
    00082   }
    00083
    00084   int
    00085   codecvt<char, char, mbstate_t>::
    00086   do_encoding() const throw()
    00087   { return 1; }
    00088
    00089   bool
    00090   codecvt<char, char, mbstate_t>::
    00091   do_always_noconv() const throw()
    00092   { return true; }
    00093
    00094   int
    00095   codecvt<char, char, mbstate_t>::
    00096   do_length (const state_type&, const extern_type* __from,
    00097          const extern_type* __end, size_t __max) const
    00098   { return min(__max, static_cast<size_t>(__end - __from)); }
    00099
    00100   int
    00101   codecvt<char, char, mbstate_t>::
    00102   do_max_length() const throw()
    00103   { return 1; }
    00104
    00105 #ifdef _GLIBCPP_USE_WCHAR_T
    00106   // codecvt<wchar_t, char, mbstate_t> required specialization
    00107   codecvt<wchar_t, char, mbstate_t>::
    00108   codecvt(size_t __refs)
    00109   : __codecvt_abstract_base<wchar_t, char, mbstate_t>(__refs) { }
    00110
    00111   codecvt<wchar_t, char, mbstate_t>::
    00112   ~codecvt() { }
    00113
    00114   codecvt_base::result
    00115   codecvt<wchar_t, char, mbstate_t>::
    00116   do_out(state_type& __state, const intern_type* __from,
    00117      const intern_type* __from_end, const intern_type*& __from_next,
    00118      extern_type* __to, extern_type* __to_end,
    00119      extern_type*& __to_next) const
    00120   {
    00121     result __ret = error;
    00122     size_t __len = min(__from_end - __from, __to_end - __to);
    00123     size_t __conv = wcsrtombs(__to, &__from, __len, &__state);
    00124
    00125     if (__conv == __len)
    00126       {
    00127     __from_next = __from;
    00128     __to_next = __to + __conv;
    00129     __ret = ok;
    00130       }
    00131     else if (__conv > 0 && __conv < __len)
    00132       {
    00133     __from_next = __from;
    00134     __to_next = __to + __conv;
    00135     __ret = partial;
    00136       }
    00137     else
    00138       __ret = error;
    00139
    00140     return __ret;
    00141   }
    00142
    00143   codecvt_base::result
    00144   codecvt<wchar_t, char, mbstate_t>::
    00145   do_unshift(state_type&, extern_type* __to,
    00146          extern_type*, extern_type*& __to_next) const
    00147   {
    00148     __to_next = __to;
    00149     return noconv;
    00150   }
    00151
    00152   codecvt_base::result
    00153   codecvt<wchar_t, char, mbstate_t>::
    00154   do_in(state_type& __state, const extern_type* __from,
    00155     const extern_type* __from_end, const extern_type*& __from_next,
    00156     intern_type* __to, intern_type* __to_end,
    00157     intern_type*& __to_next) const
    00158   {
    00159     result __ret = error;
    00160     size_t __len = min(__from_end - __from, __to_end - __to);
    00161     size_t __conv = mbsrtowcs(__to, &__from, __len, &__state);
    00162
    00163     if (__conv == __len)
    00164       {
    00165     __from_next = __from;
    00166     __to_next = __to + __conv;
    00167     __ret = ok;
    00168       }
    00169     else if (__conv > 0 && __conv < __len)
    00170       {
    00171     __from_next = __from;
    00172     __to_next = __to + __conv;
    00173     __ret = partial;
    00174       }
    00175     else
    00176       __ret = error;
    00177
    00178     return __ret;
    00179   }
    00180
    00181   int
    00182   codecvt<wchar_t, char, mbstate_t>::
    00183   do_encoding() const throw()
    00184   { return sizeof(wchar_t); }
    00185
    00186   bool
    00187   codecvt<wchar_t, char, mbstate_t>::
    00188   do_always_noconv() const throw()
    00189   { return false; }
    00190
    00191   int
    00192   codecvt<wchar_t, char, mbstate_t>::
    00193   do_length(const state_type&, const extern_type* __from,
    00194         const extern_type* __end, size_t __max) const
    00195   { return min(__max, static_cast<size_t>(__end - __from)); }
    00196
    00197   int
    00198   codecvt<wchar_t, char, mbstate_t>::
    00199   do_max_length() const throw()
    00200   { return 1; }
    00201 #endif //  _GLIBCPP_USE_WCHAR_T
    00202 } // namespace std