Boost.Locale
util.hpp
1 //
2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // https://www.boost.org/LICENSE_1_0.txt
6 
7 #ifndef BOOST_LOCALE_UTIL_HPP
8 #define BOOST_LOCALE_UTIL_HPP
9 
10 #include <boost/locale/generator.hpp>
11 #include <boost/locale/utf.hpp>
12 #include <boost/assert.hpp>
13 #include <boost/cstdint.hpp>
14 #include <locale>
15 #ifndef BOOST_NO_CXX11_SMART_PTR
16 #include <memory>
17 #endif
18 #include <typeinfo>
19 
20 namespace boost {
21 namespace locale {
26 namespace util {
27 
41  BOOST_LOCALE_DECL
42  std::string get_system_locale(bool use_utf8_on_windows = false);
43 
61  BOOST_LOCALE_DECL
62  std::locale create_info(std::locale const &in,std::string const &name);
63 
64 
79  class BOOST_LOCALE_DECL base_converter {
80  public:
81 
87  static const uint32_t illegal=utf::illegal;
88 
93  static const uint32_t incomplete=utf::incomplete;
94 
95  virtual ~base_converter();
100  virtual int max_len() const
101  {
102  return 1;
103  }
113  virtual bool is_thread_safe() const
114  {
115  return false;
116  }
120  virtual base_converter *clone() const
121  {
122  BOOST_ASSERT(typeid(*this)==typeid(base_converter));
123  return new base_converter();
124  }
125 
141  virtual uint32_t to_unicode(char const *&begin,char const *end)
142  {
143  if(begin == end)
144  return incomplete;
145  unsigned char cp = *begin;
146  if(cp <= 0x7F) {
147  begin++;
148  return cp;
149  }
150  return illegal;
151  }
163 
164  virtual uint32_t from_unicode(uint32_t u,char *begin,char const *end)
165  {
166  if(begin==end)
167  return incomplete;
168  if(u >= 0x80)
169  return illegal;
170  *begin = static_cast<char>(u);
171  return 1;
172  }
173  };
174 
175  #if BOOST_LOCALE_USE_AUTO_PTR
176  BOOST_LOCALE_DECL std::auto_ptr<base_converter> create_utf8_converter();
188  BOOST_LOCALE_DECL std::auto_ptr<base_converter> create_simple_converter(std::string const &encoding);
189 
190 
202  BOOST_LOCALE_DECL
203  std::locale create_codecvt(std::locale const &in,std::auto_ptr<base_converter> cvt,character_facet_type type);
204  #endif
205 
206  #ifndef BOOST_NO_CXX11_SMART_PTR
207  BOOST_LOCALE_DECL std::unique_ptr<base_converter> create_utf8_converter_unique_ptr();
219  BOOST_LOCALE_DECL std::unique_ptr<base_converter> create_simple_converter_unique_ptr(std::string const &encoding);
220 
232  BOOST_LOCALE_DECL
233  std::locale create_codecvt(std::locale const &in,std::unique_ptr<base_converter> cvt,character_facet_type type);
234  #endif
235 
240  BOOST_LOCALE_DECL base_converter *create_utf8_converter_new_ptr();
248  BOOST_LOCALE_DECL base_converter *create_simple_converter_new_ptr(std::string const &encoding);
249 
263  BOOST_LOCALE_DECL
264  std::locale create_codecvt_from_pointer(std::locale const &in,base_converter *cvt,character_facet_type type);
265 
270  BOOST_LOCALE_DECL
271  std::locale create_utf8_codecvt(std::locale const &in,character_facet_type type);
272 
279  BOOST_LOCALE_DECL
280  std::locale create_simple_codecvt(std::locale const &in,std::string const &encoding,character_facet_type type);
281 } // util
282 } // locale
283 } // boost
284 
285 #endif
virtual base_converter * clone() const
Definition: util.hpp:120
static const code_point incomplete
Special constant that defines incomplete code point.
Definition: utf.hpp:44
std::locale create_codecvt_from_pointer(std::locale const &in, base_converter *cvt, character_facet_type type)
virtual uint32_t to_unicode(char const *&begin, char const *end)
Definition: util.hpp:141
std::locale create_codecvt(std::locale const &in, std::unique_ptr< base_converter > cvt, character_facet_type type)
std::locale create_info(std::locale const &in, std::string const &name)
Installs information facet to locale in based on locale name name.
std::unique_ptr< base_converter > create_utf8_converter_unique_ptr()
base_converter * create_simple_converter_new_ptr(std::string const &encoding)
uint32_t character_facet_type
type that specifies the character type that locales can be generated for
Definition: generator.hpp:41
static const code_point illegal
Special constant that defines illegal code point.
Definition: utf.hpp:39
virtual int max_len() const
Definition: util.hpp:100
std::unique_ptr< base_converter > create_simple_converter_unique_ptr(std::string const &encoding)
base_converter * create_utf8_converter_new_ptr()
This class represent a simple stateless converter from UCS-4 and to UCS-4 for each single code point.
Definition: util.hpp:79
std::string get_system_locale(bool use_utf8_on_windows=false)
Return default system locale name in POSIX format.
std::locale create_simple_codecvt(std::locale const &in, std::string const &encoding, character_facet_type type)
virtual uint32_t from_unicode(uint32_t u, char *begin, char const *end)
Definition: util.hpp:164
std::locale create_utf8_codecvt(std::locale const &in, character_facet_type type)
virtual bool is_thread_safe() const
Definition: util.hpp:113