forked from vpinball/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
def.cpp
245 lines (210 loc) · 6.28 KB
/
def.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "stdafx.h"
#include "Intshcut.h"
unsigned long long tinymt64state[2] = { 'T', 'M' };
float sz2f(const string& sz)
{
const int len = (int)sz.length()+1;
WCHAR * const wzT = new WCHAR[len];
MultiByteToWideCharNull(CP_ACP, 0, sz.c_str(), -1, wzT, len);
CComVariant var = wzT;
float result;
if (SUCCEEDED(VariantChangeType(&var, &var, 0, VT_R4)))
{
result = V_R4(&var);
VariantClear(&var);
}
else
result = 0.0f; //!! use inf or NaN instead?
delete[] wzT;
return result;
}
string f2sz(const float f)
{
CComVariant var = f;
if (SUCCEEDED(VariantChangeType(&var, &var, 0, VT_BSTR)))
{
const WCHAR * const wzT = V_BSTR(&var);
char tmp[256];
WideCharToMultiByteNull(CP_ACP, 0, wzT, -1, tmp, 256, nullptr, nullptr);
VariantClear(&var);
return tmp;
}
else
return "0.0"s; //!! must this be somehow localized, i.e. . vs ,
}
void WideStrNCopy(const WCHAR *wzin, WCHAR *wzout, const DWORD wzoutMaxLen)
{
DWORD i = 0;
while (*wzin && (++i < wzoutMaxLen)) { *wzout++ = *wzin++; }
*wzout = 0;
}
void WideStrCat(const WCHAR *wzin, WCHAR *wzout, const DWORD wzoutMaxLen)
{
DWORD i = lstrlenW(wzout);
wzout += i;
while (*wzin && (++i < wzoutMaxLen)) { *wzout++ = *wzin++; }
*wzout = 0;
}
int WideStrCmp(const WCHAR *wz1, const WCHAR *wz2)
{
while (*wz1 != L'\0')
{
if (*wz1 != *wz2)
{
if (*wz1 > *wz2)
return 1; // If *wz2 == 0, then wz1 will return as higher, which is correct
else if (*wz1 < *wz2)
return -1;
}
wz1++;
wz2++;
}
if (*wz2 != L'\0')
return -1; // wz2 is longer - and therefore higher
return 0;
}
int WzSzStrCmp(const WCHAR *wz1, const char *sz2)
{
while (*wz1 != L'\0')
if (*wz1++ != *sz2++)
return 1;
if (*sz2 != L'\0')
return 1;
return 0;
}
int WzSzStrNCmp(const WCHAR *wz1, const char *sz2, const DWORD maxComparisonLen)
{
DWORD i = 0;
while (*wz1 != L'\0' && i < maxComparisonLen)
{
if (*wz1++ != *sz2++)
return 1;
i++;
}
if (*sz2 != L'\0')
return 1;
return 0;
}
LocalString::LocalString(const int resid)
{
if (resid > 0)
/*const int cchar =*/ LoadString(g_pvp->theInstance, resid, m_szbuffer, sizeof(m_szbuffer));
else
m_szbuffer[0] = '\0';
}
LocalStringW::LocalStringW(const int resid)
{
if (resid > 0)
LoadStringW(g_pvp->theInstance, resid, m_szbuffer, sizeof(m_szbuffer)/sizeof(WCHAR));
else
m_szbuffer[0] = L'\0';
}
WCHAR *MakeWide(const string& sz)
{
const int len = (int)sz.length()+1;
WCHAR * const wzT = new WCHAR[len];
MultiByteToWideCharNull(CP_ACP, 0, sz.c_str(), -1, wzT, len);
return wzT;
}
string MakeString(const wstring &wz)
{
// Somewhat overkill since we copy the string twice, once in the temp buffer for conversion, then in the string constructor
const int len = (int)wz.length();
char *const szT = new char[len + 1];
WideCharToMultiByteNull(CP_ACP, 0, wz.c_str(), -1, szT, len + 1, nullptr, nullptr);
/*const*/ string result(szT); // const removed for auto-move
delete [] szT;
return result;
}
wstring MakeWString(const string &sz)
{
// Somewhat overkill since we copy the string twice, once in the temp buffer for conversion, then in the string constructor
const int len = (int)sz.length();
WCHAR *const wzT = new WCHAR[len + 1];
MultiByteToWideCharNull(CP_ACP, 0, sz.c_str(), -1, wzT, len + 1);
/*const*/ wstring result(wzT); // const removed for auto-move
delete [] wzT;
return result;
}
char *MakeChar(const WCHAR *const wz)
{
const int len = lstrlenW(wz);
char * const szT = new char[len + 1];
WideCharToMultiByteNull(CP_ACP, 0, wz, -1, szT, len + 1, nullptr, nullptr);
return szT;
}
HRESULT OpenURL(const string& szURL)
{
IUniformResourceLocator* pURL;
HRESULT hres = CoCreateInstance(CLSID_InternetShortcut, nullptr, CLSCTX_INPROC_SERVER, IID_IUniformResourceLocator, (void**)&pURL);
if (FAILED(hres))
{
return hres;
}
hres = pURL->SetURL(szURL.c_str(), IURL_SETURL_FL_GUESS_PROTOCOL);
if (FAILED(hres))
{
pURL->Release();
return hres;
}
//Open the URL by calling InvokeCommand
URLINVOKECOMMANDINFO ivci;
ivci.dwcbSize = sizeof(URLINVOKECOMMANDINFO);
ivci.dwFlags = IURL_INVOKECOMMAND_FL_ALLOW_UI;
ivci.hwndParent = g_pvp->GetHwnd();
ivci.pcszVerb = "open";
hres = pURL->InvokeCommand(&ivci);
pURL->Release();
return (hres);
}
char* replace(const char* const original, const char* const pattern, const char* const replacement)
{
const size_t replen = strlen(replacement);
const size_t patlen = strlen(pattern);
const size_t orilen = strlen(original);
size_t patcnt = 0;
const char * patloc;
// find how many times the pattern occurs in the original string
for (const char* oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen)
patcnt++;
{
// allocate memory for the new string
const size_t retlen = orilen + patcnt * (replen - patlen);
char * const returned = new char[retlen + 1];
//if (returned != nullptr)
{
// copy the original string,
// replacing all the instances of the pattern
char * retptr = returned;
const char* oriptr;
for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen)
{
const size_t skplen = patloc - oriptr;
// copy the section until the occurence of the pattern
strncpy(retptr, oriptr, skplen);
retptr += skplen;
// copy the replacement
strncpy(retptr, replacement, replen);
retptr += replen;
}
// copy the rest of the string.
strcpy(retptr, oriptr);
}
return returned;
}
}
// Helper function for IsOnWine
//
// This exists such that we only check if we're on wine once, and assign the result of this function to a static const var
static bool IsOnWineInternal()
{
// See https://www.winehq.org/pipermail/wine-devel/2008-September/069387.html
const HMODULE ntdllHandle = GetModuleHandleW(L"ntdll.dll");
assert(ntdllHandle != nullptr && "Could not GetModuleHandleW(L\"ntdll.dll\")");
return GetProcAddress(ntdllHandle, "wine_get_version") != nullptr;
}
bool IsOnWine()
{
static const bool result = IsOnWineInternal();
return result;
}