1 // Copyright (c) 2001 Ronald Garcia, Indiana University (garcia@osl.iu.edu)
\r
2 // Andrew Lumsdaine, Indiana University (lums@osl.iu.edu).
\r
3 // Distributed under the Boost Software License, Version 1.0. (See accompany-
\r
4 // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
6 #ifndef BOOST_UTF8_CODECVT_FACET_HPP
\r
7 #define BOOST_UTF8_CODECVT_FACET_HPP
\r
9 // MS compatible compilers support #pragma once
\r
10 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
\r
14 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
\r
15 // utf8_codecvt_facet.hpp
\r
17 // This header defines class utf8_codecvt_facet, derived fro
\r
18 // std::codecvt<wchar_t, char>, which can be used to convert utf8 data in
\r
19 // files into wchar_t strings in the application.
\r
21 // The header is NOT STANDALONE, and is not to be included by the USER.
\r
22 // There are at least two libraries which want to use this functionality, and
\r
23 // we want to avoid code duplication. It would be possible to create utf8
\r
25 // - this requires review process first
\r
26 // - in the case, when linking the a library which uses utf8
\r
27 // (say 'program_options'), user should also link to the utf8 library.
\r
28 // This seems inconvenient, and asking a user to link to an unrevieved
\r
29 // library is strange.
\r
30 // Until the above points are fixed, a library which wants to use utf8 must:
\r
31 // - include this header from one of it's headers or sources
\r
32 // - include the corresponding .cpp file from one of the sources
\r
33 // - before including either file, the library must define
\r
34 // - BOOST_UTF8_BEGIN_NAMESPACE to the namespace declaration that must be used
\r
35 // - BOOST_UTF8_END_NAMESPACE to the code to close the previous namespace
\r
37 // - BOOST_UTF8_DECL -- to the code which must be used for all 'exportable'
\r
40 // For example, program_options library might contain:
\r
41 // #define BOOST_UTF8_BEGIN_NAMESPACE <backslash character>
\r
42 // namespace boost { namespace program_options {
\r
43 // #define BOOST_UTF8_END_NAMESPACE }}
\r
44 // #define BOOST_UTF8_DECL BOOST_PROGRAM_OPTIONS_DECL
\r
45 // #include "../../detail/utf8/utf8_codecvt.cpp"
\r
47 // Essentially, each library will have its own copy of utf8 code, in
\r
48 // different namespaces.
\r
50 // Note:(Robert Ramey). I have made the following alterations in the original
\r
52 // a) Rendered utf8_codecvt<wchar_t, char> with using templates
\r
53 // b) Move longer functions outside class definition to prevent inlining
\r
54 // and make code smaller
\r
55 // c) added on a derived class to permit translation to/from current
\r
58 // See http://www.boost.org for updates, documentation, and revision history.
\r
60 // archives stored as text - note these ar templated on the basic
\r
61 // stream templates to accommodate wide (and other?) kind of characters
\r
63 // note the fact that on libraries without wide characters, ostream is
\r
64 // is not a specialization of basic_ostream which in fact is not defined
\r
65 // in such cases. So we can't use basic_ostream<OStream::char_type> but rather
\r
66 // use two template parameters
\r
68 // utf8_codecvt_facet
\r
69 // This is an implementation of a std::codecvt facet for translating
\r
70 // from UTF-8 externally to UCS-4. Note that this is not tied to
\r
71 // any specific types in order to allow customization on platforms
\r
72 // where wchar_t is not big enough.
\r
74 // NOTES: The current implementation jumps through some unpleasant hoops in
\r
75 // order to deal with signed character types. As a std::codecvt_base::result,
\r
76 // it is necessary for the ExternType to be convertible to unsigned char.
\r
77 // I chose not to tie the extern_type explicitly to char. But if any combination
\r
78 // of types other than <wchar_t,char_t> is used, then std::codecvt must be
\r
79 // specialized on those types for this to work.
\r
82 #include <cwchar> // for mbstate_t
\r
83 #include <cstddef> // for std::size_t
\r
85 #include <boost/config.hpp>
\r
86 #include <boost/detail/workaround.hpp>
\r
88 #if defined(BOOST_NO_STDC_NAMESPACE)
\r
95 #if !defined(__MSL_CPP__) && !defined(__LIBCOMO__)
\r
96 #define BOOST_CODECVT_DO_LENGTH_CONST const
\r
98 #define BOOST_CODECVT_DO_LENGTH_CONST
\r
101 // maximum lenght of a multibyte string
\r
102 #define MB_LENGTH_MAX 8
\r
104 BOOST_UTF8_BEGIN_NAMESPACE
\r
106 struct BOOST_UTF8_DECL utf8_codecvt_facet :
\r
107 public std::codecvt<wchar_t, char, std::mbstate_t>
\r
110 explicit utf8_codecvt_facet(std::size_t no_locale_manage=0)
\r
111 : std::codecvt<wchar_t, char, std::mbstate_t>(no_locale_manage)
\r
114 virtual std::codecvt_base::result do_in(
\r
115 std::mbstate_t& state,
\r
117 const char * from_end,
\r
118 const char * & from_next,
\r
124 virtual std::codecvt_base::result do_out(
\r
125 std::mbstate_t & state, const wchar_t * from,
\r
126 const wchar_t * from_end, const wchar_t* & from_next,
\r
127 char * to, char * to_end, char * & to_next
\r
130 bool invalid_continuing_octet(unsigned char octet_1) const {
\r
131 return (octet_1 < 0x80|| 0xbf< octet_1);
\r
134 bool invalid_leading_octet(unsigned char octet_1) const {
\r
135 return (0x7f < octet_1 && octet_1 < 0xc0) ||
\r
139 // continuing octets = octets except for the leading octet
\r
140 static unsigned int get_cont_octet_count(unsigned char lead_octet) {
\r
141 return get_octet_count(lead_octet) - 1;
\r
144 static unsigned int get_octet_count(unsigned char lead_octet);
\r
146 // How many "continuing octets" will be needed for this word
\r
147 // == total octets - 1.
\r
148 int get_cont_octet_out_count(wchar_t word) const ;
\r
150 virtual bool do_always_noconv() const throw() { return false; }
\r
152 // UTF-8 isn't really stateful since we rewind on partial conversions
\r
153 virtual std::codecvt_base::result do_unshift(
\r
164 virtual int do_encoding() const throw() {
\r
165 const int variable_byte_external_encoding=0;
\r
166 return variable_byte_external_encoding;
\r
169 // How many char objects can I process to get <= max_limit
\r
170 // wchar_t objects?
\r
171 virtual int do_length(
\r
172 BOOST_CODECVT_DO_LENGTH_CONST std::mbstate_t &,
\r
174 const char * from_end,
\r
175 std::size_t max_limit
\r
176 #if BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600))
\r
182 // Largest possible value do_length(state,from,from_end,1) could return.
\r
183 virtual int do_max_length() const throw () {
\r
184 return 6; // largest UTF-8 encoding of a UCS-4 character
\r
188 BOOST_UTF8_END_NAMESPACE
\r
190 #endif // BOOST_UTF8_CODECVT_FACET_HPP
\r