1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Facets</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="localization.html" title="Chapter 8. Localization" /><link rel="prev" href="localization.html" title="Chapter 8. Localization" /><link rel="next" href="containers.html" title="Chapter 9. Containers" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Facets</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="localization.html">Prev</a> </td><th width="60%" align="center">Chapter 8.
6 </th><td width="20%" align="right"> <a accesskey="n" href="containers.html">Next</a></td></tr></table><hr /></div><div class="section" title="Facets"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="std.localization.facet"></a>Facets</h2></div></div></div><div class="section" title="ctype"><div class="titlepage"><div><div><h3 class="title"><a id="std.localization.facet.ctype"></a>ctype</h3></div></div></div><div class="section" title="Implementation"><div class="titlepage"><div><div><h4 class="title"><a id="facet.ctype.impl"></a>Implementation</h4></div></div></div><div class="section" title="Specializations"><div class="titlepage"><div><div><h5 class="title"><a id="id503472"></a>Specializations</h5></div></div></div><p>
7 For the required specialization codecvt<wchar_t, char, mbstate_t> ,
8 conversions are made between the internal character set (always UCS4
9 on GNU/Linux) and whatever the currently selected locale for the
10 LC_CTYPE category implements.
12 The two required specializations are implemented as follows:
18 This is simple specialization. Implementing this was a piece of cake.
24 This specialization, by specifying all the template parameters, pretty
25 much ties the hands of implementors. As such, the implementation is
26 straightforward, involving mcsrtombs for the conversions between char
27 to wchar_t and wcsrtombs for conversions between wchar_t and char.
29 Neither of these two required specializations deals with Unicode
31 </p></div></div><div class="section" title="Future"><div class="titlepage"><div><div><h4 class="title"><a id="facet.ctype.future"></a>Future</h4></div></div></div><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
32 How to deal with the global locale issue?
33 </p></li><li class="listitem"><p>
34 How to deal with different types than char, wchar_t? </p></li><li class="listitem"><p>
35 Overlap between codecvt/ctype: narrow/widen
36 </p></li><li class="listitem"><p>
37 Mask typedef in codecvt_base, argument types in codecvt. what
38 is know about this type?
39 </p></li><li class="listitem"><p>
40 Why mask* argument in codecvt?
41 </p></li><li class="listitem"><p>
42 Can this be made (more) generic? is there a simple way to
43 straighten out the configure-time mess that is a by-product of
45 </p></li><li class="listitem"><p>
46 Get the ctype<wchar_t>::mask stuff under control. Need to
47 make some kind of static table, and not do lookup every time
48 somebody hits the do_is... functions. Too bad we can't just
49 redefine mask for ctype<wchar_t>
50 </p></li><li class="listitem"><p>
51 Rename abstract base class. See if just smash-overriding is a
52 better approach. Clarify, add sanity to naming.
53 </p></li></ul></div></div><div class="bibliography" title="Bibliography"><div class="titlepage"><div><div><h4 class="title"><a id="facet.ctype.biblio"></a>Bibliography</h4></div></div></div><div class="biblioentry" title="The GNU C Library"><a id="id379548"></a><p><span class="title"><i>
55 </i>. </span><span class="author"><span class="firstname">Roland</span> <span class="surname">McGrath</span>. </span><span class="author"><span class="firstname">Ulrich</span> <span class="surname">Drepper</span>. </span><span class="copyright">Copyright © 2007 FSF. </span><span class="pagenums">Chapters 6 Character Set Handling and 7 Locales and Internationalization. </span></p></div><div class="biblioentry" title="Correspondence"><a id="id449311"></a><p><span class="title"><i>
57 </i>. </span><span class="author"><span class="firstname">Ulrich</span> <span class="surname">Drepper</span>. </span><span class="copyright">Copyright © 2002 . </span></p></div><div class="biblioentry" title="ISO/IEC 14882:1998 Programming languages - C++"><a id="id416640"></a><p><span class="title"><i>
58 ISO/IEC 14882:1998 Programming languages - C++
59 </i>. </span><span class="copyright">Copyright © 1998 ISO. </span></p></div><div class="biblioentry" title="ISO/IEC 9899:1999 Programming languages - C"><a id="id416658"></a><p><span class="title"><i>
60 ISO/IEC 9899:1999 Programming languages - C
61 </i>. </span><span class="copyright">Copyright © 1999 ISO. </span></p></div><div class="biblioentry"><a id="id497267"></a><p><span class="biblioid">
62 <a class="ulink" href="http://www.unix.org/version3/ieee_std.html" target="_top">
63 <em class="citetitle">
64 The Open Group Base Specifications, Issue 6 (IEEE Std. 1003.1-2004)
67 . </span><span class="copyright">Copyright © 1999
68 The Open Group/The Institute of Electrical and Electronics Engineers, Inc.. </span></p></div><div class="biblioentry" title="The C++ Programming Language, Special Edition"><a id="id381518"></a><p><span class="title"><i>
69 The C++ Programming Language, Special Edition
70 </i>. </span><span class="author"><span class="firstname">Bjarne</span> <span class="surname">Stroustrup</span>. </span><span class="copyright">Copyright © 2000 Addison Wesley, Inc.. </span><span class="pagenums">Appendix D. </span><span class="publisher"><span class="publishername">
72 . </span></span></p></div><div class="biblioentry" title="Standard C++ IOStreams and Locales"><a id="id503670"></a><p><span class="title"><i>
73 Standard C++ IOStreams and Locales
74 </i>. </span><span class="subtitle">
75 Advanced Programmer's Guide and Reference
76 . </span><span class="author"><span class="firstname">Angelika</span> <span class="surname">Langer</span>. </span><span class="author"><span class="firstname">Klaus</span> <span class="surname">Kreft</span>. </span><span class="copyright">Copyright © 2000 Addison Wesley Longman, Inc.. </span><span class="publisher"><span class="publishername">
77 Addison Wesley Longman
78 . </span></span></p></div></div></div><div class="section" title="codecvt"><div class="titlepage"><div><div><h3 class="title"><a id="std.localization.facet.codecvt"></a>codecvt</h3></div></div></div><p>
79 The standard class codecvt attempts to address conversions between
80 different character encoding schemes. In particular, the standard
81 attempts to detail conversions between the implementation-defined wide
82 characters (hereafter referred to as wchar_t) and the standard type
83 char that is so beloved in classic <span class="quote">“<span class="quote">C</span>”</span> (which can now be
84 referred to as narrow characters.) This document attempts to describe
85 how the GNU libstdc++ implementation deals with the conversion between
86 wide and narrow characters, and also presents a framework for dealing
87 with the huge number of other encodings that iconv can convert,
88 including Unicode and UTF8. Design issues and requirements are
89 addressed, and examples of correct usage for both the required
90 specializations for wide and narrow characters and the
91 implementation-provided extended functionality are given.
92 </p><div class="section" title="Requirements"><div class="titlepage"><div><div><h4 class="title"><a id="facet.codecvt.req"></a>Requirements</h4></div></div></div><p>
93 Around page 425 of the C++ Standard, this charming heading comes into view:
94 </p><div class="blockquote"><blockquote class="blockquote"><p>
95 22.2.1.5 - Template class codecvt
96 </p></blockquote></div><p>
97 The text around the codecvt definition gives some clues:
98 </p><div class="blockquote"><blockquote class="blockquote"><p>
99 <span class="emphasis"><em>
100 -1- The class codecvt<internT,externT,stateT> is for use when
101 converting from one codeset to another, such as from wide characters
102 to multibyte characters, between wide character encodings such as
105 </p></blockquote></div><p>
106 Hmm. So, in some unspecified way, Unicode encodings and
107 translations between other character sets should be handled by this
109 </p><div class="blockquote"><blockquote class="blockquote"><p>
110 <span class="emphasis"><em>
111 -2- The stateT argument selects the pair of codesets being mapped between.
113 </p></blockquote></div><p>
114 Ah ha! Another clue...
115 </p><div class="blockquote"><blockquote class="blockquote"><p>
116 <span class="emphasis"><em>
117 -3- The instantiations required in the Table ??
118 (lib.locale.category), namely codecvt<wchar_t,char,mbstate_t> and
119 codecvt<char,char,mbstate_t>, convert the implementation-defined
120 native character set. codecvt<char,char,mbstate_t> implements a
121 degenerate conversion; it does not convert at
122 all. codecvt<wchar_t,char,mbstate_t> converts between the native
123 character sets for tiny and wide characters. Instantiations on
124 mbstate_t perform conversion between encodings known to the library
125 implementor. Other encodings can be converted by specializing on a
126 user-defined stateT type. The stateT object can contain any state that
127 is useful to communicate to or from the specialized do_convert member.
129 </p></blockquote></div><p>
130 At this point, a couple points become clear:
132 One: The standard clearly implies that attempts to add non-required
133 (yet useful and widely used) conversions need to do so through the
134 third template parameter, stateT.</p><p>
135 Two: The required conversions, by specifying mbstate_t as the third
136 template parameter, imply an implementation strategy that is mostly
137 (or wholly) based on the underlying C library, and the functions
138 mcsrtombs and wcsrtombs in particular.</p></div><div class="section" title="Design"><div class="titlepage"><div><div><h4 class="title"><a id="facet.codecvt.design"></a>Design</h4></div></div></div><div class="section" title="wchar_t Size"><div class="titlepage"><div><div><h5 class="title"><a id="codecvt.design.wchar_t_size"></a><span class="type">wchar_t</span> Size</h5></div></div></div><p>
139 The simple implementation detail of wchar_t's size seems to
140 repeatedly confound people. Many systems use a two byte,
141 unsigned integral type to represent wide characters, and use an
142 internal encoding of Unicode or UCS2. (See AIX, Microsoft NT,
143 Java, others.) Other systems, use a four byte, unsigned integral
144 type to represent wide characters, and use an internal encoding
145 of UCS4. (GNU/Linux systems using glibc, in particular.) The C
146 programming language (and thus C++) does not specify a specific
147 size for the type wchar_t.
149 Thus, portable C++ code cannot assume a byte size (or endianness) either.
150 </p></div><div class="section" title="Support for Unicode"><div class="titlepage"><div><div><h5 class="title"><a id="codecvt.design.unicode"></a>Support for Unicode</h5></div></div></div><p>
151 Probably the most frequently asked question about code conversion
152 is: "So dudes, what's the deal with Unicode strings?"
153 The dude part is optional, but apparently the usefulness of
154 Unicode strings is pretty widely appreciated. Sadly, this specific
155 encoding (And other useful encodings like UTF8, UCS4, ISO 8859-10,
156 etc etc etc) are not mentioned in the C++ standard.
158 A couple of comments:
160 The thought that all one needs to convert between two arbitrary
161 codesets is two types and some kind of state argument is
162 unfortunate. In particular, encodings may be stateless. The naming
163 of the third parameter as stateT is unfortunate, as what is really
164 needed is some kind of generalized type that accounts for the
165 issues that abstract encodings will need. The minimum information
166 that is required includes:
167 </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
168 Identifiers for each of the codesets involved in the
169 conversion. For example, using the iconv family of functions
170 from the Single Unix Specification (what used to be called
171 X/Open) hosted on the GNU/Linux operating system allows
172 bi-directional mapping between far more than the following
173 tantalizing possibilities:
175 (An edited list taken from <code class="code">`iconv --list`</code> on a
176 Red Hat 6.2/Intel system:
177 </p><div class="blockquote"><blockquote class="blockquote"><pre class="programlisting">
178 8859_1, 8859_9, 10646-1:1993, 10646-1:1993/UCS4, ARABIC, ARABIC7,
179 ASCII, EUC-CN, EUC-JP, EUC-KR, EUC-TW, GREEK-CCIcode, GREEK, GREEK7-OLD,
180 GREEK7, GREEK8, HEBREW, ISO-8859-1, ISO-8859-2, ISO-8859-3,
181 ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8,
182 ISO-8859-9, ISO-8859-10, ISO-8859-11, ISO-8859-13, ISO-8859-14,
183 ISO-8859-15, ISO-10646, ISO-10646/UCS2, ISO-10646/UCS4,
184 ISO-10646/UTF-8, ISO-10646/UTF8, SHIFT-JIS, SHIFT_JIS, UCS-2, UCS-4,
185 UCS2, UCS4, UNICODE, UNICODEBIG, UNICODELIcodeLE, US-ASCII, US, UTF-8,
186 UTF-16, UTF8, UTF16).
187 </pre></blockquote></div><p>
188 For iconv-based implementations, string literals for each of the
189 encodings (i.e. "UCS-2" and "UTF-8") are necessary,
191 non-iconv implementations a table of enumerated values or some other
192 mechanism may be required.
193 </p></li><li class="listitem"><p>
194 Maximum length of the identifying string literal.
195 </p></li><li class="listitem"><p>
196 Some encodings require explicit endian-ness. As such, some kind
197 of endian marker or other byte-order marker will be necessary. See
198 "Footnotes for C/C++ developers" in Haible for more information on
199 UCS-2/Unicode endian issues. (Summary: big endian seems most likely,
200 however implementations, most notably Microsoft, vary.)
201 </p></li><li class="listitem"><p>
202 Types representing the conversion state, for conversions involving
203 the machinery in the "C" library, or the conversion descriptor, for
204 conversions using iconv (such as the type iconv_t.) Note that the
205 conversion descriptor encodes more information than a simple encoding
207 </p></li><li class="listitem"><p>
208 Conversion descriptors for both directions of encoding. (i.e., both
209 UCS-2 to UTF-8 and UTF-8 to UCS-2.)
210 </p></li><li class="listitem"><p>
211 Something to indicate if the conversion requested if valid.
212 </p></li><li class="listitem"><p>
213 Something to represent if the conversion descriptors are valid.
214 </p></li><li class="listitem"><p>
215 Some way to enforce strict type checking on the internal and
216 external types. As part of this, the size of the internal and
217 external types will need to be known.
218 </p></li></ul></div></div><div class="section" title="Other Issues"><div class="titlepage"><div><div><h5 class="title"><a id="codecvt.design.issues"></a>Other Issues</h5></div></div></div><p>
219 In addition, multi-threaded and multi-locale environments also impact
220 the design and requirements for code conversions. In particular, they
221 affect the required specialization codecvt<wchar_t, char, mbstate_t>
222 when implemented using standard "C" functions.
224 Three problems arise, one big, one of medium importance, and one small.
226 First, the small: mcsrtombs and wcsrtombs may not be multithread-safe
227 on all systems required by the GNU tools. For GNU/Linux and glibc,
228 this is not an issue.
230 Of medium concern, in the grand scope of things, is that the functions
231 used to implement this specialization work on null-terminated
232 strings. Buffers, especially file buffers, may not be null-terminated,
233 thus giving conversions that end prematurely or are otherwise
236 The last, and fundamental problem, is the assumption of a global
237 locale for all the "C" functions referenced above. For something like
238 C++ iostreams (where codecvt is explicitly used) the notion of
239 multiple locales is fundamental. In practice, most users may not run
240 into this limitation. However, as a quality of implementation issue,
241 the GNU C++ library would like to offer a solution that allows
242 multiple locales and or simultaneous usage with computationally
243 correct results. In short, libstdc++ is trying to offer, as an
244 option, a high-quality implementation, damn the additional complexity!
246 For the required specialization codecvt<wchar_t, char, mbstate_t> ,
247 conversions are made between the internal character set (always UCS4
248 on GNU/Linux) and whatever the currently selected locale for the
249 LC_CTYPE category implements.
250 </p></div></div><div class="section" title="Implementation"><div class="titlepage"><div><div><h4 class="title"><a id="facet.codecvt.impl"></a>Implementation</h4></div></div></div><p>
251 The two required specializations are implemented as follows:
254 codecvt<char, char, mbstate_t>
257 This is a degenerate (i.e., does nothing) specialization. Implementing
258 this was a piece of cake.
261 codecvt<char, wchar_t, mbstate_t>
264 This specialization, by specifying all the template parameters, pretty
265 much ties the hands of implementors. As such, the implementation is
266 straightforward, involving mcsrtombs for the conversions between char
267 to wchar_t and wcsrtombs for conversions between wchar_t and char.
269 Neither of these two required specializations deals with Unicode
270 characters. As such, libstdc++ implements a partial specialization
271 of the codecvt class with and iconv wrapper class, encoding_state as the
272 third template parameter.
274 This implementation should be standards conformant. First of all, the
275 standard explicitly points out that instantiations on the third
276 template parameter, stateT, are the proper way to implement
277 non-required conversions. Second of all, the standard says (in Chapter
278 17) that partial specializations of required classes are a-ok. Third
279 of all, the requirements for the stateT type elsewhere in the standard
280 (see 21.1.2 traits typedefs) only indicate that this type be copy
283 As such, the type encoding_state is defined as a non-templatized, POD
284 type to be used as the third type of a codecvt instantiation. This
285 type is just a wrapper class for iconv, and provides an easy interface
286 to iconv functionality.
288 There are two constructors for encoding_state:
291 encoding_state() : __in_desc(0), __out_desc(0)
294 This default constructor sets the internal encoding to some default
295 (currently UCS4) and the external encoding to whatever is returned by
296 nl_langinfo(CODESET).
299 encoding_state(const char* __int, const char* __ext)
302 This constructor takes as parameters string literals that indicate the
303 desired internal and external encoding. There are no defaults for
306 One of the issues with iconv is that the string literals identifying
307 conversions are not standardized. Because of this, the thought of
308 mandating and or enforcing some set of pre-determined valid
309 identifiers seems iffy: thus, a more practical (and non-migraine
310 inducing) strategy was implemented: end-users can specify any string
311 (subject to a pre-determined length qualifier, currently 32 bytes) for
312 encodings. It is up to the user to make sure that these strings are
313 valid on the target system.
320 Strangely enough, this member function attempts to open conversion
321 descriptors for a given encoding_state object. If the conversion
322 descriptors are not valid, the conversion descriptors returned will
323 not be valid and the resulting calls to the codecvt conversion
324 functions will return error.
331 Provides a way to see if the given encoding_state object has been
332 properly initialized. If the string literals describing the desired
333 internal and external encoding are not valid, initialization will
334 fail, and this will return false. If the internal and external
335 encodings are valid, but iconv_open could not allocate conversion
336 descriptors, this will also return false. Otherwise, the object is
337 ready to convert and will return true.
340 encoding_state(const encoding_state&)
343 As iconv allocates memory and sets up conversion descriptors, the copy
344 constructor can only copy the member data pertaining to the internal
345 and external code conversions, and not the conversion descriptors
348 Definitions for all the required codecvt member functions are provided
349 for this specialization, and usage of codecvt<internal character type,
350 external character type, encoding_state> is consistent with other
352 </p></div><div class="section" title="Use"><div class="titlepage"><div><div><h4 class="title"><a id="facet.codecvt.use"></a>Use</h4></div></div></div><p>A conversions involving string literal.</p><pre class="programlisting">
353 typedef codecvt_base::result result;
354 typedef unsigned short unicode_t;
355 typedef unicode_t int_type;
356 typedef char ext_type;
357 typedef encoding_state state_type;
358 typedef codecvt<int_type, ext_type, state_type> unicode_codecvt;
360 const ext_type* e_lit = "black pearl jasmine tea";
361 int size = strlen(e_lit);
362 int_type i_lit_base[24] =
363 { 25088, 27648, 24832, 25344, 27392, 8192, 28672, 25856, 24832, 29184,
364 27648, 8192, 27136, 24832, 29440, 27904, 26880, 28160, 25856, 8192, 29696,
367 const int_type* i_lit = i_lit_base;
368 const ext_type* efrom_next;
369 const int_type* ifrom_next;
370 ext_type* e_arr = new ext_type[size + 1];
372 int_type* i_arr = new int_type[size + 1];
375 // construct a locale object with the specialized facet.
376 locale loc(locale::classic(), new unicode_codecvt);
377 // sanity check the constructed locale has the specialized facet.
378 VERIFY( has_facet<unicode_codecvt>(loc) );
379 const unicode_codecvt& cvt = use_facet<unicode_codecvt>(loc);
380 // convert between const char* and unicode strings
381 unicode_codecvt::state_type state01("UNICODE", "ISO_8859-1");
382 initialize_state(state01);
383 result r1 = cvt.in(state01, e_lit, e_lit + size, efrom_next,
384 i_arr, i_arr + size, ito_next);
385 VERIFY( r1 == codecvt_base::ok );
386 VERIFY( !int_traits::compare(i_arr, i_lit, size) );
387 VERIFY( efrom_next == e_lit + size );
388 VERIFY( ito_next == i_arr + size );
389 </pre></div><div class="section" title="Future"><div class="titlepage"><div><div><h4 class="title"><a id="facet.codecvt.future"></a>Future</h4></div></div></div><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
390 a. things that are sketchy, or remain unimplemented:
391 do_encoding, max_length and length member functions
392 are only weakly implemented. I have no idea how to do
393 this correctly, and in a generic manner. Nathan?
394 </p></li><li class="listitem"><p>
395 b. conversions involving std::string
396 </p><div class="itemizedlist"><ul class="itemizedlist" type="circle"><li class="listitem"><p>
397 how should operators != and == work for string of
398 different/same encoding?
399 </p></li><li class="listitem"><p>
400 what is equal? A byte by byte comparison or an
401 encoding then byte comparison?
402 </p></li><li class="listitem"><p>
403 conversions between narrow, wide, and unicode strings
404 </p></li></ul></div></li><li class="listitem"><p>
405 c. conversions involving std::filebuf and std::ostream
406 </p><div class="itemizedlist"><ul class="itemizedlist" type="circle"><li class="listitem"><p>
407 how to initialize the state object in a
408 standards-conformant manner?
409 </p></li><li class="listitem"><p>
410 how to synchronize the "C" and "C++"
411 conversion information?
412 </p></li><li class="listitem"><p>
413 wchar_t/char internal buffers and conversions between
414 internal/external buffers?
415 </p></li></ul></div></li></ul></div></div><div class="bibliography" title="Bibliography"><div class="titlepage"><div><div><h4 class="title"><a id="facet.codecvt.biblio"></a>Bibliography</h4></div></div></div><div class="biblioentry" title="The GNU C Library"><a id="id413506"></a><p><span class="title"><i>
417 </i>. </span><span class="author"><span class="firstname">Roland</span> <span class="surname">McGrath</span>. </span><span class="author"><span class="firstname">Ulrich</span> <span class="surname">Drepper</span>. </span><span class="copyright">Copyright © 2007 FSF. </span><span class="pagenums">
418 Chapters 6 Character Set Handling and 7 Locales and Internationalization
419 . </span></p></div><div class="biblioentry" title="Correspondence"><a id="id410520"></a><p><span class="title"><i>
421 </i>. </span><span class="author"><span class="firstname">Ulrich</span> <span class="surname">Drepper</span>. </span><span class="copyright">Copyright © 2002 . </span></p></div><div class="biblioentry" title="ISO/IEC 14882:1998 Programming languages - C++"><a id="id410548"></a><p><span class="title"><i>
422 ISO/IEC 14882:1998 Programming languages - C++
423 </i>. </span><span class="copyright">Copyright © 1998 ISO. </span></p></div><div class="biblioentry" title="ISO/IEC 9899:1999 Programming languages - C"><a id="id479822"></a><p><span class="title"><i>
424 ISO/IEC 9899:1999 Programming languages - C
425 </i>. </span><span class="copyright">Copyright © 1999 ISO. </span></p></div><div class="biblioentry"><a id="id479841"></a><p><span class="biblioid">
426 <a class="ulink" href="http://www.opengroup.org/austin" target="_top">
427 <em class="citetitle">
428 System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008)
431 . </span><span class="copyright">Copyright © 2008
432 The Open Group/The Institute of Electrical and Electronics
434 . </span></p></div><div class="biblioentry" title="The C++ Programming Language, Special Edition"><a id="id401535"></a><p><span class="title"><i>
435 The C++ Programming Language, Special Edition
436 </i>. </span><span class="author"><span class="firstname">Bjarne</span> <span class="surname">Stroustrup</span>. </span><span class="copyright">Copyright © 2000 Addison Wesley, Inc.. </span><span class="pagenums">Appendix D. </span><span class="publisher"><span class="publishername">
438 . </span></span></p></div><div class="biblioentry" title="Standard C++ IOStreams and Locales"><a id="id390266"></a><p><span class="title"><i>
439 Standard C++ IOStreams and Locales
440 </i>. </span><span class="subtitle">
441 Advanced Programmer's Guide and Reference
442 . </span><span class="author"><span class="firstname">Angelika</span> <span class="surname">Langer</span>. </span><span class="author"><span class="firstname">Klaus</span> <span class="surname">Kreft</span>. </span><span class="copyright">Copyright © 2000 Addison Wesley Longman, Inc.. </span><span class="publisher"><span class="publishername">
443 Addison Wesley Longman
444 . </span></span></p></div><div class="biblioentry"><a id="id486188"></a><p><span class="biblioid">
445 <a class="ulink" href="http://www.lysator.liu.se/c/na1.html" target="_top">
446 <em class="citetitle">
447 A brief description of Normative Addendum 1
450 . </span><span class="author"><span class="firstname">Clive</span> <span class="surname">Feather</span>. </span><span class="pagenums">Extended Character Sets. </span></p></div><div class="biblioentry"><a id="id420899"></a><p><span class="biblioid">
451 <a class="ulink" href="http://tldp.org/HOWTO/Unicode-HOWTO.html" target="_top">
452 <em class="citetitle">
456 . </span><span class="author"><span class="firstname">Bruno</span> <span class="surname">Haible</span>. </span></p></div><div class="biblioentry"><a id="id404329"></a><p><span class="biblioid">
457 <a class="ulink" href="http://www.cl.cam.ac.uk/~mgk25/unicode.html" target="_top">
458 <em class="citetitle">
459 UTF-8 and Unicode FAQ for Unix/Linux
462 . </span><span class="author"><span class="firstname">Markus</span> <span class="surname">Khun</span>. </span></p></div></div></div><div class="section" title="messages"><div class="titlepage"><div><div><h3 class="title"><a id="manual.localization.facet.messages"></a>messages</h3></div></div></div><p>
463 The std::messages facet implements message retrieval functionality
464 equivalent to Java's java.text.MessageFormat .using either GNU gettext
465 or IEEE 1003.1-200 functions.
466 </p><div class="section" title="Requirements"><div class="titlepage"><div><div><h4 class="title"><a id="facet.messages.req"></a>Requirements</h4></div></div></div><p>
467 The std::messages facet is probably the most vaguely defined facet in
468 the standard library. It's assumed that this facility was built into
469 the standard library in order to convert string literals from one
470 locale to the other. For instance, converting the "C" locale's
471 <code class="code">const char* c = "please"</code> to a German-localized <code class="code">"bitte"</code>
472 during program execution.
473 </p><div class="blockquote"><blockquote class="blockquote"><p>
474 22.2.7.1 - Template class messages [lib.locale.messages]
475 </p></blockquote></div><p>
476 This class has three public member functions, which directly
477 correspond to three protected virtual member functions.
479 The public member functions are:
481 <code class="code">catalog open(const string&, const locale&) const</code>
483 <code class="code">string_type get(catalog, int, int, const string_type&) const</code>
485 <code class="code">void close(catalog) const</code>
487 While the virtual functions are:
489 <code class="code">catalog do_open(const string&, const locale&) const</code>
490 </p><div class="blockquote"><blockquote class="blockquote"><p>
491 <span class="emphasis"><em>
492 -1- Returns: A value that may be passed to get() to retrieve a
493 message, from the message catalog identified by the string name
494 according to an implementation-defined mapping. The result can be used
495 until it is passed to close(). Returns a value less than 0 if no such
496 catalog can be opened.
498 </p></blockquote></div><p>
499 <code class="code">string_type do_get(catalog, int, int, const string_type&) const</code>
500 </p><div class="blockquote"><blockquote class="blockquote"><p>
501 <span class="emphasis"><em>
502 -3- Requires: A catalog cat obtained from open() and not yet closed.
503 -4- Returns: A message identified by arguments set, msgid, and dfault,
504 according to an implementation-defined mapping. If no such message can
505 be found, returns dfault.
507 </p></blockquote></div><p>
508 <code class="code">void do_close(catalog) const</code>
509 </p><div class="blockquote"><blockquote class="blockquote"><p>
510 <span class="emphasis"><em>
511 -5- Requires: A catalog cat obtained from open() and not yet closed.
512 -6- Effects: Releases unspecified resources associated with cat.
513 -7- Notes: The limit on such resources, if any, is implementation-defined.
515 </p></blockquote></div></div><div class="section" title="Design"><div class="titlepage"><div><div><h4 class="title"><a id="facet.messages.design"></a>Design</h4></div></div></div><p>
516 A couple of notes on the standard.
518 First, why is <code class="code">messages_base::catalog</code> specified as a typedef
519 to int? This makes sense for implementations that use
520 <code class="code">catopen</code>, but not for others. Fortunately, it's not heavily
521 used and so only a minor irritant.
523 Second, by making the member functions <code class="code">const</code>, it is
524 impossible to save state in them. Thus, storing away information used
525 in the 'open' member function for use in 'get' is impossible. This is
528 The 'open' member function in particular seems to be oddly
529 designed. The signature seems quite peculiar. Why specify a <code class="code">const
530 string& </code> argument, for instance, instead of just <code class="code">const
531 char*</code>? Or, why specify a <code class="code">const locale&</code> argument that is
532 to be used in the 'get' member function? How, exactly, is this locale
533 argument useful? What was the intent? It might make sense if a locale
534 argument was associated with a given default message string in the
535 'open' member function, for instance. Quite murky and unclear, on
538 Lastly, it seems odd that messages, which explicitly require code
539 conversion, don't use the codecvt facet. Because the messages facet
540 has only one template parameter, it is assumed that ctype, and not
541 codecvt, is to be used to convert between character sets.
543 It is implicitly assumed that the locale for the default message
544 string in 'get' is in the "C" locale. Thus, all source code is assumed
545 to be written in English, so translations are always from "en_US" to
546 other, explicitly named locales.
547 </p></div><div class="section" title="Implementation"><div class="titlepage"><div><div><h4 class="title"><a id="facet.messages.impl"></a>Implementation</h4></div></div></div><div class="section" title="Models"><div class="titlepage"><div><div><h5 class="title"><a id="messages.impl.models"></a>Models</h5></div></div></div><p>
548 This is a relatively simple class, on the face of it. The standard
549 specifies very little in concrete terms, so generic
550 implementations that are conforming yet do very little are the
551 norm. Adding functionality that would be useful to programmers and
552 comparable to Java's java.text.MessageFormat takes a bit of work,
553 and is highly dependent on the capabilities of the underlying
556 Three different mechanisms have been provided, selectable via
558 </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
561 This model does very little, and is what is used by default.
562 </p></li><li class="listitem"><p>
565 The gnu model is complete and fully tested. It's based on the
566 GNU gettext package, which is part of glibc. It uses the
567 functions <code class="code">textdomain, bindtextdomain, gettext</code> to
568 implement full functionality. Creating message catalogs is a
569 relatively straight-forward process and is lightly documented
570 below, and fully documented in gettext's distributed
572 </p></li><li class="listitem"><p>
575 This is a complete, though untested, implementation based on
576 the IEEE standard. The functions <code class="code">catopen, catgets,
577 catclose</code> are used to retrieve locale-specific messages
578 given the appropriate message catalogs that have been
579 constructed for their use. Note, the script <code class="code">
580 po2msg.sed</code> that is part of the gettext distribution can
581 convert gettext catalogs into catalogs that
582 <code class="code">catopen</code> can use.
583 </p></li></ul></div><p>
584 A new, standards-conformant non-virtual member function signature was
585 added for 'open' so that a directory could be specified with a given
586 message catalog. This simplifies calling conventions for the gnu
588 </p></div><div class="section" title="The GNU Model"><div class="titlepage"><div><div><h5 class="title"><a id="messages.impl.gnu"></a>The GNU Model</h5></div></div></div><p>
589 The messages facet, because it is retrieving and converting
590 between characters sets, depends on the ctype and perhaps the
591 codecvt facet in a given locale. In addition, underlying "C"
592 library locale support is necessary for more than just the
593 <code class="code">LC_MESSAGES</code> mask: <code class="code">LC_CTYPE</code> is also
594 necessary. To avoid any unpleasantness, all bits of the "C" mask
595 (i.e. <code class="code">LC_ALL</code>) are set before retrieving messages.
597 Making the message catalogs can be initially tricky, but become
598 quite simple with practice. For complete info, see the gettext
599 documentation. Here's an idea of what is required:
600 </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
601 Make a source file with the required string literals that need
602 to be translated. See <code class="code">intl/string_literals.cc</code> for
604 </p></li><li class="listitem"><p>
605 Make initial catalog (see "4 Making the PO Template File" from
606 the gettext docs).</p><p>
607 <code class="code"> xgettext --c++ --debug string_literals.cc -o libstdc++.pot </code>
608 </p></li><li class="listitem"><p>Make language and country-specific locale catalogs.</p><p>
609 <code class="code">cp libstdc++.pot fr_FR.po</code>
611 <code class="code">cp libstdc++.pot de_DE.po</code>
612 </p></li><li class="listitem"><p>
613 Edit localized catalogs in emacs so that strings are
616 <code class="code">emacs fr_FR.po</code>
617 </p></li><li class="listitem"><p>Make the binary mo files.</p><p>
618 <code class="code">msgfmt fr_FR.po -o fr_FR.mo</code>
620 <code class="code">msgfmt de_DE.po -o de_DE.mo</code>
621 </p></li><li class="listitem"><p>Copy the binary files into the correct directory structure.</p><p>
622 <code class="code">cp fr_FR.mo (dir)/fr_FR/LC_MESSAGES/libstdc++.mo</code>
624 <code class="code">cp de_DE.mo (dir)/de_DE/LC_MESSAGES/libstdc++.mo</code>
625 </p></li><li class="listitem"><p>Use the new message catalogs.</p><p>
626 <code class="code">locale loc_de("de_DE");</code>
629 use_facet<messages<char> >(loc_de).open("libstdc++", locale(), dir);
631 </p></li></ul></div></div></div><div class="section" title="Use"><div class="titlepage"><div><div><h4 class="title"><a id="facet.messages.use"></a>Use</h4></div></div></div><p>
632 A simple example using the GNU model of message conversion.
633 </p><pre class="programlisting">
634 #include <iostream>
635 #include <locale>
640 typedef messages<char>::catalog catalog;
642 "/mnt/egcs/build/i686-pc-linux-gnu/libstdc++/po/share/locale";
643 const locale loc_de("de_DE");
644 const messages<char>& mssg_de = use_facet<messages<char> >(loc_de);
646 catalog cat_de = mssg_de.open("libstdc++", loc_de, dir);
647 string s01 = mssg_de.get(cat_de, 0, 0, "please");
648 string s02 = mssg_de.get(cat_de, 0, 0, "thank you");
649 cout << "please in german:" << s01 << '\n';
650 cout << "thank you in german:" << s02 << '\n';
651 mssg_de.close(cat_de);
653 </pre></div><div class="section" title="Future"><div class="titlepage"><div><div><h4 class="title"><a id="facet.messages.future"></a>Future</h4></div></div></div><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
654 Things that are sketchy, or remain unimplemented:
655 </p><div class="itemizedlist"><ul class="itemizedlist" type="circle"><li class="listitem"><p>
656 _M_convert_from_char, _M_convert_to_char are in flux,
657 depending on how the library ends up doing character set
658 conversions. It might not be possible to do a real character
659 set based conversion, due to the fact that the template
660 parameter for messages is not enough to instantiate the
661 codecvt facet (1 supplied, need at least 2 but would prefer
663 </p></li><li class="listitem"><p>
664 There are issues with gettext needing the global locale set
665 to extract a message. This dependence on the global locale
666 makes the current "gnu" model non MT-safe. Future versions
667 of glibc, i.e. glibc 2.3.x will fix this, and the C++ library
668 bits are already in place.
669 </p></li></ul></div></li><li class="listitem"><p>
670 Development versions of the GNU "C" library, glibc 2.3 will allow
671 a more efficient, MT implementation of std::messages, and will
672 allow the removal of the _M_name_messages data member. If this is
673 done, it will change the library ABI. The C++ parts to support
674 glibc 2.3 have already been coded, but are not in use: once this
675 version of the "C" library is released, the marked parts of the
676 messages implementation can be switched over to the new "C"
677 library functionality.
678 </p></li><li class="listitem"><p>
679 At some point in the near future, std::numpunct will probably use
680 std::messages facilities to implement truename/falsename
681 correctly. This is currently not done, but entries in
682 libstdc++.pot have already been made for "true" and "false" string
683 literals, so all that remains is the std::numpunct coding and the
684 configure/make hassles to make the installed library search its
685 own catalog. Currently the libstdc++.mo catalog is only searched
686 for the testsuite cases involving messages members.
687 </p></li><li class="listitem"><p> The following member functions:</p><p>
690 open(const basic_string<char>& __s, const locale& __loc) const
695 open(const basic_string<char>&, const locale&, const char*) const;
698 Don't actually return a "value less than 0 if no such catalog
699 can be opened" as required by the standard in the "gnu"
700 model. As of this writing, it is unknown how to query to see
701 if a specified message catalog exists using the gettext
703 </p></li></ul></div></div><div class="bibliography" title="Bibliography"><div class="titlepage"><div><div><h4 class="title"><a id="facet.messages.biblio"></a>Bibliography</h4></div></div></div><div class="biblioentry" title="The GNU C Library"><a id="id400918"></a><p><span class="title"><i>
705 </i>. </span><span class="author"><span class="firstname">Roland</span> <span class="surname">McGrath</span>. </span><span class="author"><span class="firstname">Ulrich</span> <span class="surname">Drepper</span>. </span><span class="copyright">Copyright © 2007 FSF. </span><span class="pagenums">Chapters 6 Character Set Handling, and 7 Locales and Internationalization
706 . </span></p></div><div class="biblioentry" title="Correspondence"><a id="id508558"></a><p><span class="title"><i>
708 </i>. </span><span class="author"><span class="firstname">Ulrich</span> <span class="surname">Drepper</span>. </span><span class="copyright">Copyright © 2002 . </span></p></div><div class="biblioentry" title="ISO/IEC 14882:1998 Programming languages - C++"><a id="id417528"></a><p><span class="title"><i>
709 ISO/IEC 14882:1998 Programming languages - C++
710 </i>. </span><span class="copyright">Copyright © 1998 ISO. </span></p></div><div class="biblioentry" title="ISO/IEC 9899:1999 Programming languages - C"><a id="id417546"></a><p><span class="title"><i>
711 ISO/IEC 9899:1999 Programming languages - C
712 </i>. </span><span class="copyright">Copyright © 1999 ISO. </span></p></div><div class="biblioentry"><a id="id417565"></a><p><span class="biblioid">
713 <a class="ulink" href="http://www.opengroup.org/austin" target="_top">
714 <em class="citetitle">
715 System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008)
718 . </span><span class="copyright">Copyright © 2008
719 The Open Group/The Institute of Electrical and Electronics
721 . </span></p></div><div class="biblioentry" title="The C++ Programming Language, Special Edition"><a id="id386723"></a><p><span class="title"><i>
722 The C++ Programming Language, Special Edition
723 </i>. </span><span class="author"><span class="firstname">Bjarne</span> <span class="surname">Stroustrup</span>. </span><span class="copyright">Copyright © 2000 Addison Wesley, Inc.. </span><span class="pagenums">Appendix D. </span><span class="publisher"><span class="publishername">
725 . </span></span></p></div><div class="biblioentry" title="Standard C++ IOStreams and Locales"><a id="id403269"></a><p><span class="title"><i>
726 Standard C++ IOStreams and Locales
727 </i>. </span><span class="subtitle">
728 Advanced Programmer's Guide and Reference
729 . </span><span class="author"><span class="firstname">Angelika</span> <span class="surname">Langer</span>. </span><span class="author"><span class="firstname">Klaus</span> <span class="surname">Kreft</span>. </span><span class="copyright">Copyright © 2000 Addison Wesley Longman, Inc.. </span><span class="publisher"><span class="publishername">
730 Addison Wesley Longman
731 . </span></span></p></div><div class="biblioentry"><a id="id471633"></a><p><span class="biblioid">
732 <a class="ulink" href="http://java.sun.com/reference/api/index.html" target="_top">
733 <em class="citetitle">
734 API Specifications, Java Platform
737 . </span><span class="pagenums">java.util.Properties, java.text.MessageFormat,
738 java.util.Locale, java.util.ResourceBundle
739 . </span></p></div><div class="biblioentry"><a id="id471656"></a><p><span class="biblioid">
740 <a class="ulink" href="http://www.gnu.org/software/gettext/" target="_top">
741 <em class="citetitle">
742 GNU gettext tools, version 0.10.38, Native Language Support
746 . </span></p></div></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="localization.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="localization.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="containers.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 8.
749 </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 9.
752 </td></tr></table></div></body></html>