STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
codecvt_specializations.h File Reference
#include <bits/c++config.h>
#include <locale>
#include <iconv.h>

Go to the source code of this file.

Functions

namespace __gnu_cxx _GLIBCXX_VISIBILITY (default)
 

Detailed Description

This file is a GNU extension to the Standard C++ Library.

Function Documentation

namespace __gnu_cxx _GLIBCXX_VISIBILITY ( default  )

Extension to use iconv for dealing with character encodings.

encoding_char_traits

codecvt<InternT, _ExternT, encoding_state> specialization.

43 {
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 
47  // This includes conversions and comparisons between various character
48  // sets. This object encapsulates data that may need to be shared between
49  // char_traits, codecvt and ctype.
50  class encoding_state
51  {
52  public:
53  // Types:
54  // NB: A conversion descriptor subsumes and enhances the
55  // functionality of a simple state type such as mbstate_t.
56  typedef iconv_t descriptor_type;
57 
58  protected:
59  // Name of internal character set encoding.
60  std::string _M_int_enc;
61 
62  // Name of external character set encoding.
63  std::string _M_ext_enc;
64 
65  // Conversion descriptor between external encoding to internal encoding.
66  descriptor_type _M_in_desc;
67 
68  // Conversion descriptor between internal encoding to external encoding.
69  descriptor_type _M_out_desc;
70 
71  // The byte-order marker for the external encoding, if necessary.
72  int _M_ext_bom;
73 
74  // The byte-order marker for the internal encoding, if necessary.
75  int _M_int_bom;
76 
77  // Number of external bytes needed to construct one complete
78  // character in the internal encoding.
79  // NB: -1 indicates variable, or stateful, encodings.
80  int _M_bytes;
81 
82  public:
83  explicit
84  encoding_state()
85  : _M_in_desc(0), _M_out_desc(0), _M_ext_bom(0), _M_int_bom(0), _M_bytes(0)
86  { }
87 
88  explicit
89  encoding_state(const char* __int, const char* __ext,
90  int __ibom = 0, int __ebom = 0, int __bytes = 1)
91  : _M_int_enc(__int), _M_ext_enc(__ext), _M_in_desc(0), _M_out_desc(0),
92  _M_ext_bom(__ebom), _M_int_bom(__ibom), _M_bytes(__bytes)
93  { init(); }
94 
95  // 21.1.2 traits typedefs
96  // p4
97  // typedef STATE_T state_type
98  // requires: state_type shall meet the requirements of
99  // CopyConstructible types (20.1.3)
100  // NB: This does not preserve the actual state of the conversion
101  // descriptor member, but it does duplicate the encoding
102  // information.
103  encoding_state(const encoding_state& __obj) : _M_in_desc(0), _M_out_desc(0)
104  { construct(__obj); }
105 
106  // Need assignment operator as well.
107  encoding_state&
108  operator=(const encoding_state& __obj)
109  {
110  construct(__obj);
111  return *this;
112  }
113 
114  ~encoding_state()
115  { destroy(); }
116 
117  bool
118  good() const throw()
119  {
120  const descriptor_type __err = (iconv_t)(-1);
121  bool __test = _M_in_desc && _M_in_desc != __err;
122  __test &= _M_out_desc && _M_out_desc != __err;
123  return __test;
124  }
125 
126  int
127  character_ratio() const
128  { return _M_bytes; }
129 
130  const std::string
131  internal_encoding() const
132  { return _M_int_enc; }
133 
134  int
135  internal_bom() const
136  { return _M_int_bom; }
137 
138  const std::string
139  external_encoding() const
140  { return _M_ext_enc; }
141 
142  int
143  external_bom() const
144  { return _M_ext_bom; }
145 
146  const descriptor_type&
147  in_descriptor() const
148  { return _M_in_desc; }
149 
150  const descriptor_type&
151  out_descriptor() const
152  { return _M_out_desc; }
153 
154  protected:
155  void
156  init()
157  {
158  const descriptor_type __err = (iconv_t)(-1);
159  const bool __have_encodings = _M_int_enc.size() && _M_ext_enc.size();
160  if (!_M_in_desc && __have_encodings)
161  {
162  _M_in_desc = iconv_open(_M_int_enc.c_str(), _M_ext_enc.c_str());
163  if (_M_in_desc == __err)
164  std::__throw_runtime_error(__N("encoding_state::_M_init "
165  "creating iconv input descriptor failed"));
166  }
167  if (!_M_out_desc && __have_encodings)
168  {
169  _M_out_desc = iconv_open(_M_ext_enc.c_str(), _M_int_enc.c_str());
170  if (_M_out_desc == __err)
171  std::__throw_runtime_error(__N("encoding_state::_M_init "
172  "creating iconv output descriptor failed"));
173  }
174  }
175 
176  void
177  construct(const encoding_state& __obj)
178  {
179  destroy();
180  _M_int_enc = __obj._M_int_enc;
181  _M_ext_enc = __obj._M_ext_enc;
182  _M_ext_bom = __obj._M_ext_bom;
183  _M_int_bom = __obj._M_int_bom;
184  _M_bytes = __obj._M_bytes;
185  init();
186  }
187 
188  void
189  destroy() throw()
190  {
191  const descriptor_type __err = (iconv_t)(-1);
192  if (_M_in_desc && _M_in_desc != __err)
193  {
194  iconv_close(_M_in_desc);
195  _M_in_desc = 0;
196  }
197  if (_M_out_desc && _M_out_desc != __err)
198  {
199  iconv_close(_M_out_desc);
200  _M_out_desc = 0;
201  }
202  }
203  };
204 
206  // Custom traits type with encoding_state for the state type, and the
207  // associated fpos<encoding_state> for the position type, all other
208  // bits equivalent to the required char_traits instantiations.
209  template<typename _CharT>
210  struct encoding_char_traits : public std::char_traits<_CharT>
211  {
212  typedef encoding_state state_type;
213  typedef typename std::fpos<state_type> pos_type;
214  };
215 
216 _GLIBCXX_END_NAMESPACE_VERSION
217 } // namespace
basic_string< char > string
Definition: string:1153