OSDN Git Service

PATCH: [ 1874047 ] Simpler temp file class
authorKimmo Varis <kimmov@gmail.com>
Fri, 18 Jan 2008 20:03:57 +0000 (20:03 +0000)
committerKimmo Varis <kimmov@gmail.com>
Fri, 18 Jan 2008 20:03:57 +0000 (20:03 +0000)
Src/Merge.dsp
Src/MergeDoc.cpp
Src/MergeDoc.h
Src/PathContext.cpp
Src/PathContext.h
Src/TempFile.cpp [new file with mode: 0644]
Src/TempFile.h [new file with mode: 0644]

index fe2da6b..a1ea832 100644 (file)
@@ -935,6 +935,10 @@ SOURCE=.\SyntaxColors.cpp
 # End Source File
 # Begin Source File
 
+SOURCE=.\TempFile.cpp
+# End Source File
+# Begin Source File
+
 SOURCE=.\TestFilterDlg.cpp
 # End Source File
 # Begin Source File
@@ -1581,6 +1585,10 @@ SOURCE=.\SyntaxColors.h
 # End Source File
 # Begin Source File
 
+SOURCE=.\TempFile.h
+# End Source File
+# Begin Source File
+
 SOURCE=.\TestFilterDlg.h
 # End Source File
 # Begin Source File
index 77c4046..584d107 100644 (file)
@@ -32,7 +32,7 @@
 #include "UnicodeString.h"
 #include "Merge.h"
 #include "MainFrm.h"
-
+#include "Environment.h"
 #include "Ucs2Utf8.h"
 #include "diffcontext.h"       // FILE_SAME
 #include "MovedLines.h"
@@ -62,6 +62,7 @@
 #include "MergeLineFlags.h"
 #include "FileOrFolderSelect.h"
 #include "LineFiltersList.h"
+#include "TempFile.h"
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
@@ -111,8 +112,7 @@ END_MESSAGE_MAP()
  * @brief Constructor.
  */
 CMergeDoc::CMergeDoc()
-: m_pTempFiles(NULL)
-, m_bEnableRescan(TRUE)
+: m_bEnableRescan(TRUE)
 , m_nCurDiff(-1)
 , m_pDirDoc(NULL)
 {
@@ -164,7 +164,6 @@ CMergeDoc::~CMergeDoc()
        }
 
        delete m_pInfoUnpacker;
-       delete m_pTempFiles;
        delete m_pSaveFileInfo[0];
        delete m_pSaveFileInfo[1];
        delete m_pRescanFileInfo[0];
@@ -181,8 +180,8 @@ void CMergeDoc::DeleteContents ()
        CDocument::DeleteContents ();
        m_ptBuf[0]->FreeAll ();
        m_ptBuf[1]->FreeAll ();
-       delete m_pTempFiles;
-       m_pTempFiles = NULL;
+       m_tempFiles[0].Delete();
+       m_tempFiles[1].Delete();
 }
 
 void CMergeDoc::OnFileEvent (WPARAM /*wEvent*/, LPCTSTR /*pszPathName*/)
@@ -408,25 +407,30 @@ int CMergeDoc::Rescan(BOOL &bBinary, BOOL &bIdentical,
                FALSE, 1);
        m_LastRescan = COleDateTime::GetCurrentTime();
 
-       // get the desired files to temp locations so we can edit them dynamically
-       if (m_pTempFiles == NULL)
+       String temp1 = m_tempFiles[0].GetPath();
+       if (temp1.empty())
        {
-               m_pTempFiles = new TempFileContext;
-               if (m_pTempFiles == NULL)
-                       return RESCAN_TEMP_ERR;
-
-               if (!m_pTempFiles->CreateFiles(m_filePaths))
-                       return RESCAN_TEMP_ERR;
-
-               m_ptBuf[0]->SetTempPath(m_pTempFiles->GetTempPath());
-               m_ptBuf[1]->SetTempPath(m_pTempFiles->GetTempPath());
+               temp1 = m_tempFiles[0].CreateFromFile(m_filePaths.GetLeft().c_str(),
+                               _T("lt_wmdoc"));
        }
