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