-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCWC2MB.h
More file actions
55 lines (49 loc) · 1.14 KB
/
CWC2MB.h
File metadata and controls
55 lines (49 loc) · 1.14 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef __CWC2MB__H
#define __CWC2MB__H
template<int STATIC_MAX = 256>
class CWC2MBEx
{
public:
CWC2MBEx(const wchar_t *szInput)
{
init(szInput, CP_UTF8);
}
CWC2MBEx(const wchar_t *szInput, UINT nCodePage)
{
init(szInput, nCodePage);
}
~CWC2MBEx()
{
delete[] m_psz;
}
operator const char*() const
{
return(m_psz ? m_psz : m_szBuffer);
}
private:
void init(const wchar_t *szInput, UINT nCodePage)
{
m_szBuffer[0] = 0;
if(!szInput)
{
m_psz = NULL;
return;
}
int nLengthA = (int)(wcslen(szInput)) + 1;
int nLengthW = nLengthA;
bool isFailed = (0 == WideCharToMultiByte(nCodePage, 0, szInput, nLengthA, m_szBuffer, STATIC_MAX, NULL, NULL));
if(isFailed && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
nLengthW = WideCharToMultiByte(nCodePage, 0, szInput, nLengthA, NULL, 0, NULL, NULL);
m_psz = new char[nLengthW];
isFailed = (0 == WideCharToMultiByte(nCodePage, 0, szInput, nLengthA, m_psz, nLengthW, NULL, NULL));
}
}
private:
char *m_psz = NULL;
char m_szBuffer[STATIC_MAX];
CWC2MBEx(const CWC2MBEx&) = delete;
CWC2MBEx& operator=(const CWC2MBEx&) = delete;
};
typedef CWC2MBEx<> CWC2MB;
#endif