CrashServer: ship new library in installer
[TortoiseGit.git] / src / crashrpt / Utility.cpp
blob49b85635d843e8dc01e9454d731304c3ea1f0fd5
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Module: Utility.cpp
4 //
5 // Desc: See Utility.h
6 //
7 // Copyright (c) 2007 TortoiseSVN
8 // Copyright (c) 2003 Michael Carruth
9 //
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "stdafx.h"
13 #include "Utility.h"
14 #include "resource.h"
16 namespace CUtility {
18 BSTR CUtility::AllocSysString(string s)
20 #if defined(_UNICODE) || defined(OLE2ANSI)
21 BSTR bstr = ::SysAllocStringLen(s.c_str(), s.size());
22 #else
23 int nLen = MultiByteToWideChar(CP_ACP, 0, s.c_str(),
24 s.size(), NULL, NULL);
25 BSTR bstr = ::SysAllocStringLen(NULL, nLen);
26 if(bstr != NULL)
27 MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size(), bstr, nLen);
28 #endif
29 return bstr;
32 FILETIME CUtility::getLastWriteFileTime(string sFile)
34 FILETIME ftLocal = {0};
35 HANDLE hFind;
36 WIN32_FIND_DATA ff32;
37 hFind = FindFirstFile(sFile.c_str(), &ff32);
38 if (INVALID_HANDLE_VALUE != hFind)
40 FileTimeToLocalFileTime(&(ff32.ftLastWriteTime), &ftLocal);
41 FindClose(hFind);
43 return ftLocal;
46 FILETIME CUtility::getLastWriteFileTime(WCHAR * wszFile)
48 FILETIME ftLocal = {0};
49 HANDLE hFind;
50 WIN32_FIND_DATAW ff32;
51 hFind = FindFirstFileW(wszFile, &ff32);
52 if (INVALID_HANDLE_VALUE != hFind)
54 FileTimeToLocalFileTime(&(ff32.ftLastWriteTime), &ftLocal);
55 FindClose(hFind);
57 return ftLocal;
60 string CUtility::getAppName()
62 TCHAR szFileName[_MAX_PATH];
63 GetModuleFileName(NULL, szFileName, _MAX_FNAME);
65 string sAppName; // Extract from last '\' to '.'
67 *_tcsrchr(szFileName, '.') = '\0';
68 sAppName = (_tcsrchr(szFileName, '\\')+1);
70 return sAppName;
74 string CUtility::getTempFileName()
76 static int counter = 0;
77 TCHAR szTempDir[MAX_PATH - 14] = _T("");
78 TCHAR szTempFile[MAX_PATH] = _T("");
80 if (GetTempPath(MAX_PATH - 14, szTempDir))
81 GetTempFileName(szTempDir, getAppName().c_str(), ++counter, szTempFile);
83 return szTempFile;
87 string CUtility::getSaveFileName()
89 string sFilter = _T("Zip Files (*.zip)");
91 OPENFILENAME ofn = {0}; // common dialog box structure
92 TCHAR szFile[MAX_PATH] = {0}; // buffer for file name
93 // Initialize OPENFILENAME
94 ofn.lStructSize = sizeof(OPENFILENAME);
95 ofn.hwndOwner = NULL;
96 ofn.lpstrFile = szFile;
97 ofn.nMaxFile = _countof(szFile);
98 ofn.Flags = OFN_OVERWRITEPROMPT;
100 ofn.lpstrFilter = sFilter.c_str();
101 ofn.nFilterIndex = 1;
102 // Display the Open dialog box.
103 bool bTargetSelected = !!GetSaveFileName(&ofn);
105 DeleteFile(ofn.lpstrFile); // Just in-case it already exist
106 return ofn.lpstrFile;