-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utility.cpp
234 lines (194 loc) · 7.5 KB
/
Utility.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
#include "stdafx.h"
#include "Utility.h"
#include "registry.h"
#include "RegExp.h"
#include <string.h>
// invert case, capitalize
void MakeInvertCase(CString & rString)
{
INT nLen = rString.GetLength();
for(INT i = 0; i < nLen; i++) {
INT ch = rString[i];
if( isalpha(ch) ) {
if( isupper(ch) ) rString.SetAt(i, tolower(ch));
else rString.SetAt(i, toupper(ch));
}
}
}
void MakeCapitalize(CString & rString)
{
INT nLen = rString.GetLength(); BOOL bAlpha = FALSE;
for(INT i = 0; i < nLen; i++) {
INT ch = rString[i];
if( isalpha(ch) ) {
if( bAlpha ) rString.SetAt(i, tolower(ch));
else rString.SetAt(i, toupper(ch));
bAlpha = TRUE;
} else bAlpha = FALSE;
}
}
// find string
INT ForwardFindString(LPCTSTR lpszString, LPCTSTR lpszSubStr, INT nFrom, BOOL bWholeWord, BOOL bMatchCase)
{
INT nLen1 = strlen(lpszString); if( ! nLen1 ) return -1;
INT nLen2 = strlen(lpszSubStr); if( ! nLen2 ) return -1;
if( nFrom < 0 ) nFrom = 0; if( nFrom > nLen1 ) return -1;
TCHAR * pString = (TCHAR *)lpszString;
TCHAR * pSubStr = (TCHAR *)lpszSubStr;
if( ! bMatchCase ) {
pString = new TCHAR[nLen1+1]; strcpy(pString, lpszString); _strlwr(pString);
pSubStr = new TCHAR[nLen2+1]; strcpy(pSubStr, lpszSubStr); _strlwr(pSubStr);
}
INT nFound = -1; TCHAR * pResult, * pOffset = pString + nFrom;
while( pOffset < pString + nLen1 && ( pResult = strstr(pOffset, pSubStr) ) ) {
if( bWholeWord ) {
UINT nBef = (pResult > pString) ? pResult[-1] : 0;
UINT nAft = pResult[nLen2];
if( ! __iscsym(nBef) && ! __iscsym(nAft) ) { nFound = pResult - pString; break; }
} else { nFound = pResult - pString; break; }
pOffset = pResult + 1;
}
if( ! bMatchCase ) {
delete [] pString;
delete [] pSubStr;
}
return nFound;
}
INT ReverseFindString(LPCTSTR lpszString, LPCTSTR lpszSubStr, INT nFrom, BOOL bWholeWord, BOOL bMatchCase)
{
INT nLen1 = strlen(lpszString); if( ! nLen1 ) return -1;
INT nLen2 = strlen(lpszSubStr); if( ! nLen2 ) return -1;
if( nFrom < 0 ) nFrom = nLen1; if( nFrom > nLen1 ) return -1;
TCHAR * pString = (TCHAR *)lpszString;
TCHAR * pSubStr = (TCHAR *)lpszSubStr;
if( ! bMatchCase ) {
pString = new TCHAR[nLen1+1]; strcpy(pString, lpszString); _strlwr(pString);
pSubStr = new TCHAR[nLen2+1]; strcpy(pSubStr, lpszSubStr); _strlwr(pSubStr);
}
INT nFound = -1; TCHAR * pResult, * pOffset = pString;
while( pOffset < pString + nLen1 && ( pResult = strstr(pOffset, pSubStr) ) ) {
if( pResult + nLen2 > pString + nFrom ) {
break; // stop search if we get here...
} else if( bWholeWord ) {
UINT nBef = (pResult > pString) ? pResult[-1] : 0;
UINT nAft = pResult[nLen2];
if( ! __iscsym(nBef) && ! __iscsym(nAft) ) nFound = pResult - pString;
} else nFound = pResult - pString;
pOffset = pResult + 1;
}
if( ! bMatchCase ) {
delete [] pString;
delete [] pSubStr;
}
return nFound;
}
// regular expression
INT ForwardFindStringRegExp(LPCTSTR lpszString, LPCTSTR lpszRegExp, CRegExp & clsRegExp, INT nFrom, BOOL bWholeWord, BOOL bMatchCase)
{
INT nLen1 = strlen(lpszString); // if( ! nLen1 ) return -1; -- can be zero length string
INT nLen2 = strlen(lpszRegExp); if( ! nLen2 ) return -1;
if( nFrom < 0 ) nFrom = 0; if( nFrom > nLen1 ) return -1;
TCHAR * pString = (TCHAR *)lpszString;
TCHAR * pRegExp = (TCHAR *)lpszRegExp;
if( ! bMatchCase ) {
static CString szString; szString = lpszString; szString.MakeLower();
pString = (TCHAR *)(LPCTSTR)szString;
}
INT nResult, nFound = -1; TCHAR * pOffset = pString + nFrom;
while( pOffset <= pString + nLen1 && ( nResult = clsRegExp.RegFind(pOffset) ) >= 0 ) {
INT nLen3 = clsRegExp.GetFoundLength();
if( pRegExp[0] == '^' && pOffset - pString + nResult > 0 ) {
break; // there is no more possibility
} else if( pRegExp[nLen2-1] == '$' && pOffset - pString + nResult + nLen3 < nLen1 ) {
// continue; skip this search result...
} else if( bWholeWord ) {
UINT nBef = (pOffset + nResult > pString) ? pOffset[nResult - 1] : 0;
UINT nAft = pOffset[nResult + nLen3];
if( ! __iscsym(nBef) && ! __iscsym(nAft) ) { nFound = pOffset - pString + nResult; break; }
} else { nFound = pOffset - pString + nResult; break; }
pOffset = pOffset + nResult + 1;
}
return nFound;
}
INT ReverseFindStringRegExp(LPCTSTR lpszString, LPCTSTR lpszRegExp, CRegExp & clsRegExp, INT nFrom, BOOL bWholeWord, BOOL bMatchCase)
{
INT nLen1 = strlen(lpszString); // if( ! nLen1 ) return -1; -- can be zero length string
INT nLen2 = strlen(lpszRegExp); if( ! nLen2 ) return -1;
if( nFrom < 0 ) nFrom = nLen1; if( nFrom > nLen1 ) return -1;
TCHAR * pString = (TCHAR *)lpszString;
TCHAR * pRegExp = (TCHAR *)lpszRegExp;
if( ! bMatchCase ) {
static CString szString; szString = lpszString; szString.MakeLower();
pString = (TCHAR *)(LPCTSTR)szString;
}
INT nResult, nFound = -1, nLength = 0; TCHAR * pOffset = pString;
while( pOffset <= pString + nLen1 && ( nResult = clsRegExp.RegFind(pOffset) ) >= 0 ) {
INT nLocal = -1, nLen3 = clsRegExp.GetFoundLength();
if( pOffset + nResult + nLen3 > pString + nFrom ) {
break; // stop search if we get here
} else if( pRegExp[0] == '^' && pOffset - pString + nResult > 0 ) {
break; // there is no more possibility
} else if( pRegExp[nLen2-1] == '$' && pOffset - pString + nResult + nLen3 < nLen1 ) {
// continue; skip this search result...
} else if( bWholeWord ) {
UINT nBef = (pOffset + nResult > pString) ? pOffset[nResult - 1] : 0;
UINT nAft = pOffset[nResult + nLen3];
if( ! __iscsym(nBef) && ! __iscsym(nAft) ) nLocal = pOffset - pString + nResult;
} else nLocal = pOffset - pString + nResult;
// greedy rule of regular expression
if( nLocal >= 0 && nLocal + nLen3 != nFound + nLength ) { nFound = nLocal; nLength = nLen3; }
pOffset = pOffset + nResult + 1;
}
// to retrieve information from search result later
if( nFound >= 0 ) clsRegExp.RegFind(pString + nFound);
return nFound;
}
// system environment
CString GetMsDosShellPath()
{
TCHAR szShellPath[MAX_PATH];
GetEnvironmentVariable("COMSPEC", szShellPath, MAX_PATH);
return szShellPath;
}
CString GetDefaultBrowserPath()
{
TCHAR szKey[MAX_PATH + MAX_PATH];
if( ! GetRegKeyValue(HKEY_CLASSES_ROOT, ".htm", "", szKey, MAX_PATH + MAX_PATH) ) return "";
lstrcat(szKey, "\\shell\\open\\command");
if( ! GetRegKeyValue(HKEY_CLASSES_ROOT, szKey, "", szKey, MAX_PATH + MAX_PATH) ) return "";
TCHAR * pos;
pos = _tcsstr(szKey, _T("\"%1\""));
if (pos == NULL) { // No quotes found
pos = _tcsstr(szKey, _T("%1")); // Check for %1, without quotes
if (pos == NULL) // No parameter at all...
pos = szKey+lstrlen(szKey)-1;
else * pos = '\0'; // Remove the parameter
} else * pos = '\0'; // Remove the parameter
return szKey;
}
// goto url
HINSTANCE GotoURL(LPCTSTR URL, INT nCmdShow)
{
// First try ShellExecute()
HINSTANCE hResult = ShellExecute(NULL, _T("open"), URL, NULL, NULL, nCmdShow);
// If it failed, get the .htm regkey and lookup the program
if ((UINT)hResult <= HINSTANCE_ERROR) {
CString szCommand = GetDefaultBrowserPath();
if( szCommand.GetLength() ) {
szCommand += " ";
szCommand += URL;
hResult = (HINSTANCE) WinExec(szCommand, nCmdShow);
}
}
return hResult;
}
// get sub menu
CMenu * GetSubMenuByText(CMenu * pMenu, LPCTSTR lpszText)
{
CString szMenuText; INT nMenuCount = pMenu->GetMenuItemCount();
for( INT i = 0; i < nMenuCount; i++ ) {
pMenu->GetMenuString( i, szMenuText, MF_BYPOSITION );
if( ! szMenuText.Compare(lpszText) ) return pMenu->GetSubMenu( i );
}
return NULL;
}