forked from Srar/node-tap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.hpp
29 lines (25 loc) · 839 Bytes
/
util.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef UTIL_H
#define UTIL_H
#include <nan.h>
#include <string>
#include <cstring>
#include <windows.h>
inline std::string Utf8Encode(const std::wstring &wstr)
{
if (wstr.empty())
return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
inline std::wstring Utf8Decode(const std::string &str)
{
if (str.empty())
return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
#endif