Menu

[r504]: / trunk / src / AppUtils.cpp  Maximize  Restore  History

Download this file

511 lines (432 with data), 13.2 kB

  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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// CommitMonitor - simple checker for new commits in svn repositories
// Copyright (C) 2007-2010 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "StdAfx.h"
#include "AppUtils.h"
#include "Registry.h"
#include <shlwapi.h>
#include <shlobj.h>
#include <fstream>
#include <cctype>
#include <algorithm>
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "version.lib")
static const char iri_escape_chars[256] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 128 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
static const char uri_char_validity[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0,
/* 64 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0,
/* 128 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 192 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
CAppUtils::CAppUtils(void)
{
}
CAppUtils::~CAppUtils(void)
{
}
string CAppUtils::PathEscape(const string& path)
{
string ret2;
int c;
int i;
for (i=0; path[i]; ++i)
{
c = (unsigned char)path[i];
if (iri_escape_chars[c])
{
// no escaping needed for that char
ret2 += (unsigned char)path[i];
}
else
{
// char needs escaping
char temp[7] = {0};
sprintf_s(temp, 7, "%%%02X", (unsigned char)c);
ret2 += temp;
}
}
string ret;
for (i=0; ret2[i]; ++i)
{
c = (unsigned char)ret2[i];
if (uri_autoescape_chars[c])
{
// no escaping needed for that char
ret += (unsigned char)ret2[i];
}
else
{
// char needs escaping
char temp[7] = {0};
sprintf_s(temp, 7, "%%%02X", (unsigned char)c);
ret += temp;
}
}
return ret;
}
wstring CAppUtils::GetDataDir()
{
bool bPortable = false;
wstring appname = CAppUtils::GetAppName();
std::transform(appname.begin(), appname.end(), appname.begin(), std::tolower);
if ((appname.find(_T("portable")) != wstring::npos) ||
(appname.find(_T("local")) != wstring::npos))
bPortable = true;
if (bPortable)
{
return CAppUtils::GetAppDirectory();
}
return CAppUtils::GetAppDataDir();
}
wstring CAppUtils::GetAppDataDir()
{
WCHAR path[MAX_PATH];
SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);
PathAppend(path, _T("CommitMonitor"));
if (!PathFileExists(path))
CreateDirectory(path, NULL);
return wstring(path);
}
wstring CAppUtils::GetAppDirectory(HMODULE hMod /* = NULL */)
{
TCHAR pathbuf[MAX_PATH] = {0};
wstring path;
DWORD bufferlen = MAX_PATH;
GetModuleFileName(hMod, pathbuf, bufferlen);
path = pathbuf;
path = path.substr(0, path.find_last_of('\\'));
return path;
}
wstring CAppUtils::GetAppName(HMODULE hMod /* = NULL */)
{
TCHAR pathbuf[MAX_PATH] = {0};
wstring path;
DWORD bufferlen = MAX_PATH;
GetModuleFileName(hMod, pathbuf, bufferlen);
path = pathbuf;
path = path.substr(path.find_last_of('\\')+1);
return path;
}
/* Number of micro-seconds between the beginning of the Windows epoch
* (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970)
*/
#define APR_DELTA_EPOCH_IN_USEC APR_TIME_C(11644473600000000);
__inline void AprTimeToFileTime(LPFILETIME pft, apr_time_t t)
{
LONGLONG ll;
t += APR_DELTA_EPOCH_IN_USEC;
ll = t * 10;
pft->dwLowDateTime = (DWORD)ll;
pft->dwHighDateTime = (DWORD) (ll >> 32);
return;
}
wstring CAppUtils::ConvertDate(apr_time_t time)
{
FILETIME ft = {0};
AprTimeToFileTime(&ft, time);
// Convert UTC to local time
SYSTEMTIME systemtime;
FileTimeToSystemTime(&ft,&systemtime);
SYSTEMTIME localsystime;
SystemTimeToTzSpecificLocalTime(NULL, &systemtime,&localsystime);
TCHAR timebuf[1024] = {0};
TCHAR datebuf[1024] = {0};
LCID locale = MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT);
GetDateFormat(locale, DATE_SHORTDATE, &localsystime, NULL, datebuf, 1024);
GetTimeFormat(locale, 0, &localsystime, NULL, timebuf, 1024);
wstring sRet = datebuf;
sRet += _T(" ");
sRet += timebuf;
return sRet;
}
void CAppUtils::SearchReplace(wstring& str, const wstring& toreplace, const wstring& replacewith)
{
wstring result;
wstring::size_type pos = 0;
for ( ; ; ) // while (true)
{
wstring::size_type next = str.find(toreplace, pos);
result.append(str, pos, next-pos);
if( next != std::string::npos )
{
result.append(replacewith);
pos = next + toreplace.size();
}
else
{
break; // exit loop
}
}
str.swap(result);
}
vector<wstring> CAppUtils::tokenize_str(const wstring& str, const wstring& delims)
{
// Skip delims at beginning, find start of first token
wstring::size_type lastPos = str.find_first_not_of(delims, 0);
// Find next delimiter @ end of token
wstring::size_type pos = str.find_first_of(delims, lastPos);
// output vector
vector<wstring> tokens;
while (wstring::npos != pos || wstring::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delims. Note the "not_of". this is beginning of token
lastPos = str.find_first_not_of(delims, pos);
// Find next delimiter at end of token.
pos = str.find_first_of(delims, lastPos);
}
return tokens;
}
bool CAppUtils::LaunchApplication(const wstring& sCommandLine, bool bWaitForStartup)
{
STARTUPINFO startup;
PROCESS_INFORMATION process;
memset(&startup, 0, sizeof(startup));
startup.cb = sizeof(startup);
memset(&process, 0, sizeof(process));
TCHAR * cmdbuf = new TCHAR[sCommandLine.length()+1];
_tcscpy_s(cmdbuf, sCommandLine.length()+1, sCommandLine.c_str());
if (CreateProcess(NULL, cmdbuf, NULL, NULL, FALSE, 0, 0, 0, &startup, &process)==0)
{
delete [] cmdbuf;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
::MessageBox(NULL, (LPCWSTR)lpMsgBuf, _T("CommitMonitor"), MB_ICONERROR|MB_TASKMODAL);
LocalFree(lpMsgBuf);
return false;
}
delete [] cmdbuf;
if (bWaitForStartup)
{
WaitForInputIdle(process.hProcess, 10000);
}
CloseHandle(process.hThread);
CloseHandle(process.hProcess);
return true;
}
wstring CAppUtils::GetTempFilePath()
{
DWORD len = ::GetTempPath(0, NULL);
TCHAR * temppath = new TCHAR[len+1];
TCHAR * tempF = new TCHAR[len+50];
::GetTempPath (len+1, temppath);
wstring tempfile;
::GetTempFileName (temppath, TEXT("cm_"), 0, tempF);
tempfile = wstring(tempF);
//now create the tempfile, so that subsequent calls to GetTempFile() return
//different filenames.
HANDLE hFile = CreateFile(tempfile.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL);
CloseHandle(hFile);
delete temppath;
delete tempF;
return tempfile;
}
wstring CAppUtils::ConvertName(const wstring& name)
{
TCHAR convertedName[4096] = {0};
_tcscpy_s(convertedName, 4096, name.c_str());
int cI = 0;
while (convertedName[cI])
{
switch (convertedName[cI])
{
case '/':
case '\\':
case '?':
case ':':
case '*':
case '.':
case '<':
case '>':
case '\"':
case '|':
convertedName[cI] = '_';
}
cI++;
}
return wstring(convertedName);
}
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
bool CAppUtils::IsWow64()
{
BOOL bIsWow64 = false;
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
if (NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
{
// handle error
}
}
return !!bIsWow64;
}
wstring CAppUtils::GetTSVNPath()
{
wstring sRet;
CRegStdString tsvninstalled = CRegStdString(_T("Software\\TortoiseSVN\\ProcPath"), _T(""), false, HKEY_LOCAL_MACHINE);
sRet = wstring(tsvninstalled);
if (sRet.empty())
{
if (IsWow64())
{
CRegStdString tsvninstalled64 = CRegStdString(_T("Software\\TortoiseSVN\\ProcPath"), _T(""), false, HKEY_LOCAL_MACHINE, KEY_WOW64_64KEY);
sRet = wstring(tsvninstalled64);
}
}
return sRet;
}
wstring CAppUtils::GetVersionStringFromExe(LPCTSTR path)
{
struct TRANSARRAY
{
WORD wLanguageID;
WORD wCharacterSet;
};
wstring sVersion;
DWORD dwReserved,dwBufferSize;
dwBufferSize = GetFileVersionInfoSize((LPTSTR)path,&dwReserved);
if (dwBufferSize > 0)
{
LPVOID pBuffer = (void*) malloc(dwBufferSize);
if (pBuffer != (void*) NULL)
{
UINT nInfoSize = 0,
nFixedLength = 0;
LPSTR lpVersion = NULL;
VOID* lpFixedPointer;
TRANSARRAY* lpTransArray;
TCHAR strLangProduktVersion[MAX_PATH];
GetFileVersionInfo((LPTSTR)path,
dwReserved,
dwBufferSize,
pBuffer);
VerQueryValue( pBuffer,
_T("\\VarFileInfo\\Translation"),
&lpFixedPointer,
&nFixedLength);
lpTransArray = (TRANSARRAY*) lpFixedPointer;
_stprintf_s(strLangProduktVersion, MAX_PATH,
_T("\\StringFileInfo\\%04x%04x\\ProductVersion"),
lpTransArray[0].wLanguageID,
lpTransArray[0].wCharacterSet);
VerQueryValue(pBuffer,
(LPTSTR)strLangProduktVersion,
(LPVOID *)&lpVersion,
&nInfoSize);
sVersion = (LPCTSTR)lpVersion;
free(pBuffer);
}
}
return sVersion;
}
bool CAppUtils::ExtractBinResource(const wstring& strCustomResName, int nResourceId, const wstring& strOutputPath)
{
HGLOBAL hResourceLoaded; // handle to loaded resource
HRSRC hRes; // handle/ptr. to res. info.
char *lpResLock; // pointer to resource data
DWORD dwSizeRes;
wstring strAppLocation;
strAppLocation = CAppUtils::GetAppDirectory();
// find location of the resource and get handle to it
hRes = FindResource(NULL, MAKEINTRESOURCE(nResourceId), strCustomResName.c_str());
if (hRes == NULL)
return false;
// loads the specified resource into global memory.
hResourceLoaded = LoadResource(NULL, hRes);
if (hResourceLoaded == NULL)
return false;
// get a pointer to the loaded resource!
lpResLock = (char*)LockResource(hResourceLoaded);
if (lpResLock == NULL)
return false;
// determine the size of the resource, so we know how much to write out to file!
dwSizeRes = SizeofResource(NULL, hRes);
try
{
std::ofstream outputFile(strOutputPath.c_str(), std::ios::binary);
outputFile.write((const char*)lpResLock, dwSizeRes);
outputFile.close();
}
catch (exception e)
{
return false;
}
return true;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.