#include <windows.h>
#include <tlhelp32.h>
#include <shlwapi.h>
+#include <algorithm>
#include "paths.h"
#include "Environment.h"
#include "Constants.h"
using std::vector;
+static bool CleanupWMtempfolder(const vector <int>& processIDs);
+static bool WMrunning(const vector<int>& processIDs, int iPI);
+
/**
* @brief Delete the temp file when instance is deleted.
*/
Delete();
}
-/**
- * @brief Create a temporary file name with default prefix.
- */
-void TempFile::Create()
-{
- Create(NULL);
-}
/**
* @brief Create a temporary file with given prefix.
* @param [in] ext extension for temp file name.
* @return Created temp file path.
*/
-String TempFile::Create(LPCTSTR prefix, LPCTSTR ext)
+String TempFile::Create(const String& prefix, const String& ext)
{
String temp = env_GetTempPath();
if (temp.empty())
{
- return TEXT("");
+ return _T("");
}
String pref = prefix;
if (pref.empty())
- pref = TEXT("wmtmp");
+ pref = _T("wmtmp");
temp = env_GetTempFileName(temp, pref, NULL);
if (!temp.empty())
{
- if (ext)
+ if (!ext.empty())
{
String tempext = temp + ext;
if (MoveFile(temp.c_str(), tempext.c_str()))
* @param [in] prefix Prefix for the temporary filename.
* @return Full path to the temporary file.
*/
-String TempFile::CreateFromFile(LPCTSTR filepath, LPCTSTR prefix)
+String TempFile::CreateFromFile(const String& filepath, const String& prefix)
{
String temp = env_GetTempPath();
if (temp.empty())
{
- return TEXT("");
+ return _T("");
}
String pref = prefix;
if (pref.empty())
- pref = TEXT("wmtmp");
+ pref = _T("wmtmp");
temp = env_GetTempFileName(temp, pref, NULL);
if (!temp.empty())
{
// Scratchpads don't have a file to copy.
m_path = temp;
- if (::CopyFile(filepath, temp.c_str(), FALSE))
+ if (::CopyFile(filepath.c_str(), temp.c_str(), FALSE))
{
::SetFileAttributes(temp.c_str(), FILE_ATTRIBUTE_NORMAL);
}
}
/**
- * @brief Get temp file path (including filename).
- * @return Full path to temp file.
- */
-String TempFile::GetPath()
-{
- return m_path;
-}
-
-/**
* @brief Delete the temporary file, if it exists.
* @return true if there was no error.
*/
if (!m_path.empty())
success = !!DeleteFile(m_path.c_str());
if (success)
- m_path = TEXT("");
+ m_path = _T("");
return !!success;
}
/**
* @param [in] processIDs List of process IDs.
* @return true if all temp folders were deleted, FALSE otherwise.
*/
-bool CleanupWMtempfolder(vector <int> processIDs)
+static bool CleanupWMtempfolder(const vector <int>& processIDs)
{
String foldername;
String tempfolderPID;
* @param [in] iPI ProcessID to check.
* @return true if processID was found from the list, FALSE otherwise.
*/
-bool WMrunning(vector<int> processIDs, int iPI)
+static bool WMrunning(const vector<int>& processIDs, int iPI)
{
- vector<int>::iterator iter = processIDs.begin();
- while (iter != processIDs.end())
- {
- if (*iter == iPI)
- return true;
- ++iter;
- }
- return FALSE;
+ return std::find(processIDs.begin(), processIDs.end(), iPI) != processIDs.end();
}
/**
public:
TempFile() {}
~TempFile();
- void Create();
- String Create(const TCHAR *prefix = NULL, const TCHAR *ext = NULL);
- String CreateFromFile(const TCHAR *filepath, const TCHAR *prefix);
- String GetPath();
+ String Create(const String& prefix = _T(""), const String& ext = _T(""));
+ String CreateFromFile(const String& filepath, const String& prefix);
+ /**
+ * @brief Get temp file path (including filename).
+ * @return Full path to temp file.
+ */
+ const String& GetPath() const { return m_path; }
bool Delete();
private:
};
void CleanupWMtemp();
-bool CleanupWMtempfolder(std::vector <int> processIDs);
-bool WMrunning(std::vector <int> processIDs, int iPI);
bool ClearTempfolder(const String &pathName);
#endif // _TEMP_FILE_