Boost.Nowide
convert.hpp
1 //
2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_NOWIDE_CONVERT_H_INCLUDED
9 #define BOOST_NOWIDE_CONVERT_H_INCLUDED
10 
11 #include <string>
12 #include <boost/locale/encoding_utf.hpp>
13 
14 namespace boost {
15 namespace nowide {
25  template<typename CharOut,typename CharIn>
26  CharOut *basic_convert(CharOut *buffer,size_t buffer_size,CharIn const *source_begin,CharIn const *source_end)
27  {
28  CharOut *rv = buffer;
29  if(buffer_size == 0)
30  return 0;
31  buffer_size --;
32  while(source_begin!=source_end) {
33  using namespace boost::locale::utf;
34  code_point c = utf_traits<CharIn>::template decode<CharIn const *>(source_begin,source_end);
35  if(c==illegal || c==incomplete) {
36  rv = 0;
37  break;
38  }
39  size_t width = utf_traits<CharOut>::width(c);
40  if(buffer_size < width) {
41  rv=0;
42  break;
43  }
44  buffer = utf_traits<CharOut>::template encode<CharOut *>(c,buffer);
45  buffer_size -= width;
46  }
47  *buffer++ = 0;
48  return rv;
49  }
50 
52  namespace details {
53  //
54  // wcslen defined only in C99... So we will not use it
55  //
56  template<typename Char>
57  Char const *basic_strend(Char const *s)
58  {
59  while(*s)
60  s++;
61  return s;
62  }
63  }
65 
73  inline char *narrow(char *output,size_t output_size,wchar_t const *source)
74  {
75  return basic_convert(output,output_size,source,details::basic_strend(source));
76  }
84  inline char *narrow(char *output,size_t output_size,wchar_t const *begin,wchar_t const *end)
85  {
86  return basic_convert(output,output_size,begin,end);
87  }
95  inline wchar_t *widen(wchar_t *output,size_t output_size,char const *source)
96  {
97  return basic_convert(output,output_size,source,details::basic_strend(source));
98  }
106  inline wchar_t *widen(wchar_t *output,size_t output_size,char const *begin,char const *end)
107  {
108  return basic_convert(output,output_size,begin,end);
109  }
110 
111 
117  inline std::string narrow(wchar_t const *s)
118  {
119  return boost::locale::conv::utf_to_utf<char>(s);
120  }
126  inline std::wstring widen(char const *s)
127  {
128  return boost::locale::conv::utf_to_utf<wchar_t>(s);
129  }
135  inline std::string narrow(std::wstring const &s)
136  {
137  return boost::locale::conv::utf_to_utf<char>(s);
138  }
144  inline std::wstring widen(std::string const &s)
145  {
146  return boost::locale::conv::utf_to_utf<wchar_t>(s);
147  }
148 
149 } // nowide
150 } // namespace boost
151 
152 #endif
153 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
char * narrow(char *output, size_t output_size, wchar_t const *source)
Definition: convert.hpp:73
Definition: args.hpp:18
wchar_t * widen(wchar_t *output, size_t output_size, char const *source)
Definition: convert.hpp:95
CharOut * basic_convert(CharOut *buffer, size_t buffer_size, CharIn const *source_begin, CharIn const *source_end)
Template function that converts a buffer of UTF sequences in range [source_begin,source_end) to the o...
Definition: convert.hpp:26