OSDN Git Service

Leave the class name as CIniOptionsMgr, but rename the filename to IniOptionsMgr.*
[winmerge-jp/winmerge-jp.git] / Src / Environment.cpp
index 13aadcf..fcbfeab 100644 (file)
@@ -4,10 +4,14 @@
  * @brief Environment related routines.
  */
 
+#include "pch.h"
 #define POCO_NO_UNWINDOWS 1
 #include "Environment.h"
 #include <windows.h>
+#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 <shlobj.h>
+#pragma warning (pop)
 #include <sstream>
 #include <Poco/Path.h>
 #include <Poco/Process.h>
@@ -38,7 +42,7 @@ void SetTemporaryPath(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 GetTemporaryPath()
 {
@@ -60,18 +64,23 @@ String GetTemporaryPath()
  * @param [out] pnerr Error code if error happened.
  * @return Full path for temporary file or empty string if error happened.
  */
-String GetTemporaryFileName(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)
+       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;
 }
@@ -81,7 +90,7 @@ String GetTempChildPath()
        String path;
        do
        {
-               path = paths::ConcatPath(GetTemporaryPath(), string_format(_T("%08x"), rand()));
+               path = paths::ConcatPath(GetTemporaryPath(), strutils::format(_T("%08x"), rand()));
        } while (paths::IsDirectory(path) || !paths::CreateIfNeeded(path));
        return path;
 }
@@ -96,7 +105,7 @@ String GetProgPath()
        if (strProgPath.empty())
        {
                TCHAR temp[MAX_PATH] = {0};
-               GetModuleFileName(NULL, temp, MAX_PATH);
+               GetModuleFileName(nullptr, temp, MAX_PATH);
                strProgPath = paths::GetPathOnly(temp);
        }
        return strProgPath;
@@ -123,7 +132,7 @@ String GetMyDocuments()
 {
        TCHAR path[MAX_PATH];
        path[0] = _T('\0');
-       SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, path);
+       SHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, 0, path);
        return path;
 }
 
@@ -163,8 +172,8 @@ static bool launchProgram(const String& sCmd, WORD wShowWindow)
        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;