forked from abcdls0905/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin_encoding.h
175 lines (149 loc) · 3.55 KB
/
win_encoding.h
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//--------------------------------------------------------------------
// 文件名: win_encoding.h
// 内 容:
// 说 明:
// 创建日期: 2010年12月14日
// 创建人: 陆利民
// 版权所有: 苏州蜗牛电子有限公司
//--------------------------------------------------------------------
#ifndef _SYSTEM_WIN_ENCODING_H
#define _SYSTEM_WIN_ENCODING_H
#include <windows.h>
// 获得转换到宽字符串后长度(包括结束符)
inline size_t Port_GetToWideStrLen(const char* s)
{
return MultiByteToWideChar(CP_ACP, 0, s, -1, NULL, 0);
}
// 获得转换到字符串后的长度(包括结束符)
inline size_t Port_GetToStringLen(const wchar_t* ws)
{
return WideCharToMultiByte(CP_ACP, 0, ws, -1, NULL, 0, NULL, NULL);
}
// 字符串转换到宽字符串
inline const wchar_t* Port_StringToWideStr(const char* info, wchar_t* buf,
size_t byte_size)
{
const size_t len = byte_size / sizeof(wchar_t);
int res = MultiByteToWideChar(CP_ACP, 0, info, -1, buf, int(len));
if (res == 0)
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
buf[len - 1] = 0;
}
else
{
buf[0] = 0;
}
}
return buf;
}
// 宽字符串转换到字符串
inline const char* Port_WideStrToString(const wchar_t* info, char* buf,
size_t byte_size)
{
int res = WideCharToMultiByte(CP_ACP, 0, info, -1, buf, int(byte_size),
NULL, NULL);
if (0 == res)
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
buf[byte_size - 1] = 0;
}
else
{
buf[0] = 0;
}
}
return buf;
}
// 获得UTF8转换到宽字符串后长度(包括结束符)
inline size_t Port_GetUTF8ToWideStrLen(const char* s)
{
return MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
}
// 获得宽字符串转换到UTF8后的长度(包括结束符)
inline size_t Port_GetWideStrToUTF8Len(const wchar_t* ws)
{
return WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL);
}
// UTF8转换到宽字符串
inline const wchar_t* Port_UTF8ToWideStr(const char* info, wchar_t* buf,
size_t byte_size)
{
const size_t len = byte_size / sizeof(wchar_t);
int res = MultiByteToWideChar(CP_UTF8, 0, info, -1, buf, int(len));
if (res == 0)
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
buf[len - 1] = 0;
}
else
{
buf[0] = 0;
}
}
return buf;
}
// 宽字符串转换到UTF8
inline const char* Port_WideStrToUTF8(const wchar_t* info, char* buf,
size_t byte_size)
{
int res = WideCharToMultiByte(CP_UTF8, 0, info, -1, buf, int(byte_size),
NULL, NULL);
if (0 == res)
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
buf[byte_size - 1] = 0;
}
else
{
buf[0] = 0;
}
}
return buf;
}
// UCS2转换到宽字符串
inline const wchar_t* Port_UCS2ToWideStr(const unsigned short* info,
wchar_t* buf, size_t byte_size)
{
// 在WINDOWS系统宽字符是2个字节,不需要转换
size_t buf_len = byte_size / sizeof(wchar_t);
size_t info_len = wcslen((const wchar_t*)info);
if (info_len >= buf_len)
{
memcpy(buf, info, (buf_len - 1) * sizeof(wchar_t));
buf[buf_len - 1] = 0;
}
else
{
memcpy(buf, info, (info_len + 1) * sizeof(wchar_t));
}
return buf;
}
// 宽字符串转换到UCS2转换到
inline const unsigned short* Port_WideStrToUCS2(const wchar_t* info,
unsigned short* buf, size_t byte_size)
{
// 在WINDOWS系统宽字符是2个字节,不需要转换
size_t buf_len = byte_size / sizeof(unsigned short);
size_t info_len = wcslen(info);
if (info_len >= buf_len)
{
memcpy(buf, info, (buf_len - 1) * sizeof(unsigned short));
buf[buf_len - 1] = 0;
}
else
{
memcpy(buf, info, (info_len + 1) * sizeof(unsigned short));
}
return buf;
}
// 获得UCS2字符串长度
inline size_t Port_GetUCS2Len(const unsigned short* info)
{
return wcslen((const wchar_t*)info);
}
#endif // _SYSTEM_WIN_ENCODING_H