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