From 35524f9a03209ed654302107e8ebabc28476057e Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Fri, 4 Jan 2008 15:55:30 +0000 Subject: [PATCH] PATCH: [ 1863305 ] Move temp path functions to own files --- Src/7zCommon.cpp | 5 +-- Src/Common/LogFile.cpp | 5 +-- Src/Common/multiformatText.cpp | 5 +-- Src/ConfigLog.cpp | 4 +-- Src/DiffWrapper.cpp | 3 +- Src/Environment.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++ Src/Environment.h | 17 +++++++++++ Src/FileTransform.cpp | 11 ++++--- Src/Merge.dsp | 11 +++++++ Src/PathContext.cpp | 5 +-- Src/VSSHelper.cpp | 5 +-- Src/paths.cpp | 58 ----------------------------------- Src/paths.h | 4 +-- 13 files changed, 123 insertions(+), 79 deletions(-) create mode 100644 Src/Environment.cpp create mode 100644 Src/Environment.h diff --git a/Src/7zCommon.cpp b/Src/7zCommon.cpp index 02bd23432..956164cea 100644 --- a/Src/7zCommon.cpp +++ b/Src/7zCommon.cpp @@ -91,7 +91,7 @@ DATE: BY: DESCRIPTION: Change recommended version of 7-Zip to 4.57 */ -// RCS ID line follows -- this is updated by CVS +// ID line follows -- this is updated by SVN // $Id$ #include "stdafx.h" @@ -106,6 +106,7 @@ DATE: BY: DESCRIPTION: #include #include #include +#include "Environment.h" #ifdef _DEBUG #define new DEBUG_NEW @@ -604,7 +605,7 @@ CString NTAPI GetClearTempPath(LPVOID pOwner, LPCTSTR pchExt) strPath.Format ( pOwner ? _T("%sWINMERGE.%08lX\\%08lX.%s") : _T("%sWINMERGE.%08lX"), - paths_GetTempPath(), GetCurrentProcessId(), pOwner, pchExt + env_GetTempPath(), GetCurrentProcessId(), pOwner, pchExt ); // SHFileOperation expects a ZZ terminated list of paths! int cchPath = strPath.GetLength(); diff --git a/Src/Common/LogFile.cpp b/Src/Common/LogFile.cpp index ca6027c85..3f61b75bd 100644 --- a/Src/Common/LogFile.cpp +++ b/Src/Common/LogFile.cpp @@ -12,6 +12,7 @@ #include #include "UnicodeString.h" #include "paths.h" +#include "Environment.h" #ifdef _DEBUG #undef THIS_FILE @@ -68,7 +69,7 @@ CString CLogFile::SetFile(const CString & strFile, const CString & strPath, m_strLogPath = strFile; else { - String temp = paths_GetTempPath(); + String temp = env_GetTempPath(); m_strLogPath = temp.c_str(); } @@ -281,7 +282,7 @@ void CLogFile::Prune(FILE *f) TCHAR buf[8196] = {0}; DWORD amt; FILE *tf; - String tempfile = paths_GetTempFileName(_T("."), _T("LOG")); + String tempfile = env_GetTempFileName(_T("."), _T("LOG")); DeleteFile(tempfile.c_str()); if ((tf = _tfopen(tempfile.c_str(), _T("w"))) != NULL) { diff --git a/Src/Common/multiformatText.cpp b/Src/Common/multiformatText.cpp index 0f67d94ee..f8e9171b4 100644 --- a/Src/Common/multiformatText.cpp +++ b/Src/Common/multiformatText.cpp @@ -25,7 +25,7 @@ * * @date Created: 2003-11-24 */ -// RCS ID line follows -- this is updated by CVS +// ID line follows -- this is updated by SVN // $Id$ #include "StdAfx.h" @@ -35,6 +35,7 @@ #include "paths.h" #include "UniFile.h" #include "codepage.h" +#include "Environment.h" #ifdef _DEBUG #define new DEBUG_NEW @@ -95,7 +96,7 @@ LPCTSTR storageForPlugins::GetDestFileName() { if (m_tempFilenameDst.empty()) { - m_tempFilenameDst = paths_GetTempFileName(paths_GetTempPath(), _T ("_WM")); + m_tempFilenameDst = env_GetTempFileName(env_GetTempPath(), _T ("_WM")); } return m_tempFilenameDst.c_str(); } diff --git a/Src/ConfigLog.cpp b/Src/ConfigLog.cpp index bd14575fb..f37479ce4 100644 --- a/Src/ConfigLog.cpp +++ b/Src/ConfigLog.cpp @@ -37,7 +37,7 @@ #include "codepage.h" #include "7zcommon.h" #include "CompareOptions.h" - +#include "Environment.h" // Static function declarations static bool LoadYesNoFromConfig(CfgSettings * cfgSettings, LPCTSTR name, BOOL * pbflag); @@ -376,7 +376,7 @@ BOOL CConfigLog::DoFile(bool writing, CString &sError) m_sFileName = _T("WinMerge.txt"); // Get path to $temp/WinMerge.txt - m_sFileName.Insert(0, paths_GetTempPath()); + m_sFileName.Insert(0, env_GetTempPath()); if (!m_file.Open(m_sFileName, CFile::modeCreate | CFile::modeWrite)) { diff --git a/Src/DiffWrapper.cpp b/Src/DiffWrapper.cpp index eec1f09ec..10b471aa6 100644 --- a/Src/DiffWrapper.cpp +++ b/Src/DiffWrapper.cpp @@ -43,6 +43,7 @@ #include "FileTextStats.h" #include "FolderCmp.h" #include "FilterCommentsManager.h" +#include "Environment.h" #ifdef _DEBUG #define new DEBUG_NEW @@ -738,7 +739,7 @@ BOOL CDiffWrapper::RunFileDiff() // what differences diff-engine sees! #ifdef _DEBUG // throw the diff into a temp file - String sTempPath = paths_GetTempPath(); // get path to Temp folder + String sTempPath = env_GetTempPath(); // get path to Temp folder String path = paths_ConcatPath(sTempPath, _T("Diff.txt")); outfile = _tfopen(path.c_str(), _T("w+")); diff --git a/Src/Environment.cpp b/Src/Environment.cpp new file mode 100644 index 000000000..d6f597e7d --- /dev/null +++ b/Src/Environment.cpp @@ -0,0 +1,69 @@ +/** + * @file Environment.cpp + * + * @brief Environment related routines. + */ +// ID line follows -- this is updated by SVN +// $Id$ + +#include "stdafx.h" +#include "paths.h" +#include "Environment.h" + +// Static string used by paths_GetTempPath() for storing temp path. +static String strTempPath; + +/** + * @brief Get folder for temporary files. + * This function returns system temp folder. + * @param [out] pnerr Error code if erorr happened. + * @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? + */ +LPCTSTR env_GetTempPath(int * pnerr) +{ + if (strTempPath.empty()) + { + int cchTempPath = GetTempPath(0, 0); + strTempPath.resize(cchTempPath - 1); + if (!GetTempPath(cchTempPath, &*strTempPath.begin())) + { + int err = GetLastError(); + if (pnerr) + *pnerr = err; +#ifdef _DEBUG + CString sysErr = GetSysError(err); // for debugging +#endif + return strTempPath.c_str(); // empty + } + strTempPath = paths_GetLongPath(strTempPath.c_str()); + } + return strTempPath.c_str(); +} + +/** + * @brief Get filename for temporary file. + * @param [in] lpPathName Temporary file folder. + * @param [in] lpPrefixString Prefix to use for filename. + * @param [out] pnerr Error code if error happened. + * @return Full path for temporary file or empty string if error happened. + */ +String env_GetTempFileName(LPCTSTR lpPathName, LPCTSTR lpPrefixString, int * pnerr) +{ + TCHAR buffer[MAX_PATH] = {0}; + if (_tcslen(lpPathName) > MAX_PATH-14) + return _T(""); // failure + int rtn = GetTempFileName(lpPathName, lpPrefixString, 0, buffer); + if (!rtn) + { + int err = GetLastError(); + if (pnerr) + *pnerr = err; +#ifdef _DEBUG + CString sysErr = GetSysError(err); // for debugging +#endif + return _T(""); + } + return buffer; +} diff --git a/Src/Environment.h b/Src/Environment.h new file mode 100644 index 000000000..fb5a95f03 --- /dev/null +++ b/Src/Environment.h @@ -0,0 +1,17 @@ +/** + * @file Environment.h + * + * @brief Declaration file for Environment-related routines. + */ +// ID line follows -- this is updated by SVN +// $Id$ + +#ifndef _ENVIRONMENT_H_ +#define _ENVIRONMENT_H_ + +LPCTSTR env_GetTempPath(int * pnerr = NULL); +String env_GetTempFileName(LPCTSTR lpPathName, LPCTSTR lpPrefixString, + int * pnerr = NULL); + + +#endif // _ENVIRONMENT_H_ diff --git a/Src/FileTransform.cpp b/Src/FileTransform.cpp index 0f0fd48a7..8dfe31a99 100644 --- a/Src/FileTransform.cpp +++ b/Src/FileTransform.cpp @@ -23,7 +23,7 @@ * * @brief Implementation of file transformations */ -// RCS ID line follows -- this is updated by CVS +// ID line follows -- this is updated by SVN // $Id$ #include "StdAfx.h" @@ -33,6 +33,7 @@ #include "paths.h" #include "multiformatText.h" #include "UniMarkdownFile.h" +#include "Environment.h" #ifdef _DEBUG #define new DEBUG_NEW @@ -428,10 +429,10 @@ BOOL FileTransform_Prediffing(String & filepath, LPCTSTR filteredText, Prediffin BOOL FileTransform_NormalizeUnicode(String & filepath, BOOL bMayOverwrite) { - String tempDir = paths_GetTempPath(); + String tempDir = env_GetTempPath(); if (tempDir.empty()) return FALSE; - String tempFilepath = paths_GetTempFileName(tempDir.c_str(), _T("_WM")); + String tempFilepath = env_GetTempFileName(tempDir.c_str(), _T("_WM")); if (tempFilepath.empty()) return FALSE; @@ -473,10 +474,10 @@ BOOL FileTransform_NormalizeUnicode(String & filepath, BOOL bMayOverwrite) // TODO : convert Ansi to UTF8 if other file is unicode or uses a different codepage BOOL FileTransform_UCS2ToUTF8(String & filepath, BOOL bMayOverwrite) { - String tempDir = paths_GetTempPath(); + String tempDir = env_GetTempPath(); if (tempDir.empty()) return FALSE; - String tempFilepath = paths_GetTempFileName(tempDir.c_str(), _T("_WM")); + String tempFilepath = env_GetTempFileName(tempDir.c_str(), _T("_WM")); if (tempFilepath.empty()) return FALSE; diff --git a/Src/Merge.dsp b/Src/Merge.dsp index d6ca32c4d..222b9d897 100644 --- a/Src/Merge.dsp +++ b/Src/Merge.dsp @@ -443,6 +443,10 @@ SOURCE=.\EditorFilepathBar.cpp # End Source File # Begin Source File +SOURCE=.\Environment.cpp +# End Source File +# Begin Source File + SOURCE=.\FileActionScript.cpp # End Source File # Begin Source File @@ -1209,6 +1213,10 @@ SOURCE=.\EditorFilepathBar.h # End Source File # Begin Source File +SOURCE=.\Environment.h +# End Source File +# Begin Source File + SOURCE=.\Exceptions.h # End Source File # Begin Source File @@ -2252,6 +2260,7 @@ SOURCE=.\diffutils\src\CONTEXT.C # Begin Source File SOURCE=.\Diff.cpp + !IF "$(CFG)" == "Merge - Win32 Debug" # SUBTRACT CPP /Yc /Yu @@ -2275,6 +2284,8 @@ SOURCE=.\Diff.cpp # ADD CPP /O2 # SUBTRACT CPP /Yc /Yu +!ENDIF + # End Source File # Begin Source File diff --git a/Src/PathContext.cpp b/Src/PathContext.cpp index e6fc2689b..274368f62 100644 --- a/Src/PathContext.cpp +++ b/Src/PathContext.cpp @@ -4,12 +4,13 @@ * @brief Implementation file for PathInfo and PathContext * */ -// RCS ID line follows -- this is updated by CVS +// ID line follows -- this is updated by SVN // $Id$ #include "stdafx.h" #include "PathContext.h" #include "paths.h" +#include "Environment.h" /** * @brief Copy constructor. @@ -172,7 +173,7 @@ BOOL TempFileContext::CreateFiles(const PathContext &paths) if (GetLeft().empty()) { int nerr=0; - String sTempPath = paths_GetTempFileName(strTempPath, _T("_LT"), &nerr); + String sTempPath = env_GetTempFileName(strTempPath, _T("_LT"), &nerr); if (sTempPath.empty()) { LogErrorString(Fmt(_T("GetTempFileName() for left-side failed: %s"), diff --git a/Src/VSSHelper.cpp b/Src/VSSHelper.cpp index 782949c8d..e36692c86 100644 --- a/Src/VSSHelper.cpp +++ b/Src/VSSHelper.cpp @@ -28,6 +28,7 @@ #include "VSSHelper.h" #include "coretools.h" #include "paths.h" +#include "Environment.h" CString VSSHelper::GetProjectBase() { @@ -59,14 +60,14 @@ BOOL VSSHelper::ReLinkVCProj(CString strSavePath, CString * psError) BOOL bVCPROJ = FALSE; int nerr; - String tempPath = paths_GetTempPath(&nerr); + String tempPath = env_GetTempPath(&nerr); if (tempPath.empty()) { LogErrorString(Fmt(_T("CMainFrame::ReLinkVCProj() - couldn't get temppath: %s") , GetSysError(nerr))); return FALSE; } - String tempFile = paths_GetTempFileName(tempPath.c_str(), _T("_LT"), &nerr); + String tempFile = env_GetTempFileName(tempPath.c_str(), _T("_LT"), &nerr); if (tempFile.empty()) { LogErrorString(Fmt(_T("CMainFrame::ReLinkVCProj() - couldn't get tempfile: %s") diff --git a/Src/paths.cpp b/Src/paths.cpp index 63eec995f..a05cc646b 100644 --- a/Src/paths.cpp +++ b/Src/paths.cpp @@ -335,38 +335,6 @@ bool paths_CreateIfNeeded(LPCTSTR szPath) return true; } -// Static string used by paths_GetTempPath() for storing temp path. -static String strTempPath; - -/** - * @brief Get folder for temporary files. - * This function returns system temp folder. - * @param [out] pnerr Error code if erorr happened. - * @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? - */ -LPCTSTR paths_GetTempPath(int * pnerr) -{ - if (strTempPath.empty()) - { - int cchTempPath = GetTempPath(0, 0); - strTempPath.resize(cchTempPath - 1); - if (!GetTempPath(cchTempPath, &*strTempPath.begin())) - { - int err = GetLastError(); - if (pnerr) - *pnerr = err; -#ifdef _DEBUG - CString sysErr = GetSysError(err); // for debugging -#endif - return strTempPath.c_str(); // empty - } - strTempPath = paths_GetLongPath(strTempPath.c_str()); - } - return strTempPath.c_str(); -} - /** * @brief Check if paths are both folders or files. * This function checks if paths are "compatible" as in many places we need @@ -573,32 +541,6 @@ BOOL paths_IsPathAbsolute(const CString &path) } /** - * @brief Get filename for temporary file. - * @param [in] lpPathName Temporary file folder. - * @param [in] lpPrefixString Prefix to use for filename. - * @param [out] pnerr Error code if error happened. - * @return Full path for temporary file or empty string if error happened. - */ -String paths_GetTempFileName(LPCTSTR lpPathName, LPCTSTR lpPrefixString, int * pnerr) -{ - TCHAR buffer[MAX_PATH] = {0}; - if (_tcslen(lpPathName) > MAX_PATH-14) - return _T(""); // failure - int rtn = GetTempFileName(lpPathName, lpPrefixString, 0, buffer); - if (!rtn) - { - int err = GetLastError(); - if (pnerr) - *pnerr = err; -#ifdef _DEBUG - CString sysErr = GetSysError(err); // for debugging -#endif - return _T(""); - } - return buffer; -} - -/** * @brief Checks if folder exists and creates it if needed. * This function checks if folder exists and creates it if not. * @param [in] sPath diff --git a/Src/paths.h b/Src/paths.h index 62517c87a..2d26d7295 100644 --- a/Src/paths.h +++ b/Src/paths.h @@ -3,7 +3,7 @@ * * @brief Declaration file for path routines */ -// RCS ID line follows -- this is updated by CVS +// ID line follows -- this is updated by SVN // $Id$ #ifndef paths_h_included @@ -27,8 +27,6 @@ PATH_EXISTENCE paths_DoesPathExist(LPCTSTR szPath); void paths_normalize(String & sPath); String paths_GetLongPath(LPCTSTR szPath, BOOL bExpandEnvs = TRUE); bool paths_CreateIfNeeded(LPCTSTR szPath); -LPCTSTR paths_GetTempPath(int * pnerr=NULL); -String paths_GetTempFileName(LPCTSTR lpPathName, LPCTSTR lpPrefixString, int * pnerr=NULL); PATH_EXISTENCE GetPairComparability(LPCTSTR pszLeft, LPCTSTR pszRight); CString ExpandShortcut(const CString &inFile); String paths_ConcatPath(const String & path, const String & subpath); -- 2.11.0