-
Notifications
You must be signed in to change notification settings - Fork 444
/
misc.cpp
319 lines (289 loc) · 7.45 KB
/
misc.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "common.h"
#include "settings.h"
CRITICAL_SECTION CCriticalSectionLock::m_cs[20];
OWNED_CRITIAL_SECTION COwnedCriticalSectionLock::m_cs[2];
LONG CThreadCounter::interlock; //snowie!! C++还需要额外申明,汗
TCHAR CGdippSettings::m_szexeName[MAX_PATH+1] = {0};
#ifdef _UNICODE
#undef CharPrev
#define CharPrev(s, c) ((c) - 1)
#endif
//手抜きのパス操作関数群
BOOL WINAPI PathIsRelative(LPCTSTR pszPath)
{
if (!pszPath || !*pszPath) {
return FALSE;
}
const TCHAR ch1 = pszPath[0];
const TCHAR ch2 = pszPath[1];
if (ch1 == _T('\\') && ch2 == _T('\\')) {
//UNC
return FALSE;
}
if (ch1 == _T('\\') || (ch1 && ch2 == _T(':'))) {
//絶対パス
return FALSE;
}
return TRUE;
}
BOOL WINAPI PathRemoveFileSpec(LPTSTR pszPath)
{
if (!pszPath) {
return FALSE;
}
LPTSTR p = pszPath + _tcslen(pszPath);
while (p >= pszPath) {
switch (*p) {
case _T('\\'):
case _T('/'):
if (p > pszPath) {
//c:\aaa -> c:\ <
//c:\ -> c:\ <
switch (*(p - 1)) {
case _T('\\'):
case _T('/'):
case _T(':'):
break;
default:
goto END;
}
}
case _T(':'):
// c:aaa -> c:
p++;
goto END;
}
if (p <= pszPath) {
break;
}
p = CharPrev(NULL, p);
}
END:
if (*p) {
*p = _T('\0');
return TRUE;
}
return FALSE;
}
LPTSTR WINAPI PathFindExtension(LPCTSTR pszPath)
{
if (!pszPath) {
return NULL;
}
LPCTSTR p, pszEnd;
p = pszEnd = pszPath + _tcslen(pszPath);
while (p > pszPath) {
switch (*p) {
case _T('.'):
return (LPTSTR)p;
case _T('\\'):
case _T('/'):
case _T(':'):
return (LPTSTR)pszEnd;
}
p = CharPrev(pszPath, p);
}
return (LPTSTR)pszEnd;
}
LPTSTR WINAPI PathAddBackslash(LPTSTR pszPath)
{
if (!pszPath) {
return NULL;
}
int cch = _tcslen(pszPath);
if (cch + 1 >= MAX_PATH) {
return NULL;
}
LPTSTR p = pszPath + cch;
switch (*CharPrev(pszPath, p)) {
case _T('\\'):
case _T('/'):
break;
default:
p[0] = _T('\\');
p[1] = _T('\0');
break;
}
return pszPath;
}
LPTSTR WINAPI PathCombine(LPTSTR pszDest, LPCTSTR pszDir, LPCTSTR pszFile)
{
if (!pszDest || !pszDir || !pszFile) {
return NULL;
}
//かなり手抜き
TCHAR szCurDir[MAX_PATH], szDir[MAX_PATH+1];
GetCurrentDirectory(MAX_PATH, szCurDir);
_tcsncpy(szDir, pszDir, MAX_PATH - 1);
szDir[MAX_PATH - 1] = _T('\0');
PathAddBackslash(szDir);
if (!SetCurrentDirectory(szDir)) {
*pszDest = _T('\0');
return NULL;
}
TCHAR szFile[MAX_PATH];
_tcsncpy(szFile, pszFile, MAX_PATH - 1);
szFile[MAX_PATH - 1] = _T('\0');
GetFullPathName(szFile, MAX_PATH, pszDest, NULL);
SetCurrentDirectory(szCurDir);
TRACE(_T("PathCombine: %s\n"), pszDest);
return pszDest;
}
LPWSTR _StrDupExAtoW(LPCSTR pszMB, int cchMB /*= -1*/, LPWSTR pszStack /*= NULL*/, int cchStack /*= 0*/, int* pcchWC /*= NULL*/, int nACP)
{
int _cchWC;
if (!pcchWC) {
pcchWC = &_cchWC;
}
*pcchWC = 0;
if (!pszMB) {
return NULL;
}
if (cchMB == -1) {
cchMB = strlen(pszMB);
}
const int cchWC = MultiByteToWideChar(nACP, 0, pszMB, cchMB, NULL, 0);
if(cchWC < 0) {
return NULL;
}
LPWSTR pszWC;
if(cchWC < cchStack && pszStack) {
pszWC = pszStack;
ZeroMemory(pszWC, sizeof(WCHAR) * (cchWC + 1));
} else {
pszWC = (LPWSTR)calloc(sizeof(WCHAR), cchWC + 1);
if (!pszWC) {
return NULL;
}
}
MultiByteToWideChar(nACP, 0, pszMB, cchMB, pszWC, cchWC);
pszWC[cchWC] = L'\0';
*pcchWC = cchWC;
return pszWC;
}
#ifdef _DEBUG
typedef struct {
LPCSTR name;
UINT flag;
UINT mask;
UINT xor;
} FLAG_NAME_MAP;
#define DEF_FLAG_NAME(f,m,x) { #f, f, m, x }
#define END_FLAG() { NULL }
LPCSTR Dbg_GetFlagNames(UINT flags, const FLAG_NAME_MAP* p, LPSTR worker)
{
if (!worker)
return "";
if (!flags || !p)
return "0";
LPSTR wk = worker;
*wk = '\0';
while (p->name) {
if (((flags & p->mask) ^ p->xor) == p->flag) {
if (wk != worker) {
memcpy(wk, " | ", 4);
wk += 3;
}
strcpy(wk, p->name);
wk += strlen(p->name);
flags &= ~p->mask;
}
p++;
}
if (flags) {
if (wk != worker) {
memcpy(wk, " | ", 4);
wk += 3;
}
sprintf(wk, "%04X", flags);
}
return *worker ? worker : "0";
}
void Dbg_TraceExtTextOutW(int nXStart, int nYStart, UINT fuOptions, LPCWSTR lpString, int cbString, const int* lpDx)
{
LPWSTR p;
if (fuOptions & ETO_GLYPH_INDEX) {
p = (LPWSTR)_alloca((cbString * 5 + 1) * sizeof(WCHAR));
LPWSTR q = p;
for (int i = 0; i < cbString; ++i) {
q += wsprintf(q, _T(" %04X"), lpString[i]);
}
} else {
p = (LPWSTR)_alloca((cbString + 1) * sizeof(WCHAR));
memcpy(p, lpString, cbString * sizeof(WCHAR));
p[cbString] = 0;
}
static const FLAG_NAME_MAP c_map[] = {
DEF_FLAG_NAME(ETO_OPAQUE, ETO_OPAQUE, 0),
DEF_FLAG_NAME(ETO_CLIPPED, ETO_CLIPPED, 0),
DEF_FLAG_NAME(ETO_GLYPH_INDEX, ETO_GLYPH_INDEX, 0),
DEF_FLAG_NAME(ETO_RTLREADING, ETO_RTLREADING, 0),
DEF_FLAG_NAME(ETO_NUMERICSLOCAL, ETO_NUMERICSLOCAL, 0),
DEF_FLAG_NAME(ETO_NUMERICSLATIN, ETO_NUMERICSLATIN, 0),
DEF_FLAG_NAME(ETO_IGNORELANGUAGE, ETO_IGNORELANGUAGE, 0),
DEF_FLAG_NAME(ETO_PDY, ETO_PDY, 0),
END_FLAG(),
};
CHAR wk[1024];
TRACE(_T("ExtTextOutW(%d, %d, %hs, \"%ls\", %d, %hs)\n")
, nXStart, nYStart, Dbg_GetFlagNames(fuOptions, c_map, wk), p, cbString, lpDx ? "{...}" : "NULL");
}
void Dbg_TraceScriptItemize(const WCHAR* pwcInChars, int cInChars)
{
LPWSTR p = (LPWSTR)_alloca((cInChars + 1) * sizeof(WCHAR));
memcpy(p, pwcInChars, cInChars * sizeof(WCHAR));
p[cInChars] = 0;
TRACE(_T("ScriptItemize(\"%ls\", %d)\n"), p, cInChars);
}
void Dbg_TraceScriptShape(const WCHAR* pwcChars, int cChars, const SCRIPT_ANALYSIS* psa, const WORD* pwOutGlyphs, int cGlyphs)
{
LPWSTR pc = (LPWSTR)_alloca((cChars + 1) * sizeof(WCHAR));
memcpy(pc, pwcChars, cChars * sizeof(WCHAR));
pc[cChars] = 0;
LPWSTR pg;
if (pwOutGlyphs) {
if (psa->fNoGlyphIndex) {
pg = (LPWSTR)_alloca((cGlyphs + 1) * sizeof(WCHAR));
memcpy(pg, pwOutGlyphs, cGlyphs * sizeof(WCHAR));
pg[cGlyphs] = 0;
} else {
pg = (LPWSTR)_alloca((cGlyphs * 5 + 1) * sizeof(WCHAR));
LPWSTR q = pg;
for (int i = 0; i < cGlyphs; ++i) {
q += wsprintf(q, _T(" %04X"), pwOutGlyphs[i]);
}
}
}
TRACE(_T("ScriptShape(\"%ls\", %d, Script=%d%S%S%S%S%S%S, BidiLevel=%d%S%S%S%S%S%S%S%S"),
pc, cChars,
psa->eScript, psa->fRTL ? ", RTL" : "", psa->fLayoutRTL ? ", LayoutRTL" : "",
psa->fLinkBefore ? ", LinkBefore" : "", psa->fLinkAfter ? ", LinkAfter" : "",
psa->fLogicalOrder ? ", LogicalOrder" : "", psa->fNoGlyphIndex ? ", NoGlyphIndex" : "",
psa->s.uBidiLevel, psa->s.fOverrideDirection ? ", OverrideDirection" : "",
psa->s.fInhibitSymSwap ? ", InhibitSymSwap" : "", psa->s.fCharShape ? ", CharShape" : "",
psa->s.fDigitSubstitute ? ", DigitSubstitute" : "", psa->s.fInhibitLigate ? ", InhibitLigate" : "",
psa->s.fDisplayZWG ? ", DisplayZWG" : "", psa->s.fArabicNumContext ? ", ArabicNumContext" : "",
psa->s.fGcpClusters ? ", GcpClusters" : ""
);
if (pwOutGlyphs) {
TRACE(_T(", \"%ls\", %d"), pg, cGlyphs);
}
TRACE(_T(")\n"));
}
void Dbg_TractGetTextExtent(LPCSTR lpString, int cbString, LPSIZE lpSize)
{
LPSTR p = (LPSTR)_alloca((cbString + 1) * sizeof(CHAR));
memcpy(p, lpString, cbString * sizeof(CHAR));
p[cbString] = 0;
TRACE(_T("GetTextExtentA(\"%hs\", %d) = { %d, %d }\n")
, p, cbString, lpSize->cx, lpSize->cy);
}
void Dbg_TractGetTextExtent(LPCWSTR lpString, int cbString, LPSIZE lpSize)
{
LPWSTR p = (LPWSTR)_alloca((cbString + 1) * sizeof(WCHAR));
memcpy(p, lpString, cbString * sizeof(WCHAR));
p[cbString] = 0;
TRACE(_T("GetTextExtentW(\"%ls\", %d) = { %d, %d }\n")
, p, cbString, lpSize->cx, lpSize->cy);
}
#endif