+       String temp2 = m_tempFiles[1].GetPath();
+       if (temp2.empty())
+       {
+               temp2 = m_tempFiles[1].CreateFromFile(m_filePaths.GetRight().c_str(),
+                               _T("rt_wmdoc"));
+       }
+       if (temp1.empty() || temp2.empty())
+               return RESCAN_TEMP_ERR;
+
+       String tempPath = env_GetTempPath(NULL);
+       m_ptBuf[0]->SetTempPath(tempPath);
+       m_ptBuf[1]->SetTempPath(tempPath);
 
        // output buffers to temp files (in UTF-8 if TCHAR=wchar_t or buffer was Unicode)
        if (bBinary == FALSE)
        {
-               SaveBuffForDiff(*m_ptBuf[0], m_pTempFiles->GetLeft().c_str());
-               SaveBuffForDiff(*m_ptBuf[1], m_pTempFiles->GetRight().c_str());
+               SaveBuffForDiff(*m_ptBuf[0], m_tempFiles[0].GetPath().c_str());
+               SaveBuffForDiff(*m_ptBuf[1], m_tempFiles[1].GetPath().c_str());
        }
 
        // Set up DiffWrapper
@@ -441,7 +445,7 @@ int CMergeDoc::Rescan(BOOL &bBinary, BOOL &bIdentical,
                m_diffWrapper.GetMovedLines()->Clear();
 
        // Set paths for diffing and run diff
-       m_diffWrapper.SetPaths(m_pTempFiles->GetLeft(), m_pTempFiles->GetRight(), TRUE);
+       m_diffWrapper.SetPaths(m_tempFiles[0].GetPath(), m_tempFiles[1].GetPath(), TRUE);
        m_diffWrapper.SetCompareFiles(m_filePaths.GetLeft(), m_filePaths.GetRight());
        diffSuccess = m_diffWrapper.RunFileDiff();
 
@@ -1456,7 +1460,7 @@ void CMergeDoc::CDiffTextBuffer::ReadLineFromBuffer(TCHAR *lpLineBegin, DWORD dw
 }
 
 /// Sets path for temporary files
-void CMergeDoc::CDiffTextBuffer::SetTempPath(CString path)
+void CMergeDoc::CDiffTextBuffer::SetTempPath(String path)
 {
        m_strTempPath = path;
 }
@@ -1867,11 +1871,10 @@ int CMergeDoc::CDiffTextBuffer::SaveToFile (LPCTSTR pszFileName,
        }
        else
        {
-               sIntermediateFilename.resize(_MAX_PATH);
-               LPTSTR intermedBuffer = &*sIntermediateFilename.begin();
-               if (!::GetTempFileName(m_strTempPath, _T("MRG"), 0, intermedBuffer))
+               sIntermediateFilename = env_GetTempFileName(m_strTempPath.c_str(),
+                       _T("MRG_"), NULL);
+               if (sIntermediateFilename.empty())
                        return SAVE_FAILED;  //Nothing to do if even tempfile name fails
-               sIntermediateFilename.resize(_tcslen(intermedBuffer));
                bOpenSuccess = !!file.OpenCreate(sIntermediateFilename.c_str());
        }
 
index ef47456..2326277 100644 (file)
 #include "DiffList.h"
 #include "stringdiffs.h"
 
+#ifndef _TEMP_FILE_
+#include "TempFile.h"
+#endif
+
 #ifndef _PATH_CONTEXT_H_
 #include "PathContext.h"
 #endif
@@ -135,10 +139,10 @@ class CDiffTextBuffer : public CGhostTextBuffer
        {
                friend class CMergeDoc;
 private :
-               CMergeDoc * m_pOwnerDoc;
+               CMergeDoc * m_pOwnerDoc; /**< Merge document owning this buffer. */
                int m_nThisPane; /**< Left/Right side */
                BOOL FlagIsSet(UINT line, DWORD flag);
-               CString m_strTempPath;
+               String m_strTempPath; /**< Temporary files folder. */
                int unpackerSubcode;
                /* 
                 * @brief Unicode encoding from ucr::UNICODESET 
@@ -156,7 +160,7 @@ private :
                int NoteCRLFStyleFromBuffer(TCHAR *lpLineBegin, DWORD dwLineLen = 0);
                void ReadLineFromBuffer(TCHAR *lpLineBegin, DWORD dwLineNum, DWORD dwLineLen = 0);
 public :
-               void SetTempPath(CString path);
+               void SetTempPath(String path);
                virtual void AddUndoRecord (BOOL bInsert, const CPoint & ptStartPos, const CPoint & ptEndPos,
                        LPCTSTR pszText, int nLinesToValidate, int nActionType = CE_ACTION_UNKNOWN, CDWordArray *paSavedRevisonNumbers = NULL);
                bool curUndoGroup();
@@ -328,7 +332,7 @@ protected:
        BUFFERTYPE m_nBufferType[2];
        BOOL m_bMergingMode; /**< Merging or Edit mode */
        BOOL m_bEditAfterRescan[2]; /**< Left/right doc edited after rescanning */
-       TempFileContext * m_pTempFiles; /**< Temp files for compared files */
+       TempFile m_tempFiles[2]; /**< Temp files for compared files */
 
 // friend access
        friend class RescanSuppress;
index 274368f..49cbae8 100644 (file)
@@ -139,125 +139,3 @@ void PathContext::Swap()
 {
        m_pathLeft.m_sPath.swap(m_pathRight.m_sPath);
 }
-
-/**
- * @brief Destructor, deletes existing temp files.
- */
-TempFileContext::~TempFileContext()
-{
-       if (FilesExist())
-               DeleteFiles();
-}
-
-/**
- * @brief Create temp files.
- *
- * Creates new temp files. Temp files are named based on
- * PathContext paths given as parameter.
- *
- * @param [in] paths PathContext whose paths are used as basis.
- */
-BOOL TempFileContext::CreateFiles(const PathContext &paths)
-{
-       TCHAR strTempPath[MAX_PATH] = {0};
-
-       if (!::GetTempPath(MAX_PATH, strTempPath))
-       {
-               LogErrorString(Fmt(_T("GetTempPath() failed: %s"),
-                       GetSysError(GetLastError())));
-               return FALSE;
-       }
-
-       m_sTempPath = strTempPath;
-
-       if (GetLeft().empty())
-       {
-               int nerr=0;
-               String sTempPath = env_GetTempFileName(strTempPath, _T("_LT"), &nerr);
-               if (sTempPath.empty())
-               {
-                       LogErrorString(Fmt(_T("GetTempFileName() for left-side failed: %s"),
-                               GetSysError(nerr)));
-                       return FALSE;
-               }
-               SetLeft(sTempPath.c_str());
-
-               if (!paths.GetLeft().empty())
-               {
-                       if (!::CopyFile(paths.GetLeft().c_str(), GetLeft().c_str(), FALSE))
-                       {
-                               LogErrorString(Fmt(_T("CopyFile() (copy left-side temp file) failed: %s"),
-                                       GetSysError(GetLastError())));
-                               return FALSE;
-                       }
-               }
-               ::SetFileAttributes(GetLeft().c_str(), FILE_ATTRIBUTE_NORMAL);
-       }
-       
-       if (GetRight().empty())
-       {
-               TCHAR name[MAX_PATH];
-               if (!::GetTempFileName(strTempPath, _T("_RT"), 0, name))
-               {
-                       LogErrorString(Fmt(_T("GetTempFileName() for right-side failed: %s"),
-                               strTempPath, GetSysError(GetLastError())));
-                       return FALSE;
-               }
-               SetRight(name);
-
-               if (!paths.GetRight().empty())
-               {
-                       if (!::CopyFile(paths.GetRight().c_str(), GetRight().c_str(), FALSE))
-                       {
-                               LogErrorString(Fmt(_T("CopyFile() (copy right-side temp file) failed: %s"),
-                                       GetSysError(GetLastError())));
-                               return FALSE;
-                       }
-               }
-               ::SetFileAttributes(GetRight().c_str(), FILE_ATTRIBUTE_NORMAL);
-       }
-       return TRUE;
-}
-
-/**
- * @brief Check if temp files exist.
- * @return TRUE if one of temp files exist.
- */
-BOOL TempFileContext::FilesExist() const
-{
-       BOOL bLeftExists = FALSE;
-       BOOL bRightExists = FALSE;
-
-       if (!GetLeft().empty())
-               bLeftExists = (paths_DoesPathExist(GetLeft().c_str()) == IS_EXISTING_FILE);
-       if (!GetRight().empty())
-               bLeftExists = (paths_DoesPathExist(GetRight().c_str()) == IS_EXISTING_FILE);
-       
-       return bLeftExists || bRightExists;
-}
-
-/**
- * @brief Delete temp files.
- */
-void TempFileContext::DeleteFiles()
-{
-       if (!GetLeft().empty())
-       {
-               if (!::DeleteFile(GetLeft().c_str()))
-               {
-                       LogErrorString(Fmt(_T("DeleteFile(%s) (deleting left-side temp file) failed: %s"),
-                               GetLeft(), GetSysError(GetLastError())));
-               }
-               SetLeft(_T(""));
-
-       }
-       if (!GetRight().empty())
-       {
-               if (!::DeleteFile(GetRight().c_str()))
-               {
-                       LogErrorString(Fmt(_T("DeleteFile(%s) (deleting right-side temp file) failed: %s"),
-                               GetRight(), GetSysError(GetLastError())));
-               }
-               SetRight(_T(""));
-       }
-}
index 3619728..852d46f 100644 (file)
@@ -3,7 +3,7 @@
  *
  *  @brief Declarations of PathInfo and PathContext
  */
-// RCS ID line follows -- this is updated by CVS
+// ID line follows -- this is updated by SVN
 // $Id$
 
 
@@ -54,20 +54,4 @@ private:
        PathInfo m_pathRight; /**< Second path (right path at start */
 };
 
-/**
- * @brief Temp files for compared files
- */
-class TempFileContext : public PathContext
-{
-public:
-       ~TempFileContext();
-       BOOL CreateFiles(const PathContext &paths);
-       BOOL FilesExist() const;
-       void DeleteFiles();
-       const CString & GetTempPath() const { return m_sTempPath; }
-
-private:
-       CString m_sTempPath;
-};
-
 #endif  // _PATH_CONTEXT_H_
diff --git a/Src/TempFile.cpp b/Src/TempFile.cpp
new file mode 100644 (file)
index 0000000..c3700fc
--- /dev/null
@@ -0,0 +1,110 @@
+/**
+ * @file  TempFile.cpp
+ *
+ * @brief Implementation file for TempFile
+ *
+ */
+// ID line follows -- this is updated by SVN
+// $Id$
+
+#include "stdafx.h" // Required by paths.cpp :(
+//#include "Windows.h"
+#include "UnicodeString.h"
+#include "TempFile.h"
+#include "paths.h"
+#include "Environment.h"
+
+/**
+ * @brief Delete the temp file when instance is deleted.
+ */
+TempFile::~TempFile()
+{
+       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] prefix A prefix for temp file name.
+ * @return Created temp file path.
+ */
+String TempFile::Create(LPCTSTR prefix)
+{
+       String temp = env_GetTempPath(NULL);
+       if (temp.empty())
+       {
+               return _T("");
+       }
+
+       String pref = prefix;
+       if (pref.empty())
+               pref = _T("wmtmp");
+
+       temp = env_GetTempFileName(temp.c_str(), pref.c_str(), NULL);
+       if (!temp.empty())
+               m_path = temp;
+
+       return temp;
+}
+
+/**
+ * @brief Create a temporary file from existing file's contents.
+ * This function creates a temporary file to temp folder and copies
+ * given file's contents to there.
+ * @param [in] filepath Full path to existing file.
+ * @param [in] prefix Prefix for the temporary filename.
+ * @return Full path to the temporary file.
+ */
+String TempFile::CreateFromFile(LPCTSTR filepath, LPCTSTR prefix)
+{
+       String temp = env_GetTempPath(NULL);
+       if (temp.empty())
+       {
+               return _T("");
+       }
+
+       String pref = prefix;
+       if (pref.empty())
+               pref = _T("wmtmp");
+
+       temp = env_GetTempFileName(temp.c_str(), pref.c_str(), NULL);
+       if (!temp.empty())
+       {
+               if (::CopyFile(filepath, temp.c_str(), FALSE))
+               {
+                       ::SetFileAttributes(temp.c_str(), FILE_ATTRIBUTE_NORMAL);
+                       m_path = temp;
+               }
+       }
+       return temp;
+}
+
+/**
+ * @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.
+ */
+bool TempFile::Delete()
+{
+       BOOL success = true;
+       if (!m_path.empty())
+               success = DeleteFile(m_path.c_str());
+       if (success)
+               m_path = _T("");
+       return !!success;
+}
diff --git a/Src/TempFile.h b/Src/TempFile.h
new file mode 100644 (file)
index 0000000..91f7861
--- /dev/null
@@ -0,0 +1,32 @@
+/**
+ *  @file TempFile.h
+ *
+ *  @brief Declaration of TempFile
+ */
+// ID line follows -- this is updated by SVN
+// $Id$
+
+#ifndef _TEMP_FILE_
+#define _TEMP_FILE_
+
+/**
+ * @brief A simple temporary file holder class.
+ * This class creates and holds temporary file names. When instance
+ * gets destroyed, the temporary file is also deleted.
+ */
+class TempFile
+{
+public:
+       TempFile() {}
+       ~TempFile();
+       void Create();
+       String Create(LPCTSTR prefix = NULL);
+       String CreateFromFile(LPCTSTR filepath, LPCTSTR prefix);
+       String GetPath();
+       bool Delete();
+
+private:
+       String m_path; /**< Temporary file path. */
+};
+
+#endif // _TEMP_FILE_