X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=Src%2FEnvironment.cpp;h=fcbfeabbcba80578370fd8cba7fc7cb62dbd5b80;hb=40c7433ddcc7ada26335a0f66470636b5d102e9f;hp=b96a3e479b1cfd32aedbdd8269ac8fbcbef19355;hpb=056b6c35e3bf3b59f62f967e0edf903c5580617f;p=winmerge-jp%2Fwinmerge-jp.git diff --git a/Src/Environment.cpp b/Src/Environment.cpp index b96a3e479..fcbfeabbc 100644 --- a/Src/Environment.cpp +++ b/Src/Environment.cpp @@ -3,14 +3,15 @@ * * @brief Environment related routines. */ -// ID line follows -- this is updated by SVN -// $Id$ +#include "pch.h" #define POCO_NO_UNWINDOWS 1 #include "Environment.h" #include +#pragma warning (push) // prevent "warning C4091: 'typedef ': ignored on left of 'tagGPFIDL_FLAGS' when no variable is declared" +#pragma warning (disable:4091) // VC bug when using XP enabled toolsets. #include -#include +#pragma warning (pop) #include #include #include @@ -20,17 +21,20 @@ using Poco::Path; using Poco::Process; +namespace env +{ + /** * @brief Temp path. - * Static string used by env_GetTempPath() for storing temp path. + * Static string used by GetTemporaryPath() for storing temp path. */ static String strTempPath; static String strProgPath; -void env_SetTempPath(const String& path) +void SetTemporaryPath(const String& path) { - strTempPath = paths_AddTrailingSlash(path); - paths_CreateIfNeeded(strTempPath); + strTempPath = paths::AddTrailingSlash(paths::GetLongPath(path)); + paths::CreateIfNeeded(strTempPath); } /** @@ -38,17 +42,17 @@ void env_SetTempPath(const String& path) * This function returns system temp folder. * @return Temp path, or empty string if error happened. * @note Temp path is cached after first call. - * @todo Should we return NULL for error case? + * @todo Should we return `nullptr` for error case? */ -String env_GetTempPath() +String GetTemporaryPath() { if (strTempPath.empty()) { - strTempPath = env_GetSystemTempPath(); + strTempPath = GetSystemTempPath(); if (strTempPath.empty()) return strTempPath; - paths_CreateIfNeeded(strTempPath); + paths::CreateIfNeeded(strTempPath); } return strTempPath; } @@ -60,44 +64,49 @@ String env_GetTempPath() * @param [out] pnerr Error code if error happened. * @return Full path for temporary file or empty string if error happened. */ -String env_GetTempFileName(const String& lpPathName, const String& lpPrefixString, int * pnerr) +String GetTemporaryFileName(const String& lpPathName, const String& lpPrefixString, int * pnerr /*= nullptr*/) { TCHAR buffer[MAX_PATH] = {0}; if (lpPathName.length() > MAX_PATH-14) return _T(""); // failure - int rtn = GetTempFileName(lpPathName.c_str(), lpPrefixString.c_str(), 0, buffer); - if (!rtn) + int rtn = ::GetTempFileName(lpPathName.c_str(), lpPrefixString.c_str(), 0, buffer); + if (rtn == 0) { - int err = GetLastError(); - if (pnerr) - *pnerr = err; - return _T(""); + paths::CreateIfNeeded(lpPathName); + rtn = ::GetTempFileName(lpPathName.c_str(), lpPrefixString.c_str(), 0, buffer); + if (rtn == 0) + { + int err = GetLastError(); + if (pnerr != nullptr) + *pnerr = err; + return _T(""); + } } return buffer; } -String env_GetTempChildPath() +String GetTempChildPath() { String path; do { - path = paths_ConcatPath(env_GetTempPath(), string_format(_T("%08x"), rand())); - } while (paths_IsDirectory(path) || !paths_CreateIfNeeded(path)); + path = paths::ConcatPath(GetTemporaryPath(), strutils::format(_T("%08x"), rand())); + } while (paths::IsDirectory(path) || !paths::CreateIfNeeded(path)); return path; } -void env_SetProgPath(const String& path) +void SetProgPath(const String& path) { - strProgPath = paths_AddTrailingSlash(path); + strProgPath = paths::AddTrailingSlash(path); } -String env_GetProgPath() +String GetProgPath() { if (strProgPath.empty()) { TCHAR temp[MAX_PATH] = {0}; - GetModuleFileName(NULL, temp, MAX_PATH); - strProgPath = paths_GetPathOnly(temp); + GetModuleFileName(nullptr, temp, MAX_PATH); + strProgPath = paths::GetPathOnly(temp); } return strProgPath; } @@ -106,11 +115,12 @@ String env_GetProgPath() * @brief Get Windows directory. * @return Windows directory. */ -String env_GetWindowsDirectory() +String GetWindowsDirectory() { - TCHAR buf[MAX_PATH] = {0}; - GetWindowsDirectory(buf, MAX_PATH); - return buf; + TCHAR path[MAX_PATH]; + path[0] = _T('\0'); + ::GetWindowsDirectory(path, MAX_PATH); + return path; } /** @@ -118,25 +128,11 @@ String env_GetWindowsDirectory() * This function returns full path to User's My Documents -folder. * @return Full path to My Documents -folder. */ -String env_GetMyDocuments() +String GetMyDocuments() { - LPITEMIDLIST pidl; - LPMALLOC pMalloc; - String path; - - HRESULT rv = SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl); - if (rv == S_OK) - { - TCHAR szPath[MAX_PATH] = {0}; - if (SHGetPathFromIDList(pidl, szPath)) - { - path = szPath; - } - - SHGetMalloc(&pMalloc); - pMalloc->Free(pidl); - pMalloc->Release(); - } + TCHAR path[MAX_PATH]; + path[0] = _T('\0'); + SHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, 0, path); return path; } @@ -147,7 +143,7 @@ String env_GetMyDocuments() * @param [in] name Additional name used as part of the string. * @return Unique string for the instance. */ -String env_GetPerInstanceString(const String& name) +String GetPerInstanceString(const String& name) { std::basic_stringstream stream; stream << name << Process::id(); @@ -158,7 +154,7 @@ String env_GetPerInstanceString(const String& name) * @brief Get system temporary directory. * @return System temporary director. */ -String env_GetSystemTempPath() +String GetSystemTempPath() { try { @@ -172,13 +168,12 @@ String env_GetSystemTempPath() static bool launchProgram(const String& sCmd, WORD wShowWindow) { - STARTUPINFO stInfo = {0}; - stInfo.cb = sizeof(STARTUPINFO); + STARTUPINFO stInfo = { sizeof(STARTUPINFO) }; stInfo.dwFlags = STARTF_USESHOWWINDOW; stInfo.wShowWindow = wShowWindow; PROCESS_INFORMATION processInfo; - BOOL retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(), - NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, + bool retVal = !!CreateProcess(nullptr, (LPTSTR)sCmd.c_str(), + nullptr, nullptr, FALSE, CREATE_DEFAULT_ERROR_MODE, nullptr, nullptr, &stInfo, &processInfo); if (!retVal) return false; @@ -190,9 +185,9 @@ static bool launchProgram(const String& sCmd, WORD wShowWindow) /** * @brief Load registry keys from .reg file if existing .reg file */ -bool env_LoadRegistryFromFile(const String& sRegFilePath) +bool LoadRegistryFromFile(const String& sRegFilePath) { - if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE) + if (paths::DoesPathExist(sRegFilePath) != paths::IS_EXISTING_FILE) return false; return launchProgram(_T("reg.exe import \"") + sRegFilePath + _T("\""), SW_HIDE); } @@ -200,10 +195,12 @@ bool env_LoadRegistryFromFile(const String& sRegFilePath) /** * @brief Save registry keys to .reg file if existing .reg file */ -bool env_SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir) +bool SaveRegistryToFile(const String& sRegFilePath, const String& sRegDir) { - if (paths_DoesPathExist(sRegFilePath) != IS_EXISTING_FILE) + if (paths::DoesPathExist(sRegFilePath) != paths::IS_EXISTING_FILE) return false; DeleteFile(sRegFilePath.c_str()); return launchProgram(_T("reg.exe export HKCU\\") + sRegDir + _T(" \"") + sRegFilePath + _T("\""), SW_HIDE); } + +}