Click on the banner to return to the class reference home page.

codecvt


...codecvt_base codecvt... ...locale::facet

Summary

Code conversion facet.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <locale>
class codecvt_base;

template <class internT, class externT, class stateT>
class codecvt;

Description

The codecvt<internT,externT,stateT> template provides code conversion facilities. Default implementations of codecvt<char,wchar_t,mbstate_t> and codecvt<wchar_t,char,mbstate_t> use ctype<wchar_t>::widen and ctype<wchar_t>::narrow respectively. The default implementation of codecvt<wchar_t,wchar_t,mbstate_t> simply uses memcpy (no particular conversion applied).

Interface

class codecvt_base {
public:
  enum result { ok, partial, error, noconv };
};

template <class internT, class externT, class stateT>
class codecvt : public locale::facet, public codecvt_base {
public:
  typedef internT  intern_type;
  typedef externT  extern_type;
  typedef stateT   state_type;

  explicit codecvt(size_t = 0)
  result out(stateT&, const internT*, 
            const internT*, const internT*&,
            externT*, externT*, externT*&) const;
  result in(stateT&, const externT*, 
            const externT*, const externT*&,
            internT*, internT*, internT*&) const;

  bool always_noconv() const throw();
  int length(const stateT&, const internT*, const internT*,
             size_t) const;

  int max_length() const throw();
  int encoding() const throw();
  static locale::id id;

protected:
  ~codecvt();  // virtual
  virtual result do_out(stateT&,
                        const internT*, 
                        const internT*, 
                        const internT*&,
                        externT*, externT*,
                        externT*&) const;
  virtual result do_in(stateT&,
                       const externT*, 
                       const externT*, 
                       const externT*&,
                       internT*, internT*,
                       internT*&) const;

  virtual bool do_always_noconv() const throw();
  virtual int do_length(const stateT&, const internT*, 
                        const internT*,
                        size_t) const;

  virtual int do_max_length() const throw();
  virtual int do_encoding() const throw();
};

Types

intern_type
extern_type
state_type

Constructors and Destructors

explicit codecvt(size_t refs = 0)
~codecvt();  // virtual and protected

Facet ID

static locale::id id;

Public Member Functions

The public members of the codecvt facet provide an interface to protected members. Each public member xxx has a corresponding virtual protected member do_xxx. All work is delegated to these protected members. For instance, the long version of the public length function simply calls its protected cousin do_length.

bool 
always_noconv() const 
  throw();

int
encoding() const
  throw();

result 
in(stateT& state, const externT* from, 
   const externT* from_end, const externT*& from_next,
   internT* to, internT* to_limit, internT*& to_next) const;

int 
length(const stateT&, const internT* from, 
       const internT* end,
       size_t max) const;

int 
max_length() const 
  throw();

result 
out(stateT& state, const internT* from, 
    const internT* from_end, const internT*& from_next,
    externT* to, externT* to_limit, externT*& to_next) const;

Protected Member Functions

virtual bool 
do_always_noconv() const 
  throw();
virtual int 
do_encoding() const 
  throw();
virtual result 
do_in(stateT& state,
      const externT* from, 
      const externT* from_end, 
      const externT*& from_next,
      internT* to, internT* to_limit,
      internT*& to_next) const;

virtual result 
do_out(stateT& state,
       const internT* from, 
       const internT* from_end, 
       const internT*& from_next,
       externT* to, externT* to_limit,
       externT*& to_next) const;
Return Value
Meaning
ok
completed the conversion
partial
not all source characters converted
error
encountered a from_type character it could not convert
noconv
no conversion was needed





virtual int 
do_length(const stateT&, const internT* from, 
          const internT* end,
          size_t max) const;
virtual int 
do_max_length() const throw();
virtual result 
do_out(stateT& state,
       const internT* from, 
       const internT* from_end, 
       const internT*& from_next,
       externT* to, externT* to_limit,
       externT*& to_next) const;

Example

//
// codecvt.cpp
//
#include <sstream>
#include "codecvte.h"

int main ()
{
  using namespace std;

  mbstate_t state;

  // A string of ISO characters and buffers to hold 
  // conversions
  string ins("\xfc \xcc \xcd \x61 \xe1 \xd9 \xc6 \xf5");
  string ins2(ins.size(),'.');
  string outs(ins.size(),'.');

  // Print initial contents of buffers
  cout << "Before:\n" << ins << endl;
  cout << ins2 << endl;
  cout << outs << endl << endl;

  // Initialize buffers
  string::iterator in_it = ins.begin();
  string::iterator out_it = outs.begin();

  // Create a user defined codecvt fact
  // This facet converts from ISO Latin 
  // Alphabet No. 1 (ISO 8859-1) to 
  // U.S. ASCII code page 437
  // This facet replaces the default for
  // codecvt<char,char,mbstate_t>
  locale loc(locale(),new ex_codecvt);

  // Now get the facet from the locale 
  const codecvt<char,char,mbstate_t>& cdcvt = 
#ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
    use_facet<codecvt<char,char,mbstate_t> >(loc);
#else
    use_facet(loc,(codecvt<char,char,mbstate_t>*)0);
#endif

  // convert the buffer
  cdcvt.in(state,ins.begin(),ins.end(),in_it,
           outs.begin(),outs.end(),out_it);
     
  cout << "After in:\n" << ins << endl;
  cout << ins2 << endl;
  cout << outs << endl << endl;

  // Lastly, convert back to the original codeset
  in_it = ins.begin();
  out_it = outs.begin();
  cdcvt.out(state, outs.begin(),outs.end(),out_it,
            ins2.begin(),ins2.end(),in_it);
     
  cout << "After out:\n" << ins << endl;
  cout << ins2 << endl;
  cout << outs << endl;

  return 0;
}

See Also

locale, facets, codecvt_byname


©Copyright 1996, Rogue Wave Software, Inc.