-
Notifications
You must be signed in to change notification settings - Fork 0
/
wstring.cpp
143 lines (111 loc) · 3.17 KB
/
wstring.cpp
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
#ifndef __WSTRING_CPP
#define __WSTRING_CPP
#include "wstring.h"
#include "misc.h"
///////////////////////////////////////////////////////////////////////////////
//
// copies a single byte string to a unicode string
//
// param: str - buffer for single byte string
// wStr - unicode string
//
// return: success - not null
// failure - null
//
///////////////////////////////////////////////////////////////////////////////
int MBTWC(PSTR str, PWSTR wStr, int size)
{
if(!wStr)
return 0;
*wStr = '\0';
return MultiByteToWideChar(CP_ACP, 0, str, -1, wStr, size);
}
///////////////////////////////////////////////////////////////////////////////
//
// copies a unicode string to a single byte string
//
// param: wStr - unicode string
// str - buffer for single byte string
//
// return: success - not null
// failure - null
//
///////////////////////////////////////////////////////////////////////////////
int WCTMB(PWSTR wStr, PSTR str, int size)
{
if(!str)
return 0;
*str = '\0';
return WideCharToMultiByte(CP_ACP, NULL, wStr, -1, str, size, NULL, NULL);
}
///////////////////////////////////////////////////////////////////////////////
//
// allocates memory for a unicode string and copies a single byte string to a
// unicode string
//
// param: str - single byte string
// strLen - length of str or -1 if str is null terminated
//
// return: success - unicode string (must be deallocated with FreeStr)
// failure - null
//
///////////////////////////////////////////////////////////////////////////////
PWSTR S2W(PSTR str, int strLen)
{
if(!str)
return NULL;
PWSTR wStr = NULL;
int len = strLen == -1 ? strlen(str) + 1 : strLen;
MBTWC(str, wStr = (PWSTR)NewMem(sizeof(WCHAR) * len), len);
return wStr;
}
///////////////////////////////////////////////////////////////////////////////
//
// allocates memory for a single byte string and copies a unicode string to a
// single byte string
//
// param: wStr - unicode string
// strLen - length of wStr or -1 if wStr is null terminated
//
// return: success - single byte string (must be deallocated with FreeStr)
// failure - null
//
///////////////////////////////////////////////////////////////////////////////
PSTR W2S(PWSTR wStr, int strLen)
{
if(!wStr)
return NULL;
PSTR str = NULL;
int len = strLen == -1 ? wcslen(wStr) + 1 : strLen;
WCTMB(wStr, str = (PSTR)NewMem(len), len);
return str;
}
///////////////////////////////////////////////////////////////////////////////
//
// frees a single byte string
//
// param: str - single byte string
//
// return: nothing
//
///////////////////////////////////////////////////////////////////////////////
void FreeStr(PSTR str)
{
if(str)
FreeMem(str);
}
///////////////////////////////////////////////////////////////////////////////
//
// frees a unicode string
//
// param: wStr - unicode string
//
// return: nothing
//
///////////////////////////////////////////////////////////////////////////////
void FreeStr(PWSTR wStr)
{
if(wStr)
FreeMem(wStr);
}
#endif