1 //
2 //  Copyright (c) 2009-2011 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 
9 #ifndef BOOST_LOCALE_CONV_IMPL_HPP
10 #define BOOST_LOCALE_CONV_IMPL_HPP
11 
12 #include <boost/locale/config.hpp>
13 #include <boost/locale/encoding.hpp>
14 namespace boost {
15     namespace locale {
16         namespace conv {
17             namespace impl {
18 
19                 template<typename CharType>
utf_name()20                 char const *utf_name()
21                 {
22                     union {
23                         char first;
24                         uint16_t u16;
25                         uint32_t u32;
26                     } v;
27 
28                     if(sizeof(CharType) == 1) {
29                         return "UTF-8";
30                     }
31                     else if(sizeof(CharType) == 2) {
32                         v.u16 = 1;
33                         if(v.first == 1) {
34                             return "UTF-16LE";
35                         }
36                         else {
37                             return "UTF-16BE";
38                         }
39                     }
40                     else if(sizeof(CharType) == 4) {
41                         v.u32 = 1;
42                         if(v.first == 1) {
43                             return "UTF-32LE";
44                         }
45                         else {
46                             return "UTF-32BE";
47                         }
48 
49                     }
50                     else {
51                         return "Unknown Character Encoding";
52                     }
53                 }
54 
55                 std::string normalize_encoding(char const *encoding);
56 
compare_encodings(char const * l,char const * r)57                 inline int compare_encodings(char const *l,char const *r)
58                 {
59                     return normalize_encoding(l).compare(normalize_encoding(r));
60                 }
61 
62                 #if defined(BOOST_WINDOWS)  || defined(__CYGWIN__)
63                 int encoding_to_windows_codepage(char const *ccharset);
64                 #endif
65 
66                 class converter_between {
67                 public:
68                     typedef char char_type;
69 
70                     typedef std::string string_type;
71 
72                     virtual bool open(char const *to_charset,char const *from_charset,method_type how) = 0;
73 
74                     virtual std::string convert(char const *begin,char const *end) = 0;
75 
~converter_between()76                     virtual ~converter_between()
77                     {
78                     }
79                 };
80 
81                 template<typename CharType>
82                 class converter_from_utf {
83                 public:
84                     typedef CharType char_type;
85 
86                     typedef std::basic_string<char_type> string_type;
87 
88                     virtual bool open(char const *charset,method_type how) = 0;
89 
90                     virtual std::string convert(CharType const *begin,CharType const *end) = 0;
91 
~converter_from_utf()92                     virtual ~converter_from_utf()
93                     {
94                     }
95                 };
96 
97                 template<typename CharType>
98                 class converter_to_utf {
99                 public:
100                     typedef CharType char_type;
101 
102                     typedef std::basic_string<char_type> string_type;
103 
104                     virtual bool open(char const *charset,method_type how) = 0;
105 
106                     virtual string_type convert(char const *begin,char const *end) = 0;
107 
~converter_to_utf()108                     virtual ~converter_to_utf()
109                     {
110                     }
111                 };
112             }
113         }
114     }
115 }
116 
117 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
118 #endif
119