Samchon Framework for CPP  1.0.0
Charset.cpp
1 #include <samchon/library/Charset.hpp>
2 
3 #include <codecvt>
4 #include <atlstr.h>
5 
6 using namespace std;
7 using namespace samchon::library;
8 
9 /* -------------------------------------------------------------------
10  TO MULTIBYTE
11 ------------------------------------------------------------------- */
12 auto Charset::toMultibyte(const string &source) -> string
13 {
14  wstring &wstr = toUnicode(source, UTF8);
15  string &dest = toMultibyte(wstr);
16 
17  return dest;
18 }
19 
20 auto Charset::toMultibyte(const wstring &source) -> string
21 {
22  int len = WideCharToMultiByte(CP_ACP, 0, &source[0], -1, NULL, 0, NULL, NULL);
23  std::string str(len, 0);
24  WideCharToMultiByte(CP_ACP, 0, &source[0], -1, &str[0], len, NULL, NULL);
25 
26  return str;
27 
28  /*typedef codecvt<wchar_t, char, mbstate_t> codecvt_t;
29 
30  locale &loc = locale("");
31 
32  codecvt_t const& codecvt = use_facet<codecvt_t>(loc);
33  mbstate_t state = mbstate_t();
34  vector<char> buf(source.size() * codecvt.max_length());
35  wchar_t const* in_next = source.c_str();
36  char* out_next = &buf[0];
37 
38  codecvt_base::result r =
39  codecvt.out
40  (
41  state,
42  source.c_str(), source.c_str() + source.size(), in_next,
43  &buf[0], &buf[0] + buf.size(), out_next
44  );
45 
46  if (r == codecvt_base::error)
47  throw runtime_error("can't convert wstring to string");
48 
49  return string(buf.begin(), buf.end());*/
50 }
51 
52 /* -------------------------------------------------------------------
53  TO UTF-8
54 ------------------------------------------------------------------- */
55 auto Charset::toUTF8(const string &source) -> string
56 {
57  wstring &wstr = toUnicode(source, MULTIBYTE);
58  string &dest = toUTF8(wstr);
59 
60  if (dest.back() == NULL)
61  dest.pop_back();
62 
63  return dest;
64 }
65 
66 auto Charset::toUTF8(const wstring &source) -> string
67 {
68  int len = WideCharToMultiByte(CP_UTF8, 0, &source[0], -1, NULL, 0, NULL, NULL);
69  std::string str(len, 0);
70  WideCharToMultiByte(CP_UTF8, 0, &source[0], -1, &str[0], len, NULL, NULL);
71 
72  return str;
73 
74  /*wstring_convert<codecvt_utf8_utf16<wchar_t>> utf8Converter;
75 
76  return utf8Converter.to_bytes(source);*/
77 }
78 
79 /* -------------------------------------------------------------------
80  TO UNICODE
81 ------------------------------------------------------------------- */
82 auto Charset::toUnicode(const string &source, int charset) -> wstring
83 {
84  if (charset == MULTIBYTE)
85  {
86  int nLen = MultiByteToWideChar(CP_ACP, 0, &source[0], (int)source.size(), NULL, NULL);
87  std::wstring wstr(nLen, 0);
88  MultiByteToWideChar(CP_ACP, 0, &source[0], (int)source.size(), &wstr[0], nLen);
89 
90  return wstr;
91 
92  /*locale &loc = locale("");
93 
94  typedef codecvt<wchar_t, char, mbstate_t> codecvt_t;
95  codecvt_t const& codecvt = use_facet<codecvt_t>(loc);
96  mbstate_t state = mbstate_t();
97  vector<wchar_t> buf(source.size());
98  char const* in_next = source.c_str();
99  wchar_t* out_next = &buf[0];
100 
101  codecvt_base::result r =
102  codecvt.in
103  (
104  state,
105  source.c_str(), source.c_str() + source.size(), in_next,
106  &buf[0], &buf[0] + buf.size(), out_next
107  );
108 
109  if (r == codecvt_base::error)
110  throw runtime_error("can't convert string to wstring");
111 
112  return wstring(buf.begin(), buf.end());*/
113  }
114  else if (charset == UTF8)
115  {
116  int nLen = MultiByteToWideChar(CP_UTF8, 0, &source[0], (int)source.size(), NULL, NULL);
117  std::wstring wstr(nLen, 0);
118  MultiByteToWideChar(CP_UTF8, 0, &source[0], (int)source.size(), &wstr[0], nLen);
119 
120  return wstr;
121 
122  /*wstring_convert<codecvt_utf8_utf16<wchar_t>> utf8Converter;
123  wstring &wstr = move(utf8Converter.from_bytes(source));
124 
125  return wstr;*/
126  }
127  else
128  return L"";
129 }
Definition: RWMutex.hpp:4
Package of libraries.
Definition: library.hpp:84